Exam #2 (Fall 2018) Flashcards
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., b.
How many objects exist in the following code fragment?
String m;
0
How many objects exist in the following code fragment?
String s = “Taco”;
1
Which of the following actually creates an object?
a. new
b. constructor
c. private
d. None of the above.
a. new
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. It has the same value ‘a’ has
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); } }
null
0.0
false
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); } }
Yes
Where in memory objects reside in Java? (Circle all that apply)
a. Heap
b. Stack
c. main method
d. None of the above.
a. Heap
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. The program will crash.
Name one immutable Java class mentioned in lecture:
One Possible Answer: String