Card Set Information

2014-04-09 14:14:02

Java OCA
Show Answers:

  1. Given the following definition of the classes Animal, Lion, and Jumpable, select the correct combinations of assignments of a variable (select 2 options):

    interface Jumpable {}
    class Animal {}
    class Lion extends Animal implements Jumpable {}

    a Jumpable var1 = new Jumpable();

    b Animal var2 = new Animal();

    c Lion var3 = new Animal();

    d Jumpable var4 = new Animal();

    e Jumpable var5 = new Lion();
    Answer: b, e

    Explanation: Option (a) is incorrect. An interface can’t be instantiated.

    Option (c) is incorrect. A reference variable of a derived class can’t be used to refer to an object of its base class.

    Option (d) is incorrect. A reference variable of type Jumpable can’t be used to refer to an object of the class Animal because Animal doesn’t implement the interface Jumpable.
  2. Which of the following statements are true? (Select 3 options.)

    a A Java class can define multiple methods.

    b A Java class can define multiple variables.

    c A Java class can be defined in multiple packages.

    d A Java class can import multiple packages.

    e A Java class can’t define more than 108 constructors.

    f End-of-line comments can’t follow import or package statements.

    g Multiline comments can only be defined within a method definition.
    Answer: a, b, d

    Explanation: Option (c) is incorrect. The same class can’t be defined in multiple packages. If you try to define the same class—say, the class Person—in the packages com.ejava and com.eoracle, you’re defining two classes with the same name but in separate packages. In this case, com.ejava.Personand com.eoracle.Person will refer to two different classes.

    Option (e) is incorrect because there is no theoretical limit on the number of constructors that can be defined by a class.

    Option (f) is incorrect because end-of-line comments can follow any line of code.

    Option (g) is incorrect because multiline comments can also be placed outside a method definition.
  3. Given the following code, which option, if used to replace /* INSERT CODE HERE */, will make the code print 1? (Select 1 option.)

    try {
    String[][] names = {{"Andre", "Mike"}, null, {"Pedro"}};
    System.out.println(names[2][1].substring(0, 2));
    catch (/* INSERT CODE HERE */) {

    a IndexPositionException e

    b NullPointerException e

    c ArrayIndexOutOfBoundsException e

    d ArrayOutOfBoundsException e
    • The method must be marked as a public method
    • The method must be marked as a static method
    • The name of the method must be main
    • The return type of this method must be void
    • The method must accept a method argument of a String array or a variable argument of type String
  4. What are the four access modifiers?
    • - public (least restrictive)
    • - protected
    • - default
    • - private (most restrictive)