Create an account to track your scores
and create your own practice tests:
Test: AP Computer Science A
For this question, consider the following code:
private static class Philosopher {
private String name;
private String favoriteSubject;
public Philosopher(String n, String f) {
name = n;
favoriteSubject = f;
}
public String getName() {
return name;
}
public String getFavoriteSubject() {
return favoriteSubject;
}
public void speak() {
System.out.println("Hello, World! My name is "+name + ". My favorite subject is "+favoriteSubject);
}
}
private static class Nominalist extends Philosopher {
boolean franciscan;
public Nominalist(String n,boolean frank) {
super(n,"logic");
franciscan = frank;
}
public void speak() {
super.speak();
if(franciscan) {
System.out.println("I am a Franciscan");
} else {
System.out.println("I am not a Franciscan");
}
}
public String whoMightHaveTaughtMe() {
if(franciscan) {
return "Perhaps William of Ockham?....";
} else {
return "Perhaps it was Durandus of St. Pourçain — scandalous, a Dominican nominalist!";
}
}
}
public static void main(String[] args) {
Philosopher p = new Nominalist("Nicanor",false);
System.out.println(p.whoMightHaveTaughtMe());
}
1. | What is the output for this main method? |
Perhaps William of Ockham?....
There is no output.
Perhaps it was Durandus of St. Pourçain — scandalous, a Dominican nominalist!
This will not compile. You cannot call a subclass function within the scope of the main method.
This will not compile. That method is defined on the subclass, but the variable is defined as the superclass Philosopher, meaning that the method is not visible.
Certified Tutor
Certified Tutor
