Sun Certified Java Programmer

Practice Exam (11 - 20 Questions)



Question 11
What will happen if you compile/run the following code?

    1:    public class Q11
    2:    {
    3:        static String str1 = "main method with String[] args";
    4:        static String str2 = "main method with int[] args";
    5:
    6:        public static void main(String[] args)
    7:        {
    8:            System.out.println(str1);
    9:        }
    10:        
    11:       public static void main(int[] args)
    12:       {
    13:           System.out.println(str2);
    14:       }        
    15:    }

    A) Duplicate method main(), compilation error at line 6.
    B) Duplicate method main(), compilation error at line 11.
    C) Prints "main method with main String[] args".
    D) Prints "main method with main int[] args".

    Answer

Question 12
What is the output of the following code?

    1:    class Test
    2:    {
    3:        Test(int i)
    4:        {
    5:            System.out.println("Test(" +i +")");
    6:        }
    7:    }
    8:
    9:    public class Q12
    10:   {
    11:        static Test  t1 = new Test(1);
    12:
    13:        Test         t2 = new Test(2);
    14:
    15:        static Test  t3 = new Test(3);
    16:
    17:        public static void main(String[] args)
    18:        {    
    19:            Q12 Q = new Q12();
    20:        }
    21:    }	

    A) Test(1)
       Test(2)
       Test(3)

    B) Test(3)
       Test(2)
       Test(1)
   
   C) Test(2)
      Test(1) 
      Test(3)
   
   D) Test(1)
      Test(3) 
      Test(2)
      
      Answer

Question 13
What is the output of the following code?

    1:    int i = 16;
    2:    int j = 17;
    3:        
    4:    System.out.println("i >> 1  =  " + (i >> 1));
    5:    System.out.println("j >> 1  =  " + (j >> 1));

    A) Prints  "i >> 1 = 8"
               "j >> 1 = 8"
           
    B) Prints  "i >> 1 = 7"
               "j >> 1 = 7"

    C) Prints  "i >> 1 = 8"
               "j >> 1 = 9"

    D) Prints  "i >> 1 = 7"
               "j >> 1 = 8"

    Answer               

Question 14
What is the output of the following code?

    1:    int i = 45678;
    2:    int j = ~i;
    3:
    4:    System.out.println(j);

    A) Compilation error at line 2. ~ operator applicable to boolean values only.
    B) Prints 45677.
    C) Prints -45677.
    D) Prints -45679.