Finding The Output

Question 11 :

What will be the output of the program?

public class ExamQuestion7 
{  
    static int j; 
    static void methodA(int i)
    {
        boolean b; 
        do
        { 
            b = i<10 | methodB(4); /* Line 9 */
            b = i<10 || methodB(8);  /* Line 10 */
        }while (!b); 
    } 
    static boolean methodB(int i)
    {
        j += i; 
        return true; 
    } 
    public static void main(String[] args)
    {
        methodA(0); 
        System.out.println( "j = " + j ); 
    } 
}


A). j = 0
B). j = 4
C). j = 8
D). The code will run with no output
Answer : Option B

Explanation :

The lines to watch here are lines 9 & 10. Line 9 features the non-shortcut version of the OR operator so both of its operands will be evaluated and therefore methodB(4) is executed.

However line 10 has the shortcut version of the OR operator and if the 1st of its operands evaluates to true (which in this case is true), then the 2nd operand isn't evaluated, so methodB(8) never gets called.

The loop is only executed once, b is initialized to false and is assigned true on line 9. Thus j = 4.


Question 12 :

What will be the output of the program?

try 
{
    Float f1 = new Float("3.0");
    int x = f1.intValue();
    byte b = f1.byteValue();
    double d = f1.doubleValue();
    System.out.println(x + b + d);
}
catch (NumberFormatException e) /* Line 9 */
{
    System.out.println("bad number"); /* Line 11 */
}


A). 9.0
B). bad number
C). Compilation fails on line 9.
D). Compilation fails on line 11.
Answer : Option A

Explanation :

The xxxValue() methods convert any numeric wrapper object's value to any primitive type. When narrowing is necessary, significant bits are dropped and the results are difficult to calculate.


Question 13 :

What will be the output of the program?

class Q207 
{ 
    public static void main(String[] args) 
    {
        int i1 = 5; 
        int i2 = 6; 
        String s1 = "7"; 
        System.out.println(i1 + i2 + s1); /* Line 8 */
    } 
}


A). 18
B). 117
C). 567
D). Compiler error
Answer : Option B

Explanation :

This question is about the + (plus) operator and the overriden + (string cocatanation) operator. The rules that apply when you have a mixed expression of numbers and strings are:

If either operand is a String, the + operator concatenates the operands.

If both operands are numeric, the + operator adds the operands.

The expression on line 6 above can be read as "Add the values i1 and i2 together, then take the sum and convert it to a string and concatenate it with the String from the variable s1". In code, the compiler probably interprets the expression on line 8 above as:

System.out.println( new StringBuffer() 
.append(new Integer(i1 + i2).toString()) 
.append(s1) 
.toString() ); 


Question 14 :

What will be the output of the program?

public class SqrtExample 
{
    public static void main(String [] args) 
    {
        double value = -9.0;
        System.out.println( Math.sqrt(value));
    }
}


A). 3.0
B). -3.0
C). NaN
D). Compilation fails.
Answer : Option C

Explanation :

The sqrt() method returns NaN (not a number) when it's argument is less than zero.


Question 15 :

What will be the output of the program?

String s = "ABC"; 
s.toLowerCase(); 
s += "def"; 
System.out.println(s);


A). ABC
B). abc
C). ABCdef
D). Compile Error
Answer : Option C

Explanation :

String objects are immutable. The object s above is set to "ABC". Now ask yourself if this object is changed and if so where - remember strings are immutable.

Line 2 returns a string object but does not change the originag string object s, so after line 2 s is still "ABC".

So what's happening on line 3? Java will treat line 3 like the following:

s = new StringBuffer().append(s).append("def").toString();

This effectively creates a new String object and stores its reference in the variable s, the old String object containing "ABC" is no longer referenced by a live thread and becomes available for garbage collection.