0

Java SE 8 Oracle Certified Associate - 1Z0-808 - #3

1. Câu hỏi

2. Phân tích

Đọc từng dòng code để phân tích, mình tập trung vào 2 line 13

list.remove(list.indexOf(0));
  • Kiểm tra logic của method indexOf see java docs Trong trường hợp này,
list.indexOf(0) = 2;

Biểu thức trở thành

list.remove(2);
  • remove(int) để xóa phần tử theo index
  • remove(Object) để xóa phần tử

Vì hàm list.indexOf(0) trả về giá trị kiểu int, vậy method remove(int) được thực hiện, xóa phần tử ở index = 2 (giá trị 0) Vậy list còn 2 phần tử (2,1)

Đáp án: image.png

3. Kết luận

Cần xem lại kiến thức về về hàm indexOf(int), remove(int) và remove(Object) của List Source cho câu hỏi


All Rights Reserved

Viblo
Let's register a Viblo Account to get more interesting posts.