Get 2 days of unlimited access
Study Guides (350,000)
US (190,000)
UMD (10,000)
CMSC (700)
Midterm

CMSC 131 Study Guide - Midterm Guide: Duplicate Code, Static Variable, Instance VariableExam


Department
Computer Science
Course Code
CMSC 131
Professor
Fawzi Emad
Study Guide
Midterm

This preview shows pages 1-2. to view the full 8 pages of the document.
1
University of Maryland College Park
Dept of Computer Science
CMSC131 Fall 2017
Midterm II Key
Last Name (PRINT): _____________________________________________________
First Name (PRINT): _____________________________________________________
University Directory ID (e.g., umcpturtle)_____________________________________
I pledge on my honor that I have not given or received any unauthorized assistance on this examination.
Your signature: _____________________________________________________________
Grader Use Only
#1
Problem #1 (General Questions)
(20)
#2
Problem #2 (Memory Map)
(30)
#3
Problem #3 (Class Definition)
(95)
#4
Problem #4 (String Manipulation)
(55)
Total
Total
(200)

Only pages 1-2 are available for preview. Some parts have been intentionally blurred.

2
Problem #1 (General Questions)
1. (3 pts) If the garbage collector stops working which of the following can eventually be exhausted if we continue
creating objects. Circle all that apply.
a. Stack
b. Heap
c. Area where static variable resides.
d. None of the above.
Answer: b.
2. (3 pts) A method should be defined as static if (circle all that apply):
a. Makes no reference to instance variables.
b. Makes a reference to at least one instance variable.
c. Makes a reference to static method.
d. None of the above.
Answer: a.
3. (3 pts) Which of the following represents “no address”? Circle all that apply.
a. 0
b. null
c. false
d. None of the above.
Answer: b.
4. (3 pts) How many objects are present in the following code fragment?
StringBuffer b;
int x = 10;
Number of Objects: ____
Answer: 0
5. (3 pts) When is the code associated with a finally block executed?
a. Only when the exception occurs.
b. Always
c. Only if no exception occurs.
d. None of the above.
Answer: b.
You're Reading a Preview

Unlock to view full version

Only pages 1-2 are available for preview. Some parts have been intentionally blurred.

3
6. (3 pts) What is the actual task a constructor method performs?
a. Creating the object in the stack.
b. Initializing the object.
c. Moving the object from the stack to the heap.
d. None of the above.
Answer: b.
7. (2 pts) Which of the following applies to the “this” reference?
a. It is a reference to the current object.
b. Can be used by both static and non-static methods.
c. It is initialized for you.
d. None of the above.
Answer: a. and c.
Problem #2 (Memory Map)
One the next page draw a memory diagram showing both the stack and the heap at the moment this program reaches the
point identified by /* HERE */
public class Fruit {
private String name;
private int size;
public Fruit(String nameIn, int sizeIn) {
name = new String(nameIn);
size = sizeIn;
}
public void increaseSize(int x) {
size += x;
}
public String toString() {
return name + " " + size;
}
}
public class Driver {
public static void task(Fruit param, int delta) {
param.increaseSize(delta);
delta += 1000;
Fruit other = param;
other.increaseSize(20);
param = null;
/* HERE */
}
public static void main(String[] args) {
String p = "orange";
int k = 10, m = 5;
Fruit f1 = new Fruit(p, k);
task(f1, m);
}
}
You're Reading a Preview

Unlock to view full version