Control Self Test With Answers


1) Which of the following is not a valid statement?

a)    ;
b)   {  }
c)   (  );
d)   {  };                       ( c )
 

2) Which of the following lines needs be commented out in order
    for the following code to compile? Select a, b, c or d.

                      {
/*a*/               int i=3;
/*b*/              {  int i= 7;
/*c*/               i=13;  }
/*d*/              { int i= 11; }
                      }                              ( a )
 

3) A static initializer does not need which of the following.

a) braces
b) a block statement
c) the static keyword
d) an identifier                                    ( d )
 

4) In the following if else statements what will the result be?

    if (false)
     System.out.println("A");
       else if (false)
     System.out.println("B");
       else if (true)
     System.out.println("C");
      else
     System.out.println("D");

a) A
b) B
c) C
d) D                                                      ( c )
 

5) True or False?

while(true){ }   and    for( ; ; ){  }  both do the same thing. (True)
 

6) Which of the following keywords prevents fall through in a switch statement?
    a) switch
    b) case
    c) break
    d) default                                             ( c )
 

7)  What will the following switch print out?

    int x =0;
    switch( x ) {
    case 1: System.out.println("1");
    break;
    case 2: System.out.println("2");
    break;
    default: System.out.println("3");
    }

a) 0
b) 1
c) 2
d) 3                                             ( d )
 

8) Only one of the following for loops will compile? Which is it?

 a)   for(int a=1; b=4; b<6; b++) { }
 b)   for(int x=0, int y=3; x<5;  x++){ }
 c)   int z;
       for(z=0, int t=4; z<10;z++,t++){  }
 d)   for(int x=0, z=1; x<4;x++, z++){ }                     ( d )
 

9 )  What will the following code output?

         labelA:    for(int x=1; x < 3; x++){
                         for(int y=1; y < 3; y++){
                         if(y != 1)
                         continue  labelA;
                        System.out.println(x + " " + y );
                      }
                   }                                                                          // 2 marks
a) 1 1

b) 1 1
    2 2

c) 1 1
    2 1

d) 1 1
    2 2
    3 3                                                   ( c )