Sample Questions
Sample Questions
These are sample questions that cover topics on reference aliasing, and are thus meant to substitute
studying the lectures and material on the covered topics.
B. Sample Questions
Assume that a Student class is already defined, and it has an attribute name and a constructor that
initializes the Student’s name from the input string. Answer the following questions:
1. Consider the following fragment of Java code (inside some main method):
Student p1 = new Student("Sam"); Student
p2 = new Student("Tam");
System.out.println(p1 != p2);
True
E. One line output to the console:
false
F. None of the above.
2. Consider the following fragment of Java code (inside some main method):
Student p1 = new Student("Sam");
Student p2 = new Student("Tam");
Student[] Students = new Student[2];
System.out.println(Students[Students.length()] != null);
true
E. One line output to the console:
false
F. None of the above.
3. Consider the following fragment of Java code (inside some main method):
Student p1 = new Student("Sam ");
Student p2 = new Student("Tam ");
Student[] Students = new Student[2];
System.out.println(Students[Students.length] != null);
true
E. One line output to the console:
false
F. None of the above.
4. Consider the following fragment of Java code (inside some main method):
Student p1 = new Student("Sam ");
Student p2 = new Student("Tam ");
Student[] Students = new Student[2];
System.out.println(Students[Students.length - 1] != null);
true
E. One line output to the console:
false
F. None of the above.
2
5. Consider the following fragment of Java code (inside some main method):
Student p1 = new Student("Sam");
Student p2 = new Student("Tam");
Student[] Students = new Student[2];
System.out.println(Students[Students.length - 1].name.equals("Sam"));
true
E. One line output to the console:
false
F. None of the above.
6. Consider the following fragment of Java code (inside some main method):
Student p1 = new Student("Sam");
Student p2 = new Student("Tam");
Student[] Students = {p1, p2};
p1 = p2; System.out.println(Students[0] == p1);
true
E. One line output to the console:
false
F. None of the above.
3
7. Consider the following fragment of Java code (inside some main method):
Student p1 = new Student("Sam");
Student p2 = new Student("Tam");
Student[] Students = {p1, p2};
p1 = p2;
Students[0] = p2;
System.out.println(Students[0] == p1);
true
E. One line output to the console:
false
F. None of the above.
8. Consider the following fragment of Java code (inside some main method):
Student p1 = new Student("Sam");
Student p2 = new Student("Tam");
Student[] Students = {p1, p2};
p1 = Students[1];
Students[0] = p2;
p2.setName("Jam");
System.out.println(p1.name);
Sam
E. One line output to the console:
Tam
F. One line output to the console:
Jam
G. None of the above.
9. An object that refers to part of itself within its own methods can use which of the following reserved
words to denote this relationship?
A) inner
B) i
C) private
D) this
E) static
Answer: D
12. Which of the following loops would adequately add 1 to each element stored in values?
A) for (j=1; j<values.length; j++) values[j]++;
B) for (j=0; j<values.length; j++) values[j]++;
C) for (j=0; j<=values.length; j++) values[j]++;
D) for (j=0; j<values.length-1; j++) values[j]++;
E) for (j=1; j<values.length-1; j++) values[j]++;
Answer: B
5
15. Analyze the following code:
int v = values[0][0];
for (int row = 0; row < values.length; row++)
for (int column = 0; column < values[row].length; column++)
if (v < values[row][column])
v = values[row][column];
System.out.print(v);
}
}
a. 1
b. 3
c. 5
d. 6
e. 33
Key:e
17. Suppose List<String> list = new ArrayList<String>. Which of the following operations are correct?
a. list.add("Red");
b. list.add(new Integer(100));
c. list.add(new java.util.Date());
6
d. list.add(new ArrayList());
Key:a
18. When you create an ArrayList using ArrayList<String> x = new ArrayList<>(2), ________
a. two elements are created in the array list.
b. no elements are currently in the array list.
c. the array list size is currently 2.
d. the array list capacity is currently 0.
Key:b