{{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

Comment (3)

Save
{{ articles.views | formatCount}} Views

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;
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!

Topics:
java ,quiz ,exception handling

Comment (3)

Save
{{ articles.views | formatCount}} Views

Opinions expressed by DZone contributors are their own.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8392122,"math_prob":0.9589461,"size":2778,"snap":"2019-51-2020-05","text_gpt3_token_len":689,"char_repetition_ratio":0.16510454,"word_repetition_ratio":0.0970297,"special_character_ratio":0.26781857,"punctuation_ratio":0.15187714,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97864,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-08T02:29:13Z\",\"WARC-Record-ID\":\"<urn:uuid:21792224-34cb-4ed3-b8b2-be12d0c6c034>\",\"Content-Length\":\"150209\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2f8c5699-fc87-4a57-aeb1-b5f32eba1427>\",\"WARC-Concurrent-To\":\"<urn:uuid:ef1063f7-4226-4fc3-8316-0402ee952bde>\",\"WARC-IP-Address\":\"34.204.24.245\",\"WARC-Target-URI\":\"https://dzone.com/articles/handling-exceptions\",\"WARC-Payload-Digest\":\"sha1:UCWYODQVBHO3UTFOJ5NCS4CGP3KVV2IN\",\"WARC-Block-Digest\":\"sha1:ZKZPG2XEFIRCTRNBHBWKSPR3X4Q6QYDN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540504338.31_warc_CC-MAIN-20191208021121-20191208045121-00390.warc.gz\"}"}