Telegram Group & Telegram Channel
What will be the output of the code?

A. false true false true
B. true true true true
C. false false true true
D. false false false true
E. false true true true
F. Error
G. None of the above

Explanation:
1. String Pool and `intern()` Method:
- In Java, string literals (e.g., `"Hello, World!"`) are stored in the string pool.
- The intern() method returns a canonical representation for the string object. If the pool already contains a string equal to the current string object, the string from the pool is returned.

2. Line 1: `System.out.println(str1 == str2);`:
- str1 is a string literal stored in the string pool.
- str2 is a new string object created using the new keyword, so it is not in the string pool.
- The comparison == checks if both references point to the same object. Since str1 and str2 are different objects, the result is false.

3. Line 2: `System.out.println(str1 == str3);`:
- str3 is assigned directly to str1, so both references point to the same object in the string pool.
- Therefore, the result of str1 == str3 is true.

4. Line 3: `System.out.println(str1 == str4);`:
- str4 is str2.intern(), which returns the reference from the string pool.
- Since str1 is already in the string pool and str4 is now the interned version of str2, both point to the same object.
- Thus, str1 == str4 is true.

5. Line 4: `System.out.println(str1 == str5);`:
- str5 is the result of string concatenation using literals "Hello, " and "World!". This concatenation is resolved at compile time, and the resulting string is stored in the string pool.
- Since str1 is "Hello, World!" and str5 is also "Hello, World!" stored in the pool, str1 == str5 is true.

Output:

false
true
true
true


Correct Answer: E



tg-me.com/topJavaQuizExplain/295
Create:
Last Update:

What will be the output of the code?

A. false true false true
B. true true true true
C. false false true true
D. false false false true
E. false true true true
F. Error
G. None of the above

Explanation:
1. String Pool and `intern()` Method:
- In Java, string literals (e.g., `"Hello, World!"`) are stored in the string pool.
- The intern() method returns a canonical representation for the string object. If the pool already contains a string equal to the current string object, the string from the pool is returned.

2. Line 1: `System.out.println(str1 == str2);`:
- str1 is a string literal stored in the string pool.
- str2 is a new string object created using the new keyword, so it is not in the string pool.
- The comparison == checks if both references point to the same object. Since str1 and str2 are different objects, the result is false.

3. Line 2: `System.out.println(str1 == str3);`:
- str3 is assigned directly to str1, so both references point to the same object in the string pool.
- Therefore, the result of str1 == str3 is true.

4. Line 3: `System.out.println(str1 == str4);`:
- str4 is str2.intern(), which returns the reference from the string pool.
- Since str1 is already in the string pool and str4 is now the interned version of str2, both point to the same object.
- Thus, str1 == str4 is true.

5. Line 4: `System.out.println(str1 == str5);`:
- str5 is the result of string concatenation using literals "Hello, " and "World!". This concatenation is resolved at compile time, and the resulting string is stored in the string pool.
- Since str1 is "Hello, World!" and str5 is also "Hello, World!" stored in the pool, str1 == str5 is true.

Output:


false
true
true
true


Correct Answer: E

BY Explanations “Top Java Quiz Questions”


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/topJavaQuizExplain/295

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

The Singapore stock market has alternated between positive and negative finishes through the last five trading days since the end of the two-day winning streak in which it had added more than a dozen points or 0.4 percent. The Straits Times Index now sits just above the 3,060-point plateau and it's likely to see a narrow trading range on Monday.

Telegram is riding high, adding tens of million of users this year. Now the bill is coming due.Telegram is one of the few significant social-media challengers to Facebook Inc., FB -1.90% on a trajectory toward one billion users active each month by the end of 2022, up from roughly 550 million today.

telegram from us


Telegram Explanations “Top Java Quiz Questions”
FROM USA