Sun Certified Java Programmer

Practice Exam

Created by Sreenivasa Kumar Majji

Paper 1

Paper 2

Paper 3


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.
    
    Answer

Question 15
What will happen when you invoke the following method?

    1:    void infiniteLoop()
    2:    {
    3:        byte b = 1;
    4:        
    5:        while ( ++b > 0 )
    6:            ;
    7:        System.out.println("Welcome to Java");
    8:    }
    
    A) The loop never ends(infiniteLoop).
    B) Prints "Welcome to Java".
    C) Compilation error at line 5. ++ operator should not be used for byte type variables.
    D) Prints nothing.
    
    Answer

Question 16
In the following applet, how many Buttons will be displayed?
    
    1:    import java.applet.*;
    2:    import java.awt.*;
    3:
    4:    public class Q16 extends Applet
    5:    {
    6:        Button okButton = new Button("Ok");
    7:
    8:        public void init()
    9:        {
    10:            add(okButton);
    11:            add(okButton);
    12:            add(okButton);
    13:            add(okButton);
    14:
    15:            add(new Button("Cancel"));
    16:            add(new Button("Cancel"));
    17:            add(new Button("Cancel"));
    18:            add(new Button("Cancel"));
    19:
    20:            setSize(300,300);
    21:        }
    22:    }

    A) 1 Button with label "Ok" and 1 Button with label "Cancel" .
    B) 1 Button with label "Ok" and 4 Buttons with label "Cancel" .
    C) 4 Buttons with label "Ok" and 1 Button with label "Cancel" .
    D) 4 Buttons with label "Ok" and 4 Buttons with label "Cancel" .
    
    Answer

Question 17
In the following, which is correct Container-Default layout combination?

    A) Applet - FlowLayout
    B) Applet - BorderLayout
    C) Applet - CardLayout
    D) Frame - Flowlayout
    E) Frame - BorderLayout
    F) Frame - CardLayout
    G) Panel - FlowLayout
    H) Panel - BorderLayout.
    
    Answer

Question 18
What is the output of the following code?

    1:    String str = "Welcome";
    2:
    3:    str.concat(" to Java!");
    4:
    5:    System.out.println(str);

    A) Strings are immutable, compilation error at line 3.
    B) Strings are immutable, runtime exception at line 3.
    C) Prints "Welcome".
    D) Prints "Welcome to Java!".
    
    Answer

Question 19
What is the output of the following code?

    1:    class MyClass
    2:    {
    3:        static int maxElements;
    4:    
    5:        MyClass(int maxElements)
    6:        {
    7:            this.maxElements = maxElements;
    8:        }
    9:    
    10:    }
    11:
    12:    public class Q19
    13:    {
    14:        public static void main(String[] args)
    15:        {
    16:    
    17:            MyClass a = new MyClass(100);
    18:            MyClass b = new MyClass(100);
    19:        
    20:            if(a.equals(b))
    21:                System.out.println("Objects have the same values");
    22:            else
    23:                System.out.println("Objects have different values");
    24:        }
    25:    }

    A) Compilation error at line 20. equals() method was not defined.
    B) Compiles fine, runtime exception at line 20.
    C) Prints "Objects have the same values".
    D) Prints "Objects have different values";
    
    Answer

Question 20
    1:    import java.applet.*;
    2:    import java.awt.*;
    3:
    4:    public class Q20 extends Applet
    5:    {
    6:        Button  okButton = new Button("Ok");
    7:    
    8:        public void init()
    9:        {
    10:            setLayout(new BorderLayout());
    11:        
    12:            add("South",  okButton);
    13:            add("North",  okButton);
    14:            add("East",   okButton);
    15:            add("West",   okButton);
    16:            add("Center", okButon);
    17:
    18:            setSize(300,300);
    19:        }
    20:    }

    The above Applet will display

    A) Five Buttons with label "Ok" at Top, Bottom, Right, Left and Center of the Applet.
    B) Only one Button with label "Ok" at the Top of the Applet.
    C) Only one Button with label "Ok" at the Bottom of the applet.
    D) Only one Button with label "Ok" at the Center of the Applet.
    
    Answer

Question No 11
    C. Here the main method was overloaded, so it won't give compilation error. 

Back to Question 11

Question No 12
    D. No matter where they declared, static variables will be intitialized before non-static variables.
Back to Question 12

Question No 13
    A. 16 >> 1 is 8 and 17 >> 1 also 8.
Back to Question 13

Question No 14
    D. Java allows you to use ~ operator for integer type variables.  The simple way to calculate is ~i  = (- i) - 1.
Back to Question 14

Question No 15
    B. Here the variable 'b' will go upto 127. 
       After that overflow will occur, so 'b' will be set to -ve value, the loop ends and prints "Welcome to Java"
Back to Question 15

Question No 16
    B.
Back to Question 16

Question No 17
    A, E and G. For Applets and Panels FlowLayout is the default one, BorderLayout is default for Window and Frames.
Back to Question 17

Question No 18
    C. Strings are immutable. So str.concat("to Java!") will not append anything to str. 
       Infact it will create another string "Welcome to Java!" and leaves it.
Back to Question 18

Question No 19
    D. equals() method was available in base class Object. So it won't give any compilation error.
       Here MyClass is a user-defined class, so the user has to implement equals() method according to his requirments.
Back to Question 19

Question No 20
    D.
Back to Question 20
This page was accessed this times
1