Preview

02 - Constructors

 1. Constructors are specified after the fields and before any methods. They typically start with public and then the name of the class. They can take data (specified in parentheses) which is used to __________.
java_intermediate_constructor_1_intro.png

  initialize the methods

  initialize the fields.

  spot any errors

  create the class methods

 2. Constructors don?t actually construct the object. The class makes the object and then executes a constructor to initialize the values of the fields (instance variables).

  TRUE

  FALSE

 3. Referring to the following code that refers to an Employee class, note that the below class creates _______________________________.
import java.io.*;
public class EmployeeTest {

   public static void main(String args[]) {
      /* Create two objects using constructor */
      Employee empOne = new Employee("James Smith");
      Employee empTwo = new Employee("Mary Anne");

      // Invoking methods for each object created
      empOne.empAge(26);
      empOne.empDesignation("Senior Software Engineer");
      empOne.empSalary(1000);
      empOne.printEmployee();

      empTwo.empAge(21);
      empTwo.empDesignation("Software Engineer");
      empTwo.empSalary(500);
      empTwo.printEmployee();
   }
}

  two instances of the class Employee but does not invoke any methods for the objects

  two additional classes based on the class Employee for which the methods are invoked as well

  two instances of the class Employee and invokes the methods for each object.

  one instance of the class Employee and invokes its method

 4. In the following code, the constructor begins on line 10
public class Name {
    private String first;
    private String last;

    public Name(String theFirst, String theLast) {
        first = theFirst;
        last = theLast;
     }

     public void setFirst(String theFirst) {
        first = theFirst;
     }

     public void setLast(String theLast) {
        first = theLast;
     }

}

  FALSE

  TRUE

 5. To find a constructor in a class look for something with the same name as the class and _____________________.

  at least two fields

  no return type

  at least one method (without which it is not a construtor)

  a return type

 6. Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class.

  FALSE

  TRUE

 7. Is it possible for a class to have more than one constructor?

  Yes,as long as each has a unique signature (ie parameters)

  No, as it is not possible for two constructors to have the same parametesr

  Yes, but only if each constructor has the same parameters (does not have a unique signature)

  No, because two constructors would contain the same method

 8. The following constructor has ________________________________.
public class Puppy {
   public Puppy() {
   }

   public Puppy(String name) {
      // This constructor has one parameter, name.
   }
}

  one parameter - 'name'

  two parameters - 'Puppy' and 'name'

  one parameter - 'puppy'

  one method - public Puppy

 9. What is the output of the following code>
public class Puppy {
   public Puppy(String name) {
      // This constructor has one parameter, name.
      System.out.println("Passed Name is :" + name );
   }

   public static void main(String []args) {
      // Following statement would create an object myPuppy
      Puppy myPuppy = new Puppy( "tommy" );
   }
}

  new Puppy

  Passed Name is :tommy

  Passed Name is :new Puppy

  Error

 10. How would you describe the following statement?
 A constructor merely initializes the fields to their default values or in the case of a parameterized constructor, to the values passed in to the constructor.

  It is correct, although values are never passed in to a constructor.

  It is incorrect.

  It is partially correct but describes the purpose of the instance variables not the methods.

  It is correct and it describes the purpose of a constructor.