Java.lang Class

Q1: 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

A 1 and 3

B 2 and 4

C 3 and 4

D 1 and 2

ANS:A - 1 and 3

.equals() is used for content comparison in String.
So String creates one object of scp with reference of s.

Here String reference is assigns to Object.
So Object and String references are both poining to same content "hello".

The (o.equals(s))and (s.equals(o) checks content of 's' and 'o'.
The content is same it returns True.  



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.