Followers

Sunday, 5 August 2012

Constructors


Question 1

class A {A(int i) {}}  // 1
class B extends A {}   // 2
Which of the following statements are true?
a.   The compiler attempts to create a default constructor for class A.
b.  The compiler attempts to create a default constructor for class B.
c.   Compile-time error at 1.
d.  Compile-time error at 2.

Question 2

Which of the following modifiers can be applied to a constructor?
a.   private
b.  abstract
c.   final
d.  volatile
e.   native
f.   None of the above.

Question 3

Which of the following techniques can be used to prevent the instantiation of a class by any code outside of the class?
a.   Do not declare any constructors.
b.  Do not use a return statement in the constructor.
c.   Declare all constructors using the keyword void to indicate that nothing is returned.
d.  Declare all constructors using the private access modifier.
e.   None of the above.

Question 4

Which of the following modifiers can be applied to a constructor?
a.   protected
b.  public
c.   static
d.  synchronized
e.   transient

Question 5

Which of the following statements are true?
a.   The compiler will create a default constructor if no other constructor is declared.
b.  The default constructor takes no arguments.
c.   If a class A has a direct superclass, then the default constructor of class A invokes the no-argument constructor of the superclass.
d.  The default constructor declares Exception in the throws clause.
e.   The default constructor is always given the private access modifier.
f.   The default constructor is always given the public modifier.
g.  The default constructor is always given default package access.

Question 6

Suppose that the compiler generates a default constructor for a class. If a compile-time error is to be avoided, which of the following must be true?
a.   The superclass must not have any constructor other than a default constructor.
b.  The superclass must not have an accessible no-argument constructor.
c.   The no-argument superclass constructor must not have a throws clause that includes a checked exception.
d.  The no-argument superclass constructor must be declared private.
e.   None of the above

Question 7

class A {A() throws Exception {}} // 1
class B extends A {B() throws Exception {}} // 2
class C extends A {} // 3
Which of the following statements is true?
a.   Compile-time error at 1.
b.  Compile-time error at 2.
c.   Compile-time error at 3.
d.  None of the above


Answers


No.
Answer
Remark
1
b  d 
The compiler attempts to create a default constructor for class B.  Compile-time error at 2. 
If no constructor is declared explicitly, then the compiler will implicitly create a default constructor that accepts no parameters, has no throws clause, and invokes its superclass constructor. Since class A has an explicitly declared constructor, the compiler will not create an implicit default constructor. Class B does not have an explicit constructor declaration, so the compiler attempts to create a default constructor. Since class A does not have a no-parameter constructor, the attempt by class B to invoke the no parameter constructor of A would fail. As a result, a compiler error is generated at marker 2.  
2
private 
Constructors are not inherited and can not be overridden, so there is no need for the final modifier in a constructor declaration. Furthermore, an abstract constructor would be useless, since it could never be implemented. The volatile modifier can be applied to a field, but not to a constructor. Native constructors are not permitted, because it would be difficult for Java to verify that the native constructor properly invokes the superclass constructor.  
3
Declare all constructors using the private access modifier. 
If no constructors are declared explicitly; then the compiler will create one implicitly, and it will have the same access modifier as the class. The explicit declaration of any constructor will prevent the creation of a default constructor. If all constructors are declared private, then code outside of the class will not have access to the constructors and will not have the ability to create an instance of the class. Constructors do not return a value and constructor declarations do not include a return type, so the keyword void is not applicable to a constructor declaration.  
4
a  b 
protected  public 
Constructors can not be inherited, so an abstract constructor would be useless, since it could never be implemented. A static constructor would also be useless--or nearly so--since it would be unable to access the non-static members of the new instance. An object is not available to multiple threads during the construction process, so the synchronized modifier would not provide additional protection. The transient modifier can be applied to a field, but not a constructor.  
5
a  b  c 
The compiler will create a default constructor if no other constructor is declared.  The default constructor takes no arguments.  If a class A has a direct superclass, then the default constructor of class A invokes the no-argument constructor of the superclass. 
If no constructor is declared explicitly, then the compiler will implicitly insert a default constructor. The default constructor takes no arguments. The primordial class Object has no superclass; so the default constructor of type Object does not invoke a superclass constructor. If a class A has a direct superclass, then the default constructor of class A will invoke the no-argument superclass constructor. It is unlikely that the real exam would try to trick you with a question that requires you to know that the constructor of type Object does not invoke a superclass constructor. For the purposes of the real exam, it might be safer to overlook that particular unique feature of type Object. If a subclass constructor attempts to invoke the no-argument superclass constructor when none exists, then a compile-time error is generated. The access modifier implicitly assigned to the default constructor is the same as that assigned to the class. The default constructor does not have a throws clause. Consequently, a compile-time error is generated if the no-argument constructor of the superclass has a throws clause.  
6
The no-argument superclass constructor must not have a throws clause that includes a checked exception. 
The default constructor takes no arguments, and it invokes the superclass constructor with no arguments. If the superclass does not have an accessible no-argument constructor, then a compile-time error is generated. The default constructor does not have a throws clause. Consequently, a compile-time error is generated if the no-parameter constructor of the superclass has a throws clause.  
7
Compile-time error at 3. 
The compiler creates a constructor for class C implicitly. The implicitly created constructor accepts no parameters and has no throws clause. The constructors for class B and class C both invoke the constructor for A. The constructor for class A declares Exception in the throws clause. Since the constructors for B and C invoke the constructor for A implicitly, both B and C must declare Exception in their throws clause. A compile-time error is generated at marker 3, because the default constructor does not declare Exception in the throws clause.  




No comments:

Post a Comment