What is the result?

Given the code fragment:

What is the result?

Given the code fragment:

What is the result?

A.
Jesse 25
Walter 52

B.
Compilation fails only at line n1

C.
Compilation fails only at line n2

D.
Compilation fails at both line n1 and line n2



Leave a Reply 4

Your email address will not be published. Required fields are marked *


renko

renko

package q061;

public class Person {
String name;
int age = 25;

public Person(String name) {
this(); //line n1
setName(name);
}

public Person(String name, int age) {
Person(name); //line 2
setAge(age);
}
//setter and getter methods go here

public String show() {
return (name +” “+ age +” “); //+number
}

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());
}

}
Output:
Exception in thread “main” java.lang.Error: Unresolved compilation problems:
The constructor Person() is undefined
The method setName(String) is undefined for the type Person
The method Person(String) is undefined for the type Person
The method setAge(int) is undefined for the type Person

at q061.Person.(Person.java:8)
at q061.Person.main(Person.java:23)