Answer is C.
To test:
class Vehicle {
int x;
Vehicle() {
this(10); // line n1
}
Vehicle(int x) {
this.x = x;
}
}
class Car extends Vehicle {
int y;
Car() {
super(10); // line n2
}
Car(int y) {
super(y);
this.y = y;
}
public String toString() {
return super.x + ":" + this.y;
}
}
public class Test {
public static void main(String[] args) {
Vehicle y = new Car(20);
System.out.println(y);
}
}
The answer is the letter C, the code compiles normally and prints 20:20, remember that if the constructor of Car has a super for that of Vehicle, then the same value remains for the 2 to change.
Result of below code is: 20:20
public class A2 {
int x;
A2(){
this(10);
}
A2( int x){
this.x = x;
}
public static void main(String[] args) {
A2 y = new Car(20);
System.out.println(y);
}
}
class Car extends A2{
int y;
Car(){
super(10);
}
Car(int y){
super(y);
this.y = y;
}
public String toString() {
return super.x + ":" + this.y;
}
}
upvoted 4 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
MPignaProTech
2 months, 3 weeks agocarloswork
2 years, 2 months agoiSnover
2 years, 3 months agoshivkumarx
2 years, 4 months ago