OCPJP 6 Mock Exam – 3

SHARE & COMMENT :

Q 1 : What will be the output?

	public class Test1{
    public static void main(String args[]){
        System.out.println(method());
    }
    public static int method(){
       try{
          return 1;
       }
       catch(Exception e){
          return 2;
       }
       finally{
          return 3;
       }
    }
}

Choose the one below:

  1. 1
  2. 2
  3. 3
  4. 0

Q 2 : What will be the output?

	public class Test2{
   public static void main(String args[]){
       System.out.println(method());
   }
   public static int method(){
       try{
           throw new Exception();
           return 1;
       }
       catch(Exception e){
           return 2;
       }
       finally{
           return 3;
       }
    }
}

Choose the one below:

  1. 1
  2. 2
  3. 3
  4. 4
  5. Compiler error

Q 3 : What will be the output?

public class Test3{
    public static void main(String args[]){
        System.out.println(method());
    }
    public static int method(){
        try{
            throw new Exception();
        }
        catch(Exception e){
            throw new Exception();
        }
        finally{
            return 3;
        }
    }
}

Choose the one below:

  1. 3
  2. 0
  3. Runtime Exception
  4. Compiler error

Q 4 : What will be the output?

public class Test4{
    public static void main(String args[]){
        System.out.println(method());
    }
    public static int method(){
        return;
    }
}

Choose the one below:

  1. null
  2. 0
  3. Runtime exception
  4. Compiler error

Q 5 : What will be the output?

import java.io.IOException;
public class Test5{
   public static void main(String args[]){
      try{
          throw new IOException();
      }
      catch(Exception e){
          System.out.println("Excepion");
      }
      catch(IOException e){
          System.out.println("IOExcepion");
      }
       }
}

Choose the one below:

  1. Exception
  2. IOException
  3. Exception IOException
  4. Compilers error

Q 6 : What will be the output?

public class Test6{
    public static void main(String args[]) throws Exception{
        try{
            throw new Exception();
        }
        finally{
            System.out.println("No Error");
        }
     }
}

Choose the one below:

  1. No Error followed by java.lang.Exception
  2. java.lang.Exception followed by No Error
  3. No Error
  4. Compiler Error

Q 7 : What will be the output?

public class Test7{
    public static void main(String args[]) throws Exception{
        Test7 t = new Test7();
        t.method();
        System.out.println("Print");
    }
    public void method()throws Exception{
        throw new Exception();
    }
}

Choose the one below:

  1. Print
  2. Exception thrown at runtime
  3. no output
  4. Compiler Error

Q 8 : What will be the output?

public class Test8{
    public static void main(String args[]) throws Exception{
        Test8 t = new Test8();
        t.method();
        System.out.println("Print");
    }
    public void method(){
        try{
            throw new Exception();
        }catch(Exception e){}
    }
}

Choose the one below:

  1. Print
  2. Exception thrown at runtime
  3. no output
  4. Compiler Error

Q 9 : What will be the output?

public class Test9 extends A{
    public static void main(String args[]) throws Exception{
        Test9 t = new Test9();
    }
}
class A{
    A() throws Exception{
        System.out.println("A Class");
    }
}

Choose the one below:

  1. A Class
  2. Runtimxception
  3. no output
  4. Compiler Error

Q 10 : What will be the output?

public class Test10 extends A{
    Test10()throws Exception{
        System.out.println("Test10 Class");
    }
    public static void main(String args[]) throws Exception{
        Test10 t = new Test10();
    }
}
class A{
    A() throws Exception{
        System.out.println("A Class");
    }
}

Choose the one below:

  1. A Class Test10 Class
  2. Runtimxception
  3. no output
  4. Compiler Error

Q 11 : What will be the output?

public class Test11 extends A{
    Test11()throws Exception{
        System.out.println("Test10 Class");
    }
    Test11(int i){}
    public static void main(String args[]) throws Exception{
        Test11 t = new Test11();
    }
}
class A{
    A() throws Exception{
        System.out.println("A Class");
    }
}

Choose the one below:

  1. A Class Test10 Class
  2. Runtimxception
  3. no output
  4. Compiler Error

Q 12 : What will be the output?

import java.io.IOException;
    public class Test12 extends A{
        public void method() throws Exception{
        System.out.println("Subclass");
    }
    public static void main(String args[]) throws Exception{
        A a = new A();
        a.method();
        a = new Test12();
        a.method();
    }
}
class A{
    public void method() throws IOException{
        System.out.println("Superclass");
    }
}

Choose the one below:

  1. Subclass Superclass
  2. Runtimxception
  3. Superclass Superclass
  4. Compiler Error

Q 13 : What are the legal arguments types for switch?

  1. int
  2. byte
  3. char
  4. All the above.

Q 14 : Which of the following are valif if constructs?

  1. if(2>3){}
  2. if(false){}
  3. if(false){}
  4. if(true)

Q 15 : What will be the output?

public class Test15{
    public static void main(String args[]) throws Exception{
        for (int i = 0;i < 3;i++){
            for (int j = 0;j < 3;j++){
                System.out.print(i);
                System.out.print(j+",");
                break;
            }
         }
     }
}

Choose the one below:

  1. 00,
  2. 00,10,20,
  3. 000102
  4. None of the above

Q 16 : What will be the output?

public class Test16 extends A{
    Test16(){
        System.out.println("Sub");
    }
    public static void main(String args[]) {
        Test16 t = new test16();
    }
}
class A{
    A(int i){
        System.out.println("Super");
    }
}

Choose the one below:

  1. Super Sub
  2. Super
  3. Sub
  4. Compiler Error

Q 17 : What will be the output?

public class Test17 extends A{
    Test17(int i){
        System.out.println(i);
        super(2);
    }
    public static void main(String args[]) {
        Test17 t = new Test17(5);
    }
}
class A{
    A(int i){
        System.out.println(i);
    }
}

Choose the one below:

  1. 5 2
  2. 2 5
  3. 5 5
  4. Compiler error

Q 18 : What will be the output?

public class Test18 {
    Test18(){
        this(7);
    }
    Test18(int i){
        this(1.0);
        Test18(i);
    }
    Test18(float f){
        System.out.println(f * 2);
    }
    Test18(double d){
        System.out.println(d * 3);
    }
    void Test18(int i){
        System.out.println(i);
    }
    public static void main(String args[]) {
        Test18 t = new Test18();
    }
}

Choose the one below:

  1. 3.0 7
  2. 2.0 7
  3. 7 3.0
  4. Compiler Error

Q 19 : What will be the output?

public class Test19 {
    float f;
    Test19(){
       this(f);
       f = 3;
    }
    Test19(float f){
       System.out.println(f);
    }
    public static void main(String args[]) {
       Test19 t = new Test19();
    }
}

Choose the one below:

  1. 0.0
  2. 0
  3. 3
  4. Compiler error

Q 20 : What will be the output?

public class Test20 extends A{
    Test20(){
        this("Hi");
    }
    Test20(String str){
        System.out.println(str);
    }
    public static void main(String args[]) {
        Test20 t = new Test20();
    }
}
class A{
    A(){
        System.out.println("Super");
    }
}

Choose the one below:

  1. Super Hi
  2. Hi Super
  3. Super
  4. Compiler Error

Q 21 : What will be the output?

public class Test21{
    public static void main(String args[]) {
        Test21 t;
        t.method();
    }
    public static void method(){
        System.out.println("NullPointerException");
    }
}

Choose the one below:

  1. Nothing is printed.
  2. RuntimeException
  3. NullPointerException
  4. Compiler Error

Q 22 : What will be the output?

public class Test22{
    public static void main(String args[]) {
        Test22 t = null;
        t.method();
    }
    public static void method(){
        System.out.println("NullPointerException");
    }
}

Choose the one below:

  1. Nothing is printed.
  2. RuntimeException
  3. NullPointerException
  4. Compiler Error

Q 23 : Which of the following modifiers are allowed in constructor?

  1. private
  2. default
  3. public
  4. static

Q 24 : Which of the following modifiers are allowed for top-level classes?

  1. private
  2. static
  3. public
  4. strictfp

Q 25 : which one of the keyword cannot be used with instance variables?

  1. transient
  2. volatile
  3. synchronized
  4. abstract

Answers

Q 1 : 3 is correct. 3.

Q 2 : 4 is correct. Compiler Error.

Q 3 : 4 is correct. Compiler Error.

Q 4 : 4 is correct. Compiler Error.

Q 5 : 4 is correct. Compiler Error.

Q 6 : 1 is correct. No Error followed by java.lang.Exception

Q 7 : 2 is correct. Exception thrown at runtime.

Q 8 : 1 is correct. Print.

Q 9 : 4 is correct. Compiler Error.

Q 10 : 1 is correct. A Class Test10 Class.

Q 11 : 4 is correct. Compiler Error.

Q 12 : 4 is correct. Compiler Error.

Q 13 : 4 is correct. All the above.

Q 14 : 1 is correct. if(2>3){}
2 is correct. if(false){}
4 is correct. if(true){}

Q 15 : 2 is correct. 00,10,20,

Q 16 : 4 is correct. Compiler Error.

Q 17 : 4 is correct. Compiler Error.

Q 18 : 1 is correct. 3.0 7

Q 19 : 4 is correct. Compiler Error.

Q 20 : 1 is correct. Super Hi.

Q 21 : 4 is correct. Compiler Error.

Q 22 : 3 is correct. NullPointerException.

Q 23 : 1 is correct. private.
3 is correct. public.

Q 24 : 4 is correct. strictfp.
3 is correct. public.

Q 25 : 3 is correct. synchronized.
4 is correct. abstrct.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better