In: Computer Science
Fill in each blank with the correct answer/output. Assume each statement happens in order and that one statement may affect the next statement. Hand trace this code instead of using your IDE. String s = "abcdefghijklmnop"; ArrayList r = new ArrayList(); r.add("abc"); r.add("cde"); r.set(1,"789"); r.add("xyz"); r.add("123"); Collections.sort(r); r.remove(2); The first index position in an array is __________. System.out.print( s.substring(0,1) ); // LINE 2 System.out.print( s.substring(2,3) ); // LINE 3 System.out.print( s.substring(5,6) ); // LINE 4 System.out.print( r.get(0) ); // LINE 5 System.out.print(r.get(0).substring(0,1)); // LINE 6 System.out.print( r.get(2) ); // LINE 7 System.out.print( r.indexOf("123")); // LINE 8 System.out.print( r.contains("abc")); // LINE 9 System.out.print( r.isEmpty()); // LINE 10 r.set(1, "\\\\"); System.out.print(r); // LINE 11 r.remove(1); System.out.print(r); // LINE 12 r.add("one"); System.out.print(r); // LINE 13 r.add(0,"five"); System.out.print(r); // LINE 14 r.clear(); System.out.print(r); // LINE 15 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.
String s = "abcdefghijklmnop"; ArrayList r = new ArrayList(); r.add("abc"); r.add("cde"); r.set(1,"789"); r.add("xyz"); r.add("123"); Collections.sort(r); Value of the list r here is [123, 789, abc, xyz] r.remove(2); Value of the list r here is [123, 789, xyz] System.out.print( s.substring(0,1) ); // LINE This prints first character of String s It prints a System.out.print( s.substring(2,3) ); // LINE 3 This prints third character of String s It prints c System.out.print( s.substring(5,6) ); // LINE 4 This prints sixth character of String s It prints f System.out.print( r.get(0) ); // LINE 5 It prints first element in the list r It prints 123 System.out.print(((String) r.get(0)).substring(0,1)); // LINE 6 It prints first character of first elment in the list So, It prints 1 System.out.print( r.get(2) ); // LINE 7 It prints Second element in the list System.out.print( r.indexOf("123")); // LINE 8 It prints index of "123" in list r System.out.print( r.contains("abc")); // LINE 9 It prints true if "abc" in the list or It prints false System.out.print( r.isEmpty()); // LINE 10 It prints true if the list is empty or It prints false r.set(1, "\\\\"); System.out.print(r); // LINE 11 Sets the value at index to \\ r.remove(1); removes value at index 1 System.out.print(r); // LINE 12 prints the list r.add("one"); appends value "one" to list System.out.print(r); prints the list // LINE 13 r.add(0,"five"); adds "five" at start System.out.print(r); // LINE 14 prints the list r.clear(); clears the list System.out.print(r); // LINE 15 prints the listThe first index position in an array is 0 Output: -------- a c f 123 1 xyz 0 false false [123, \\, xyz] [123, xyz] [123, xyz, one][five, 123, xyz, one] []