Answer is B.
To test:
class Person {
String name;
int age = 25;
Person (String name) { // line 1
setName(name);
}
public Person (String name, int age) {
Person(name); // line 2
setAge(age);
}
// setter and getter methods go here
public void setName (String name) { this.name = name; }
public void setAge (int age) { this.age = age; }
public String show() {
return name + " " + age;
}
}
public class Teste {
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());
}
}
Correct Answer is B, the line 1 compiles normaly and execute because even though the constructor is not public, the main method is in the same class and can be seen to be instantiated. On line 2, the constructor is wrong because a method is being used inside it.
Question is written wrong here, compilation only fails at n2
upvoted 2 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.
fvelazqueznava
3 months, 1 week agoakbiyik
1 year, 1 month agoeilla
1 year, 1 month agocarloswork
1 year, 2 months agoiSnover
1 year, 3 months agoshivkumarx
1 year, 4 months ago