1 . What will be the output?
[code lang=”java”]
public class Test1 extends Base{
Test1(long i){}
public static void main(String args[]){}
}
class Base{
Base(int i){}
}
[/code]
Choose the one below:
- Base(){}
- Base(long l){}
- Base(float f){}
- It will compile properly
2 . What will be the output?
[code lang=”java”]
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);
}
}
[/code]
Choose the one below:
- 2
- 4
- Compiler Error
- It will compile if we add super(2) in Test2()
3 . which of the following keywords can be applied to constructors?
- private
- public
- void
- All the above
4 . which of the following are valid constructors?
- Test4(){}
- void Test4(){}
- private Test4(){}
- All the above
5 . which of the following statements are true?
- constructors can be overriden
- constructors can be overloaded
- constructors can return a value
- constructors can be static
6 . What will be the output?
[code lang=”java”]
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);
}
}
[/code]
Choose the one below:
- Int
- Byte
- Short
- Compiler error
7 . What will be the output?
[code lang=”java”]
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);
}
}
[/code]
Choose the one below:
- Byte Int Int
- Byte Byte Byte
- Byte Int Byte
- Int Int Int
8 . What will be the output?
[code lang=”java”]
public class Test8 extends Base{
Test8(){}
public static void main(String args[]){}
}
class Base{
Base() throws Exception{}
}
[/code]
Choose the one below:
- Byte Int Int
- Byte Byte Byte
- Byte Int Byte
- Int Int Int
9 . What will be the output?
[code lang=”java”]
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");
}
}
[/code]
Choose the one below:
- Base
- Compiler Error
- Runtime exception
- None of the above
10 . What will be the output?
[code lang=”java”]
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.");
}
}
[/code]
Choose the one below:
- Base
- Compiler Error
- Runtime exception
- None of the above
11 . What will be the output?
[code lang=”java”]
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; }
}
[/code]
Choose the one below:
- The code will fail to compile
- 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
- class Test11 has three constructors
- 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?
- The default constructor has a return type of void
- The default constructor takes a sungle parameter
- The default constructor takes no parameters
- 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