CMSC Midterm 2 Study Guide
Verified Exam Questions and Answers Latest update 2026/2027
Question:
What is meant by "the state" of an object? -
Answer:
The data that the
object maintains.
Question:
What is meant by "the behavior" of an object? -
Answer:
These are
"actions" that the object can perform (via methods).
Question:
How is the state of an object represented in the corresponding class definition? -
Answer:
Using instance variables.
Question:
How is the behavior of an object represented in the corresponding class definition? -
Answer:
Using instance methods.
Question:
What is a "reference" variable? -
Answer:
A reference variable stores the
memory address of an object. We say that the variable "refers" to the object.
Question:
Given the code fragment String q = new String("Cat"); and the code fragment double x = 7.9; is
it correct to say that "q is a String object"? Is it correct to say that "q refers to a String object"? Is
it correct to say that "x is a double"? Is it correct to say that "x refers to a double"? -
Answer:
You should NOT say "q is a String". You SHOULD say "q refers to a
String". It is correct to say "x is a double", but you should NOT say "x refers to a double", since
x is not a reference variable.
,Question:
Consider the assignment statement: x = y; -
Answer:
If the variables are of
type "int", does the integer get copied? If the variables are of type "String", does the String get
copied?
For primitive types (like int), the data is copied. For reference variables (like a String reference),
the memory locations are copied, NOT the objects themselves.
Question:
True/False: In order to run a static method, you must run it for a particular object (the current
object). -
Answer:
False
Question:
True/False: In order to run an instance method, you must run it for a particular object (the current
object). -
Answer:
True
Question:
True/False: An instance variable can be declared as "static". -
Answer:
False
Question:
True/False: The same class can contain some members that are static and some members that are
non-static. -
Answer:
True
Question:
What is "the heap"? -
Answer:
The heap is the region in memory where
Objects are stored. When objects are no longer being used, that memory is "recycled"
automatically so that it can be used by other Objects later.
, Question:
Draw the memory map (including the "stack" and the "heap") for the following code fragment: -
Answer:
double x = 7.9;
double y = x;
String q = new String("Cat");
String r = q;
String s = new String(r);
The stack would have x with the value 7.9 and y with the value 7.9
and would also have q and r and s with references to String objects
on the heap.
The heap would have two String objects; one for "Cat" that q and r
reference and then another for "Cat" that s references.
Question:
What is actually stored in the variable "q" in the previous question? -
Answer:
q effectively holds the memory location of where the String "Cat" is in RAM
Question:
Given the code fragment above, is it correct to say that "q is a String object"? Is it correct to say
that "q refers to a String object"? Is it correct to say that "x is a double"? Is it correct to say that
"x refers to a double"? -
Answer:
You should NOT say "q is a String".
You SHOULD say "q refers to a String". It is correct to say "x is a double", but you should NOT
say "x refers to a double", since x is not a reference variable.
Question:
How many String objects are actually created (instantiated) in the code fragment above? -
Answer:
two
Verified Exam Questions and Answers Latest update 2026/2027
Question:
What is meant by "the state" of an object? -
Answer:
The data that the
object maintains.
Question:
What is meant by "the behavior" of an object? -
Answer:
These are
"actions" that the object can perform (via methods).
Question:
How is the state of an object represented in the corresponding class definition? -
Answer:
Using instance variables.
Question:
How is the behavior of an object represented in the corresponding class definition? -
Answer:
Using instance methods.
Question:
What is a "reference" variable? -
Answer:
A reference variable stores the
memory address of an object. We say that the variable "refers" to the object.
Question:
Given the code fragment String q = new String("Cat"); and the code fragment double x = 7.9; is
it correct to say that "q is a String object"? Is it correct to say that "q refers to a String object"? Is
it correct to say that "x is a double"? Is it correct to say that "x refers to a double"? -
Answer:
You should NOT say "q is a String". You SHOULD say "q refers to a
String". It is correct to say "x is a double", but you should NOT say "x refers to a double", since
x is not a reference variable.
,Question:
Consider the assignment statement: x = y; -
Answer:
If the variables are of
type "int", does the integer get copied? If the variables are of type "String", does the String get
copied?
For primitive types (like int), the data is copied. For reference variables (like a String reference),
the memory locations are copied, NOT the objects themselves.
Question:
True/False: In order to run a static method, you must run it for a particular object (the current
object). -
Answer:
False
Question:
True/False: In order to run an instance method, you must run it for a particular object (the current
object). -
Answer:
True
Question:
True/False: An instance variable can be declared as "static". -
Answer:
False
Question:
True/False: The same class can contain some members that are static and some members that are
non-static. -
Answer:
True
Question:
What is "the heap"? -
Answer:
The heap is the region in memory where
Objects are stored. When objects are no longer being used, that memory is "recycled"
automatically so that it can be used by other Objects later.
, Question:
Draw the memory map (including the "stack" and the "heap") for the following code fragment: -
Answer:
double x = 7.9;
double y = x;
String q = new String("Cat");
String r = q;
String s = new String(r);
The stack would have x with the value 7.9 and y with the value 7.9
and would also have q and r and s with references to String objects
on the heap.
The heap would have two String objects; one for "Cat" that q and r
reference and then another for "Cat" that s references.
Question:
What is actually stored in the variable "q" in the previous question? -
Answer:
q effectively holds the memory location of where the String "Cat" is in RAM
Question:
Given the code fragment above, is it correct to say that "q is a String object"? Is it correct to say
that "q refers to a String object"? Is it correct to say that "x is a double"? Is it correct to say that
"x refers to a double"? -
Answer:
You should NOT say "q is a String".
You SHOULD say "q refers to a String". It is correct to say "x is a double", but you should NOT
say "x refers to a double", since x is not a reference variable.
Question:
How many String objects are actually created (instantiated) in the code fragment above? -
Answer:
two