Self-Review Questions

  1. Does declaration create an object?
    No.

  2. When you create an object do you always need a variable associated with the new object?
    No, anonymous objects exist

  3. What are three differences between constructors and methods?
    1. A constructor has the same name as the class.
    2. A constructor has no return value.
    3. A constructor is called with the new operator when a class is instantiated or by using a special reference this()

  4. Can static methods call non-static methods in the same class?
    No.

  5. What does the keyword final specified for a variable mean?
    Means the value of the variable won't change.

  6. What is encapsulation?
    1. data hiding
    2. implementation hiding
    3. all of the above
    4. none of the above

  7. What is a method's signature?
    1. the name of the method along with the number of its parameters.
    2. the name of the method along with the number and type of its parameters.
    3. the name of the method along with the number, type and order of its parameters.
    4. the name of the method, its return type and the number, type, order of its parameters.

  8. Examine the following code segment
    public class Demo
    {
       public static void main(String[] ags)
       {
          Test o1 = new Test();
          Test o2 = new Test();
          Test o3 = o2;
          System.out.println(o1.number);
       }
    }
    class Test
    {
       static int number=0;
       public Test()
       {
          number++;
       }
    }
    
    What is the output?
    1. 0
    2. 1
    3. 2
    4. none of the above

  9. Analyze the following code and choose the best answer:
    public class Analyze
    {
       private int x;
    
       public static void main(String[] args)
       {
          Analyze tmp = new Analyze();
          System.out.println(tmp.x);
       }
    }
    
    1. Since x is private, you cannot access it;
    2. You can access x without creating an object;
    3. Since x is an instance variable, it can be accessed through an object;
    4. You cannot create a self-referenced object;

  10. Examine the following code segment
    class Scope
    {
       private int x;
    
       public Scope () {x = 0;}
    
       public void method()
       {
          int x = 1;
          System.out.print( this.x );
       }
    }
    
    What is the output?
    1. 0
    2. 1
    3. none of the above