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

Automist automates your software deliver experience. It's how modern teams deliver modern software.

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!

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!

Topics:
java ,quiz ,exception handling

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}