latest articles

Recent Posts Widget

java constructors


01.Which line contains a valid constructor in the following class definition?
public class TestClass{
int i, j;
public TestClass getInstance() { return new TestClass(); } //1
public void TestClass(int x, int y) { i = x; j = y; } //2
public TestClass TestClass() { return new TestClass(); } //3
public ~TestClass() { } //4
}
Select 1 option
A. Line 1
B. Line 2
C. Line 3
D. Line 4
E. None of the above.

02.What will be the result of attempting to compile the following program?
public class TestClass{
long l1;
public void TestClass(long pLong) { l1 = pLong ; } //(1)
public static void main(String args[]){
TestClass a, b ;
a = new TestClass(); //(2)
b = new TestClass(5); //(3)
}
}

Select 1 option
A. A compilation error will be encountered at (1), since constructors should not
specify a return value.
B. A compilation error will be encountered at (2), since the class does not have a
default constructor.
C. A compilation error will be encountered at (3).
D. The program will compile correctly.
E. It will not compile because parameter type of the constructor is different than the
type of value passed to it.
03.Which of these statements are true?
Select 2 options:
A. All classes must explicitly define a constructor.
B. A constructor can be declared private.
C. A constructor can declare a return value.
D. A constructor must initialize all the member variables of a class.
E. A constructor can access the non-static members of a class.
Check Answer
04.Note: This question may be considered too advanced for this exam. Given:
class MySuper{
         public MySuper(int i){ }
}
abstract class MySub extends MySuper{
       public MySub(int i){ super(i); }
       public abstract void m1();
}
class MyTest{
        public static void main(String[] args){
              MySub ms = new MySub(){
              public void m1() { System.out.println("In MySub.m1()"); };
              ms.m1();
        }
}
What will be the output when the above code is compiled and run?
Select 1 option
A. It will not compile.
B. It will throw an exception at run time.
C. It will print In MySub.m1()
D. It will compile and run without any exception but will not print anything.
05.Which of the following are true about the "default" constructor?
Select 1 option
A. It is provided by the compiler only if the class and any of its super classes does not
define any constructor.
B. It takes no arguments.
C. A default constructor is used to return a default value.
D. To define a default constructor, you must use the default keyword.
E. It is always public.
06.Which lines contain a valid constructor in the following code?
public class TestClass{
public TestClass(int a, int b) { } // 1
public void TestClass(int a) { } // 2
public TestClass(String s); // 3
private TestClass(String s, int a) { } //4
public TestClass(String s1, String s2) { }; //5
}
Select 3 options
A. Line // 1
B. Line // 2
C. Line // 3
D. Line // 4
E. Line // 5
 07.Under what situations does a class get a default constructor?
Select 1 option
A. All classes in Java get a default constructor.
B. You have to define at least one constructor to get the default constructor.
C. If the class does not define any constructors explicitly.
D. All classes get default constructor from Object class.
E. None of the above.
Check Answer
08.You can call only public and protected constructors of the super class from a subclass
if the subclass is not in the same package because only those are inherited.
Select 1 option:
A. True
B. False
09.Given a class named Test, which of these would be valid definitions for the
constructors for the class?
Select 1 option
A. Test(Test b) { }
B. Test Test( ) { }
C. private final Test( ) { }
D. void Test( ) { }
E. public static void Test(String args[ ] ) { }
Check Answer
10.Given the following code, which of these constructors can be added to class B without
causing a compile time error?
class A{
int i;
public A(int x) { this.i = x; }
}
class B extends A{
int j;
public B(int x, int y) { super(x); this.j = y; }
}
Select 2 options
A. B( ) { }
B. B(int y ) { j = y; }
C. B(int y ) { super(y*2 ); j = y; }
D. B(int y ) { i = y; j = y*2; }
E. B(int z ) { this(z, z); }
Read more ...