Discussion
What will be the output of the program?
import java.util.*;
public class Trial67
{
public static void main(String[] args)
{
List<Integer> l = new ArrayList<Integer>(
Arrays.asList(10, 20, 30));
System.out.println(
l.contains(20) + " " + l.indexOf(40) + " " + l.get(l.indexOf(30)));
}
}
Answer: C
contains reports the presence of a value by equality, indexOf returns -1 for something absent rather than throwing, and the last call looks up the index of 30 and reads it back. Feeding an unchecked -1 from indexOf into get is the bug this sentinel invites.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development