Preview

01 - Anatomy of a Class

 1. A class is a blueprint from which individual objects are created.
public class Dog {
   String breed;
   int age;
   String color;

   void barking() {
   }

   void hungry() {
   }

   void sleeping() {
   }
}
java_class_into_question_intermediate_1.jpg

  TRUE

  FALSE

 2. In the above example, barking(), hungry() and sleeping() are _________.

 3. Programmers use ______ to represent a physical object or nonphysical concept, real or imagined, by defining a class.

  strings

  code

  variables

  computers

 4. A class is defined based on the ______________________of the object or concept.

  variables / strings

  errors / mistakes

  attributes and/or behaviors

  syntax / lexical notations

 5. In the below code, 'name' and 'cell' are both:
private String name;
private String cell;

  classes

  cells

  fields

  constructors

 6. The keywords public and private affect the ______ of classes, data, constructors, and methods.

  deletion

  access

  deconstruction

  creation

 7. Fields are also called i__________ variables or object variables or properties.

  iteration

  instance

  intuitive

  instantiated

 8. On what line(s) are the field declarations in the below code?
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;
     }

}

  line 2 and 3

  line 5 and 6

  line 1 and 2

  There are no explicit field declarations

 9. The keyword private _____________ to the declaring class, while the keyword public allows access from classes outside the declaring class.

  allows complete access

  restricts variables from being created

  restricts access

  defines public variables

 10. Access to attributes should be kept internal to the class. Therefore, instance variables are designated as ____________.

 11. Access to behaviors can be internal or external to the class. Therefore, _________ can be designated as either public or private.

 12. ________ hold the data for an object. _________ record what an object needs to know to do work in the program. ________ are also called instance variables or object variables or properties.

 13. Setting something to private is comparable to the access rights you may have on your _________________. Only you should have direct access to it.

  personal diary

  local road

  telephone directory

  internet connection

 14. Fields are declared ______________________.They start with private then the type of the field and then a name for the field.

  in between the call to the main program and the class declaration

  at the very top of the whole program

  just before the class declaration

  right after the class declaration.

 15. The following line would ask the _______ to load all the classes available in directory java_installation/java/io ?
import java.io.*;

  byte sequence

  programmer

  compiler

  source code

 16. How many instance variables can you count in the below code?
import java.io.*;
public class Employee {

   String name;
   int age;
   String designation;
   double salary;

   // This is the constructor of the class Employee
   public Employee(String name) {
      this.name = name;
   }

   // Assign the age of the Employee  to the variable age.
   public void empAge(int empAge) {
      age = empAge;
   }

   /* Assign the designation to the variable designation.*/
   public void empDesignation(String empDesig) {
      designation = empDesig;
   }

   /* Assign the salary to the variable	salary.*/
   public void empSalary(double empSalary) {
      salary = empSalary;
   }

   /* Print the Employee details */
   public void printEmployee() {
      System.out.println("Name:"+ name );
      System.out.println("Age:" + age );
      System.out.println("Designation:" + designation );
      System.out.println("Salary:" + salary);
   }
}

  2

  4

  3

  1

 17. The above class has ____ explicitly defined constructor, which takes ___ parameter.

  two / one

  one / two

  one / one

  two / two

 18. Referring to the code above and the Employee class, and the below code, 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();
   }
}

  one instance of the class Employee and invokes the methods for two different objects

  three instances of the class Employee and invokes the methods for just one object

  a single instance of the class Employee and invokes the methods for two objects

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

 19. 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).

  initialize the constants in the program (constant values)

  finalize the values of the fields (method variables)

  initialize the values of the methods (method variables)

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

  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)

  public and then the name of the class.