1Z0-811 · Question #67
Given the code fragment: int num[] = new int[3]; num[1] = 10; num[2] = 15; List<Integer> lst = new ArrayList<> (3); lst.add(10); lst.add(15); System.out.println (num); System.out.println (lst); What…
The correct answer is D. a memory address in hexadecimal number format [10, 15]. Printing num (a plain Java array) invokes the default Object.toString() method, which produces a string like [I@1b6d3586 - a type prefix and hash code rendered as hex - not the array's contents. Printing lst (an ArrayList) produces [10, 15] because ArrayList overrides…
Question
Options
- Aa memory address1 in hexadecimal number format a memory address2 in hexadecimal number format
- B10, 15
- C0, 10, 15
- Da memory address in hexadecimal number format [10, 15]
How the community answered
(18 responses)- A17% (3)
- B6% (1)
- C6% (1)
- D72% (13)
Explanation
Printing num (a plain Java array) invokes the default Object.toString() method, which produces a string like [I@1b6d3586 - a type prefix and hash code rendered as hex - not the array's contents. Printing lst (an ArrayList) produces [10, 15] because ArrayList overrides toString() to display its elements, making option D the only output that matches both behaviors.
Why the distractors are wrong:
- A is wrong because it shows two memory addresses, implying
ArrayListalso lacks atoString()override - it does not. - B is wrong because it shows only values with no memory address, ignoring how raw arrays print.
- C is wrong because it includes
0(the default value ofnum[0]), but array contents never print this way regardless - and again ignores the hex-address output.
Memory tip: Think "Arrays are address, Lists are legible." Plain arrays inherit Object's opaque toString(); Java Collections Framework classes (ArrayList, LinkedList, etc.) all override it to show their contents in [...] format. If you need readable array output, use Arrays.toString(num).
Topics
Community Discussion
No community discussion yet for this question.