首页 OCA 自测回顾
文章
取消

OCA 自测回顾

OCA 自测题回顾

  1. What is the output of the following code? (Choose all that apply)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    interface HasTail { int getTailLength(); } 
    abstract class Puma implements HasTail { 
        protected int getTailLength() {return 4;} 
    } 
    public class Cougar extends Puma { 
        public static void main(String[] args) { 
            Puma puma = new Puma(); 
            System.out.println(puma.getTailLength()); 
     } 
           
        public int getTailLength(int length) {return 2;} 
    } 
    

    A. 2

    B. 4

    C. The code will not compile because of line 3.

    D. The code will not compile because of line 5.

    E. The code will not compile because of line 7.

    F. The output cannot be determined from the code provided.

    答案是: C, D, E. First, the method getTailLength() in the interface HasTail is assumed to be public, since it is part of an interface. The implementation of the method on line 3 is therefore an invalid override, as protected is a more restrictive access modifier than public, so option C is correct. Next, the class Cougar implements an overloaded version of getTailLength(), but since the declaration in the parent class Puma is invalid, it needs to implement a public version of the method. Since it does not, the declaration of Cougar is invalid, so option D is correct. Option E is correct, since Puma is marked abstract and cannot be instantiated. The overloaded method on line 11 is declared correctly, so option F is not correct. Finally, as the code has multiple compiler errors, options A, B, and G can be eliminated.

  2. What is the result of the following program?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    public class MathFunctions { 
        public static void addToInt(int x, int amountToAdd) { 
            x = x + amountToAdd; 
        } 
        public static void main(String[] args) { 
            int a = 15; 
            int b = 10; 
            MathFunctions.addToInt(a, b); 
            System.out.println(a); } } 
    

    A. 10

    B. 15

    C. 25

    D. Compiler error on line 3.

    E. Compiler error on line 8.

    F. None of the above.

    答案是: B. The code compiles successfully, so options D and E are incorrect. The value of a cannot be changed by the addToInt method, no matter what the method does, because only a copy of the variable is passed into the parameter x. Therefore, a does not change and the output on line 9 is 15.

  3. What is the result of the following code?

    1
    2
    3
    4
    5
    6
    7
    
    int[] array = {6,9,8}; 
    List<Integer> list = new ArrayList<>(); 
    list.add(array[0]); 
    list.add(array[2]); 
    list.set(1, array[1]); 
    list.remove(0); 
    System.out.println(list); 
    

    A. [8]

    B. [9]

    C. Something like [Ljava.lang.String;@160bc7c0

    D. An exception is thrown.

    E. The code does not compile.

    答案是: B. The array is allowed to use an anonymous initializer because it is in the same line as the declaration. The ArrayList uses the diamond operator allowed since Java 7. This specifies the type matches the one on the left without having to re-type it. After adding the two elements, list contains [6, 8]. We then replace the element at index 1 with 9, resulting in [6, 9]. Finally, we remove the element at index 0, leaving [9]. Option C is incorrect because arrays output something like that rather than an ArrayList.

  4. What is the output of the following code?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    public class Deer { 
        public Deer() { System.out.print("Deer"); } 
        public Deer(int age) { System.out.print("DeerAge"); } 
        private boolean hasHorns() { return false; } 
        public static void main(String[] args) { 
            Deer deer = new Reindeer(5); 
            System.out.println(","+deer.hasHorns()); 
        } 
    } 
    class Reindeer extends Deer { 
        public Reindeer(int age) { System.out.print("Reindeer"); } 
        public boolean hasHorns() { return true; } 
    } 
    

    A. DeerReindeer,false

    B. DeerReindeer,true

    C. ReindeerDeer,false

    D. ReindeerDeer,true

    E. DeerAgeReindeer,false

    F. DeerAgeReindeer,true

    G. The code will not compile because of line 7.

    H. The code will not compile because of line 12.

    答案是:A. The code compiles and runs without issue, so options G and H are incorrect. First, the Reindeer object is instantiated using the constructor that takes an int value. Since there is no explicit call to the parent constructor, the default no-argument super() is inserted as the first line of the constructor. The output is then Deer, followed by Reindeer in the child constructor, so only options A and B can be correct. Next, the method hasHorns() looks like an overridden method, but it is actually a hidden method since it is declared private in the parent class. Because the hidden method is referenced in the parent class, the parent version is used, so the code outputs false, and option A is the correct answer.

  5. Which of the following statements are true? (Choose all that apply)

    A. Checked exceptions are intended to be thrown by the JVM (and not the programmer).

    B. Checked exceptions are required to be caught or declared.

    C. Errors are intended to be thrown by the JVM (and not the programmer).

    D. Errors are required to be caught or declared.

    E. Runtime exceptions are intended to be thrown by the JVM (and not the programmer).

    F. Runtime exceptions are required to be caught or declared.

    答案是:B, C. Only checked exceptions are required to be handled (caught) or declared. Runtime exceptions are commonly thrown by both the JVM and programmer code. Checked exceptions are usually thrown by programmer code. Errors are intended to be thrown by the JVM. While a programmer could throw one, this would be a horrible practice.

  6. Which are true of the following code? (Choose all that apply)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    import java.util.*; 
    public class Grasshopper { 
        public Grasshopper(String n) { 
        name = n; 
    } 
    public static void main(String[] args) { 
        Grasshopper one = new Grasshopper("g1"); 
        Grasshopper two = new Grasshopper("g2"); 
        one = two; 
        two = null; 
        one = null; 
    } 
    private String name; } 
    

    A. Immediately after line 9, no grasshopper objects are eligible for garbage collection.

    B. Immediately after line 10, no grasshopper objects are eligible for garbage collection.

    C. Immediately after line 9, only one grasshopper object is eligible for garbage collection.

    D. Immediately after line 10, only one grasshopper object is eligible for garbage collection.

    E. Immediately after line 11, only one grasshopper object is eligible for garbage collection.

    F. The code compiles.

    G. The code does not compile.

    答案是:C, D, F. Immediately after line 9, only Grasshopper g1 is eligible for garbage collection since both one and two point to Grasshopper g2. Immediately after line 10, we still only have Grasshopper g1 eligible for garbage collection. Reference two points to g2 and reference two is null. Immediately after line 11, both Grasshopper objects are eligible for garbage collection since both one and two point to null. The code does compile. Although it is traditional to declare instance variables early in the class, you don’t have to.

  7. What is the output of the following program?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    public class FeedingSchedule { 
    public static void main(String[] args) { 
    	int x = 5, j = 0; 
    	OUTER: for(int i=0; i<3; ) 
    		INNER: do { 
                i++; x++; 
                if(x > 10) break INNER; 
                x += 4; 
                j++; 
             } while(j <= 2); 
     		System.out.println(x); 
    } } 
    

    A. 10

    B. 12

    C. 13

    D. 17

    E. The code will not compile because of line 4.

    F. The code will not compile because of line 6.

    答案是:B. The code compiles and runs without issue; therefore, options E and F are incorrect. This type of problem is best examined one loop iteration at a time:

    • On the first iteration of the outer loop i is 0, so the loop continues.
    • On the first iteration of the inner loop, i is updated to 1 and x to 6. The if-then statement branch is not executed, and x is increased to 10 and j to 1.
    • On the second iteration of the inner loop (since j = 1 and 1 <= 2), i is updated to 2 and x to 11. At this point, the if-then branch will evaluate to true for the remainder of the program run, which causes the flow to break out of the inner loop each time it is reached.
    • On the second iteration of the outer loop (since i = 2), i is updated to 3 and x to 12. As before, the inner loop is broken since x is still greater than 10.
    • On the third iteration of the outer loop, the outer loop is broken, as i is already not less than 3. The most recent value of x, 12, is output, so the answer is option B.
  8. Assuming we have a valid, non-null HenHouse object whose value is initialized by the blank line shown here, which of the following are possible outputs of this application?(Choose all that apply)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    class Chicken {} 
    interface HenHouse { public java.util.List<Chicken> getChickens(); } 
    public class ChickenSong { 
    public static void main(String[] args) { 
    HenHouse house = ______________ 
        Chicken chicken = house.getChickens().get(0); 
        for(int i=0; i<house.getChickens().size(); 
            chicken = house.getChickens().get(i++)) { 
            System.out.println("Cluck"); 
    } } } 
    

    A. The code will not compile because of line 6.

    B. The code will not compile because of lines 7–8.

    C. The application will compile but not produce any output.

    D. The application will output Cluck exactly once.

    E. The application will output Cluck more than once.

    F. The application will compile but produce an exception at runtime.

    答案是:D, E, F. The code compiles without issue, so options A and B are incorrect. If house.getChickens() returns an array of one element, the code will output Cluck once, so option D is correct. If house.getChickens() returns an array of multiple elements, the code will output Cluck once for each element in the array, so option E is correct. Alternatively, if house.getChickens() returns an array of zero elements, then the code will throw an IndexOutOfBoundsException on the call to house.getChickens().get(0); therefore, option C is not possible and option F is correct. The code will also throw an exception if the array returned by house.getChickens() is null, so option F is possible under multiple circumstances.

  9. What individual changes, if any, would allow the following code to compile? (Choose all that apply)

    1
    2
    3
    
     public interface Animal { public default String getName() { return null; } }
     interface Mammal { public default String getName() { return null; } }
     abstract class Otter implements Mammal, Animal {}
    

    A. The code compiles without issue.

    B. Remove the default method modifier and method implementation on line 1.

    C. Remove the default method modifier and method implementation on line 2.

    D. Remove the default method modifier and method implementation on lines 1 and 2.

    E. Change the return value on line 1 from null to "Animal".

    F. Override the getName() method with an abstract method in the Otter class.

    G. Override the getName() method with a concrete method in the Otter class.

    答案是:D, F, G. The code does not compile, since a class cannot inherit two interfaces that both define default methods with the same signature, unless the class implementing the interfaces overrides it with an abstract or concrete method. Therefore, option A is incorrect and options F and G are correct. The alternate approach is to make the getName() method abstract in the interfaces, because a class may inherit two abstract methods with the same signature. The change must be made to both interfaces, though, so options B and C are incorrect if taken individually, and option D is correct since the changes are taken together.

  10. Which of the following lines can be inserted at line 11 to print true? (Choose all that apply)

    1
    2
    3
    4
    5
    6
    
    public static void main(String[] args) {
        // INSERT CODE HERE
    }
    private static boolean test(Predicate<Integer> p) {
        return p.test(5);
    }
    

    A. System.out.println(test(i -> i == 5));

    B. System.out.println(test(i -> {i == 5;}));

    C. System.out.println(test((i) -> i == 5));

    D. System.out.println(test((int i) -> i == 5);

    E. System.out.println(test((int i) -> {return i == 5;}));

    F. System.out.println(test((i) -> {return i == 5;}));

    答案是:A, C, F. The only functional programming interface you need to memorize for the exam is Predicate. It takes a single parameter and returns a boolean. Lambda expressions with one parameter are allowed to omit the parentheses around the parameter list, making options A and C correct. The return statement is optional when a single statement is in the body, making option F correct. Option B is incorrect because a return statement must be used if braces are included around the body. Options D and E are incorrect because the type is Integer in the predicate and int in the lambda. Autoboxing works for collections not inferring predicates. If these two were changed to Integer, they would be correct.

  11. Which of the following are checked exceptions? (Choose all that apply)

    A. Exception

    B. IllegalArgumentException

    C. IOException

    D. NullPointerException

    E. NumberFormatException

    F. StackOverflowError

    答案是:A, C. Option A is the exception base class, which is a checked exception. Options B, D, and E extend RuntimeException directly or indirectly and therefore are unchecked exceptions. Option F is a throwable and not an exception, and so should not be caught or declared.

本文由作者按照 CC BY 4.0 进行授权