Java Programming - Java.lang Class - Discussion
Discussion Forum : Java.lang Class - Finding the output (Q.No. 6)
6.
What will be the output of the program?
public class Test178
{
public static void main(String[] args)
{
String s = "foo";
Object o = (Object)s;
if (s.equals(o))
{
System.out.print("AAA");
}
else
{
System.out.print("BBB");
}
if (o.equals(s))
{
System.out.print("CCC");
}
else
{
System.out.print("DDD");
}
}
}
Discussion:
16 comments Page 2 of 2.
Shah Ishan V said:
1 decade ago
Here, s and o both are referring to the same object.
When encounter the line Object o = (Object) s;
So, Output is : AAACCC.
When encounter the line Object o = (Object) s;
So, Output is : AAACCC.
Naresh said:
1 decade ago
Both s and o are referring to same content.
So s.equals(o) and o.equals(s) will become true.
Hence AAACCC printed
So s.equals(o) and o.equals(s) will become true.
Hence AAACCC printed
E.DIVYA said:
1 decade ago
Here s and o refers the same value so s.equal (0) & o.equal (s) are true.
The answer will be AAACCC.
The answer will be AAACCC.
Ankit said:
1 decade ago
Very nice example to test the knowledge of inheritance and method overridding.
Belete said:
10 years ago
Very interesting and motivation questions for Java programming.
Seema said:
8 years ago
Yes, I too got the same output, i.e AAACCC.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers