Multiple Choice Questions 4

  1. Analyzing the following code:
    class Test
    { public static void main(String args[])
    { NClass nc = new NClass();
    nc.t = nc.t++;
    }
    }
    class Foo
    { int t;
    private NClass()
    { }
    }

  2. The program has a compilation error because the NClass class has a private constructor.
    The program does not compile because the parameter list of the main method is wrong.
    The program compiles, but has a runtime error because t has no initial value.
    The program compiles and runs fine.

    In the following code, suppose that f is an instance of Foo. Answer Questions 2–3.
    class Foo
    { int i;
    static int s;
    void imethod()
    { }
    static void smethod()
    { }
    }

  3. Which of the following statements is incorrect?

  4. System.out.println(f.i);
    System.out.println(f.s);
    int t = f.imethod();
    f.smethod();

  5. Which of the following statements is incorrect?

  6. System.out.println(Foo.i);
    System.out.println(Foo.s);
    f.imethod();
    Foo.smethod();

    Questions 4–6 are based on the following code:
    class Foo
    { static int i = 0;
    static int j = 0;
    public static void main(String[] args)
    { int i = 2;
    int k = 3;
    { int j = 3;
    System.out.println("i + j is " + i+j);
    }
    k = i + j;
    System.out.println("k is "+k);
    System.out.println("j is "+j);
    }
    }
    }

  7. What is the printout for i+j?

  8. 20
    21
    22
    23

  9. What is the printout for j?

  10. 0
    1
    2
    3

  11. What is the printout for k?

  12. 0
    1
    2
    3

  13. Analyze the following code:
    public class Test
    { public static void main (String args[])
    { int i = 0;
    for (int i=0; i<10; i++);
    System.out.println(i+4);
    }
    }

  14. The program has a syntax error because of the semicolon (;) on the for loop line.
    The program compiles despite the semicolon (;) on the for loop line, and displays 4.
    The program compiles despite the semicolon (;) on the for loop line, and displays 14.
    None of the above

  15. Suppose TestCircle and Circle in Example 5.1 are in two separate files named TestCircle.java and Circle.java, respectively. What is the outcome of compiling TestCircle.java and then Circle.java?

  16. Only TestCircle.java compiles.
    Only Circle.java compiles.
    Both compile fine.
    Neither compiles successfully.

  17. What modifier should you use so that a class in a different package cannot access the class?

  18. public
    private
    protected
    Use the default modifier

  19. What modifier should you use so that a class in a different package cannot access the class, but its subclasses can access it?

  20. public
    private
    protected
    Use the default modifier

  21. To restrict access of a static member to the class itself,

  22. Use the private modifier.
    You cannot use the private modifier with the static modifier.
    Use the protected modifier.
    None of the above.

  23. 12. Analyze the following code:
    class Test
    { private int t;
    public static void main(String[] args)
    { int x;
    System.out.println(t);
    }
    }

  24. The variable t is not initialized and therefore causes errors.
    he variable t is private and therefore cannot be accessed in the main method.
    The main method cannot access an instance variable without creating an object.
    The variable x is not initialized and therefore causes errors.

  25. Which of the following is a possible output from invoking Math.random()?

  26. 323.4
    0.
    34
    2000
    None of the above

  27. Analyze the following code:
    class Test
    { double radius;
    public static void main(String[] args)
    { final static double PI= 3.15169;
    double area = radius*radius*PI;
    System.out.println("Area is "+area);
    }
    }

  28. The program has syntax errors because the variable radius is not initialized.
    The program has syntax errors because a constant PI is defined inside a method.
    The program has no syntax errors but will get a runtime error because radius is not initialized.
    The program compiles and runs fine.

  29. Analyze the following code:
    class Test
    { public static void main(String[] args)
    { A a = new A("test");
    a.print();
    }
    }
    class A
    { String s;
    A(String s)
    { this.s = s; }
    private void print()
    { System.out.println(s);
    }
    }

  30. The program compiles fine, but has a runtime error because the print() method is private.
    The program has a compilation error because the print() method in class A is private.
    The program runs fine and prints Test.
    None of the above.

Visited times since 5 Feb 1999.