What does Student[] roll = new Student[3]; create?
Q2
What happens when roll[0].getName() is called immediately after the previous
declaration?
Q3
Which import is required for ArrayList?
Q4
Which declaration is correct?
Q5
How is the number of elements of an ArrayList obtained?
Q6
What is the output?
ArrayList<String> list = new ArrayList<>();
list.add("a"); list.add("b"); list.add(0, "c");
System.out.print(list);
Q7
What is the output?
ArrayList<String> m = new ArrayList<>();
m.add("Louvre"); m.add("Prado"); m.add("Uffizi"); m.add("Tate");
m.remove(2);
System.out.print(m);
Q8
For ArrayList<Integer> n containing [10, 20, 30], what does n.remove(2)
remove?
Q9
For the same list, which call removes the value 20?
Q10
What is the output?
ArrayList<Integer> n = new ArrayList<>();
n.add(5); n.add(7);
int x = n.get(0) + n.get(1);
System.out.print(x);
Q11
The automatic conversion that lets n.add(5) work on an ArrayList<Integer> is
called:
Q12
What does list.set(1, "X") do?
Q13
What is the valid index range for get on a list of size 4?
Q14
What is the output?
ArrayList<String> a = new ArrayList<>();
a.add("x");
System.out.print(a.isEmpty() + " " + a.contains("x") + " " + a.indexOf("y"));
Q15
Which statement about ArrayList is false?
Q16
Which loop prints every element of ArrayList<String> list?
Q17
Why can removing elements inside a forward for loop skip items?
Q18
Which is the main advantage of an array over an ArrayList?
Q19
Why is StringBuilder preferred to repeated String concatenation in a loop?
Q20
What is the output?
StringBuilder sb = new StringBuilder("Java");
sb.append(" SE").insert(0, "* ");
System.out.print(sb);
Q21
What is the output?
StringBuilder sb = new StringBuilder("abcdef");
sb.delete(1, 3);
System.out.print(sb);
Q22
What is the output?
StringBuilder b = new StringBuilder("stressed");
System.out.print(b.reverse());
Q23
What is the result of compiling the following?
String b = "stressed";
System.out.print(b.reverse());
Q24
What is the output?
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 3; i++) sb.append(i).append(",");
System.out.print(sb.length());
Q25
Which StringBuilder method returns an ordinary String?
Q26
What is the output?
StringBuilder a = new StringBuilder("one");
StringBuilder b = a;
b.append(" two");
System.out.print(a);
Q27
Why does the previous question behave differently from the same code written with
String?
Q28
What is the output?
ArrayList<Student> c = new ArrayList<>();
c.add(new Student("Ali", 40));
c.add(new Student("Sara", 80));
int passed = 0;
for (Student s : c) if (s.isPassed()) passed++;
System.out.print(c.size() + " " + passed);
(assume isPassed() returns grade >= 50)
Q29
For contains to work as expected on an ArrayList of your own objects, the class
must:
Q30
Which statement correctly declares a list that can hold Student objects?
Q31
What is printed?
ArrayList<String> t = new ArrayList<>();
t.add("a"); t.add("b"); t.add("c");
for (int i = t.size() - 1; i >= 0; i--) System.out.print(t.get(i));
Q32
Which call empties a list completely?
Q33
What is the output?
ArrayList<Integer> v = new ArrayList<>();
for (int i = 0; i < 5; i++) v.add(i * i);
System.out.print(v.get(3) + " " + v.size());
Q34Short answer
Explain, in terms of objects created in memory, the difference
between building a 10,000-character report with String concatenation and with
StringBuilder.
Q35Short answer
A list ArrayList<Integer> scores contains
[3, 3, 7, 9, 3]. Write the code that removes every occurrence of the value 3, and explain
why the obvious forward loop fails.
Q36Programming
Write a class Classroom that holds an
ArrayList<Student> and offers addStudent(Student), removeStudent(int id),
findStudent(int id) returning the object or null, averageGrade() and
topStudent(). Write a driver that adds four students, removes one, and prints the average
and the best student.
Q37Programming
Write a method
public static String compress(String s) that uses a StringBuilder to turn
"aaabccddd" into "a3b1c2d3", and a main that tests it on at least three inputs
including the empty string.