Given the code below, what should one type to invoke the myMethod() method at the "?????????" in the main method given? (You may assume the Number class is visible to the main method.)
public class Number { int num = 1; Number(int n) { num = n; } public void myMethod() { System.out.println("Howdy!"); } }
public class Main{ public static void main(String[] args) { Number n = new Number(3); ????????? } }
What keyword references the implicit parameter?
Explain the error in the code below:
public class Square { private double sideLength_; public Square(double sideLength) { sideLength_ = sideLength; } public double getArea() { return sideLength_*sideLength_; } }
public class Main { public static void main(String[] args) { Square s = new Square(3.0); System.out.println(Square.getArea()); } }
What single line of code should replace the comment below:
public class Circle { private double radius; public Circle() { radius = 1.0; } public boolean hasRadius(double radius) { //return true if the circle's radius equals //the one given, and false otherwise } }
First, consider the following two classes:
public class Paint { int red; int green; int blue; public Paint() { } public Paint(int red, int green, int blue) { this.red = red; this.green = green; this.blue = blue; } public void setGreen(int green) { this.green = green; } public String toString() { return (this.red + "," + this.green + "," + this.blue); } }
public class ColorManipulator { public static void increaseGreen(int r, int g, int b) { g = 255; } public static void main(String[] args) { Paint paintA = new Paint(255,0,0); Paint paintB = new Paint(); System.out.println(paintB.toString()); paintB = paintA; paintA.setGreen(255); System.out.println(paintB.toString()); int red = 255; int blue = 0; int green = 0; increaseGreen(red, blue, green); System.out.println(red + "," + blue + "," + green); int[] colA = {255,0,0}; int[] colB = colA; colB[1] = 255; System.out.println(colA[0]+","+colA[1]+","+colA[2]); } }
Constructors should have a _____________ visibility modifier if they are to be used in other, non-related classes, while instance fields should generally have a _____________ visibility modifier to allow for maximum flexibility in changing the the internal structure of the class in the future, without impacting other classes. (Fill in the blank.)
What type of "special method" has no return type declared (not even void
)?
What does the following code print?
public class CharWrapper { char c = 'a'; CharWrapper(char c) { this.c = c; } public void setChar(char c) { this.c = c; } public char getChar() { return this.c; } }
public class WrapperTester { public static void main(String[] args) { CharWrapper c1 = new CharWrapper('b'); CharWrapper c2 = new CharWrapper('c'); c1 = c2; c2.setChar('d'); System.out.println(c1.getChar()); } }
What is printed as a result of line A in the ShapeTester class?
What is printed as a result of line B in the ShapeTester class?
Describe the error that would result from uncommenting line C in the ShapeTester class.
What other words could legally be used in place of "Shape" in line D in the ShapeTester class?
What is printed as a result of lines E?
What is printed as a result of line F?
One of the following actions can be taken without altering the output of the program when it is run. The other three actions produce errors. Identify which three produce errors and explain them. For the remaining action, simply write "no error produced".
adding the word "final" after the word "public" in the line: "public class Polygon extends Shape {" of the Polygon class
removing the occurrence of "implements Colorable" from the Shape class:
removing the first occurrence of the word "abstract" in the Shape class.
removing the last two occurrences of the word "abstract" in the Shape class.
The printWhoAmI(int ConfidenceLevel)
method of the Square class ________________ the printWhoAmI()
method of the Polygon class (fill in the blank with an appropriate word that starts with "o".).
What is printed to the console as a result of line G of the ShapeTester class?
Complete the following code to be added to the Circle class that will return true only if o is of type Circle and has the same radius. (Note: it is possible to do this in a single line of code.)
public boolean equals (Object o) { return }
The equals() method of the Circle class written above ____________ the equals() method of the Object class (fill in the blank with an appropriate word that starts with "o").
An instance variable or method that has the default level of visibility (i.e., has no visibility modifier specified) is accessible from which of the following? (Circle all that apply.)
Explain the error in the following code:
public class SomeClass { private int num_; public SomeClass() { num_ = 10; } public int someMethod() { return num_; } }
public class SomeOtherClass extends SomeClass { public SomeOtherClass() { super(); } private int someMethod() { return super.someMethod() + 1; } }
Circle all of the following statements that are true. (There may or may not be more than one.)
Circle all of the following statements that are true. (There may or may not be more than one.)
Circle the methods below that are required of any class that implements a MouseListener interface.
n.myMethod();
this
the getArea()
method is not static, but was called in a static way
return this.radius == radius;
0,0,0 255,255,0 255,0,0 255,255,0
public
private
a constructor does not declare a return type
(as opposed to a method that declares a return
type of void
to indicate it won't return
anything)
d
shape created at (5.0, 3.0) polygon created at given position square created at given position
shape created polygon created
square polygon shape at 5.0, 3.0
circle shape at 1.0, 2.0
I am a polygon!
public boolean equals (Object o) {
return (o instanceof Circle) && this.radius_ == ((Circle) o).getRadius();
}
someMethod()
of the SomeOtherClass
class is less than that of the method of the same name in the super class SomeClass
.