OCPJP 6 Mock Exam -18

1 . What will be the output?

	public class Test1 extends Base{
    Test1(long i){}
    public static void main(String args[]){}
}
class Base{
    Base(int i){}
}

Choose the one below:

  1. Base(){}
  2. Base(long l){}
  3. Base(float f){}
  4. It will compile properly

2 . What will be the output?

	public class Test2 extends Base{
    Test2(long i){
        super(2);
    }
    Test2(){}
    public static void main(String args[]){
        new Test2(2);
    }
}
class Base{
    Base(int i){
        System.out.println(i);
    }
    Base(byte i){
        System.out.println(i*2);
    }
}

Choose the one below:

  1. 2
  2. 4
  3. Compiler Error
  4. It will compile if we add super(2) in Test2()

3 . which of the following keywords can be applied to constructors?

  1. private
  2. public
  3. void
  4. All the above

4 . which of the following are valid constructors?

  1. Test4(){}
  2. void Test4(){}
  3. private Test4(){}
  4. All the above

5 . which of the following statements are true?

  1. constructors can be overriden
  2. constructors can be overloaded
  3. constructors can return a value
  4. constructors can be static

6 . What will be the output?

	public class Test6{
    Test6(int i, int j){
        System.out.println("Int");
    }
    Test6(short i, short j){
        System.out.println("Short");
    }
    Test6(byte i, byte j){
        System.out.println("Byte");
    }
    public static void main(String args[]){
        Test6 t = new Test6(1,2);
    }
}

Choose the one below:

  1. Int
  2. Byte
  3. Short
  4. Compiler error

7 . What will be the output?

	public class Test7{
    Test7(int i, int j){
        System.out.println("Int");
    }
    Test7(short i, short j){
        System.out.println("Short");
    }
    Test7(byte i, byte j){
        System.out.println("Byte");
    }
    public static void main(String args[]){
        byte b1 = 1;
        byte b2 = 2;
        Test7 t1 = new Test7(b1,b1);
        Test7 t2 = new Test7(b1*1,b1*2);
        Test7 t3 = new Test7(b1*b1,b1*b2);
    }
}

Choose the one below:

  1. Byte Int Int
  2. Byte Byte Byte
  3. Byte Int Byte
  4. Int Int Int

8 . What will be the output?

	public class Test8 extends Base{
    Test8(){}
    public static void main(String args[]){}
}
class Base{
    Base() throws Exception{}
}

Choose the one below:

  1. Byte Int Int
  2. Byte Byte Byte
  3. Byte Int Byte
  4. Int Int Int

9 . What will be the output?

	public class Test9 extends Base{
   Test9()throws Exception{}
   Test9(int i) {}
   public static void main(String args[])throws Exception{
      new Test9();
   }
}
class Base{
   Base() throws Exception{
      System.out.println("Base");
   }
}

Choose the one below:

  1. Base
  2. Compiler Error
  3. Runtime exception
  4. None of the above

10 . What will be the output?

	public class Test10 extends Base{
    Test10(){}
    Test10(int i) {}
    public static void main(String args[]){
        new Test10();
    }
}
class Base{
    Base() {
        System.out.println("Base");
    }
    Base(int i) throws Exception{
        System.out.println("No argument constructor.");
    }
}

Choose the one below:

  1. Base
  2. Compiler Error
  3. Runtime exception
  4. None of the above

11 . What will be the output?

	class Test11 extends B {
    private Test11() { super(); }
    public Test11(String msg) { this(); }
    public Test11(int i) {}
}
class A {
    public A() {}
    public A(int i) { this(); }
}
class B extends A {
    public boolean B(String msg) { return false; }
}

Choose the one below:

  1. The code will fail to compile
  2. The constructor in A that takes an int as argument will never be called as a result of constructing an object of class B or Test11
  3. class Test11 has three constructors
  4. At most one of the constructors of each class is called as a result of constructing an object of class Test11

12 . Which of the following statements are true?

  1. The default constructor has a return type of void
  2. The default constructor takes a sungle parameter
  3. The default constructor takes no parameters
  4. The default constructor is not created if the class has anyconstructors of its own

Answers

1 : 1 is correct. Base(){}.

2 : 4 is correct. It will compile if we add super(2) in Test2().

3 : 1 & 2 is correct.
1 . private
2 . public

4 : 1 & 3 is correct.
1 . Test4(){}
3 . private Test4(){}

5 : 2 is correct. Constructors can be overloaded.

6 : 1 is correct. Int.

7 : 3 is correct. Byte Byte Int.

8 : 3 is correct. Byte Byte Int.

9 : 2 is correct. Compiler Error.

10 : 1 is correct. Base.

11 : 2 & 3 is correct.
2 . The constructor in A that takes an int as argument will never be called as a result of constructing an object of class B or Test11
3 . class Test11 has three constructors

12 : 3 & 4 is correct.
3 . The default constructor takes no parameters
4 . The default constructor is not created if the class has any constructors of its own

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!