Preview

04 - Writing constructors for subclasses

 1. A constructor is similar to a method (but not actually a method) that is _____________ when an object is instantiated.

  automatically created by the object

  encapsulated efficiently

  automatically duplicated

   invoked automatically

 2. Here, Test() has same name as that of the class. However, it has a return type void. Hence, it?s a method NOT a constructor.
class Test {
    void Test() {
        // method body
    }
}

  FALSE

  TRUE

 3. How do you initialize inherited private variables if you do not have direct access to them in the subclass?

  You cannot do this

  In Java you can put a call to the child class using a subclass method

  You can use a process called overriding

  In Java you can put a call to the parent constructor using as the first line in a subclass constructor

 4. In Java, the superclass constructor can be called from the_____________by using the keyword super and passing appropriate parameters, for example super(); or super(theName); as in the code below.
public class Employee extends Person
{
    public class Employee()
    {
        super(); // calls the Person() constructor
    }
    public class Employee(String theName)
    {
        super(theName); // calls Person(theName) constructor
    }
}

  None of these answers are correct

   first line of a subclass constructor

  middle of the subclass where the method is declared

  child class variable declaration

 5. If a subclass has no call to a superclass constructor using super as the first line in a subclass constructor then the compiler will automatically add a super() call as the first line in a constructor.

  FALSE

  TRUE

 6. What is the output of the following code?
class Person
{
   private String name;

   public Person(String theName)
   {
      this.name = theName;
   }

   public String getName()
   {
      return name;
   }

   public boolean setName(String theNewName)
   {
      if (theNewName != null)
      {
         this.name = theNewName;
         return true;
      }
      return false;
   }
}

public class Employee extends Person
{

   private static int nextId = 1;
   private int id;

   public Employee(String theName)
   {
      super(theName);
      id = nextId;
      nextId++;
   }

   public int getId()
   {
      return id;
   }

   public static void main(String[] args)
   {
      Employee emp = new Employee("Jonathan Tracker");
      System.out.println(emp.getName());
      System.out.println(emp.getId());
   }
}

  Jonathan Tracker,1

  Error - the sub class has not be created correctly

  1

  Jonathan Tracker

 7. The actual parameters passed in the call to the _______________provide values that the constructor can use to initialize the object's instance variables

  superclass constructor

  base constructor

  child class constructor

  subclass constructor

 8. Refer to the following example: What is the output?
class Teacher {
   String designation = "Teacher";
   String collegeName = "TestandTrack";
   void does(){
	System.out.println("Teaching");
   }
}

public class PhysicsTeacher extends Teacher{
   String mainSubject = "Computing";
   public static void main(String args[]){
	PhysicsTeacher obj = new PhysicsTeacher();
	System.out.println(obj.collegeName);
	System.out.println(obj.designation);
	System.out.println(obj.mainSubject);
	obj.does();
   }
}

  Error. The reason for the error is that the constructor has been incorrectly created.

  TestandTrack,Teacher,Computing,Teaching

  TestandTrack,Teaching,Computing

  Only TestandTrack followed by Teacher will be printed.

 9. The superclass constructor can be called explicitly using the super keyword, but it should be ______ statement in a constructor.

  first

  second last

  last

  second

 10. In inheritance, the objects are constructed __________________.

  from the top down

  from the bottom up

  from instance variables

  incorrectly, but it does allow for efficiency

 11. Recap: Constructors do not actually construct the object. The class makes the object and then executes a constructor to ______________________________________.

   initialize the values of the fields (instance variables).

  initialize the constants in the program (constant values)

  initialize the values of the methods (method variables)

  finalize the values of the fields (method variables)

 12. Recap: Constructors are specified after the fields and before any methods. They typically start with _____________________________.

  public and then the name of the class.

  private and then the name of the class

  the name of the class and then public

  the name(s) of the fields and the name of the class followed by either public or private (usually private)

 13. What is the output of the following code?
class ParentClass{
   //Parent class constructor
   ParentClass(){
	System.out.println("Constructor of Parent");
   }
}
class JavaExample extends ParentClass{
   JavaExample(){
	/* It by default invokes the constructor of parent class
	 * You can use super() to call the constructor of parent.
	 * It should be the first statement in the child class
	 * constructor, you can also call the parameterized constructor
	 * of parent class by using super like this: super(10), now
	 * this will invoke the parameterized constructor of int arg
	 */
	System.out.println("Constructor of Child");
   }
   public static void main(String args[]){
	//Creating the object of child class
	new JavaExample();
   }
}

  Constructor of Parent,Constructor of Child

  Constructor of Child, Constructor of Parent

  Constructor of Child

  Constructor of Parent

 14. Constructors cannot be abstract or static or final.

  TRUE

  FALSE

 15. Constructors in Java can be overridden. In other words, constructors can be inherited.

  TRUE

  FALSE

 16. The super keyword refers to the superclass, immediately above of the calling class in the hierarchy.

  TRUE

  FALSE

 17. The use of multiple super keywords to access an ancestor class other than the direct parent is not permitted.

  FALSE

  TRUE

 18. A constructor cannot be overloaded.

  TRUE

  FALSE

 19. A Java constructor name must exactly match with the class name (including case).

  TRUE

  FALSE

 20. A Java constructor must not have a return type.

  FALSE

  TRUE