Working with Methods and Encapsulation practice questions

From OCA Java SE 7 (1Z0-803) · 27 questions on this topic

Working with Methods and Encapsulation practice questions from OCA Java SE 7 (1Z0-803). This pack has 27 questions tagged Working with Methods and Encapsulation, drawn from its timed mock exams. 8 of them are worked through in full below — the question, every option, why each is right or wrong, and the explanation.

Worked examples for Working with Methods and Encapsulation

  1. Question 1

    What is the result of compiling the following program? ```java public class Main { Main() { System.out.print("A"); this(5); } Main(int x) { System.out.print("B"); } public static void main(String[] args) { new Main(); } } ```

    1. A. AB

      The code never runs; a this() call must be the first statement, and here a print precedes it.

    2. B. BA

      The constructor does not compile because this(5) is not the first statement, so no output is produced.

    3. C. Compilation failsCorrect answer

      An explicit this(...) invocation must be the first statement of the constructor, but a print precedes it here.

    4. D. A

      The misplaced this() call is a compile error, so nothing prints.

    Explanation

    An explicit constructor invocation — this(...) or super(...) — must be the FIRST statement of the constructor. Here a print precedes it, so the compiler rejects it ("call to this must be first statement in constructor").

  2. Question 2

    What is the output of the following program? ```java public class Main { static void bump(int n) { n++; } public static void main(String[] args) { int v = 7; bump(v); System.out.println(v); } } ```

    1. A. 8

      Wrong. This assumes incrementing the parameter changes the caller's variable, but n is a copy, so the increment is lost when the method returns.

    2. B. 7Correct answer

      Correct. Java passes primitives by value, so n receives a copy of v; incrementing the copy leaves the caller's v unchanged at 7.

    3. C. Compilation fails

      Wrong. The code is well-formed and compiles cleanly; passing a primitive to a method that increments a local copy is entirely legal.

    4. D. 0

      Wrong. v was initialized to 7 and nothing resets it to 0; the method's local increment never touches the caller's variable.

    Explanation

    Java is strictly pass-by-value: n receives a COPY of v, and incrementing the copy has no effect on the caller's variable. To propagate the change, bump would have to return the new value and the caller assign it.

  3. Question 3

    What is the output of the following program? ```java public class Main { static void pick(Object o) { System.out.println("object"); } static void pick(String s) { System.out.println("string"); } public static void main(String[] args) { pick(null); } } ```

    1. A. object

      Both overloads apply to null, and the compiler picks the most specific type, String, over Object.

    2. B. Compilation fails because the call is ambiguous

      String is more specific than Object, so there is a unique most-specific overload; there is no ambiguity.

    3. C. It throws a NullPointerException

      Passing null selects an overload at compile time; nothing is dereferenced, so no NPE.

    4. D. stringCorrect answer

      null fits both overloads and the most specific parameter type wins, so pick(String) is chosen.

    Explanation

    null is applicable to both overloads, and the compiler picks the MOST SPECIFIC parameter type: String is a subtype of Object, so pick(String) wins. Ambiguity (`Compilation fails because the call is ambiguous`) only arises when neither candidate is more specific — e.g. String vs StringBuilder.

  4. Question 4

    What is the output of the following program? ```java public class Main { public static void main(String[] args) { Base b = new Sub(); System.out.println(b.who()); } } class Base { static String who() { return "base"; } } class Sub extends Base { static String who() { return "sub"; } } ```

    1. A. baseCorrect answer

      Correct. Static methods are hidden, not overridden, so the call resolves at compile time by the reference type Base, printing "base" even though the object is a Sub.

    2. B. sub

      Wrong. This expects runtime dispatch on the object's actual type, but static methods are hidden rather than overridden, so the reference type Base decides the call.

    3. C. Compilation fails because static methods cannot be called through an instance

      Wrong. Calling a static method through an instance reference is legal (though poor style), so the code compiles; it just resolves by the reference type.

    4. D. An exception is thrown at runtime

      Wrong. The call is resolved statically at compile time and simply returns a string; nothing in this program throws at runtime.

    Explanation

    Static methods are HIDDEN, not overridden — there is no runtime dispatch for them. Calling a static method through an instance reference is legal (`Compilation fails because static methods cannot be called...` is wrong, though bad style) and resolves at compile time by the REFERENCE type, which is Base. So "base" prints even though the object is a Sub.

  5. Question 5

    What is the result of compiling the following program? ```java public class Main { static void pick(String s) { System.out.println("string"); } static void pick(StringBuilder b) { System.out.println("builder"); } public static void main(String[] args) { pick(null); } } ```

    1. A. Compilation failsCorrect answer

      String and StringBuilder are sibling types, neither more specific, so a null argument makes the call ambiguous: a compile error.

    2. B. string

      Neither overload is more specific for null, so the compiler cannot choose pick(String); it reports ambiguity.

    3. C. builder

      The call is ambiguous between the two sibling types, so pick(StringBuilder) is not selected.

    4. D. It throws a NullPointerException

      The ambiguity is caught at compile time; the program never runs to throw anything.

    Explanation

    String and StringBuilder are sibling types — neither is more specific than the other — so the null argument leaves the compiler with two equally good candidates: "reference to pick is ambiguous". An explicit cast like pick((String) null) resolves it.

  6. Question 6

    What is the output of the following program? ```java public class Main { static String tag() { return "static"; } public static void main(String[] args) { Main m = null; System.out.println(m.tag()); } } ```

    1. A. It throws a NullPointerException

      A static method call does not dereference the reference, so even a null reference causes no NPE.

    2. B. Compilation fails

      Calling a static method through a reference is legal (if discouraged), so it compiles.

    3. C. staticCorrect answer

      A static call is resolved from the reference's declared type at compile time, not from the object, so it prints static even though m is null.

    4. D. null

      The method returns the literal "static"; the null reference is irrelevant and never printed.

    Explanation

    Calling a static method through a reference is resolved at COMPILE time from the reference's declared type — the object (or null!) it points to is irrelevant and never dereferenced. So no NPE: "static" prints. This is exactly why calling statics through instances is discouraged style.

  7. Question 7

    What is the output of the following program? ```java public class Main { static int doubleIt(int n) { return n * 2; } public static void main(String[] args) { int v = 4; doubleIt(v); v = doubleIt(v + 1); System.out.println(v); } } ```

    1. A. 8

      That is doubleIt(4), but the first call's result is discarded; only the captured doubleIt(5) = 10 is printed.

    2. B. 10Correct answer

      The first call's result is discarded so v stays 4, then v = doubleIt(4 + 1) assigns 10.

    3. C. 16

      The uncaptured first call does not change v, so the argument is 5, not 8; the result is 10.

    4. D. 5

      v is reassigned to doubleIt(5) = 10, not left at the intermediate 5.

    Explanation

    The first call's result is discarded — v stays 4 (primitives can only change via reassignment). The second call computes doubleIt(5) = 10 and assigns it. Forgetting to capture a return value is the bug this question drills.

  8. Question 8

    What is the output of the following program? ```java public class Main { static int count(int... n) { return n.length; } public static void main(String[] args) { System.out.println(count() + " " + count(1, 2)); } } ```

    1. A. Compilation fails because count() passes no argument

      A varargs parameter accepts zero arguments, so count() is a legal call.

    2. B. It throws a NullPointerException for count()

      A zero-argument varargs call receives an empty array, not null, so no NPE.

    3. C. 1 2

      count() returns 0 (empty array length), not 1; count(1, 2) returns 2.

    4. D. 0 2Correct answer

      count() gets an empty length-0 array and count(1, 2) gets a length-2 array: 0 2.

    Explanation

    A varargs parameter accepts ZERO or more arguments; the zero-argument call receives an EMPTY array (length 0), never null (`It throws a NullPointerException for count()`). count(1, 2) receives a length-2 array.

Practise all 27 Working with Methods and Encapsulation questions

OCA Java SE 7 has the full set, inside timed mock exams that mirror real exam conditions — every question with a worked explanation.

Open OCA Java SE 7

Other topics in this pack