Ask Question
21 March, 00:53

Which of the following will execute without throwing an exception error?

a. String str1 = "Golden"; String str2 = "Retriever"; if (str1. equals (str2)) System. out. println ("Golden Retriever!");

b. String str1 = "Golden"; String str2 = str1. substring (4); System. out. println (str1 + str2);

c. None of the above

d. A and B above.

+3
Answers (1)
  1. 21 March, 01:13
    0
    (d) A and B above

    Explanation:

    => Writing option a in a well-formatted way, we have:

    String str1 = "Golden";

    String str2 = "Retriever";

    if (str1. equals (str2))

    System. out. println ("Golden Retriever!");

    Lines 1 and 2 of the code correctly assign the strings "Golden" and "Retriever" to the String variables str1 and str2 respectively. This will raise no error.

    Line 3 checks if the value of variables str1 and str2 are the same. This is a correct way of checking. Therefore, there will be no error.

    Line 4 prints out the string "Golden retriever" depending on whether or not the condition is the if statement is true. Since it is not true, this will not be printed. That is not bad as no error will be thrown.

    Therefore, the code in option a will not throw any error.

    => Writing option b in a well-formatted way, we have:

    String str1 = "Golden";

    String str2 = str1. substring (4);

    System. out. println (str1 + str2);

    Line 1 of the code above correctly assigns the string "Golden" to the String variable str1. This will not raise any error.

    Line 2 of the code correctly assigns to the String variable str2, the substring of str1 ("Golden") starting from the 4th index. i. e "en". This will raise no error.

    Line 3 of the code prints out the result of the concatenation of the values of str1 and str2 which is "Golden" + "en" = "Goldenen"

    Therefore, the code in option b will not throw any error either.

    Therefore, A and B above will not raise an error when they are executed.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Which of the following will execute without throwing an exception error? a. String str1 = "Golden"; String str2 = "Retriever"; if (str1. ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers