• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 3 out of 19 pages
Exam (elaborations)

CMSC Midterm 2 Study UPDATED ACTUAL Questions and CORRECT Answers

Document preview thumbnail
Preview 3 out of 19 pages

CMSC Midterm 2 Study UPDATED ACTUAL Questions and CORRECT Answers

Content preview

CMSC Midterm 2 Study UPDATED
ACTUAL Questions and CORRECT
Answers
What is meant by "the state" of an object? - CORRECT ANSWER - The data that the
object maintains.


What is meant by "the behavior" of an object? - CORRECT ANSWER - These are
"actions" that the object can perform (via methods).


How is the state of an object represented in the corresponding class definition? - CORRECT
ANSWER - Using instance variables.


How is the behavior of an object represented in the corresponding class definition? - CORRECT
ANSWER - Using instance methods.



What is a "reference" variable? - CORRECT ANSWER - A reference variable stores the
memory address of an object. We say that the variable "refers" to the object.


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"? - CORRECT
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.


Consider the assignment statement: x = y; - CORRECT 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.

,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



What is "the heap"? - CORRECT 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.


Draw the memory map (including the "stack" and the "heap") for the following code fragment: -
CORRECT 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.

, What is actually stored in the variable "q" in the previous question? - CORRECT
ANSWER - q effectively holds the memory location of where the String "Cat" is in RAM


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"? - CORRECT 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.


How many String objects are actually created (instantiated) in the code fragment above? -
CORRECT ANSWER - two


After the code fragment above has been executed, decide which of the following expressions are
true and which are false: - CORRECT ANSWER - x == y TRUE
q == r TRUE
r == s FALSE
q == s FALSE
q.equals(r) TRUE
r.equals(s) TRUE
q.equals(s) TRUE


What is aliasing? - CORRECT ANSWER - Aliasing is having more than one reference
variable that refer to the same object (for example, r and q in the previous question.)


What is meant by the term "garbage" in Java? Write a code fragment that creates garbage. -
CORRECT ANSWER - Any object that is on the heap, but no longer referred to by any
variable is called "garbage". Here is a short example:
String p = "hello";
p = "there";
(Now the String that contains "hello" is garbage.)

Document information

Uploaded on
October 26, 2025
Number of pages
19
Written in
2025/2026
Type
Exam (elaborations)
Contains
Questions & answers
$12.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
STANFORDGRADESS
4.0
(241)
Sold
1674
Followers
109
Items
121810
Last sold
1 hour ago




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions