CMSC 131 (Summer 2019) Week 05 Study Questions Flashcards

1
Q

What is meant by “the state” of an object?

A

The data that the object maintains.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is meant by “the behavior” of an object?

A

These are “actions” that the object can perform.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is the state of an object represented in the corresponding class definition?

A

Using instance variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is the behavior of an object represented in the corresponding class definition?

A

Using instance methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is “the heap”?

A

The heap is the region in memory where Objects are stored. When objects are no longer being used, that memory is “recycled” automatically so that it can be used by other Objects later

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a “reference variable”?

A

A reference variable stores the memory address of an object. We say that the variable “refers” to the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is actually stored in the variable “q” in the previous question?

A

q holds the memory location corresponding to the place where the String “cat” is in RAM

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given the code fragment above, is it correct to say that “q is a String object”? Is it correct to say that “q refers to a String object”? Is it correct to say that “x is a double”? Is it correct to say that “x refers to a double”?

A

You should NOT say “q is a String”. You SHOULD say “q refers to a String”. It is correct to say “x is a double”, but you should NOT say “x refers to a double”, since x is not a reference variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How many String objects are actually created (instantiated) in the code fragment above?

A

two

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

After the code fragment above has been executed, decide which of the following expressions are true and which are false:

x == y
q == r
r == s
q == s
q.equals(r)
r.equals(s)
q.equals(s)
A
x == y              TRUE
q == r              TRUE
r == s              FALSE
q == s              FALSE
q.equals(r)         TRUE
r.equals(s)         TRUE
q.equals(s)         TRUE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is aliasing?

A

Aliasing is having more than one reference variable that refer to the same object (for example, r and q in the previous question.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is meant by the term “garbage” in Java? Write a code fragment that creates garbage.

A

Any object that is on the heap, but no longer referred to by any variable is called “garbage”. Here is a short example:

String p = “junk”;
p = “other”;

(Now the String that says “junk” is garbage.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Consider the assignment statement:

x = y;

If the variables are of type “int”, does the integer get copied? If the variables are of type “String”, does the String get copied?

A

For primitive types (like int), the data is copied. For reference variables (like a String reference), the memory locations are copied, NOT the objects themselves.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a class called “Cat”. Invent a few “instance variables” that make sense for a Cat. Write some instance methods (behaviors) that Cats should be able to do. Make sure that you have practiced some method(s) that require parameters to be passed in. Make sure that you have practiced some methods that return a value.

A

Answers will vary…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write a driver for the Cat class that you created above. The driver should create a few Cats and then test out all of the methods that you have written. Try to test everything thoroughly!

A

Answers will vary…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly