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 6 pages
Exam (elaborations)

CMSC131 EXAM 2 REVIEW QUESTIONS WITH VERIFIED ANSWERS

Document preview thumbnail
Preview 2 out of 6 pages

CMSC131 EXAM 2 REVIEW QUESTIONS WITH VERIFIED ANSWERS

Content preview

CMSC131 EXAM 2 REVIEW QUESTIONS WITH
VERIFIED ANSWERS

Name one class discussed in class that is immutable. - Answers - String

When should we define a method as static? - Answers - If it makes no reference to
instance variables.

What is the default value of reference instance variables of a class? - Answers - null

When is space for a local integer variable allocated and when is it recovered? - Answers
- Allocated: when the method is called; recovered: when the method finishes

Why do we never use == to compare floating point numbers? - Answers - Because
floating point numbers are approximations

When is the finally block associated with exceptions executed? - Answers - Always

In which area of memory are objects created? - Answers - heap

When should we define a constant as a static constant? In other words, when should
we usestatic final vs. final while defining a constant? - Answers - We should use static
when all instances of the class will use the same constant value

Based on the following class, indicated whether the statements below are valid or
invalid.

public class Computer
{
private String make;
public Computer(String makeIn)
{
make = makeIn;
}

public void setMake(String makeIn)
{
make = makeIn;
}

public static void info()
{
System.out.println("Computer Sys");
}

, } - Answers - VALID
Computer c1 = new Computer("sun"); c1.setMake("mars");

Computer c4 = new Computer("venus"); c4.info();

Computer.info();

When is the code associated with a finally block executed? - Answers - Always

Which of the following takes care of an object that is no longer referenced by any
variables? - Answers - The garbage collector

for (int i = 1; i <= 8; i += 3)
{
if (i == 4)
{
continue;
}
System.out.println(i);
} - Answers - 1
7

What is the output of the previous code fragment if we replace continue; with break; ? -
Answers - 1

How many objects exist in the following code fragment?

String a, b;
int c; - Answers - 0

The following code compiles. Do you see any problems with the code? Write NONE if
no problems or invalid operations are present. You can assume the method is in a class
that compiles.

public void check(double val, int x)
{
if (x > 0)
{
if (val == 4.5)
{
System.out.println("expected");
}
}
} - Answers - We should not be comparing floating point values.

How many default constructors do we have in the following class?

Document information

Uploaded on
July 10, 2026
Number of pages
6
Written in
2025/2026
Type
Exam (elaborations)
Contains
Questions & answers
$13.49

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