CMSC 131 MIDTERM 2 QUESTIONS WITH VERIFIED
ANSWERS
True/False: In order to run a static method, you must run it for a particular object (the
current object). - Answers - False
True/False: In order to run an instance method, you must run it for a particular object
(the current object). - Answers - True
True/False: An instance variable can be declared as "static". - Answers - False
True/False: The same class can contain some members that are static and some
members that are non-static. - Answers - True
Which kinds of variables are given default values if you do not initialize them? (Local/
instance/static? Which ones?) - Answers - instance and static
What default values are used for the variables mentioned above? - Answers - 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? -
Answers - Lots of small ones
What is JUnit? - Answers - 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. - Answers -
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? - Answers - 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? - Answers - Yes
In what kind of methods does it make sense to use "this"? - Answers - Instance
methods and constructors.
What does "this" refer to? - Answers - 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 ? - Answers - 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? - Answers - null
Under what circumstances will Java provide a default constructor for you automatically?
- Answers - If you do not write any constructors yourself.
What is a copy constructor? Give an example. - Answers - 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)? - Answers - 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? - Answers - top
When you pop an entry from the stack, does it come off the top or bottom? - Answers -
top
Draw a diagram showing both the Stack and the Heap at the moment this program
terminates: - Answers - 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. - Answers -
TRUE
ANSWERS
True/False: In order to run a static method, you must run it for a particular object (the
current object). - Answers - False
True/False: In order to run an instance method, you must run it for a particular object
(the current object). - Answers - True
True/False: An instance variable can be declared as "static". - Answers - False
True/False: The same class can contain some members that are static and some
members that are non-static. - Answers - True
Which kinds of variables are given default values if you do not initialize them? (Local/
instance/static? Which ones?) - Answers - instance and static
What default values are used for the variables mentioned above? - Answers - 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? -
Answers - Lots of small ones
What is JUnit? - Answers - 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. - Answers -
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? - Answers - 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? - Answers - Yes
In what kind of methods does it make sense to use "this"? - Answers - Instance
methods and constructors.
What does "this" refer to? - Answers - 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 ? - Answers - 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? - Answers - null
Under what circumstances will Java provide a default constructor for you automatically?
- Answers - If you do not write any constructors yourself.
What is a copy constructor? Give an example. - Answers - 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)? - Answers - 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? - Answers - top
When you pop an entry from the stack, does it come off the top or bottom? - Answers -
top
Draw a diagram showing both the Stack and the Heap at the moment this program
terminates: - Answers - 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. - Answers -
TRUE