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

  FALSE

  TRUE

 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.

  variables

  computers

  code

  strings

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

  errors / mistakes

  attributes and/or behaviors

  syntax / lexical notations

  variables / strings

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

  constructors

  cells

  fields

  classes

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

  creation

  deletion

  access

  deconstruction

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

  instantiated

  instance

  iteration

  intuitive

 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

  There are no explicit field declarations

  line 1 and 2

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

  defines public variables

  restricts variables from being created

  restricts access

  allows complete access

 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.

  internet connection

  local road

  personal diary

  telephone directory

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

  right after the class declaration.

  at the very top of the whole program

  just before the class declaration

  in between the call to the main program and 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

  source code

  programmer

  compiler

 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);
   }
}

  1

  4

  3

  2

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

  two / two

  one / one

  one / two

  two / one

 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();
   }
}

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

  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

  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 ______________________________________.

  finalize the values of the fields (method variables)

  initialize the constants in the program (constant values)

  initialize the values of the methods (method variables)

   initialize the values of the fields (instance variables).

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

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

  the name of the class and then public

  public and then the name of the class.

  private and then the name of the class