Question: JAVA Suppose we execute the following code: Point p, q, r, s; p = new Point(3, 4); // create a point with x = 3,
JAVA
Suppose we execute the following code:
Point p, q, r, s;
p = new Point(3, 4); // create a point with x = 3, y = 4;
q = new Point(3, 4);
r = new Point(10, 20);
s = r;
q.setLocation(10, 20); // change x to 10 and y to 20
s.setLocation(3, 4);
How many of the following conditions are true? Check all that apply.
- p == q
- p == s
- q == r
- r == s
- p.getX() == 3
- q.getX() == 3
- r.getX() == 3
- s.getX() == 3
What are some things wrong with the following code?
150 String silly(Point p) {
151 String result;
152 Point last = null;
153 while (p.getX() > 0) {
154 last = p;
155 p = new Point(p.getX()-p.getY(),p.getY()+1);
156 if (p.getY() < 0) {
157 result = "Found negative";
158 p = null;
159 break;
160 }
161 }
162 return result + "last,x = " + last.getX() + ", p = " + p;
163 }
Select ALL that apply.
- Before line 153, p could be null leading to NPE
- Before line 154, last could be null, leading to NPE
- Before line 157, result could be undefined, an error
- Before line 158, p is assigned null, leading to NPE
- Before line 162, result could be undefined, an error
- Before line 162, last could be null, leading to NPE
- Before line 162, p could be null, leading to NPE
The following definition for equality on locations has problems. Which criticism is unfair or wrong?
@Override
boolean equals(Location other) {
if (x == other.x && y == other.y) {
return true;
} else {
return true;
}
}
- It should be declared public
- It should use an accessor to get x and y.
- It should take an object parameter.
- It will crash if passed null.
Which of the following actions are never performed by the default clone method?
- Throw a CloneNoSupportedException.
- Cope over all fields to the result.
- Check that the class implements Cloneable.
- Check that the method is overridden.
What is a good reason to define a static method?
- To define an operations that doesnt do anything with instances
- To define an operation that doesnt start with any instance
- To avoid modifying any variables
- For efficiency
- To declare that the method always returns the same thing.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
