DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Java Quiz 3: Handling Exceptions

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.

Sar Maroof user avatar by
Sar Maroof
·
Oct. 26, 17 · Java Zone · Survey/Contest
Like (4)
Save
Tweet
7.78K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Java (programming language) Strings Data Types Object (computer science)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top Six Kubernetes Best Practices for Fleet Management
  • Unit Vs Integration Testing: What's the Difference?
  • Application Scalability — How To Do Efficient Scaling
  • Stupid Things Orgs Do That Kill Productivity w/ Netflix, FloSports & Refactoring.club

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo