nerdexam
Oracle

1Z0-819 · Question #71

Class CustomType<T> { int count (T[] anArray, T element) { int count = 0; for (T x : anArray) { if (e.equals(element)) ++count; } return count; } } and public class Test extends CustomType { public…

The correct answer is B. The code prints 1 then prints 0. See the full explanation below for the reasoning.

Question

Class CustomType<T> { int count (T[] anArray, T element) { int count = 0; for (T x : anArray) { if (e.equals(element)) ++count; } return count; } } and public class Test extends CustomType { public static void main(String[] args) { String[] words = {"banana", "orange", "apple", "lemon"}; Integer[] numbers = {1,2,3,4,4}; CustomType<String> stringType = new CustomType<>(); CustomType<String> stringType.count(words, "apple"); System.out.println(stringType.count(words, "apple")); System.out.println(stringType.count(numbers, 3)); } } What is the result?

Options

  • AThe code fails to compile.
  • BThe code prints 1 then prints 0.
  • CThe code prints 1 then prints 3.
  • DThe code prints 2 then prints 0.
  • EThe code prints 2 then prints 3.

How the community answered

(30 responses)
  • B
    77% (23)
  • C
    3% (1)
  • D
    7% (2)
  • E
    13% (4)

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice