Tuesday, 24 July 2012

Flow Control - Objective Questions - Part 2

1)

What will be the output of the program?

for(int i = 0; i < 3; i++)
{
    switch(i)
    {
        case 0: break;
        case 1: System.out.print("one ");
        case 2: System.out.print("two ");
        case 3: System.out.print("three ");
    }
}
System.out.println("done");

A. done
B. one two done
C. one two three done
D. one two three two three done

Answer: D

The variable i will have the values 0, 1 and 2.When i is 0, nothing will be printed because of the break in case 0.When i is 1, "one two three" will be output because case 1, case 2 and case 3 will be executed (they don't have break statements).When i is 2, "two three" will be output because case 2 and case 3 will be executed (again no break statements).Finally, when the for loop finishes "done" will be output.

2)

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 < 4; z++)
        {   switch (z)
            {   case x: System.out.print("0 ");
                default: System.out.print("def ");
                case x-1: System.out.print("1 ");
                            break;
                case x-2: System.out.print("2 ");
            } } }}

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

Answer: D

When z == 0 , case x-2  is matched. When z == 1, case x-1 is matched and then the break occurs. When z == 2, case x, then default, then x-1 are all matched. When z == 3, default, then x-1  are matched. The rules for default are that it will fall through from above like any other case and that it will match when no other cases
match.

3)

public class Outer
{
    public void someOuterMethod()
    {
        //Line 5
    }
    public class Inner { }
 
    public static void main(String[] argv)
    {
        Outer ot = new Outer();
        //Line 10
    }
}

Which of the following code fragments inserted, will allow to compile?

A. new Inner(); //At line 5
B. new Inner(); //At line 10
C. new ot.Inner(); //At line 10
D. new Outer.Inner(); //At line 10

Answer: A

Option A compiles without problem.
Option B gives error - non-static variable cannot be referenced from a static context.
Option C package ot does not exist.
Option D gives error - non-static variable cannot be referenced from a static context.

4)

What will be the output of the program?

Float f = new Float("12");
switch (f)
{
    case 12: System.out.println("Twelve");
    case 0: System.out.println("Zero");
    default: System.out.println("Default");
}

A. Zero
B. Twelve
C. Default
D. Compilation fails

Answer: D

The switch statement can only be supported by integers or variables more "narrow" than an integer i.e. byte, char, short. Here a Float wrapper object is used and so the compilation fails.

5)

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.

6)

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 y: System.out.print("0 ");   /* Line 11 */
                case x-1: System.out.print("1 "); /* Line 12 */
                case x: System.out.print("2 ");   /* Line 13 */
            }
        }
    }
}

A. 0 1 2
B. 0 1 2 1 2 2
C. Compilation fails at line 11.
D. Compilation fails at line 12.

Answer: C

Case expressions must be constant expressions. Since x is marked final, lines 12 and 13 are legal; however y is not a final so the compiler will fail at line 11.

7)

What will be the output of the program?

int x = l, y = 6;
while (y--)
{
    x++;
}
System.out.println("x = " + x +" y = " + y);

A. x = 6 y = 0
B. x = 7 y = 0
C. x = 6 y = -1
D. Compilation fails.

Answer: D

Compilation fails because the while loop demands a boolean argument for it's looping condition, but in the code, it's given an int argument.

while(true) { //insert code here }

8)

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

Which statement is true?

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.

9)

What will be the output of the program?

public class Test
{
    public static void main(String [] args)
    {
        int I = 1;
        do while ( I < 1 )
        System.out.print("I is " + I);
        while ( I > 1 ) ;
    }
}

A. I is 1
B. I is 1 I is 1
C. No output is produced
D. Compilation error

Answer: C

There are two different looping constructs in this problem. The first is a do-while loop and the second is a while loop, nested inside the do-while. The body of the do-while is only a single statement-brackets are not needed. You are assured that the while expression will be evaluated at least once, followed by an evaluation of the do-while expression. Both expressions are false and no output is produced.

10)

What will be the output of the program?

int x = 3;
int y = 1;
if (x = y) /* Line 3 */
{
    System.out.println("x =" + x);
}

A. x=1
B. x=3
C. Compilation fails.
D. The code runs with no output.

Answer: C

Line 3 uses an assignment as opposed to comparison. Because of this, the if statement receives an integer value instead of a boolean. And so the compilation fails.

No comments:

Post a Comment