Tuesday 24 July 2012

Flow Control - Objective Questions - Part 1

1)

public void foo( boolean a, boolean b)
{
    if( a ) {System.out.println("A"); /* Line 5 */   }
    else if(a && b) /* Line 7 */
    { System.out.println( "A && B"); }
    else /* Line 11 */
    {         if ( !b ) { System.out.println( "notB") ;}
        else
        { System.out.println( "ELSE" ) ;
        }  } }

A. If a is true and b is true then the output is "A && B"
B. If a is true and b is false then the output is "notB"
C. If a is false and b is true then the output is "ELSE"
D. If a is false and b is false then the output is "ELSE"

Answer: C

The output is "ELSE". Only when a is false do the output lines after 11 get some chance of executing.If b is true then !b become false,then else block will execute.

2)

What will be the output of the program?

int i = l, j = -1;
switch (i)
{
    case 0, 1: j = 1; /* Line 4 */
    case 2: j = 2;
    default: j = 0;
}
System.out.println("j = " + j);

A. j = -1
B. j = 0
C. j = 1
D. Compilation fails.

Answer: D

The case statement takes only a single argument. The case statement on line 4 is given two arguments so the compiler complains.

3)

What will be the output of the program?

public class Switch2
{
    final static short x = 2;
    public static int y = 0;
    public static void main(String [] args)
    {
        for (int z=0; z < 3; z++)
        {
            switch (z)
            {
                case x: System.out.print("0 ");
                case x-1: System.out.print("1 ");
                case x-2: System.out.print("2 ");
            }
        }
    }
}

A. 0 1 2
B. 0 1 2 1 2 2
C. 2 1 0 1 0 0
D. 2 1 2 0 1 2

Answer: D

The case expressions are all legal because x is marked final, which means the expressions can be evaluated at compile time. In the first iteration of the for loop case x-2 matches, so 2 is printed. In the second iteration, x-1 is matched so 1 and 2 are printed (remember, once a match is found all remaining statements are executed until a break statement is encountered). In the third iteration, x is matched. So 0 1 and 2 are printed.

4)

public void test(int x)
{
    int odd = 1;
    if(odd) /* Line 4 */
    {
        System.out.println("odd");
    }
    else
    {
        System.out.println("even");
    }
}
Which statement is true?

A. Compilation fails.
B. "odd" will always be output.
C. "even" will always be output.
D. "odd" will be output for odd values of x, and "even" for even values.

Answer: A

The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an
integer.

5)

public class While
{
    public void loop()
    {
        int x= 0;
        while ( 1 ) /* Line 6 */
        {
            System.out.print("x plus one is " + (x + 1)); /* Line 8 */
        }
    }
}

A. There is a syntax error on line 1.
B. There are syntax errors on lines 1 and 6
C. There are syntax errors on lines 1, 6, and 8.
D. There is a syntax error on line 6.

Answer: D

Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java.

A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown.

6)

What will be the output of the program?

int i = 1, j = 10;
do
{
    if(i > j)
    {
        break;
    }
    j--;
} while (++i < 5);
System.out.println("i = " + i + " and j = " + j);

A. i = 6 and j = 5
B. i = 5 and j = 5
C. i = 6 and j = 4
D. i = 5 and j = 6

Answer: D

The order is, test i against j, if bigger, it breaks from the loop, decrements j by one, and then tests the
loop condition, where a pre-incremented by one i is tested for being lower than 5. The test is at the end of the loop, so i can reach the value of 5 before it fails. So it goes, start:
(1, 10) ( 2, 9) ( 3, 8) ( 4, 7) ( 5, 6) loop condition fails.

7)

What will be the output of the program?

boolean bool = true;
if(bool = false) /* Line 2 */
{
    System.out.println("a");
}
else if(bool) /* Line 6 */
{
    System.out.println("b");
}
else if(!bool) /* Line 10 */
{
    System.out.println("c"); /* Line 12 */
}
else
{
    System.out.println("d");
}

A. a
B. b
C. c
D. d

Answer: C

Look closely at line 2, is this an equality check (==) or an assignment (=). The condition at line 2 evaluates to false and also assigns false to bool. bool is now false so the condition at line 6 is not true. The condition at line 10 checks to see if bool is not true ( if !(bool == true) ), it isn't so line 12 is executed.

8)

What will be the output of the program?

public class If1
{
    static boolean b;
    public static void main(String [] args)
    {
        short hand = 42;
        if ( hand < 50 & !b ) /* Line 7 */
            hand++;
        if ( hand > 50 );     /* Line 9 */
        else if ( hand > 40 )
        {
            hand += 7;
            hand++;  
        }
        else
            --hand;
        System.out.println(hand);
    }
}

A. 41
B. 42
C. 50
D. 51

Answer: D

In Java, boolean instance variables are initialized to false, so the if test on line 7 is true and hand is
incremented. Line 9 is legal syntax, a do nothing statement. The else-if is true so hand has 7 added to it and
is then incremented.

9)

What will be the output of the program?

for (int i = 0; i < 4; i += 2)
{
    System.out.print(i + " ");
}
System.out.println(i); /* Line 5 */

A. 0 2 4
B. 0 2 4 5
C. 0 1 2 3 4
D. Compilation fails.

Answer: D

Compilation fails on the line 5 - System.out.println(i);  as the variable i has only been declared within the for loop. It is not a recognized variable outside the code block of loop.

10)

What will be the output of the program?

public class Delta
{
    static boolean foo(char c)
    {
        System.out.print(c);
        return true;
    }
    public static void main( String[] argv )
    {
        int i = 0;
        for (foo('A'); foo('B') && (i < 2); foo('C'))
        {
            i++;
            foo('D');
        }
    }
}

A. ABDCBDCB
B. ABCDABCD
C. Compilation fails
D. An exception is thrown at runtime.

Answer: A

In the for loop first initialization part will execute only once.then condition part,if it is true loop body
will be executed.After that increment/decrement section will be executed.and then again condition and so on.

No comments:

Post a Comment