Finding The Output

Question 21 :

What will be the output of the program?

public class Test138 
    public static void stringReplace (String text) 
        text = text.replace ('j' , 'c'); /* Line 5 */
    public static void bufferReplace (StringBuffer text) 
        text = text.append ("c");  /* Line 9 */
    public static void main (String args[]) 
        String textString = new String ("java"); 
        StringBuffer textBuffer = new StringBuffer ("java"); /* Line 14 */
        System.out.println (textString + textBuffer); 

A). java
B). javac
C). javajavac
D). Compile error
Answer : Option C

Explanation :

A string is immutable, it cannot be changed, that's the reason for the StringBuffer class. The stringReplace method does not change the string declared on line 14, so this remains set to "java".

Method parameters are always passed by value - a copy is passed into the method - if the copy changes, the original remains intact, line 5 changes the reference i.e. text points to a new String object, however this is lost when the method completes. The textBuffer is a StringBuffer so it can be changed.

This change is carried out on line 9, so "java" becomes "javac", the text reference on line 9 remains unchanged. This gives us the output of "javajavac"

Question 22 :

What will be the output of the program?

class Tree { } 
class Pine extends Tree { } 
class Oak extends Tree { } 
public class Forest1 
    public static void main (String [] args)
        Tree tree = new Pine(); 
        if( tree instanceof Pine ) 
            System.out.println ("Pine"); 
        else if( tree instanceof Tree ) 
            System.out.println ("Tree"); 
        else if( tree instanceof Oak ) 
            System.out.println ( "Oak" ); 
            System.out.println ("Oops "); 

A). Pine
B). Tree
C). Forest
D). Oops
Answer : Option A

Explanation :

The program prints "Pine".

Question 23 :

What will be the output of the program?

String d = "bookkeeper";
d = "w" + d;
d.append("woo");  /* Line 4 */

A). wookkeewoo
B). wbookkeeper
C). wbookkeewoo
D). Compilation fails.
Answer : Option D

Explanation :

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

Question 24 :

What will be the output of the program?

String a = "ABCD"; 
String b = a.toLowerCase(); 

A). abcd
C). dccd
D). dcba
Answer : Option A

Explanation :

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.

Or a new variable must be assigned its value i.e.
String c = b.replace('a','d');

Question 25 :

What will be the output of the program?

public class ExamQuestion6 
    static int x; 
    boolean catch()
        return true; 
    public static void main(String[] args)
        if ((catch() | catch()) || catch()) 

A). 1
B). 2
C). 3
D). Compilation Fails
Answer : Option D

Explanation :

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.