Exercise: Java Lang Class

Questions for: Java Lang Class

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
Answer: A
No answer description is available. Let's discuss.
What will be the output of the program?
public class Test 
{ 
    public static void main(String[] args) 
    {
        final StringBuffer a = new StringBuffer(); 
        final StringBuffer b = new StringBuffer(); 

        new Thread() 
        { 
            public void run() 
            {
                System.out.print(a.append("A")); 
                synchronized(b) 
                { 
                    System.out.print(b.append("B")); 
                } 
            } 
        }.start(); 
            
        new Thread() 
        {
            public void run() 
            {
                System.out.print(b.append("C")); 
                synchronized(a) 
                {
                    System.out.print(a.append("D")); 
                } 
            } 
        }.start(); 
    } 
}
A:
ACCBAD
B:
ABBCAD
C:
CDDACB
D:
Indeterminate output
Answer: D

It gives different output while executing the same compiled code at different times.

C:\>javac Test.java
C:\>java Test
ABBCAD
C:\>java Test
ACADCB
C:\>java Test
ACBCBAD
C:\>java Test
ABBCAD
C:\>java Test
ACBCBAD
C:\>java Test
ACBCBAD
C:\>java Test
ABBCAD
What will be the output of the program?
public class ExamQuestion6 
{
    static int x; 
    boolean catch()
    {
        x++; 
        return true; 
    } 
    public static void main(String[] args)
    {
        x=0; 
        if ((catch() | catch()) || catch()) 
            x++; 
        System.out.println(x); 
    } 
}
A:
1
B:
2
C:
3
D:
Compilation Fails
Answer: D

Initially this looks like a question about the logical and logical shortcut operators "|" and "||" but on closer inspection it should be noticed that the name of the boolean method in this code is "catch". "catch" is a reserved keyword in the Java language and cannot be used as a method name. Hence Compilation will fail.

What will be the output of the program?

String a = "ABCD"; 
String b = a.toLowerCase(); 
b.replace('a','d'); 
b.replace('b','c'); 
System.out.println(b);
A:
abcd
B:
ABCD
C:
dccd
D:
dcba
Answer: A

String objects are immutable, they cannot be changed, in this case we are talking about the replace method which returns a new String object resulting from replacing all occurrences of oldChar in this string with newChar.

b.replace(char oldChar, char newChar);

But since this is only a temporary String it must either be put to use straight away i.e.

System.out.println(b.replace('a','d'));

Or a new variable must be assigned its value i.e.

String c = b.replace('a','d');

What will be the output of the program?
String d = "bookkeeper";
d.substring(1,7);
d = "w" + d;
d.append("woo");  /* Line 4 */
System.out.println(d);
A:
wookkeewoo
B:
wbookkeeper
C:
wbookkeewoo
D:
Compilation fails.
Answer: D

In line 4 the code calls a StringBuffer method, append() on a String object.

Ad Slot (Above Pagination)
Quiz