Mock Exam – Constructor |
Q1 |
public class Test1 extends Base{ Test1(long i){} public static void main(String args[]){} } class Base{ Base(int i){} } Which of the following constructors are if present in Base, will the program will compile properly? |
A1 | Base(){} |
A2 | Base(long l){} |
A3 | Base(float f){} |
A4 | It will compile properly. |
Q2 |
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); } } what will be the output?
|
A1 | 2 |
A2 | 4 |
A3 | Compiler Error |
A4 | It will compile if we add super(2) in Test2(). |
Q3 |
which of the following keywords can be applied to constructors? |
A1 | private |
A2 | public |
A3 | void |
A4 | All the above |
Q4 | which of the following are valid constructors? |
A1 | Test4(){} |
A2 | void Test4(){} |
A3 | private Test4(){} |
A4 | All the above. |
Q5 | which of the following statements are true? |
A1 | constructors can be overriden. |
A2 | constructors can be overloaded |
A3 | constructors can return a value |
A4 | constructors can be static. |
Q6 |
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); } } which of the following statements are true? |
A1 | Int |
A2 | Byte |
A3 | Short |
A4 | Compiler error |
Q7 |
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); } } what will be the output? |
A1 | Byte Int Int |
A2 | Byte Byte Byte |
A3 | Byte Int Byte |
A4 | Int Int Int |
Q8 |
public class Test8 extends Base{ Test8(){} public static void main(String args[]){} } class Base{ Base() throws Exception{} } what is the output? |
A1 | Byte Int Int |
A2 | Byte Byte Byte |
A3 | Byte Int Byte |
A4 | Int Int Int |
Q9 |
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”); } } what is the output? |
A1 | Base |
A2 | Compiler Error. |
A3 | Runtime exception |
A4 | none of the above |
Q10 |
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.”); } } what is the output? |
A1 | Base |
A2 | Compiler Error. |
A3 | Runtime exception |
A4 | none of the above |
11 |
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; } } Select all valid answers: |
A1 | The code will fail to compile. |
A2 |
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. |
A3 | class Test11 has three constructors. |
A4 |
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? |
A1 | The default constructor has a return type of void |
A2 |
The default constructor takes a sungle parameter. |
A3 | The default constructor takes no parameters |
A4 |
The default constructor is not created if the class has anyconstructors of its own. |
Answers |
1 | Base(){} |
2 | It will compile if we add super(2) in Test2(). |
3 |
private public |
4 |
Test4(){} private Test4(){} |
5 | constructors can be overloaded. |
6 | Int |
7 | Byte Byte Int |
8 | Byte Byte Int |
9 | Compiler Error. |
10 | Base |
11 |
B) 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. C)class Test11 has three constructors. |
12 |
3) The default constructor takes no parameters 4) The default constructor is not created if the class has any constructors of its own. |