Quiz 3 Worksheet for CMSC 131 Summer 2005 - Java Programming, Quizzes of Computer Science

Information about Quiz 3 for the CMSC 131 Summer 2005 course, including instructions, quiz material, and problem sets. The quiz will be based on the exercises provided and will be a written, closed-book exam. topics such as System.out.println vs System.out.print, heap, object vs class, garbage collection, and String manipulation.

Typology: Quizzes

2019/2020

Uploaded on 11/25/2020

koofers-user-6bs
koofers-user-6bs 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 131 Summer 2005 Quiz 3 Worksheet
The third Quiz of the course will be on Friday June 24. The following list provides more
information about the quiz:
You will have 15 minutes to complete the quiz.
It will be a written quiz (not using any computer).
It will be closed-book, closed-notes, and no calculator is allowed.
Answers must be neat and legible. We recommend that you use pencil and eraser.
The quiz will be based on the exercises you will find below.
The following exercises cover the Quiz#3 material. Solutions to these exercises will not
be provided, but you are welcome to discuss your solutions with TAs and instructors
during office hours.
Problem 1 (General Questions)
a. What is the difference between System.out.println and System.out.print ?
b. What is the heap?
c. What is the difference between an object and a class?
d. Is the heap an infinite memory area? Explain.
e. What is garbage collection?
f. Are the following assignments valid?
int age = 17;
double temp = 3.4;
String m = “John” + age + “/” + temp;
g. What is a reference variable?
h. What is the difference between a reference and a primitive variable?
i. How many reference variables can be associated with an object?
j. In the following code fragment, how many objects do we have at every step?
String r;
String s; // Step 1
r = s = “Happy”; // Step2
r = new String(s); // Step 3
r = null; // Step 4
r = new String(“John”); // Step 5
k. When do we use the String equals method?
l. What would happen when you execute the following code fragment?
String k = null;
System.out.println(k);
System.out.println(k.length());
pf3
pf4
pf5

Partial preview of the text

Download Quiz 3 Worksheet for CMSC 131 Summer 2005 - Java Programming and more Quizzes Computer Science in PDF only on Docsity!

CMSC 131 Summer 2005 Quiz 3 Worksheet

The third Quiz of the course will be on Friday June 24. The following list provides more

information about the quiz:

• You will have 15 minutes to complete the quiz.

• It will be a written quiz (not using any computer).

• It will be closed-book, closed-notes, and no calculator is allowed.

• Answers must be neat and legible. We recommend that you use pencil and eraser.

• The quiz will be based on the exercises you will find below.

The following exercises cover the Quiz#3 material. Solutions to these exercises will not

be provided, but you are welcome to discuss your solutions with TAs and instructors

during office hours.

Problem 1 (General Questions)

a. What is the difference between System.out.println and System.out.print?

b. What is the heap?

c. What is the difference between an object and a class?

d. Is the heap an infinite memory area? Explain.

e. What is garbage collection?

f. Are the following assignments valid?

int age = 17; double temp = 3.4; String m = “John” + age + “/” + temp;

g. What is a reference variable?

h. What is the difference between a reference and a primitive variable?

i. How many reference variables can be associated with an object?

j. In the following code fragment, how many objects do we have at every step?

String r; String s; // Step 1 r = s = “Happy”; // Step r = new String(s); // Step 3 r = null; // Step 4 r = new String(“John”); // Step 5

k. When do we use the String equals method?

l. What would happen when you execute the following code fragment?

String k = null; System.out.println(k); System.out.println(k.length());

Problem 2

A class named Utilities has a method named minimum that takes three integers as

parameters and returns the minimum value. Implement the method and the class, and

provide a test driver that illustrates how to use the method to compute the minimum value

of three values provided by the user. Read values from the user by using JOptionPane.

Note: You may not use JOptionPane in the minimum method.

Problem 3

A memory map is a representation of the values of variables, and the objects we have in a

program. For example, given the following code segment:

public static void main(String[] args) { int x; x = 2; String p; p = new String(“Test”); String t = null; // STOP HERE }

the memory map we have until the point right before the method finishes (indicated by

the comment // STOP HERE) is:

Stack Heap

As you can see we represent references (addresses) with arrows, objects with rectangles,

and the null value with a line that has a perpendicular line at its endpoint. The stack is

where the method’s local variables reside; the heap is any area to the right of the stack.

Using the above memory map approach draw a memory map for each of the following:

a. Problem 1.j above after finishing Step 5. b. For the following method until we reached the point marked // STOP HERE public class Test { public static void main(String[] args) {

x

p

2

Test

t

i. MethodsExample e = new MethodsExample(); e.printAll("John", 20); ii. MethodsExample e = new MethodsExample(); e.validName("John"); iii. MethodsExample e; e.printName("John"); iv. new MethodsExample().printAll("Mary", 16); v. Revisit i. through iv. but assume all methods of the class MethodsExample are public. vi. Revisit i. through iv. but assume all methods of the class MethodsExample are private. Problem 5 (This was last semester’s quiz) Draw a memory diagram for the following main method, up to point where the comment “STOP HERE” appears. Specify any objects that have been created and the values of the variables a, b, c, d, e, and temp. Remember that we represent references (addresses) with arrows, objects with rectangles, and the null value with a line ended with a perpendicular line. public static void main(String[] args) { String a = "Pop"; String b = "Pop"; double temp = 10; String c = new String("Rock"); String d = new String(c); String e = c; c = d; temp = 20; d = e; // STOP HERE }

Stack Heap