latest articles

Recent Posts Widget

Constructors ( QCM 1 Answered)


01. Correct Option is : E
A. Line 1
This cannot be a constructor because even the name of the method ( getInstance )
is not same as the class name!
B. Line 2
Constructors cannot return anything. Not even void.
C. Line 3
Constructors cannot return anything. Not even void.
D. Line 4
This could have been a destructor in C++ world. And there nothing like this in java. Java has a finalize() method, which is similar to a destructor but does not work exactly as a destructor.
E. None of the above.
02.Correct Option is : C
A. A compilation error will be encountered at (1), since constructors should not specify a return value. But it becomes a valid method if you give a return type.
B. A compilation error will be encountered at (2), since the class does not have a default constructor. The class has an implicit default constructor since the class doesn't have any constructor defined.
C. A compilation error will be encountered at (3). Because (1) is a method and not a constructor. So there is no constructor that take a parameter.
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. If (1) was a valid constructor 'int' would be promoted to long at the time of passing.
Explanation:
The declaration at (1) declares a method, not a constructor because it has a return value. The method happens to have the same name as the class, but that is ok. The class has an implicit default constructor since the class contains no constructor declarations. This allows the instantiation at (2) to work.
03.Correct Options are : B E
A. All classes must explicitly define a constructor. A default no args one will be provided if not defined any.
B. A constructor can be declared private. This feature is used for implementing Singleton Classes.
C. A constructor can declare a return value.
D. A constructor must initialize all the member variables of a class. All non-final instance variables get default values if not explicitly initialized.
E. A constructor can access the non-static members of a class. A constructor is non-static, and so it can access directly both the static and nonstatic members of the class.
Explanation:
Constructors need not initialize *all* the member variables of the class. A non-final member(i.e. an instance) variable will be assigned a default value if not explicitly initialized.
Back to Questions 
04.Correct Option is : A
A. It will not compile. When you define and instantiate an anonymous inner class for an abstract class as being done here, the anonymous class is actually a subclass of the abstract class (in this case, it is a subclass of MySub). Now, since the anonymous class does not define any constructor, the compiler will add the default no-args constructor to the anonymous class, which will try to call the no-args constructor of its super class MySub. But MySub doesn't have any no-args constructor. Therefore, it will not compile. The anonymous inner class should have been created like this:
MySub ms = new MySub( someInteger ){ ... };
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. 
Back to Questions
05.Correct Option is : B
A. It is provided by the compiler only if the class and any of its super classes does not define any constructor. It is provided by the compiler if the class does not define any constructor. It is
immaterial if the super class provides a constructor or not.

B. It takes no arguments.
C. A default constructor is used to return a default value. A constructor does not return any value at all. It is meant to initialize the state of an object.
D. To define a default constructor, you must use the default keyword.
E. It is always public. The access type of a default constructor is same as the access type of the class. Thus, if a class is public, the default constructor will be public.
Explanation:
The default constructor is provided by the compiler only when a class does not define
ANY constructor explicitly. For example,
public class A{
public A() //This constructor is automatically inserted by the compiler {
super(); //Note that it calls the super class's default no-args constructor.
}
}public class A{
/**Compiler will not generate any constructor because the programmer has defined public A(int i) do something **/
}
}
06.Correct Options are : A D E
A. Line // 1
B. Line // 2 Constructors cannot return anything. Not even void.
C. Line // 3 Constructors cannot have empty bodies (i.e. they cannot be abstract)
D. Line // 4 You can apply public, private, protected to a constructor. But not static, final,
synchronized, native and abstract.
E. Line // 5 The compiler ignores the extra semi-colon.
Explanation:
It is interesting to note that public void TestClass(int a) {} // 2 will actually compile. It is not a constructor, but compiler considers it as a valid method! 
Back to Questions
07.Correct Option is : C
A. All classes in Java get a default constructor. No. If a class defines a constructor explicitly, it will not get the default constructor.
B. You have to define at least one constructor to get the default constructor. A default (no args one) will be given if the class doesn't define any.
C. If the class does not define any constructors explicitly. In this case, the compiler will add a no args constructor for this class.
D. All classes get default constructor from Object class. Constructors are NEVER inherited.
E. None of the above. 
Back to Questions
08.Correct Option is : B
A. True
B. False
Explanation:
Most of the statement is correct but remember that constructors are NEVER (whether public or otherwise) inherited. (The above statement is true for other methods though.) So, if you have a class :
class A{
public A(int i){}
}
and another class:
class B extends A{
}
You cannot do : new B(10); because that is A's constructor and it is not inherited by B. To invoke A's constructor you have to do:
class B extends A{
public B(int i){ super(i); }

09.Correct Option is : A
A. Test(Test b) { }
The constructor can take the same type as a parameter.
B. Test Test( ) { }
A constructor cannot return anything.
C. private final Test( ) { }
A constructor cannot be final, static or abstract.
D. void Test( ) { }
A constructor cannot return anything not even void.
E. public static void Test(String args[ ] ) { }
A constructor cannot be final, static or abstract.Back to Questions
10.Correct Options are : C E
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); }
Explanation:
  1. To construct an instance of a sub class, its super class needs need to be constructed first. Since an instance can only be created via a constructor, some constructor of the super class has to be invoked.  Either you explicitly call it or the compiler will add super() (i.e. no args constructor) as the first line of the sub class constructor.
    Now, if the super class ( here, A ) does not have a no args constructor, the call super()
    will fail. Hence, choices B( ) { }, B(int y ) { j = y; } and B(int y ) { i = y; j = y*2; } are not valid and choice B(int y ) { super(y*2 ); j = y; } is valid because it explicitly calls super( int ) which is available in A.
  2. Instead of calling super(...) you can also call another constructor of the base class in the first line (as given in choice B(int z ) { this(z, z); } ). Here, this(int, int) is called in the first line which in turn calls super(int). So the super class A is correctly instantiated.

Aucun commentaire:

Enregistrer un commentaire