Object-Oriented Programming l - Quiz 4 | CMSC 131, Quizzes of Computer Science

Material Type: Quiz; Class: OBJECT-ORIENTED PROG I; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Quizzes

Pre 2010

Uploaded on 02/13/2009

koofers-user-i0c
koofers-user-i0c 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Quiz 4 Solution
Six local memory locations are created on the stack and four (string) objects are created in the
heap. The variables a and b point to different strings. The variable c points to its own copy of a
string object. The variables d and e end up pointing to the same object. The local variable temp
is created for the value 20.
Note: Some Java optimizers would allow a and b to point to the same memory location because
the string is known at compile time. That is, the assignment operator (=) was applied directly to a
string without the use of "new". (Such an optimization is safe because strings are immutable,
which means they cannot be inadvertently changed.) However, no other objects can be created
without the use of “new.” Thus, in the general case (for all other types of objects), a and b would
point to different objects, as we have shown here. (In this class, you need not worry about
optimization, so the solution below is generally applicable.)
Stack Heap
20
Pop
Rock
Pop
Rock
temp
a
e
d
c
b

Partial preview of the text

Download Object-Oriented Programming l - Quiz 4 | CMSC 131 and more Quizzes Computer Science in PDF only on Docsity!

Quiz 4 Solution

Six local memory locations are created on the stack and four (string) objects are created in the heap. The variables a and b point to different strings. The variable c points to its own copy of a string object. The variables d and e end up pointing to the same object. The local variable temp is created for the value 20.

Note: Some Java optimizers would allow a and b to point to the same memory location because the string is known at compile time. That is, the assignment operator (=) was applied directly to a string without the use of "new". (Such an optimization is safe because strings are immutable, which means they cannot be inadvertently changed.) However, no other objects can be created without the use of “new.” Thus, in the general case (for all other types of objects), a and b would point to different objects, as we have shown here. (In this class, you need not worry about optimization, so the solution below is generally applicable.)

Stack Heap

Pop

Rock

Pop

Rock

temp

a

e

d

c

b