Java SampleExamQuestions
Java SampleExamQuestions
Java SampleExamQuestions
To help you study for the midterm and final, here are some questions from previous
exams I gave in Java programming courses I've taught. (Since this is the first time this
course is being offered, I have no "old" ICS 45J exams questions to share.) These
questions are not necessarily all-inclusive of the subject matter. There may be questions
on the actual exams asking about material that no question here asks about. Likewise,
there will be questions here that do not pertain to the particular test for which you may
be studying--for example, if you are getting ready for the midterm, you will find
questions here about material that will be covered by the final; just ignore those
questions for the present but, of course, don't ignore them when studying for the final!
For all program fragments, assume any necessary “imports” are present, and that the
fragments are a part of an otherwise perfect program.
2. When does Java know an object is no longer needed? And what happens to
an unneeded object's storage?
A. A > 5 || B != C
B. A >= 5 && B == C
C. !(A < 5) || (B != C)
D. A >= 5 || B == C
E. A < 5 && B == C
-2-
When answering the next 5 questions, consider this code fragment:
int sum = 0;
int i = 0;
while (i < 5)
{
sum = sum + i;
i++;
}
System.out.print(i);
System.out.print(" ");
System.out.print(sum);
6. What is the value of i when System.out.print(i) is executed?
A. 6
B. 5
C. 4
D. 3
E. unpredictable, since i is local to the loop
A. 6
B. 10
C. 15
D. 21
E. unpredictable, since sum is dependent upon a variable local to the loop
8. The fragment executes more times through the loop than is necessary. What
change to the fragment can be made that removes the unneeded passes through
the loop and doesn’t change the values of i and sum that are printed out?
-3-
9. Suppose we replace the while loop in the fragment above with a do loop.
Which of the following do loops will result in the same value of Sum printing out?
A. do C. do
{ {
i++; sum = sum + i;
sum = sum + i; i++;
} }
while (i <= 5); while (i < 5);
B. do D. do
{ {
i++; sum = sum + i;
sum = sum + i; i++;
} }
while (i < 5); while (i <= 5);
E. Both A and C
10. Suppose we replace the while loop in the fragment above with a for loop. Which of
the following for loops will result in the same value of Sum printing out?
A. for (int i = 0; i <= 5; i++)
sum = sum + i;
B. for (int i = 1; i <= 5; i++)
sum = sum + i;
C. for (int i = 1; i < 5; i++)
sum = sum + i;
D. for (int i = 2; i < 5; i++)
sum = sum + i;
E. for (int i = 1; i < 6; i++)
sum = sum + i;
-4-
12. What is the output from the following Java program fragment? Read it
carefully!
String S = ""; String T = "";
int i = 4;
for (i = 1; i <= 3; i++);
S = S + "!";
for (i = 1; i < 4; i++)
T = T + "*";
System.out.print(S);
System.out.println(T);
13. Which if statement below is equivalent to the given switch statement (that is,
produces the same output under the same conditions)? Assume answer is a previously
declared int.
switch (answer)
{ case 0: System.out.print("0 entered"); break;
case 1: System.out.print("1 entered"); break;
case 3: System.out.print("3 entered"); break;
case 5: System.out.print("5 entered"); break;
default: System.out.print("Other value entered");
}
I. if (answer == 0 || 1 || 3 || 5)
System.out.print(answer + " entered");
else
System.out.print("Other value entered");
IV. if (answer == 0)
System.out.print("0 entered");
if (answer == 1)
System.out.print("1 entered");
if (answer == 3)
System.out.print("3 entered");
if (answer == 5)
System.out.print("5 entered");
if (answer != 0 && answer != 1 &&
answer != 3 && answer != 5)
System.out.print("Other value entered");
-5-
V. if (answer == 0)
System.out.print("0 entered");
else if (answer == 1)
System.out.print("1 entered");
else if (answer == 3)
System.out.print("3 entered");
else if (answer == 5)
System.out.print("5 entered");
else if (answer != 0 & 1 & 3 & 5)
System.out.print("Other value entered");
When answering the next 5 questions, consider this program; comments indicate
where missing needed components of the program are to be placed.
class MyClass
{
// (1) definition of MyClass constructor
-6-
14. Suppose you are writing the definition of MyClass (line (1) above). Which of
the following function signatures (headers) is correct?
A. public MyClass D. public void MyClass()
B. public MyClass() E. public MyClass(void)
C. public void MyClass
15. Suppose you wish to call the method that prints the greeting, at line(2) above.
Which of the following statements will call this method correctly? myObject is
the MyClass object defined in the question above.
A. MainClass.greetings(); D. void result = greetings();
B. myObject.greetings(); E. greetings();
C. MyClass.greetings();
16. Suppose you wish to construct a MyClass object called myObject at line (3)
above. Which of the following statements will correctly do this?
A. MyClass myObject;
B. myObject.MyClass();
C. MyClass myObject = MyClass();
D. MyClass myObject = new(MyClass);
E. MyClass myObject = new MyClass();
17. Suppose you wish to call the update method at line(4) above. Which of the
following statements will call this method correctly?
A. update(myObject(3, "Hi!"));
B. update(3, "Hi!");
C. MyClass.myObject.update(3, "Hi!");
D. myObject.update(3, "Hi!");
E. MyClass.update(3, "Hi!");
18. Here’s update, completed. The intent of the method is to update the class’
fields with the values provided by the parameters num and title:
public void update(int num, String title)
{ int numOfItems = num;
reportTitle = title;
}
If this method is called, what can we say about the values of the member
variables numOfItems and reportTitle when that call completes?
A. numOfItems takes on the value of num; reportTitle takes on the value of title.
B. numOfItems takes on the value of num; reportTitle 's value remains unchanged.
C. numOfItems value remains unchanged; reportTitle takes on the value of
title.
D. Both numOfItems' and reportTitle's values remain unchanged.
E. A run-time error occurs because of the illegal redefinition of a variable inside the
function.
-7-
Use this method when answering the next 2 questions.
public static int theValue()
{
boolean notDone = true;
int foundIt = -9;
do {
System.out.print("Enter score: ");
int cntScore = getInt(); //assume getInt() is defined as in lab
if (cntScore != -99)
{ if ((cntScore >= 0) && (cntScore <= 100))
if (cntScore > foundIt)
foundIt = cntScore;
}
else
notDone = false;
} while (notDone == true);
return foundIt;
}
19. Which aspects of the fragment (of those listed below) violate good style
standards (as defined in this course)?
I. having the { after do (on the same line)—the { should be on its own line
directly underneath do
II. The constants –9, -99, 0 and 100 should have been named, and the names
should have been used in the fragment
III. Using notDone == true as the test for the loop—there is a cleaner
expression that can be used
IV. The user is not told the sentinel value
A. I and II only
B. III and IV only
C. I, II and III only
D. II, III and IV only
E. I through IV
20. Which statement best describes the intended purpose of this fragment?
-8-
21. Which of the following statements about Java arrays and ArrayLists are true?
I. Arrays are similar to objects, but technically are not true objects.
II. Once an ArrayList’s size is set, it cannot be changed without
reconstructing it.
III. Arrays can directly hold primitive types as well as object references.
IV. Array indexing begins at 0, but ArrayList indexing begins at 1.
22. Consider a program written using an array A of size 50. Now suppose you
want to change the program so that A is an ArrayList. What changes must or
should be made to the program? Your goal is to have a correctly functioning
program that follows good programming practice, while minimizing changes to
the code.
A. Yes, always
B. Yes, but only if a project file setting is made to enable such checking
C. Yes, but it is limited to single-dimension arrays
D. Yes, but only if the array is a member of a class
E. No
-9-
When answering the next four questions, use this program fragment. intObj is a
class containing one private field, an integer. int getInt() returns the value of
this integer; void setInt(int newValue) changes its value to newValue.
intObj p;
p = new intObj();
intObj q = new intObj();
q.setInt(20);
p.setInt(q.getInt()); //line 1
p.setInt(15);
q = p; //line 2
p = null;
System.out.print(p.getInt()); //line 3
A. an exception is thrown
B. the number 20 prints
C. the number 15 prints
D. the number 0 prints
E. the number stored where q is referencing is printed
- 10 -
27. Which of these situations would cause addThemUp to return incorrect results?
I. the parameter's value was negative
II. the parameter's value was zero
III. the parameter's value was positive and even
IV. the parameter's value was positive and odd
A. I only D. I and IV only
B. I and II only E. I, II, III and IV
C. II and III only
28. Which of these versions of addThemUp would return “as intended” results?
D. Both A and B
E. A, B and C
29. Suppose you wanted to use a function to initialize three (already declared) variables
that are not fields of a class. Further suppose you are doing this in a language that allows
passing parameters either by value or by reference. To initialize the variables,
A. you should pass them to the function by reference and set the values in the
function; the variables will be initialized when the function returns.
B. you should pass them to the function by value and set the values in the
function; the variables will be initialized when the function returns.
C. you should pass them to the function by value, set the values in the function,
then return these values via a return statement.
D. define these variables so they are global to the function, then set their values
within the function; that is the preferred approach.
E. you should adopt another approach: none of the above methods is a reasonable
approach.
- 11 -
30. Here is a list of steps to be taken when a function with a parameter is called:
I. execute the function (and use the formal parameter during its execution)
II. delete the temporary storage associated with the formal parameter
III. do whatever work is necessary to determine the actual parameter‘s
value
IV. do whatever work is necessary to resolve the actual parameter to a
location in memory
V. create temporary storage with the name of the formal parameter
VI. copy the value of the actual parameter to the temporary storage location
VII. copy the memory location of the actual parameter to the temporary
storage location
31. What is the output from the following Java program fragment?
(Reponses are on the next page.)
public static void main(String[] args)
{
int A = 10;
int B = 20;
update(A, B);
System.out.println(A + " " + B);
}
- 12 -
32. Suppose you have a class MyClass and want to easily replace the contents of
one object, target, with the contents of another object of MyClass, source. Which
of the following statements would correctly create the copy?
A. target = source;
B. target.clone(source);
C. target = source.clone();
D. target = source.equals();
E. target = (MyClass) source.clone();
34. In Java, what happens if code is written that could throw a checked exception
in a method that has no throws clause for that exception, and there is no catch
block defined in the method to handle that particular exception class?
A. If an accessible catch block exists for one of the exception’s ancestor classes,
then the program compiles and runs.
B. If there is no catch block that can handle the exception, the code will not
compile.
C. If the exception is thrown, and there is no catch block that can handle the
exception, the program halts.
D. If the exception is thrown, and there is no catch block that can handle the
exception, the program continues, but its results are unpredictable
E. Both A and B, taken together, fully describe what occurs.
35. A binary file opened for input only and relative access can
- 13 -
Use this information when answering the next three questions:
36. Which phrase best describes what missing action 1 should be?
37. Which phrase best describes what missing action 2 should be?
38. What is the output of this program fragment? (Ignore any leading spaces.)
double X = 123.321;
String Y = "Hi!";
System.out.format("%7.3f%s", X, Y);
A. 23.32Hi! D. 1.23e2Hi!
B. 123.321Hi! E. none; a compile- or run-time error occurs
C. +23.32Hi!
- 14 -
39. When using hasNext() on a Scanner, how do we know when the end of file
has been reached?
40. If a class is not qualified as public or private, what does that imply about its
public methods and fields?
41. What’s the connection between public classes and .java file names?
(Assume the file contains no inner classes.)
A. The file can have at most one public class; if present, it must have the same
name as the file.
B. The file must have at least one public class and none of them can have the
same name as the file.
C. It is common practice to name a file after one of the public classes in it, but
Java does not require it.
D. If the file does not contain a public class, its name must not match the names
of any of the classes in it.
E. There is no connection!
- 15 -
Use this information when answering the next seven questions:
Classes Square, EqiTriangle and Pentagon are derived from Polygon. Square
and EqiTriangle each have, among other public methods, one named area(),
which takes no parameters and returns as type double the area of a Square and
EqiTriangle, respectively. Pentagon does not define a method named area().
I. S = T III. T = P
II. P = T IV. P = S
A. II and IV only
B. II and III only
C. II, III and IV only
D. I, II and IV only
E. I, II, III and IV
A. Pentagon D. Byte
B. Polygon E. Class
C. Object
- 16 -
47. Suppose you wish to access the pentagon stored at position 3 of an
ArrayList<Pentagon> pentagonGroup and invoke the area() method on it. Which
statement below does this correctly?
A. pentagonGroup.get(3).area();
B. pentagonGroup.area().get(3);
C. pentagonGroup.(Pentagon).get(3).area();
D. pentagonGroup[3].area();
E. (Pentagon)pentagonGroup[3].area();
48. Suppose you wish to call Polygon's area() method in the definition of
EqiTriangle's area() method; both area() methods have the same signature.
How is this done?
A. (polygon)area(); D. area();
B. super.area(); E. parent.area();
C. this.area();
49. Suppose Polygon's area() function is made abstract. Which of the following
statements are then true?
I. An applet is a class that must be derived from the Java Applet class
II. An applet is invoked from HTML statements, rather than a "main()"
III. An applet almost always overrides the paint() method
IV. An applet must be compiled into a .class files before it can be used
A. I and II only
B. II and III only
C. I, II and III only
D. II, III and IV only
E. I, II, III and IV
- 17 -
51. When is the paint() method called?
52. The graphics window in Java has its origin in a specified position, and has the
x and y coordinates’ values increasing in specified directions. Which of the
selections below correctly gives these specifications?
53. To “notice” an event (and react to it), Java requires you to do which of the
following things?
I. define your own “handler” class that implements the appropriate Java-
provided listener interface
II. define your own “handler” class that extends the appropriate Java-
provided adapter class
III. construct an object of your “handler” class
IV. add your handler object to the list of listeners Java maintains
A. Either I or II, and III and IV
B. I, II, III and IV
C. II and III only
D. I and III only
E. Either I or II, and IV only
- 18 -