Ask Question
4 March, 05:57

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. 4 March, 08:04
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers