Java OCA OCP Practice Question 811

Question

Given the following two classes in the same package, which constructors contain compiler errors? (Choose three.)

public class Shape { 
   public Shape(boolean stillIn) { 
      super(); /*from  w  ww.ja va 2 s  . c o m*/
   } 
} 
? 
public class Rectangle extends Shape { 
   public Rectangle()  {} 
   public Rectangle(int deep) { 
      super(false); 
      this(); 
   } 
   public Rectangle(String now, int... deep) { 
      this(3); 
   } 
   public Rectangle(long deep) { 
      this("check",deep); 
   } 
   public Rectangle(double test) { 
      super(test>5 ? true : false); 
   } 
} 
A.  public Shape(boolean stillIn)
B.  public Rectangle()
C.  public Rectangle(int deep)
D.  public Rectangle(String now, int... deep)
E.  public Rectangle(long deep)
F.  public Rectangle(double test)


B, C, E.

Note

The constructors declared by Options A, D, and F compile without issue.

Option B does not compile.

Since there is no call to a parent constructor or constructor in the same class, the compiler inserts a no-argument super() call as the first line of the constructor.

Because Shape does not have a no-argument constructor, the no-argument constructor Rectangle()does not compile.

Option C also does not compile because super() and this() cannot be called in the same constructor.

Note that if the super() statement was removed, it would still not compile since this would be a recursive constructor call.

Option E does not compile.

There is no matching constructor that can take a String followed by a long value.

If the input argument deep was an int in this constructor, then it would match the constructor used in Option D and compile without issue.




PreviousNext

Related