CMSC 131 UMD Midterm 2 UPDATED
ACTUAL Questions and CORRECT
Answers
True/False: In order to run a static method, you must run it for a particular object (the current
object). - CORRECT ANSWER - False
True/False: In order to run an instance method, you must run it for a particular object (the current
object). - CORRECT ANSWER - True
True/False: An instance variable can be declared as "static". - CORRECT ANSWER -
False
True/False: The same class can contain some members that are static and some members that are
non-static. - CORRECT ANSWER - True
Which kinds of variables are given default values if you do not initialize them? (Local/
instance/static? Which ones?) - CORRECT ANSWER - instance and static
What default values are used for the variables mentioned above? - CORRECT ANSWER -
0 for all primitive types (false for boolean, ASCII code 0 for char); "null" for reference variables
When writing JUnit tests, is it better to write many small tests, or a few long ones? - CORRECT
ANSWER - Lots of small ones
What is JUnit? - CORRECT ANSWER - A useful feature of Java that allows us to
quickly write "unit tests" for classes we are writing. The tests can be run conveniently and
repeatedly so that as changes are made to the code we can quickly verify that everything still
works properly!
,Give an example of the correct syntax for "assertTrue" in a JUnit test. - CORRECT
ANSWER - assertTrue(x < 4); // test will pass if and only if the value of x is less than 4
If a JUnit test has several separate assertions in it, will the test continue after one of the
assertions fails? - CORRECT ANSWER - No
If a JUnit test suite has several different JUnit tests in it (separate methods), will the rest of the
tests run after one of them fails? - CORRECT ANSWER - Yes
In what kind of methods does it make sense to use "this"? - CORRECT ANSWER -
Instance methods and constructors.
What does "this" refer to? - CORRECT ANSWER - The "current object".
If you do not write any constructors, what values will instance variables of the following
primitive types be assigned: int, double, boolean, char ? - CORRECT ANSWER - 0 for
int, 0.0 for double, false for boolean, ASCII code 0 for char
If you do not write any constructors, what values will instance variables that are references by
assigned? - CORRECT ANSWER - null
Under what circumstances will Java provide a default constructor for you automatically? -
CORRECT ANSWER - If you do not write any constructors yourself.
What is a copy constructor? Give an example. - CORRECT ANSWER - The copy
constructor accepts an argument that is the same type as the object being constructed. It
initializes the fields of the current object to match that of the parameter. Example:
public Cat(Cat x) {
numWhiskers = x.numWhiskers;
name = x.name;
}
, What is a Stack (in general, not just in Java)? - CORRECT ANSWER - A stack is a
simple linear data type in which elements are both inserted ("pushed") and removed ("popped")
from the same end. (We usually picture the stack vertically, and say that we "push" and "pop"
items from the top.)
When you push an entry into the stack does it go on the top or bottom? - CORRECT
ANSWER - top
When you pop an entry from the stack, does it come off the top or bottom? - CORRECT
ANSWER - top
Draw a diagram showing both the Stack and the Heap at the moment this program terminates: -
CORRECT ANSWER - public static void main(String[] args) {
int x = 0;
String y = new String("xyz");
String z = y;
String a = new String(y);
f(x, y, z, a);
}
public static void f(int j, String k, String m, String r) {
System.exit(1);
}
True/False: In java, when you pass a reference variable as an argument to a method, it is possible
for the method to modify the object to which the variable refers. - CORRECT ANSWER -
TRUE
ACTUAL Questions and CORRECT
Answers
True/False: In order to run a static method, you must run it for a particular object (the current
object). - CORRECT ANSWER - False
True/False: In order to run an instance method, you must run it for a particular object (the current
object). - CORRECT ANSWER - True
True/False: An instance variable can be declared as "static". - CORRECT ANSWER -
False
True/False: The same class can contain some members that are static and some members that are
non-static. - CORRECT ANSWER - True
Which kinds of variables are given default values if you do not initialize them? (Local/
instance/static? Which ones?) - CORRECT ANSWER - instance and static
What default values are used for the variables mentioned above? - CORRECT ANSWER -
0 for all primitive types (false for boolean, ASCII code 0 for char); "null" for reference variables
When writing JUnit tests, is it better to write many small tests, or a few long ones? - CORRECT
ANSWER - Lots of small ones
What is JUnit? - CORRECT ANSWER - A useful feature of Java that allows us to
quickly write "unit tests" for classes we are writing. The tests can be run conveniently and
repeatedly so that as changes are made to the code we can quickly verify that everything still
works properly!
,Give an example of the correct syntax for "assertTrue" in a JUnit test. - CORRECT
ANSWER - assertTrue(x < 4); // test will pass if and only if the value of x is less than 4
If a JUnit test has several separate assertions in it, will the test continue after one of the
assertions fails? - CORRECT ANSWER - No
If a JUnit test suite has several different JUnit tests in it (separate methods), will the rest of the
tests run after one of them fails? - CORRECT ANSWER - Yes
In what kind of methods does it make sense to use "this"? - CORRECT ANSWER -
Instance methods and constructors.
What does "this" refer to? - CORRECT ANSWER - The "current object".
If you do not write any constructors, what values will instance variables of the following
primitive types be assigned: int, double, boolean, char ? - CORRECT ANSWER - 0 for
int, 0.0 for double, false for boolean, ASCII code 0 for char
If you do not write any constructors, what values will instance variables that are references by
assigned? - CORRECT ANSWER - null
Under what circumstances will Java provide a default constructor for you automatically? -
CORRECT ANSWER - If you do not write any constructors yourself.
What is a copy constructor? Give an example. - CORRECT ANSWER - The copy
constructor accepts an argument that is the same type as the object being constructed. It
initializes the fields of the current object to match that of the parameter. Example:
public Cat(Cat x) {
numWhiskers = x.numWhiskers;
name = x.name;
}
, What is a Stack (in general, not just in Java)? - CORRECT ANSWER - A stack is a
simple linear data type in which elements are both inserted ("pushed") and removed ("popped")
from the same end. (We usually picture the stack vertically, and say that we "push" and "pop"
items from the top.)
When you push an entry into the stack does it go on the top or bottom? - CORRECT
ANSWER - top
When you pop an entry from the stack, does it come off the top or bottom? - CORRECT
ANSWER - top
Draw a diagram showing both the Stack and the Heap at the moment this program terminates: -
CORRECT ANSWER - public static void main(String[] args) {
int x = 0;
String y = new String("xyz");
String z = y;
String a = new String(y);
f(x, y, z, a);
}
public static void f(int j, String k, String m, String r) {
System.exit(1);
}
True/False: In java, when you pass a reference variable as an argument to a method, it is possible
for the method to modify the object to which the variable refers. - CORRECT ANSWER -
TRUE