Java.lang Class

Q1: What will be the output of the program (in jdk1.6 or above)?
public class BoolTest 
{
    public static void main(String [] args) 
    {
        Boolean b1 = new Boolean('false');
        boolean b2;
        b2 = b1.booleanValue();
        if (!b2) 
        {
            b2 = true;
            System.out.print('x ');
        }
        if (b1 & b2) /* Line 13 */
        {
            System.out.print('y ');
        }
        System.out.println('z');
    }
}

A z

B x z

C y z

D Compilation fails.

ANS:A - z

Boolean b1 is initialized with false & Boolean b2 is initialized with false due to line no 7(Boolean b2 = b1.BooleanValue();).

Now the if loop will return true as !FALSE is TRUE & thus will print x.

if( b1 & b2) => false & true =>false.

The next loop will return false as the value of b2 is changed from FALSE to TRUE because of the statement b2=true on line 10.

Thus b1=false and b2=true.

Thus the output is xz.  



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.