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, Multiple Choice, Ternary OperationExam


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

This preview shows pages 1-3. to view the full 10 pages of the document.
1
University of Maryland College Park
Dept of Computer Science
CMSC131 Fall 2015
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: _____________________________________________________________
Instructions
This exam is a closed-book and closed-notes exam.
Total point value is 200 points.
The exam is a 50 minutes exam.
Please use a pencil to complete the exam.
WRITE NEATLY.
There are three problems in the exam.
Multiple choice questions can have more than one valid/correct answers.
You do not need any import statements for coding questions.
You must write code that is efficient and that avoids code duplication.
Grader Use Only
#1
Problem #1 (General Questions)
(80)
#2
Problem #2 (Memory Map)
(40)
#3
Problem #3 (Class Definition)
(80)
Total
Total
(200)

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

2
Problem #1 (General Questions)
1. (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.
2. (3 pts) Which of the following takes care of an object that is no longer referenced by any variables?
a. The stack
b. The heap
c. The garbage collector
d. The constructor
3. (3 pts) What is the output of the following code fragment?
for (int i = 1; i <= 8; i += 3) {
if (i == 4) {
continue;
}
System.out.println(i);
}
Answer:
1
7
4. (3 pts) What is the output of the previous code fragment if we replace continue; with break; ?
Answer:
1
5. (3 pts) How many objects exist in the following code fragment?
String a, b;
int c;
Answer: 0

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

3
6. (3 pts) 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");
}
}
}
Answer: We should not be comparing floating point values.
7. (3 pts) How many default constructors do we have in the following class?
public class Apple {
private String flavor;
}
a. 0
b. 1
c. The above class does not compile.
d. None of the above
8. (6 pts) What is the output of the following program? If an exception is thrown indicate why.
public class Values {
public int a;
public boolean b;
public String c;
public Values() {
String d = c;
System.out.println(a + ", " + b + ", " + d);
}
public static void main(String[] args) {
Values v = new Values();
}
}
Answer: 0, false, null
You're Reading a Preview

Unlock to view full version