Exam #2 (Fall 2018) Flashcards

1
Q

A method should be defined as static if (circle all that apply):

a. It does not require access to instance variables.
b. We would like to call the method by using the class name (e.g., JOptionPane.showInputDialog).
c. The method makes a reference to special value this.
d. It calls a static method.
e. None of the above.

A

a., b.

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

How many objects exist in the following code fragment?

String m;

A

0

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

How many objects exist in the following code fragment?

String s = “Taco”;

A

1

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

Which of the following actually creates an object?

a. new
b. constructor
c. private
d. None of the above.

A

a. new

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

When we call a.doSomething(b) which value does the special value this (present inside of the doSomething method) have?

a. It has the same value ‘a’ has
b. It has the same value ‘b’ has
c. It has the value null
d. None of the above.

A

a. It has the same value ‘a’ has

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

What is the output of the following program?

public class Values {
private String distance;
private double cost;
private boolean completed;
public static void main(String[] args) {
Values values = new Values();
System.out.println(values.distance);
System.out.println(values.cost);
System.out.println(values.completed);
}
}
A

null
0.0
false

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

In the Values class, is there a default constructor associated with the class?

public class Values {
private String distance;
private double cost;
private boolean completed;
public static void main(String[] args) {
Values values = new Values();
System.out.println(values.distance);
System.out.println(values.cost);
System.out.println(values.completed);
}
}
A

Yes

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

Where in memory objects reside in Java? (Circle all that apply)

a. Heap
b. Stack
c. main method
d. None of the above.

A

a. Heap

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

What happens if new is called and there is no space available in the heap?

a. The program will crash.
b. The program will continue executing using the stack as a heap.
c. The main method will be used as a the heap area.
d. None of the above.

A

a. The program will crash.

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

Name one immutable Java class mentioned in lecture:

A

One Possible Answer: String

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