EECS 1022 Final Exam
Guide and Sample Questions
Fall 22
A. Study Tips for the Final Exam
a. The Exam is meant to test your understanding of the concepts taught in this course. Note that it is
is different from a programming test in which you are expected to write Java programs without
compile time or run time errors.
b. Go through the slides to review the concepts and examples. If needed, watch again part of the
lecture videos.
c. Pay special attention to the logic explained on tracing Java code (e.g., visualizing object creations
and method calls).
d. Given a piece of Java code, you are expected to conclude:
• Whether or not it has any compilation error
• f it compiles, whether or not it crashes with an exception
• If it compiles, what output it produces, and whether or not there are any logical errors (i.e.,
the output is not as expected)
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);
What happens when executing the above Java code?
A. The above Java code does not compile.
B. A NullPointerException occurs.
C. An ArrayIndexOutOfBoundsException occurs.
D. One line output to the console:
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);
What happens when executing the above Java code?
A. The above Java code does not compile.
1
B. A NullPointerException occurs.
C. An ArrayIndexOutOfBoundsException occurs.
D. One line output to the console:
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);
What happens when executing the above Java code?
A. The above Java code does not compile.
B. A NullPointerException occurs.
C. An ArrayIndexOutOfBoundsException occurs.
D. One line output to the console:
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);
What happens when executing the above Java code?
A. The above Java code does not compile.
B. A NullPointerException occurs.
C. An ArrayIndexOutOfBoundsException occurs.
D. One line output to the console:
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"));
What happens when executing the above Java code?
A. The above Java code does not compile.
B. A NullPointerException occurs.
C. An ArrayIndexOutOfBoundsException occurs.
D. One line output to the console:
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);
What happens when executing the above Java code?
A. The above Java code does not compile.
B. A NullPointerException occurs.
C. An ArrayIndexOutOfBoundsException occurs.
D. One line output to the console:
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);
What happens when executing the above Java code?
A. The above Java code does not compile.
B. A NullPointerException occurs.
C. An ArrayIndexOutOfBoundsException occurs.
D. One line output to the console:
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);
What happens when executing the above Java code?
A. The above Java code does not compile.
B. A NullPointerException occurs.
C. An ArrayIndexOutOfBoundsException occurs.
D. One line output to the console:
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
10. If a method does not have a return statement, then
A) it will produce a syntax error when compiled
B) it must be a void method
C) it can not be called from outside the class that defined the method
4
D) it must be defined to be a public method
E) it must be an int, double, float or String method
Answer: B
11. A class' constructor usually defines
A) how an object is initialized
B) how an object is interfaced
C) the number of instance data in the class
D) the number of methods in the class
E) if the instance data are accessible outside of the object directly
Answer: A
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
13. What does the following method do?
public int question15( )
{
int value1 = 0;
int value2 = 0;
for (int j = 0; j < 12; j++)
if (candy[j] > value1)
{
value1 = candy[j];
value2 = j;
}
return value2;
}
A) It returns the total number of candy bars sold
B) It returns the total number of children who sold 0 candy bars
C) It returns the total number of children who sold more than 0 candy bars
D) It returns the number of candy bars sold by the child who sold the most candy bars
E) It returns the index of the child who sold the most candy bars
Answer: E
14. Which of the following statements are correct?
a. char[][] charArray = {'a', 'b'};
b. char[2][2] charArray = {{'a', 'b'}, {'c', 'd'}};
c. char[2][] charArray = {{'a', 'b'}, {'c', 'd'}};
d. char[][] charArray = {{'a', 'b'}, {'c', 'd'}};
Key:d
5
15. Analyze the following code:
public class Test {
public static void main(String[] args) {
boolean[][] x = new boolean[3][];
x[0] = new boolean[1]; x[1] = new boolean[2];
x[2] = new boolean[3];
System.out.println("x[2][2] is " + x[2][2]);
}
}
a. The program has a compile error because new boolean[3][] is wrong.
b. The program has a runtime error because x[2][2] is null.
c. The program runs and displays x[2][2] is null.
d. The program runs and displays x[2][2] is true.
e. The program runs and displays x[2][2] is false.
Key:e x is a ragged array. (See the section on Ragged Array) x[2] has three elements with default value
false.
16. What is the output of the following program?
public class Test {
public static void main(String[] args) {
int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};
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