AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Skip to content

05 Methods / Checkpoint

Checkpoint — Methods

22 min · +25 XP · 3 exercises · 12 questions

Exercises0/3
Quiz · 70% to clear12 q

This checkpoint covers method declarations and calls, static versus instance, pass-by-value, overloading and its resolution rules, varargs, recursion, and scope.

How it works. Twelve questions and two programs; 70% on the questions and both programs accepted clears the module.

Before you start, make sure you can answer:

  • What a method signature consists of, and what it excludes.
  • What fill(int[] a) { a[0] = 9; a = new int[1]; } does to the caller's array.
  • Which overload f(5) picks among f(long), f(Integer) and f(int...).
  • What print(null) does to print(String...).
  • What every recursion needs, and what happens without it.
  • Why main cannot call an instance method directly.

The programs are a recursion with a helper and an accumulator, and a small overloaded utility exercised through a fixed input format.

Common questions

What does fill(int[] a) { a[0] = 9; a = new int[1]; } do to the caller's array?

It sets the caller's first element to 9 and nothing else. The parameter holds a copy of the reference, so writing through it changes the shared array, while a = new int[1] only points the local copy at a new array; the caller's variable is untouched.

Which overload does f(5) pick among f(long), f(Integer) and f(int...)?

f(long). The compiler tries widening before boxing and boxing before varargs, stopping at the first phase with a match, and widening int to long succeeds in the first phase. Without f(long) it would pick f(Integer), and only without both f(int...).

Why can't main call an instance method directly?

main is static, so it runs without an object and has no this, while an instance method needs an object to run on. Create one and call the method on it, as in new App().run(), or make the method static if it uses no instance state.

Java Methods Quiz: Overloading, Varargs and Recursion Test

Twelve questions and two programs on Java methods: static versus instance, pass-by-value, overload resolution, varargs, recursion and variable scope.

  • Course: Java study plan
  • Module: Methods
  • Kind: Checkpoint — cleared at 70%
  • Reading time: 22 min
  • Runtime: OpenJDK 18

Checkpoint — Methods is the checkpoint that closes the Methods module: a graded quiz and whole-program exercises, passed at 70%.

Instructions

This checkpoint covers method declarations and calls, static versus instance, pass-by-value, overloading and its resolution rules, varargs, recursion, and scope.

How it works. Twelve questions and two programs; 70% on the questions and both programs accepted clears the module.

Before you start, make sure you can answer:

  • What a method signature consists of, and what it excludes.
  • What fill(int[] a) { a[0] = 9; a = new int[1]; } does to the caller's array.
  • Which overload f(5) picks among f(long), f(Integer) and f(int...).
  • What print(null) does to print(String...).
  • What every recursion needs, and what happens without it.
  • Why main cannot call an instance method directly.

The programs are a recursion with a helper and an accumulator, and a small overloaded utility exercised through a fixed input format.

Common questions

What does fill(int[] a) { a[0] = 9; a = new int[1]; } do to the caller's array?

It sets the caller's first element to 9 and nothing else. The parameter holds a copy of the reference, so writing through it changes the shared array, while a = new int[1] only points the local copy at a new array; the caller's variable is untouched.

Which overload does f(5) pick among f(long), f(Integer) and f(int...)?

f(long). The compiler tries widening before boxing and boxing before varargs, stopping at the first phase with a match, and widening int to long succeeds in the first phase. Without f(long) it would pick f(Integer), and only without both f(int...).

Why can't main call an instance method directly?

main is static, so it runs without an object and has no this, while an instance method needs an object to run on. Create one and call the method on it, as in new App().run(), or make the method static if it uses no instance state.

Exercises

Permutations by backtracking

Read one word of 1–6 distinct lowercase letters. Print every permutation of its letters, one per line, in lexicographic order. Generate them recursively with backtracking: pick an unused letter (in alphabetical order), recurse, un-pick. Sort the letters first so the output is ordered without sorting afterwards.

Example: cab →

abc
acb
bac
bca
cab
cba

Recursive binary search with a helper

Read an integer n, then n integers in non-decreasing order, then an integer q and q query values. For each query print the index of the value in the array (any index if it occurs more than once) or -1.

Write a public wrapper static int search(int[] a, int target) that calls a private recursive helper search(int[] a, int target, int lo, int hi). Compute the midpoint as lo + (hi - lo) / 2.

Every subset

Read an integer n and n distinct words. Print every subset of the words, one per line, as the words joined by spaces (the empty subset prints as {}), in the order produced by the recursion "for each word, first leave it out, then take it" — so the empty set comes first and the full set last. Finish with subsets=<2^n>.

Example: 2 then a b →

{}
b
a
a b
subsets=4

In this module: Methods

← Scope, lifetime and static context · Arrays — fixed-size, typed, zero-based →