Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 2 out of 12 pages
Exam (elaborations)

CMSC 131 MIDTERM 2 QUESTIONS WITH VERIFIED ANSWERS

Document preview thumbnail
Preview 2 out of 12 pages

CMSC 131 MIDTERM 2 QUESTIONS WITH VERIFIED ANSWERS

Content preview

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

Document information

Uploaded on
July 10, 2026
Number of pages
12
Written in
2025/2026
Type
Exam (elaborations)
Contains
Questions & answers
$14.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.
GEEKA
3.8
(361)
Sold
2137
Followers
1449
Items
58515
Last sold
2 days 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