0% found this document useful (0 votes)
19 views7 pages

Java Code Analysis for EECS 1022 Exam

The document provides a guide and sample questions for the EECS 1022 Final Exam, focusing on Java programming concepts and code tracing. It includes study tips and a series of sample questions that test understanding of Java code execution, error handling, and object-oriented principles. The questions cover topics such as class constructors, array manipulation, and method behavior in Java.

Uploaded by

xiejohn44
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Java Code Analysis for EECS 1022 Exam

The document provides a guide and sample questions for the EECS 1022 Final Exam, focusing on Java programming concepts and code tracing. It includes study tips and a series of sample questions that test understanding of Java code execution, error handling, and object-oriented principles. The questions cover topics such as class constructors, array manipulation, and method behavior in Java.

Uploaded by

xiejohn44
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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");
[Link](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];
[Link](Students[[Link]()] != 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];
[Link](Students[[Link]] != 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];
[Link](Students[[Link] - 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];
[Link](Students[[Link] - 1].[Link]("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; [Link](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;
[Link](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;
[Link]("Jam");
[Link]([Link]);

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<[Link]; j++) values[j]++;
B) for (j=0; j<[Link]; j++) values[j]++;
C) for (j=0; j<=[Link]; j++) values[j]++;
D) for (j=0; j<[Link]-1; j++) values[j]++;
E) for (j=1; j<[Link]-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];

[Link]("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 < [Link]; row++)
for (int column = 0; column < values[row].length; column++)
if (v < values[row][column])
v = values[row][column];

[Link](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. [Link]("Red");
b. [Link](new Integer(100));
c. [Link](new [Link]());

6
d. [Link](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

Common questions

Powered by AI

In Java, aliasing occurs when multiple references point to the same object. If `Student p1` is assigned `Student p2` then changes made to the object through one reference will reflect in the other. For example, `p1` being assigned to `p2` makes changes to the name attribute in `p1` visible in `p2`. Aliasing can create unintended side-effects if not carefully managed, since both references will affect the same object instance .

The code will output `33` because it initializes a 2D array and iterates over it to find the maximum element. Starting with the first element `3`, it checks each value sequentially across both sub-arrays. Upon iterating over all elements, `33` is identified as the largest value, thus being printed .

A constructor's primary role in Java is to initialize a new object of a class. It sets up the initial state of an object by assigning values to the fields of the object when it is created .

The for-loop `for (j=0; j<values.length; j++) values[j]++;` is effective for incrementing all elements because it correctly initializes the loop at the first index, continues until the last index, and increments each iteration by one. This ensures that every element from `0` to `values.length - 1` is incremented, thus handling the entire array's range without index-related errors .

The method `question15()` returns the index of the child who sold the most candy bars. Within a loop of 12 iterations, it compares each element in the `candy` array with `value1`, updating `value1` with the current candy count and `value2` with the index whenever a larger value is found .

The `this` keyword in Java is used within an instance method or a constructor to access the object's fields and methods. It helps distinguish between class member variables and parameters or other scope variables. For example, if an object has a field `name` and a parameter `name` in its constructor, `this.name = name` assigns the parameter value to the class field, maintaining clarity and explicit reference to the object's properties .

Compilation errors occur at compile time when there are syntax violations, such as missing semicolons or incompatible data types. For example, a code fragment would not compile if it uses an incorrect method call. Runtime errors, like `NullPointerException`, occur during execution if the program logic leads to unexpected situations, such as dereferencing a null object or index out of bounds operations, seen when accessing an uninitialized element of an array .

Dereferencing elements in an uninitialized array in Java can lead to `NullPointerException` at runtime. This error occurs when code attempts to access or modify an object through a null reference. For instance, accessing `Students[Students.length].name` without properly initializing the `Students` array elements leads to such an exception, creating faults in program execution .

When `ArrayList<String> x = new ArrayList<>(2)` is initialized, no elements are currently in the array list. The capacity, however, is specified as 2, implying it's prepared to hold up to two elements efficiently without resizing .

In Java, declaring `char[][] charArray = {{'a', 'b'}, {'c', 'd'}};` is correct because it defines a two-dimensional array with explicit values. This syntax initializes the `charArray` with two inner arrays, each representing a row that can be directly assigned initial values in a compact and readable manner, demonstrating Java's ability to manage multi-dimensional data structures .

You might also like