Java OCA OCP Practice Question 1790

Question

Which declaration will result in the program compiling and printing 90, when run?.

public class Main {
  public static void main(String[] args) {
    // (1) INSERT DECLARATION HERE
    int sum = 0;
    for (int i : nums)
      sum += i;
    System.out.println(sum);
  }
}

Select the two correct answers.

(a) Object[] nums = {20, 30, 40};
(b) Number[] nums = {20, 30, 40};
(c) Integer[] nums = {20, 30, 40};
(d) int[] nums = {20, 30, 40};
(e) None of the above.


(c) and (d)

Note

Only the element type in (c), i.e., Integer, can be automatically unboxed to an int as required.

The element type in (d) is int.




PreviousNext

Related