OCA Java SE 8 Method - OCA Mock Question Method 4








Question

Given the following method, which of the method calls return 2? (Choose all that apply)

public int howMany(boolean b, boolean... b2) { 
  return b2.length; 
} 
  1. howMany();
  2. howMany(true);
  3. howMany(true, true);
  4. howMany(true, true, true);
  5. howMany(true, {true});
  6. howMany(true, {true, true});
  7. howMany(true, new boolean[2]);




Answer



D, G.

Note

D passes the initial parameter plus two more to turn into a vararg array of size 2.

G passes the initial parameter plus an array of size 2.

A does not compile because it does not pass the initial parameter.

E and F do not compile because they do not declare an array properly. It should be new boolean[] {true}.

Option B creates a vararg array of size 0 and option C creates a vararg array of size 1.