Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - Finding the output (Q.No. 27)
27.
What will be the output of the program?
String s = "hello"; 
Object o = s; 
if( o.equals(s) )
{
    System.out.println("A"); 
} 
else
{
    System.out.println("B"); 
} 
if( s.equals(o) )
{
    System.out.println("C"); 
} 
else
{ 
    System.out.println("D"); 
}
  1. A
  2. B
  3. C
  4. D
1 and 3
2 and 4
3 and 4
1 and 2
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
11 comments Page 2 of 2.

Neha said:   8 years ago
It first creates an object which has value as hello, n then its assigned to o. So both will have the same value. Now if the condition is satisfied so it won't enter else loop. Hence the answer is A and C.


Post your comments here:

Your comments will be displayed after verification.