9.13.2. Medium Multiple Choice QuestionsΒΆ
These problems are similar to those you will see on the AP CSA exam.
- Won't compile since
GradStudentdoesn't have agetInfomethod GradStudentwill inherit thegetInfomethod fromStudent. This would be true ifgetInfowas a private method.- Taco
- Objects know what class they are created as and all methods are resolved starting with that class at run time. If the method isn't found in that class the parent class is checked (and so on until it is found). So it will first look for
getInfoinGradStudentand when it doesn't find it it will look inStudent. IngetInfoit callsthis.getFood. Again, it will first look for this method inGradStudent. It will find thegetFoodmethod there and return "Taco". - Pizza
- This would be true if it was
Student s1 = new Student(); - Won't compile since you are creating a
GradStudent, not aStudent - An object of a subclass can be substituted for a parent class object. A
GradStudentis aStudent. - Won't compile since you use
this.getFood() - In object methods if you leave off the
this.when invoking an object method it will be added for you by the compiler. The keywordthisrefers to the current object which is implicitly passed to all object methods.
9-12-8: Given the following class declarations, what is the output from Student s1 = new GradStudent(); followed by s1.getInfo();?
public class Student
{
public String getFood()
{
return "Pizza";
}
public String getInfo()
{
return this.getFood();
}
}
public class GradStudent extends Student
{
public String getFood()
{
return "Taco";
}
}
- I only
- I is wrong because
yis a private field and thus can not be directly accessed from code in a client class. - II only
- I is wrong because
yis a private field and thus can not be directly accessed from code in a client class. II is correct becauseEnhancedItemhassetYas a public method. III is correct becauseEnhancedIteminherits the public methodsetXfromItem. - I and II only
- I is wrong because
yis a private field and thus can not be directly accessed from code in a client class. - II and III only
- I is wrong because
yis a private field and thus can not be directly accessed from code in a client class. II is correct becauseEnhancedItemhassetYas a public method. III is correct becauseEnhancedIteminherits the public methodsetXfromItem. - I, II, and III
- I is wrong because
yis a private field and thus can not be directly accessed from code in a client class.
9-12-9: Given the following class declarations, and EnhancedItem enItemObj = new EnhancedItem(); in a client class, which of the following statements would compile?
public class Item
{
private int x;
public void setX(int theX)
{
x = theX;
}
// ... other methods not shown
}
public class EnhancedItem extends Item
{
private int y;
public void setY(int theY)
{
y = theY;
}
// ... other methods not shown
}
I. enItemObj.y = 32;
II. enItemObj.setY(32);
III. enItemObj.setX(52);
- t1.method1(t1,t1);
- You can't pass an object of class
Test1since it is not either an object of typeTest2or an object of typeTest3. You can pass the specified type or an object that is a subclass of the specified type butTest1is not a subclass ofTest2orTest3. - t2.method1(t2,t2);
- You can't pass an object of class
Test2as a parameter of typeTest3.Test2is the parent class ofTest3not a subclass. You can pass an object of the specified type or an object of any subclass. - t3.method1(t1,t1);
- You can't pass an object of class
Test1since it is not either an object of typeTest2or an object of typeTest3. You can pass the specified type or an object that is a subclass of the specified type butTest1is not a subclass ofTest2orTest3. - t2.method1(t3,t2);
- You can't pass
t2as an object of typeTest3since it is an object of classTest2and classTest2is not either classTest3or a subclass of classTest3. ClassTest2is the parent of classTest3. - t3.method1(t3,t3);
- Since
method1is a public method of classTest1objects of any subclasses ofTest1can invoke the method. So, it can be invoked ont3since it is an object ofTest3and this is a subclass ofTest1. And, sincemethod1takes an object of classTest2andTest3as parameters. This actually means it can take an object ofTest2or any subclass ofTest2and an object ofTest3or any subclass ofTest3. So it can taket3which is an object of classTest3as an object ofTest2sinceTest3is a subclass ofTest2.
9-12-10: Given the following class declarations and initializations in a client program, which of the following is a correct call to method1?
public class Test1
{
public void method1(Test2 v1, Test3 v2)
{
// rest of method not shown
}
}
public class Test2 extends Test1
{
}
public class Test3 extends Test2
{
}
The following initializations appear in a different class.
Test1 t1 = new Test1();
Test2 t2 = new Test2();
Test3 t3 = new Test3();
- Meow Moo Woof Awk Awk
- Objects keep a reference to the class that created them. So, even if you put them in an array of
Animalobjects, they know what they are and all methods are resolved starting with the class of the object.BirdandPigdo not override speak so thespeakmethod inAnimalwill execute. - Awk Awk Awk Awk Awk
- Methods are always resolved starting with the class of the object, so
Cat,Cow, andDogwill all execute their overriddenspeakmethods, so the output will be: Meow Moo Woof Awk Awk. - This will not compile
- Because
Bird,Cow,Cat,Dog, andPigare subclasses ofAnimal, they can be stored in an array declared asAnimalwithout any compile time errors. - This will have runtime errors
- Because
Bird,Cow,Cat,Dog, andPigare subclasses ofAnimal, they can be stored in an array declared asAnimalwithout any runtime errors. - Meow Moo Woof Oink Awk
- The
Pigclass did not override thespeakmethod, so it will use the method fromAnimal, thus the output should be: Meow Moo Woof Awk Awk
9-12-11: If you have a parent class Animal that has a method speak() which returns: Awk. Cat has a speak method that returns: Meow. Bird does not have a speak method. Dog has a speak method that returns: Woof. Pig does not have a speak method. Cow has a speak method that returns: Moo. What is the output from looping through the array a created below and asking each element to speak()?
Animal[] a = { new Cat(), new Cow(), new Dog(), new Pig(), new Bird() }
- The code compiles and runs with no errors, the output is 5 6 5 6
RaceCar, while it inherits methods fromCarvia inheritance, has a separate and different constructor that sets the initial fuel amount to2 * g, thus in this case,fuelforfastCaris set to10initially.- The code compiles and runs with no errors, the output is: 5 6 10 11
- The code compiles correctly, and because
RaceCarextends theCarclass, all the public methods ofCarcan be used byRaceCarobjects. Also, a variableCarcan refer to aCarobject or an object of any subclass ofCar. An object always knows the class that created it, so even thoughfastCaris declared to be aCarthe constructor that is executed is the one forRaceCar. - The code compiles and runs with no errors, the output is 10 11 10 11
- The variable
caris aCarobject, so the constructor used is not the same as thefastCarobject which is aRaceCar. Thecarconstructor does not change the passed in parameter, so it is set to5initially. - The code won't compile.
RaceCarinherits from theCarclass so all the public methods inCarcan be accessed by any object of theRaceCarclass.- You get a runtime error
ClassCastException, whenfastCar.addFuel()is executed. RaceCarinherits from theCarclass so all the public methods inCarcan be accessed by any object of theRaceCarclass.
9-12-12: Given the following class declarations and code, what is the result when the code is run?
public class Car
{
private int fuel;
public Car() { fuel = 0; }
public Car(int g) { fuel = g; }
public void addFuel() { fuel++; }
public void display() { System.out.print(fuel + " "); }
}
public class RaceCar extends Car
{
public RaceCar(int g) { super(2*g); }
}
What is the result when the following code is compiled and run?
Car car = new Car(5);
Car fastCar = new RaceCar(5);
car.display();
car.addFuel();
car.display();
fastCar.display();
fastCar.addFuel();
fastCar.display();
- I only
- Both I and III are correct. I is correct because variable
bhas been declared to be an object of the classBookso you can invoke any public methods that are defined in theBookclass or in parents ofBook. II is not correct because you can't invoke methods in theDictionaryclass directly onbsincebis declared to be of typeBooknot typeDictionaryandDictionaryis a subclass ofBooknot a parent class ofBook. III is correct because you can castbto typeDictionaryand then invoke public methods inDictionary. - II only
- You can't invoke methods in the
Dictionaryclass directly onbsincebis declared to be of typeBooknot typeDictionaryandDictionaryis a subclass ofBooknot a parent class ofBook. The compiler checks that the method exists on the declared class type, not the run-time type of the object. - I and III only
- I is correct because variable
bhas been declared to be an object of the classBookso you can invoke any public methods that are defined in theBookclass or in parents ofBook. II is not correct because you can't invoke methods in theDictionaryclass directly onbsincebis declared to be of typeBooknot typeDictionaryandDictionaryis a subclass ofBooknot a parent class ofBook. III is correct because you can castbto typeDictionaryand then invoke public methods inDictionary. - III only
- I is also correct.
- I, II, and III
- You can't invoke methods in the
Dictionaryclass directly onbsincebis declared to be of typeBooknot typeDictionaryandDictionaryis a subclass ofBooknot a parent class ofBook. The compiler checks that the method exists on the declared class, not the run-time class.
9-12-13: Given the following class declarations and code, what is the result when the code is run?
public class Book
{
public String getISBN()
{
// implementation not shown
}
// constructors, fields, and other methods not shown
}
public class Dictionary extends Book
{
public String getDefinition(String word)
{
// implementation not shown
}
// constructors, fields, and methods not shown
}
Assume that the following declaration appears in a client class.
Book b = new Dictionary();
Which of the following statements would compile without error?
I. b.getISBN();
II. b.getDefinition("wonderful");
III. ((Dictionary) b).getDefinition("wonderful");
- Lasagna Meow Screeech
- The baseclass constructor runs first so Animal doesn't have one so then it goes to Cat's constructor and then Garfield's constructor
- Meow Screeech Lasagna
- The baseclass constructor runs first so Animal doesn't have one so then it goes to Cat's constructor and then Garfield's constructor
- Screeech Meow Lasagna
- The baseclass constructor runs first so Animal doesn't have one so then it goes to Cat's constructor and then Garfield's constructor
- Lasagna Screeech Meow
- The baseclass constructor runs first so Animal doesn't have one so then it goes to Cat's constructor and then Garfield's constructor
9-12-14: What is the output of the following code?
class Animal
{
void someSound()
{
System.out.print("Screeech ");
}
}
class Cat extends Animal
{
public Cat()
{
System.out.print("Meow ");
super.someSound();
}
}
class Garfield extends Cat
{
public Garfield()
{
System.out.print("Lasagna ");
}
}
public class MainClass
{
public static void main(String[] args)
{
Garfield garfield = new Garfield();
}
}
