[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

JAVA Programming :: Basic Concepts - Discussion

Home > JAVA Programming > Basic Concepts > MCQs Questions Discussion

25 / 64

What all gets printed when the following gets compiled and run.

public class test {
public static void main(String args[]) {
String s1 = "abc";
String s2 = "abc";
if(s1 == s2)
System.out.println(1);
else
System.out.println(2);
if(s1.equals(s2))
System.out.println(3);
else
System.out.println(4);
}
}

A1,3

B1,4

C3,4

D2,4

Answer: Option (Login/Signup)

Show Explanation

Here, the content of s1 and s2 is "abc", so in the String constant pool, a single object is created having the content as "abc", and s1 and s2 both will refer to the same object created. Since, both are referring to the same string object, hence (s1==s2) becomes True, and if part will execute.

On the other hand, the content for both s1 and s2 are the same, hence "s1.equals(s2)"  also becomes True, so again the if part gets executed.

Therefore, the output will be-

1

3

Asked In ::

Post Your Answer Here:     

Reply    
Rate This: +0 -0
    Report


Report Error

Please Login First Click Here