Q.- Given the code fragment:-
public class Person {
String name;
int age = 25;
Person (String name) { // line n1
setName (name);
}
public Person (String name, int age) {
Person (name); // line n2
setAge (age);
}
// setter and getter methods go here
public String show( ) {
return name + " " + age;
}
public static void main(String[ ] args) {
Person p1 = new Person ("Jesse");
Person p2 = new Person ("Walter", 52);
System.out.println (p1.show());
System.out.println (p2.show());
}
}
What is the result?
A) Jesse 25
Walter 52
B) Compilation fails at both line n1 and line n2.
C) Compilation fails only at line n1.
D) Compilation fails only at line n2.
⧪ please give your answer in a comment section.
public class Person {
String name;
int age = 25;
Person (String name) { // line n1
setName (name);
}
public Person (String name, int age) {
Person (name); // line n2
setAge (age);
}
// setter and getter methods go here
public String show( ) {
return name + " " + age;
}
public static void main(String[ ] args) {
Person p1 = new Person ("Jesse");
Person p2 = new Person ("Walter", 52);
System.out.println (p1.show());
System.out.println (p2.show());
}
}
What is the result?
A) Jesse 25
Walter 52
B) Compilation fails at both line n1 and line n2.
C) Compilation fails only at line n1.
D) Compilation fails only at line n2.
⧪ please give your answer in a comment section.
6 Comments
Option C
ReplyDeleteC
ReplyDeleteC
ReplyDeletePerson (name); // line n2
ReplyDeleteanswer D
Compilation fails only at line n2.
if you make Person (name); as this(name) then there is no compilation errors
D
ReplyDeleteD/ must use this() to invoke constructor
ReplyDelete