What is the rule for a super reference in a constructor? It must be in the paren
ID: 3797868 • Letter: W
Question
What is the rule for a super reference in a constructor?
It must be in the parent class' constructor.
It must be the last line of the constructor in the child class.
It must be the first line of the constructor in the child class.
Only one child class can use it.
You cannot use super in a constructor.
Consider the following class definition.
public class WhatsIt {
private int length;
private int width;
public int getArea () {
// implementation not shown
}
private int getPerimeter () {
// implementation not shown
}
}
A child class Thingy that extends WhatsIt would have access to:
getArea()
getPerimeter()
width, length, getPerimeter()
width, length, getArea()
all of the above
Questions 18 - 20 pertain to the following class, Point:
public class Point {
private double x;
private double y;
public Point() {
this (0, 0);
}
public Point(double a, double b) {
/* missing code */
}
// ... other methods not shown
}
Which of the following correctly implements the equals method?
public boolean equals(Point p) {
return (x == Point.x && y == Point.y);
}
public void equals(Point p) {
System.out.println(x == p.x && y == p.y);
}
public boolean equals(Point p) {
System.out.println(x == p.x && y == p.y);
}
public boolean equals(Point p) {
return (x == p.x && y == p.y );
}
public void equals(Point p) {
return (x == p.x && y == p.y );
}
The default constructor sets x and y to (0, 0) by calling the second constructor. What could be used to replace /* missing code */ so that this works as intended?
a = 0;
b = 0;
this(0, 0);
this (x, y);
a = x;
b = y;
x = a;
y = b;
Which of the following correctly implements a mutator method for Point?
public double getX() {
return x;
}
public double getX() {
return a;
}
public void setCoordinates (double a, double b) {
x = a;
y = b;
}
public void setCoordinates (double a, double b) {
Point p = new Point(a,b);
}
None of the above
Explanation / Answer
What is the rule for a super reference in a constructor?
Always a super reference must be the first line of the constructor.
So, the answer is:
It must be the first line of the constructor in the child class.
Consider the following class definition.
public class WhatsIt {
private int length;
private int width;
public int getArea () {
// implementation not shown
}
private int getPerimeter () {
// implementation not shown
}
}
A child class Thingy that extends WhatsIt would have access to:
Only the public members of the parent class can be accessed in the derived class.
So, the only public member in WhatsIt is: getArea.
So, the answer is: getArea()
Questions 18 - 20 pertain to the following class, Point:
public class Point {
private double x;
private double y;
public Point() {
this (0, 0);
}
public Point(double a, double b) {
/* missing code */
}
// ... other methods not shown
}
Which of the following correctly implements the equals method?
public boolean equals(Point p) {
//This could not be the case. As you're accessing x and y as if they are static variables which is not the case.
return (x == Point.x && y == Point.y);
}
public void equals(Point p) {
//This is what the answer is.
System.out.println(x == p.x && y == p.y);
}
public boolean equals(Point p) {
//This is not the case. Ofcourse , it is printing the data, but the return type is boolean, and this function is not returning anything.
System.out.println(x == p.x && y == p.y);
}
public boolean equals(Point p) {
//This is a perfect option, as this can communicate with other methods and classes.
//Observe that this method is returning a value, and can be used as required by other objects.
return (x == p.x && y == p.y );
}
public void equals(Point p) {
//This is trying to return something, but the return type is specified as void, which leads to error.
return (x == p.x && y == p.y );
}
The default constructor sets x and y to (0, 0) by calling the second constructor.
What could be used to replace /* missing code */ so that this works as intended?
The private variables x, and y should be assigned the passed values a, and b respectively.
So, the answer is:
x = a;
y = b;
Which of the following correctly implements a mutator method for Point?
public double getX() { //Mutator is a method used to set the values of the class variables.
//This is an accessor, so is not the answer choice.
return x;
}
public double getX() { //The same as the previous case, and also, its trying to access a variable a, which is not in the class.
return a;
}
public void setCoordinates (double a, double b) {
//This works perfectly fine. It will set the values of a, and b to x, and y respectively.
x = a;
y = b;
}
public void setCoordinates (double a, double b) {
//This is not the mutator, but is creating another object p of type Point.
Point p = new Point(a,b);
}
None of the above
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.