CMSC 131 Quiz 2 practice, Quizzes of Computer Science

Practice quiz 2 questions for CMSC 131

Typology: Quizzes

2021/2022

Uploaded on 02/12/2023

Bethel567
Bethel567 🇺🇸

5 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC133, SPRING2020, QUIZ #2 Rubric (DURATION: 10 MINUTES)
FIRSTNAME, LASTNAME (PRINT IN UPPERCASE):
STUDENT ID (e.g. 123456789):
TOTAL PTS: TODO
Draw a memory map for the following program at the point in the program execution indicated by the comment /*HERE*/.
public class Cabinet {
private StringBuffer items;
private int totalItems;
public Cabinet(String label) {
items = new StringBuffer(label);
totalItems = 0;
}
public Cabinet storeItem(String item) {
items.append(item);
totalItems++;
return this;
}
public String toString() {
return "Total: " + totalItems + ", Items: " + items;
}
}
public class Driver {
public static void task(Cabinet cab, String item,
int cnt) {
String f = item + cnt;
cab.storeItem(f);
cnt = 0;
/* HERE */
}
public static void main(String[] args) {
Cabinet cabinet = new Cabinet("office");
String pen = "bluepen";
int many = 2;
task(cabinet, pen, many);
System.out.println(cabinet);
}
}
Answer:
args
pen
cabinet
many
cab
cnt
item
f
2
0
bluepen2
items
totalItems
1
officebluepen2
0
length
bluepen
M
a
i
n
f
r
a
m
e
t
a
s
k
f
r
a
m
e

Partial preview of the text

Download CMSC 131 Quiz 2 practice and more Quizzes Computer Science in PDF only on Docsity!

CMSC133, SPRING2020, QUIZ #2 Rubric (DURATION: 10 MINUTES)

FIRSTNAME, LASTNAME (PRINT IN UPPERCASE):

STUDENT ID (e.g. 123456789):

TOTAL PTS: TODO

Draw a memory map for the following program at the point in the program execution indicated by the comment /HERE/.

public class Cabinet { private StringBuffer items; private int totalItems; public Cabinet(String label) { items = new StringBuffer(label); totalItems = 0; } public Cabinet storeItem(String item) { items.append(item); totalItems++; return this; } public String toString() { return "Total: " + totalItems + ", Items: " + items; } } public class Driver { public static void task(Cabinet cab, String item, int cnt) { String f = item + cnt; cab.storeItem(f); cnt = 0;

/* HERE */

public static void main(String[] args) { Cabinet cabinet = new Cabinet("office"); String pen = "bluepen"; int many = 2; task(cabinet, pen, many); System.out.println(cabinet); } }

Answer:

args

pen

cabinet

many

cab

cnt

item

f

bluepen

items

totalItems 1

officebluepen

length 0

M^ bluepen

a i n f r a m e t a s k f r a m e