Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Quiz 3: Handling Exceptions

DZone's Guide to

Java Quiz 3: Handling Exceptions

Catch up with the answer to the last Java Quiz about class and instance variables and dive into a new one surrounding exception handling.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Before we start with this week's quiz, here is the answer to Java Quiz 2: Comparing Class Variables With Instance Variables.

  1. By creating the first object and passing the argument 2 to the constructor, the statement x += i; increments the value of x by 2. The statement s += x; adds the string value of 2 to object s.  The statement s2 += x; adds the string value of 2 to static string s2.

  2. By creating the second object, we pass the value 1 to the constructor. The statement x += i; increments the value of x by 1. Therefore, x = 3. Remember that x is a static variable. The statement s += x; adds the string value of 3 to object s. String s is not static. Therefore, the value of s of the object mc is equal to 3. The statement s2 += x; adds the string value of 3 to the static object s2. String s2 is static. That is why there is only one copy available for all the objects of the class MyClass. The first value of the object s2 was the string value of 2 (see the first step). The string value of 3 is also added to s2.

  3. By creating the third object, string s wouldn't be affected because its value is of object mc — and that is 3. The statement x += 4; increments the value of the static variable x by 4. The last value of x was 3. So, x = 3 + 4 = 7. The static string s2 would write 237, and that value is the same for all the objects of MyClass. The correct answer is b.

Here is today's quiz!

What is written to the standard output as the result of executing the following code?

public class MyException {  
    int[] accounts = new int[13];  
    int nr;     
    public void  print(int  i, int  i2) {    
        try {      
            nr = accounts[i] / i2;      
            System.out.print("R");    
        }    
        catch (ArithmeticException ae) {      
            System.out.print("S");    
        }    
        catch (Exception e) {      
            System.out.print("T");    
        }  
    }  
    public static void  main(String[] args) {    
        MyException me = new  MyException();    
        me.print(13, 2);    
        me.print(12, 0);  
    }
}


  • A. This program writes "SS" to the standard output.

  • B. This program writes "TT" to the standard output.

  • C. This program writes "ST" to the standard output.

  • D. This program writes "TS" to the standard output.

  • E. This program writes "RS" to the standard output.

  • F. This program writes "SR" to the standard output.

The correct answer and its explanation will be included in the next quiz in two weeks! For more Java quizzes, puzzles, and assignments, take a look at my site!

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
java ,quiz ,exception handling

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}