Mock Exam for Java 2 Programmer

This is a set of questions for those who are preparing for the java 2 programmer exam (SCJP)
The answers are at the bottom of this page.


Q1-The following program is in file Test.java, what would be the output
        of compiling and running this program ?

        class MyFirstClass{
                MyFirstClass(){
                        System.out.println("MyFirstClass created");
                }
        }

        public class Test{
                public static void main(String str[]){
                MyFirstClass mf = new MyFirstClass();
                MyThirdClass mf = new MyThirdClass();
                }
        }

        public class MyThirdClass{
                MyThirdClass(){
                        System.out.println("MyThirdClass created");
                }
        }

a) compiles, runs and does not print anything
b) does not compile
c) compiles, runs and prints "MyFirstClass created"
d) compiles, runs and prints "MyFirstClass created" and "MyThirdClass created"



Q2-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

        public class Test implements Runnable{
                public static void main(String str[]){
                Thread t = new Thread(new Test());
                }

                public void run(){
                        for(int i=0; i<10; i++){
                        System.out.println("t ");
                        }
                }
        }
 

a) does not compile
b) compiles, runs and prints " t t t t t t t t t t "
c) compiles and does not run
d) compiles, runs and does not print anything



Q3-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

        public class Test {
                public static void main(String str[]){
                System.out.print(5+5+"s"+6+6);
                }
        }

a) compiles, runs and prints " 10s66 "
b) compiles, runs and prints " 10s12 "
c) compiles, runs and prints " 55s66 "
d) compiles, runs and does not print anything



Q4-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

        public class Test {
                static private String st;
                public static void main(String str[]){
                        System.out.print(st+"yes");
                }
        }

a) compiles, runs and prints " yes "
b) does not compiles
c) compiles, runs and prints " nullyes "
d) compiles, runs and prints " null+yes "



Q5-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

        public class Test {
                public Test(float f){
                        printMessage(f);
                }

                public void printMessage(float f){
                        System.out.println("object "+"number "+f+" created");
                }

                public static void main(String str[]){
                        Test t1;
                        Test t2 = new Test(1+9);
                        t1 = t2;
                }
        }

a) compiles, runs and prints " object number 10 created "
b) does not compiles
c) compiles, runs and prints " object number 1+9 created "
d) compiles, runs and prints " object number 10.0 created "



Q6-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

        public class Test {
                public static void main(String st[]){
                        Boolean b1 = new Boolean("true");
                        Boolean b2 = new Boolean(false);
                        System.out.println(b1);
                        System.out.println(b2);
                }

}

a) compiles, runs and prints
true
false
b) does not compile
c) compiles but runtime error
d) compiles, runs and does not print anything



Q7-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

        public class Test {
                public static void main(String st[]){
                        int i = -1>>>32;
                        System.out.println(i);
                }

}

a) compiles, runs and prints "0"
b) compiles, runs and prints "-1"
c) compiles, runs and prints "1"
d) compiles, runs and prints "-2"



Q8-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

        class MyFirstClass{
                public MyFirstClass(int k){
                        System.out.println("we are in MyFirstClass");
                }
        }

        public class Test extends MyFirstClass {

                public Test(){
                        System.out.println(",we are in Test");
                }

                public static void main(String st[]){
                        Test t1 = new Test();
                }
        }

a) compiles, runs and prints ", we are in Test we are in MyFirstClass"
b) compiles, runs and prints "we are in Test"
c) compiles, runs and prints "we are in MyFirstClass,we are in Test"
d) does not compile



Q9-The following program is compiled and run, what is printed
        on the Frame when button b2 is clicked?
 
 

        import java.awt.event.*;

        public class Test extends Frame implements ActionListener{

                private Button b1;
                private Button b2;
                private String message="message";

                public Test(){
                        setSize(300,300);
                        setLayout(new FlowLayout());
                        b1=new Button("b1");
                        b2=new Button("b2");
                        add(b1);
                        add(b2);
                        b1.addActionListener(this);
                        b2.addActionListener(this);
                        setVisible(true);
                }

                public void actionPerformed(ActionEvent ae){
                        if(ae.getSource()==b1) message=" b1 clicked";
                        if(ae.getSource()==b2) message=" b2 clicked";
                        repaint();
                }

                public void paint(Graphics g){
                        g.drawString(message,100,100);
                }

                public static void main(String st[]){
                        Test t1 = new Test();
                }
        }
 

a) "message"
b) "b1 clicked"
c) "b2 clicked"
d) "message b2 clicked"



Q10-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

        public class Test {

                public static void main(String st[]){
                        String s1 = "java";
                        String s2 = "java";
                        String s3 = "JAVA";
                        s2.toUpperCase();
                        s3.toUpperCase();
                        boolean b1 = s1==s2;
                        boolean b2 = s1==s3;
                        System.out.print(b1);
                        System.out.print(" "+b2);
                }
        }
 

a) compiles, runs and prints "true false"
b) compiles, runs and prints "true true"
c) compiles, runs and prints "false false"
d) compiles, runs and prints "false true"



Q11-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

        public class Test {

                public static void main(String st[]){
                        int j;
                        bb:
                        for(int i=1; i<10; i++){
                                for( j=1; j<3; j++){
                                        if(i>j) break bb;
                                        System.out.print(i+" ");
                                }
                        }
                }
        }
 

a) compiles, runs and prints "1 1 2 2"
b) compiles, runs and prints "1 1 1 1"
c) compiles, runs and prints "1 2"
d) compiles, runs and prints "1 1"



Q12-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

        public class Test {

                public static void main(String st[]){
                        byte b1 = 23;
                        byte b2 = 34;
                        System.out.print(b1*b2);
                }
}
 

a) does not compile
b) compiles, runs and prints "2334"
c) compiles, runs and prints "782"
d) compiles, runs and prints "b1b2"



Q13-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

        public class Test {

                public static void main(String st[]){
                        byte b1 = 23;
                        byte b2 = 34;
                        byte b3 = b1*b2;
                        System.out.print(b3);
                }
}
 

a) does not compile
b) compiles, runs and prints "2334"
c) compiles, runs and prints "782"
d) compiles, runs and prints "b1b2"



Q14-Which of the following codes is(are) ILLEGAL ?
 

a)      interface Int1{
                void meth1();
                void meth2();
        }
        public class Test implements Int1{
        public void meth1(){}
        public void meth2(){}
        public static void main(String st[]){
                }
        }

b)      interface Int1{
                void meth1();
                void meth2();
        }
        public class Test implements Int1{
        void meth1(){}
        void meth2(){}
        public static void main(String st[]){
                }
        }

c)      interface Int1{
                void meth1(){};
                void meth2();
        }
        public class Test implements Int1{
                public void meth1(){}
                public void meth2(){}
                public static void main(String st[]){
                }
        }
d)      interface Int1{
                void meth1();
                void meth2();
        }
        public class Test extends Int1{
        public void meth1(){}
        public void meth2(){}
        public static void main(String st[]){
                }
        }
 
 



Q15-Which of the following codes do(es) not compile ?
 

a)      class MyFirstClass {
                void meth1(){};
                void meth2(){};
        }

        abstract class MySecondClass {
                void meth4();
                void meth5();
        }
        public class Test extends MyFirstClass{
                public static void main(String st[]){
                }
        }

b)      class MyFirstClass {
                void meth1(){};
                void meth2(){};
        }

        abstract class MySecondClass {
                abstract void meth4();
                abstract void meth5();
        }
        public class Test extends MySecondClass{
                public static void main(String st[]){
                }
        }

c)      class MyFirstClass {
                void meth1(){};
                void meth2(){};
        }

        abstract class MySecondClass {
                abstract void meth4();
                abstract void meth5();
        }
        abstract public class Test extends MySecondClass{
                public static void main(String st[]){
                }
        }

d)      class MyFirstClass {
                void meth1(){};
                void meth2(){};
        }

        abstract class MySecondClass {
                abstract void meth4();
                bstract void meth5();
        }
        public class Test extends MyFirstClass{
                public static void main(String st[]){
                }
        }



 

Q16-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

public class Test {
  public static void main(String st[]){
     Test t1 = new Test();
     t1.method1(5,0);
  }

  public void method1(int a, int b){
     try{
        int c = a/b;
        System.out.println("after division");
     } catch (ArithmeticException ae){System.out.println("Arithmetic Exception");}
       catch (Exception e){System.out.println("Exception");}
       finally {System.out.println("Finally");}

     System.out.println("after finally");

  }
}
 

a) compiles, runs and prints
                after division
                Arithmetic Exception
                Exception
                Finally
                after finally

b) compiles, runs and prints
                after division
                Arithmetic Exception
                Finally
                after finally
c) compiles, runs and prints
                Arithmetic Exception
                Finally
                after finally
d) compiles, runs and prints
                Arithmetic Exception
                Exception
                Finally
                after finally



Q17-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

public class Test {
  public static void main(String st[]){
     Test t1 = new Test();
     try{
     t1.method1(5,0);
     } catch (ArithmeticException ae){System.out.println("Arithmetic Exception");}
       catch (Exception e){System.out.println("Exception");}
       finally {System.out.println("Finally");}
  }

  public void method1(int a, int b){
        int c = a/b;
        System.out.println("after division");
  }
}
 

a) compiles, runs and prints
                after division
                Arithmetic Exception
                Exception
                Finally
                after finally

b) compiles, runs and prints
                after division
                Arithmetic Exception
                Finally
                after finally
c) compiles, runs and prints
                Arithmetic Exception
                Finally
                after finally
d) compiles, runs and prints
                Arithmetic Exception
                Finally



Q18-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

public class Test {

  public static void main(String st[]){
     Test t2 = new Test();
     t2.method2();
  }

  public void method2(){
     int x=1;
     boolean b1=true;
     boolean b2=false;
     if((b1&b2)&&(++x<4))   System.out.println("x="+x);
     if((!b1|b2)||(++x<4))  System.out.println("x="+x);
  }
}
 

a) does not compile.
b) compiles, runs and prints "x=2"
c) compiles, runs and prints "x=1"
d) compiles, runs and prints "x=3"



Q19-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

public class Test {

  public static void main(String st[]){
     Test t3 = new Test();
     t3.method2();
  }

  public void method2(){
     double x = - 99.5;
     System.out.println("output is ="+Math.abs(Math.ceil(Math.floor(x))));
  }
}
 

a) compiles, runs and prints "output is =100.0"
b) compiles, runs and prints "output is =99.5"
c) compiles, runs and prints "output is =100"
d) compiles, runs and prints "output is =-100.0"



Q20-Which of the statements a) b) c) d) would compile if inserted at line 5
 

import java.io.*;

1: public class Test {
2:   public static void main(String st[]) throws IOException{
3:     String f="file1.java";
4:     File f1 = new File(f);
5:     // here
6:  }
7: }
 

a) InputStream iout = new InputStream(f1);
b) InputStream iout = new FileInputStream(f1);
c) FileOutputStream iout = new FileOutputStream(f1);
d) Reader iout = new FileReader(f1);



Q21-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

public class Test {
   public int x = 99;
   private String s = "Hi";
   public void method1(){
      int x = 66;
      class MyClass{
         public float y = 45.9f;
         public void method2(){
            System.out.println("x = "+x);
         }
      }
      MyClass m = new MyClass();
      m.method2();
   }
   public static void main(String st[]){
     Test t1 = new Test();
     t1.method1();
   }
}
 

a) compiles, runs and prints "99"
b) compiles, runs and prints "66"
c) does not compile
d) compiles but develops a runtime error.



Q22-Which of the statements a) b) c) d) would compile and run
if inserted at line 4.
 

1: public class Test {
2:    public static void main(String st[]){
3:    String []s[] = {{"a","b"},{"c","d","e"}};
4:        // here
5:  }
6: }
 

a) System.out.println(s[0][1]);
b) System.out.println(s[0][2]);
c) System.out.println(s[1][2]);
d) System.out.println(s[1][3]);

Q23-Which of the statements would NOT compile ?

a) int x = (int)56L;
b) float f = 9.9999;
c) char c =(byte)23;
d) boolean b = False;



Q24-The following program is in file Test.java, what would be the output
        of compiling and running this program ?
 

public class Test {
   public static void main(String st[]){
     int x = 7;
     int y = 8;
     int z = 2*x|y-1>>2;
     System.out.println("z="+z);
  }
}
 

a) does not compile.
b) compiles, runs and prints "z=14"
c) compiles, runs and prints "z=15"
d) compiles, runs and prints "z=16"



Q25-Which of the statements a) b) c) d) would NOT compile if inserted at line 6
 

1: class MyFirstClass {
2:   public int x = 1;
3:   private float y = 3.4f;
4:   protected String s = "hello";
5:   private void meth1(){
6:      // here
7:   }
8: }
9: public class Test extends MyFirstClass{
10:   public void show(){
11:      System.out.println(y);
12:   }
13:   public static void main(String st[]){
14:      new Test().show();
15:  }
16: }
 

a) System.out.println(y);
b) System.out.println(x);
c) meth1();
d) System.out.println(s);



Q26-The following program is in file Test.java, what would be the output
        of compiling and running this program ?

class MyFirstClass{
    public MyFirstClass(){
       System.out.println("Hi");
       show();
    }

    public void show(){
       System.out.println("in class MyFirstClass");
    }
}

public class Test extends MyFirstClass{
   public Test(){
      System.out.println("Hello");
   }
   public void show(){
      System.out.println("in class Test");
   }
   public static void main(String st[]){
      new Test();
  }
}

a) compiles, runs and prints
        Hello
        Hi
        in class MyFirstClass
b) compiles, runs and prints
        Hi
        Hello
        in class MyFirstClass
c) compiles, runs and prints
        Hi
        in class MyFirstClass
        Hello
d) compiles, runs and prints
        Hi
        in class Test
        Hello



Q27-The following program is in file Test.java, what would be the output
        of compiling and running this program ?

public class Test {
   int x = 0;
   float f = 0.0f;
   public Test(int a, float b){
      this.x = a;
      this.f = b;
   }
   public static void main(String st[]){
      Test t1 = new Test(1,9.9f);
      Test t2 = new Test(2,8.8f);
      Test t3 = t1.swap(t1,t2);
      System.out.println(t1.x+"  "+t1.f);
      System.out.println(t3.x+"  "+t3.f);
  }
  public Test swap(Test t1, Test t2){
      Test te = t1;
      t1 = t2;
      t2 = te;
      Test t3 = new Test(0,0.0f);
      t3.x = t2.x;
      t3.f = t2.f;
      return t3;
}
}

a) compiles, runs and prints
        1  9.9
        1  9.9
b) compiles, runs and prints
        1  9.9
        0  0.0
c) compiles, runs and prints
        1  9.9
        2  8.8
d) compiles, runs and prints
        2  8.8
        2  8.8



Q28-When you invoke repaint() for a component, which method is called?

a) paint
b) update
c) repaint
d) setVisible



Q29- Which of these names for variables are ILLEGAL ?

a) +var
b) #var
c) $var
d) _9var



Q30-The following program is in file Test.java, what would be the output
        of compiling and running this program ?

public class Test {
   public static void main(String st[]){
        String s1 = "JAVAIT";
        String s2 = "javaIT";
        String s3 = s2.toUpperCase();
        boolean b1 = s1.equals(s2);
        boolean b2 = s1.equalsIgnoreCase(s2);
        boolean b3 = s3.equals(s1);
        System.out.println(b1+"  "+b2+"  "+b3);
}
}

a) compiles, runs and prints "true  true  true"
b) compiles, runs and prints "false  true  false"
c) compiles, runs and prints "true  false  true"
d) compiles, runs and prints "false  true  true"



Q31-The following program is in file Test.java, what would be the output
        of compiling and running this program ?

import java.awt.*;

public class Test extends Frame{
   public Test(){
      setSize(200,200);
   }
   public void paint(Graphics g){
      g.drawString("Maria",50,50);
   }
   public static void main(String st[]){
        new Test();
}
}

a) compiles, runs and a frame shows up with "Maria" printed
b) compiles, runs, but no frame shows up
c) does not compile
d) compiles but does not run



Q32-The following program is in file Test.java, what would be the output
        of compiling and running this program ?

class PrintMess{
    synchronized public void show(String mess){
        try{
           Thread.sleep(1000);
        } catch (InterruptedException ie){}
        System.out.print(mess);
    }
}
class MyThread implements Runnable{
   String mess = null;
   PrintMess p = null;
   Thread t;
   public MyThread(PrintMess a, String b){
      p = a;
      mess = b;
      t = new Thread(this);
      t.start();
   }
   public void run(){
      for(int i=0; i<5; i++)
      p.show(mess);
   }
}
public class Test {
   public static void main(String st[]){
        PrintMess p = new PrintMess();
        MyThread m1 = new MyThread(p,"a");
        MyThread m2 = new MyThread(p,"b");
}
}

a) compiles, runs and prints "aaaaabbbbb"
b) compiles, runs and prints "ababababab"
c) compiles, runs and prints "bbbbbaaaaa"
d) compiles, runs and prints "baabbabbaa"



Q33-The following program is in file Test.java, what would be the output
        of compiling and running this program ?

public class Test {
   public static void main(String st[]){
   l1:
   for(int i=4; i<=7; i++)
   switch(i){
        default:{System.out.println("default");}
        case 4: {System.out.println("case 4");}
        break;
        case 5: {System.out.println("case 5");} break l1;
        case 6: {System.out.println("case 6");}
        case 7: {System.out.println("case 7");}
        }
}
}

a) compiles, runs and prints "default case 4 "
b) compiles, runs and prints "default case 4 case 5"
c) compiles, runs and prints "case 4 case 5"
d) compiles, runs and prints "case 4 case 5 case 6 case 7"



Q34-The following program is in file Test.java, what would be the output
        of compiling and running this program ?

public class Test {
   public static void main(String st[]){

      int x = 0;
      do {
         System.out.println("while");
         x = ++x + 1 + x++ ;
      }
        while(x<=8);
}
}

a) compiles, runs and prints "while while"
b) compiles, runs and prints "while while while"
c) does not compile
d) compiles but does not run



Q35-The following program is in file Test.java, what would be the output
        of compiling and running this program ?

public class Test {
   public static void main(String st[]){

      for(int i=0; i<=1 ; i++)
         {//j++;
         for(int j=0; j<=3; j++)
            if(j>=2) break;
            System.out.print(i+""+j);
         }

   }
}

a) compiles, runs and prints "00011011"
b) compiles, runs and prints "0001"
c) compiles, runs and prints "0001101112"
d) does not compile



Q36-The following program is in file Test.java, what would be the output
        of compiling and running this program ?

public class Test {
   public static void main(String st[]){
      int x = 1;
      int y = 2;
      System.out.println( x == y ? "x equals y" : " x not equal to y" );
}
}

a) compiles, runs and prints "x equals y"
b) compiles, runs and prints "x not equal to y"
c) does not compile
d) compiles, runs and prints "x == y ? "x equals y" : " x not equal to y"



Q37-The following program is in file Test.java, what would be the output
        of compiling and running this program ?

public class Test {
   float f = 2.3f;
   long l = 234;
   public Test(float a, long b){
      f = a;
      l = b;
   }
   public static void main(String st[]){
      Test t1 = new Test(4.5f , 34);
      Test t2 = new Test(6.7f , 234);
      Test t3 = new Test(7.6f , 49);
      t2 = t1 = t3;
      System.out.println(t2.f +" "+ t2.l);
}
}

a) compiles, runs and prints "7.6 49"
b) compiles, runs and prints "6.7 234"
c) does not compile
d) compiles, runs and prints "4.5 34"



Q38-The following program is in file Test.java, what would be the output
        of compiling and running this program ?

public class Test {
   public void meth1(int b, String s){
            System.out.println("meth1 int String");
   }
   public void meth1(long b, String s){
            System.out.println("meth1 long String");
   }
   public String meth1(long b, String s){
            System.out.println("meth1 long String + return ");
            return s;
   }
   public static void main(String st[]){
      new Test().meth1(2,"Hi");
}
}

a) does not compile
b) compiles, runs and prints "meth1 int String"
c) compiles, runs and prints "meth1 long String + return"
d) compiles, runs and prints "meth1 long String"



Answers
Answer to Q1:   b)
Answer to Q2:   d)
Answer to Q3:   a)
Answer to Q4:   c)
Answer to Q5:   d)
Answer to Q6:   a)
Answer to Q7:   b)
Answer to Q8:   d)
Answer to Q9:   c)
Answer to Q10:  a)
Answer to Q11:  d)
Answer to Q12:  c)
Answer to Q13:  a)
Answer to Q14:  b)  c)  d)
Answer to Q15:  a)  b)
Answer to Q16:  c)
Answer to Q17:  d)
Answer to Q18:  b)
Answer to Q19:  a)
Answer to Q20:  b) c) d)
Answer to Q21:  c)
Answer to Q22:  a) c)
Answer to Q23:  b) d)
Answer to Q24:  c)
Answer to Q25:  a) c)
Answer to Q26:  d)
Answer to Q27:  a)
Answer to Q28:  b)
Answer to Q29:  a) b)
Answer to Q30:  d)
Answer to Q31:  b)
Answer to Q32:  b)
Answer to Q33:  c)
Answer to Q34:  a)
Answer to Q35:  d)
Answer to Q36:  b)
Answer to Q37:  a)
Answer to Q38:  a)

Related:

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.