Working with Methods and Encapsulation practice questions

From OCA Java SE 8 (1Z0-808) · 22 questions on this topic

Working with Methods and Encapsulation practice questions from OCA Java SE 8 (1Z0-808). This pack has 22 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

    Which of the following statements about varargs are true?

    1. A. A method may declare at most one varargs parameterCorrect answer

      True: a method may declare only one varargs parameter (JLS 8 §8.4.1).

    2. B. The varargs parameter must be the last parameterCorrect answer

      True: the varargs parameter must come last in the parameter list (JLS 8 §8.4.1).

    3. C. Callers must pass at least one value for the varargs parameter

      False: zero arguments is fine — the method then receives an empty array, never null.

    4. D. Inside the method, the varargs parameter behaves as an arrayCorrect answer

      True: inside the method body the varargs parameter is an ordinary array, with length and indexes.

    Explanation

    One varargs per method, always last, and the method body sees it as an ordinary array with length and indexes — those three are true. Zero arguments is fine: the method receives an empty array, never null, so `Callers must pass at least one value for the varargs parameter` is false.

  2. Question 2

    What is the result of compiling the following program? ```java public class Main { static void log(int... values, String label) { System.out.println(label); } public static void main(String[] args) { log(1, 2, "sum"); } } ```

    1. A. Compilation failsCorrect answer

      The varargs parameter must be the last parameter; placing int... values before String label is rejected by the compiler.

    2. B. sum

      This would be the printed label if the method compiled, but the illegal parameter order prevents compilation.

    3. C. It compiles; the last argument becomes the label

      Varargs must come last in the declaration; a varargs parameter that is not last never compiles, regardless of how arguments are supplied.

    4. D. An exception is thrown at runtime

      The failure is at compile time, so the program never runs to throw anything.

    Explanation

    The varargs parameter must be the LAST parameter in the list — otherwise the compiler can't tell where the variable part ends and the next parameter begins. `int... values, String label` is rejected outright.

  3. Question 3

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

    1. A. object

      This picks the more general overload; when several are applicable the most specific parameter type wins, and String beats Object.

    2. B. Compilation fails because "hi" matches both methods

      Several applicable overloads are not ambiguous when one is strictly more specific than the other.

    3. C. stringobject

      Only one overload ever runs per call; the chosen method does not chain into the other.

    4. D. stringCorrect answer

      When several overloads are applicable, the most specific parameter type wins — String beats Object for a String argument (JLS 8 §15.12.2.5).

    Explanation

    When several overloads are applicable, the MOST SPECIFIC parameter type wins — String beats Object for a String argument. Only one overload ever runs per call (`stringobject` is wrong).

  4. Question 4

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

    1. A. 10

      This sums 4 + 3 + 2 + 1; the recursion multiplies instead.

    2. B. It throws a StackOverflowError

      That is unbounded recursion's fate; the n <= 1 base case stops the descent here.

    3. C. 24Correct answer

      A correctly grounded recursion multiplies 4 * 3 * 2 * 1 = 24, with the n <= 1 base case ending the descent (JLS 8 §15.12).

    4. D. 4

      That is just the argument; the method recurses and multiplies before returning.

    Explanation

    A correctly grounded recursion: 4 * 3 * 2 * 1 = 24. The n <= 1 base case stops the descent, so no stack overflow (`It throws a StackOverflowError` — that's unbounded recursion's fate). `10` sums instead of multiplying.

  5. Question 5

    What is the result of compiling the following program? ```java public class Main { public static void main(String[] args) { Safe s = new Safe(); System.out.println(s.code()); } } class Safe { private String code() { return "42"; } } ```

    1. A. Compilation failsCorrect answer

      code() is private to Safe, so it is invisible to Main even in the same file, and the call fails to compile.

    2. B. 42

      This would be returned if the method were accessible, but the private access prevents the call from compiling.

    3. C. null

      The method returns "42" when callable and is never null; regardless, the code does not compile.

    4. D. An exception is thrown at runtime

      Private access is enforced at compile time, so the program never runs.

    Explanation

    private members are invisible outside their declaring class — same package, even same FILE makes no difference: "code() has private access in Safe". Encapsulation requires a public accessor on Safe itself.

  6. Question 6

    What is the output of the following program? ```java import static java.lang.Math.PI; public class Main { public static void main(String[] args) { System.out.println((int) (PI * 2)); } } ```

    1. A. 6.28

      This is the double value of PI * 2 before the cast; the (int) cast truncates the fractional part, so the decimal is not printed.

    2. B. Compilation fails because constants cannot be statically imported

      Static import covers fields as well as methods, so PI can be used unqualified and the code compiles.

    3. C. 7

      An int cast truncates toward zero rather than rounding, so 6.28 becomes 6, not 7.

    4. D. 6Correct answer

      PI * 2 is approximately 6.28 and the int cast truncates it to 6.

    Explanation

    Static import covers FIELDS as well as methods, so PI is usable unqualified (ruling out `Compilation fails because constants cannot...`). PI * 2 ≈ 6.28, and the int cast truncates to 6 — no rounding (`7`).

  7. Question 7

    What is the result of compiling the following program? ```java public class Main { static int twice(final int n) { n = n * 2; return n; } public static void main(String[] args) { System.out.println(twice(4)); } } ```

    1. A. Compilation failsCorrect answer

      n is a final parameter, so reassigning it with n = n * 2 is rejected; a final parameter may never be assigned after receiving its argument.

    2. B. 8

      This would be the result if returning n * 2 directly were allowed, but reassigning the final parameter fails to compile, so nothing runs.

    3. C. 4

      This is the passed value, but the code never runs because reassigning the final parameter is a compile error.

    4. D. An exception is thrown at runtime

      The error is a compile-time error, not a runtime exception; the program never runs.

    Explanation

    A final PARAMETER is a final variable — it receives the argument value once and can never be reassigned inside the method. `n = n * 2` is "final parameter n may not be assigned". Returning n * 2 directly would compile.

  8. Question 8

    What is the result of compiling the following program? ```java public class Main { static int score(int n) { if (n > 0) { return n; } } public static void main(String[] args) { System.out.println(score(5)); } } ```

    1. A. 5

      This assumes the method compiles because the call happens to pass a positive value; the missing-return check is compile-time and ignores actual arguments.

    2. B. 0

      A non-void method has no implicit zero return — falling off the end is rejected outright.

    3. C. Compilation failsCorrect answer

      A non-void method must return (or throw) on every path, and when the if is false control would fall off the end: "missing return statement" (JLS 8 §8.4.7).

    4. D. An exception is thrown when n <= 0

      The problem is detected at compile time, so the program never runs to throw anything.

    Explanation

    A non-void method must return (or throw) on EVERY path. When the if is false, control would fall off the end — "missing return statement". The check is compile-time and ignores that the actual argument here is positive.

Practise all 22 Working with Methods and Encapsulation questions

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

Open OCA Java SE 8

Other topics in this pack