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?
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?