본문 바로가기
자바(Java)/자바 AP

[자바AP연습문제] 09.Inheritance

by 긱펀 2024. 3. 29.
반응형

[자바AP연습문제] 09.Inheritance

 

 

 

*09.Inheritance 단원의 복습내용은 아래 "더보기" 클릭

더보기

In this chapter you learned about inheritance. In an object-oriented program you write classes that define what objects of each class know (instance variables) and can do (methods). One class can inherit object instance variables and methods from another, which makes the amount of code that you have to write smaller and makes the classes easier to test and extend.

 

  • object - Objects do the action in an object-oriented program. An object can have things it knows (attributes) and things it can do (methods). An object is created by a class and keeps a reference to the class that created it.

  • class - A class defines what all objects of that class know (attributes) and can do (methods). You can also have data and behavior in the object that represents the class (class instance variables and methods). All objects of a class have access to class instance variables and class methods, but these can also be accessed using className.variable or className.method().

  • inheritance - One class can inherit object instance variables and methods from another. This makes it easy to reuse another class by extending it (inheriting from it). This is called specialization. You can also pull out common instance variables and/or methods from several related classes and put those in a common parent class. This is called generalization.

  • polymorphism - The runtime type of an object can be that type or any subclass of the declared type. All method calls are resolved starting with the class that created the object. If the method isn’t found in the class that created the object, then it will look in the parent class and keep looking up the inheritance tree until it finds the method. The method must exist, or the code would not have complied.

  • parent class - One class can inherit from another and the class that it is inheriting from is called the parent class. The parent class is specified in the class declaration using the extends keyword followed by the parent class name.

  • child class - The class that is doing the inheriting is called the child class. It inherits access to the object instance variables and methods in the parent class.

  • subclass - A child class is also called a subclass.

  • superclass - A parent class is also called a superclass.

  • declared type - The type that was used in the declaration. List aList = new ArrayList() has a declared type of List. This is used at compile time to check that the object has the methods that are being used in the code.

  • run-time type - The type of the class that created the object. List aList = new ArrayList() has a run-time type of ArrayList. This is used at run-time to find the method to execute.

  • overrides - A child class can have the same method signature (method name and parameter list) as a parent class. Since methods are resolved starting with the class that created the object, that method will be called instead of the inherited parent method, so the child method overrides the parent method.

  • overload - At least two methods with the same name but different parameter lists. The parameter lists can differ by the number of parameters and/or the types.

  • getter - A method that returns the value of an instance variable in an object.

  • setter - A method that sets the value of am instance variable in an object.

  • accessor - Another name for a getter method - one that returns the value of a instance variable.

  • mutator - Another name for a setter method - one that changes the value of a instance variable.

 


 

[문제1]

What best describes the purpose of a class’s constructor?

 

A. Initialize the fields in the object.
B. Determines the amount of space needed for an object and creates the object.
C. Names the new object.

 

[문제2]

Under which of these conditions is it appropriate to overload a method (ie: the class will contain two methods with the same name)?

 

A. The methods do different things.
B. The methods have different parameter names.
C. The methods have different post-conditions.
D. Two methods with the same name can never be included in the same class.
E. The methods have different numbers of parameters

 

[문제3]

A car dealership needs a program to store information about the cars for sale. For each car, they want to keep track of the following information: number of doors (2 or 4), whether the car has air conditioning, and its average number of miles per gallon. Which of the following is the best design?

 

A. Use four unrelated classes: Car, Doors, AirConditioning, and MilesPerGallon.
B. Use a class Car with three subclasses: Doors, AirConditioning, and MilesPerGallon.
C. Use a class Car, with fields: numDoors, hasAir, and milesPerGallon.
D. Use a class Car, with subclasses of Doors, AirConditioning, and MilesPerGallon.
E. Use classes: Doors, AirConditioning, and MilesPerGallon, each with a subclass Car.

 

 

[문제4]

A program is being written by a team of programmers. One programmer is implementing a class called Employee; another programmer is writing code that will use the Employee class. Which of the following aspects of the public methods and fields of the Employee class does not need to be known by both programmers?

 

A. How the methods are implemented.
B. The method names.
C. The method return types.
D. Constants
E. The number and types of the method parameters.

 

 

[문제5]

A bookstore is working on an on-line ordering system. For each type of published material (books, movies, audio tapes) they need to track the id, title, author(s), date published, and price. Which of the following would be the best design?

 

A. Create one class PublishedMaterial with the requested fields plus type.
B. Create classes Book, Movie, and AudioTape with the requested fields.
C. Create one class BookStore with the requested fields plus type.
D. Create classes for each.
E. Create the class PublishedMaterial with children classes of Book, Movie, and AudioTape.

 

 

[문제6]

Given the following class declarations, what is the output from Student s1 = new GradStudent(); followed by s1.getInfo();?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Student
{
    public String getFood()
    {
        return "Pizza";
    }
 
    public String getInfo()
    {
        return this.getFood();
    }
}
 
public class GradStudent extends Student
{
    public String getFood()
    {
        return "Taco";
    }
}
cs

 

A. Won't compile since GradStudent doesn't have a getInfo method
B. Taco
C. Pizza
D. Won't compile since you are creating a GradStudent, not a Student
E. Won't compile since you use this.getFood()

 

 

[문제7]

Given the following class declarations, and EnhancedItem enItemObj = new EnhancedItem(); in a client class, which of the following statements would compile?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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);
cs

 

A. I only
B. II only
C. I and II only
D. II and III only
E. I, II, and III

 

[문제8]

Given the following class declarations and initializations in a client program, which of the following is a correct call to method1?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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();
cs

 

A. t1.method1(t1,t1);
B. t2.method1(t2,t2);
C. t3.method1(t1,t1);
D. t2.method1(t3,t2);
E. t3.method1(t3,t3);

 

 

[문제9]

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()?

1
2
Animal[] a = { new Cat(), new Cow(), new Dog(), new Pig(), new Bird() }
 
cs

 

A. Meow Moo Woof Awk Awk
B. Awk Awk Awk Awk Awk
C. This will not compile
D. This will have runtime errors
E. Meow Moo Woof Oink Awk

 

 

[문제10]

Given the following class declarations and code, what is the result when the code is run?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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();
cs

 

A. The code compiles and runs with no errors, the output is 5 6 5 6
B. The code compiles and runs with no errors, the output is: 5 6 10 11
C. The code compiles and runs with no errors, the output is 10 11 10 11
D. The code won't compile.
E. You get a runtime error ClassCastException, when fastCar.addFuel() is executed.

 

[문제11]

Given the following class declarations and code, what is the result when the code is run?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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");
cs

 

A. I only
B. II only
C. I and III only
D. III only
E. I, II, and III

 

 

[문제12]

What is the output of the following code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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();
    }
}
cs

 

A. Lasagna Meow Screeech
B. Lasagna Screeech Meow
C. Meow Screeech Lasagna
D. Screeech Meow Lasagna

 

 

[문제13]

Assume that Base b = new Derived(); appears in a client program. What is the result of the call b.methodOne();?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Base
{
    public void methodOne()
    {
        System.out.print("A");
        methodTwo();
    }
 
    public void methodTwo()
    {
        System.out.print("B");
    }
}
 
public class Derived extends Base
{
    public void methodOne()
    {
        super.methodOne();
        System.out.print("C");
    }
 
    public void methodTwo()
    {
        super.methodTwo();
        System.out.print("D");
    }
}
cs

 

A. ABDC
B. AB
C. ABCD
D. ABC

 

[문제14]

If you have the following classes. Which of the following constructors would be valid for Point3D?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class Point2D
{
   public int x;
   public int y;
 
   public Point2D() {}
 
   public Point2D(int x,int y)
   {
      this.x = x;
      this.y = y;
   }
  // other methods
}
 
public class Point3D extends Point2D
{
   public int z;
 
   // other code
}
 
I.  public Point3D()
    {
 
    }
II. public Point3D(int x, int y, int z)
    {
       super(x,y);
       this.z = z;
    }
III. public Point3D(int x, int y)
     {
        this.x = x;
        this.y = y;
        this.z = 0;
     }
cs

 

A. II only
B. III only
C. I, II, and III
D. I and II only
E. I only

 

 


 

[문제 정답은 아래 "더보기" 클릭]

더보기

[문제1 정답]

A

(A constructor is often used to initialize the fields to their default values or in the case of a parameterized constructor, to the values passed in to the constructor.)

 

[문제2 정답]

E

(Overloading occurs when two methods perform the same essential operation, but take a different number and/or type of parameters.)

 

[문제3 정답]

C

(The number of doors, flag if it has air conditioning, and the average number of miles per gallon are attributes of a car. Each of these is a simple value so they can just be fields of a Car class.)

 

[문제4 정답]

A

(Only the programmer of the Employee class must know how the public methods work. The programmer that is using the Employee class can just use the public methods and not worry about how they are implemented.)

 

[문제5 정답]

E

(We will need to get objects based on their type so we should create classes for Book, Movie, and AudioTape. They have common fields so we should put these in a common superclass PublishedMaterial.)

 

[문제6 정답]

B

(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 getInfo in GradStudent and when it doesn't find it it will look in Student. In getInfo it calls this.getFood. Again, it will first look for this method in GradStudent. It will find the getFood method there and return "Taco".)

 

[문제7 정답]

D

(I is wrong because y is a private field and thus can not be directly accessed from code in a client class. II is correct because EnhancedItem has setY as a public method. III is correct because EnhancedItem inherits the public method setX from Item.)

 

[문제8 정답]

E

(Since method1 is a public method of class Test1 objects of any subclasses of Test1 can invoke the method. So, it can be invoked on t3 since it is an object of Test3 and this is a subclass of Test1. And, since method1 takes an object of class Test2 and Test3 as parameters. This actually means it can take an object of Test2 or any subclass of Test2 and an object of Test3 or any subclass of Test3. So it can take t3 which is an object of class Test3 as an object of Test2 since Test3 is a subclass of Test2.)

 

[문제9 정답]

A

(Objects keep a reference to the class that created them. So, even if you put them in an array of Animal objects, they know what they are and all methods are resolved starting with the class of the object. Bird and Pig do not override speak so the speak method in Animal will execute.)

 

[문제10 정답]

B

(The code compiles correctly, and because RaceCar extends the Car class, all the public methods of Car can be used by RaceCar objects. Also, a variable Car can refer to a Car object or an object of any subclass of Car. An object always knows the class that created it, so even though fastCar is declared to be a Car the constructor that is executed is the one for RaceCar.)

 

[문제11 정답]

C

( I is correct because variable b has been declared to be an object of the class Book so you can invoke any public methods that are defined in the Book class or in parents of Book. II is not correct because you can't invoke methods in the Dictionary class directly on b since b is declared to be of type Book not type Dictionary and Dictionary is a subclass of Book not a parent class of Book. III is correct because you can cast b to type Dictionary and then invoke public methods in Dictionary.)

 

[문제12 정답]

C

(The baseclass constructor runs first so Animal doesn't have one so then it goes to Cat's constructor and then Garfield's constructor)

 

[문제13 정답]

A

(Even though b is declared as type Base it is created as an object of the Derived class, so all methods to it will be resolved starting with the Derived class. So the methodOne() in Derived will be called. This method first calls super.methodOne so this will invoke the method in the superclass (which is Base). So next the methodOne in Base will execute. This prints the letter "A" and invokes this.methodTwo(). Since b is really a Derived object, we check there first to see if it has a methodTwo. It does, so execution continues in the Derived class methodTwo. This method invokes super.methodTwo. So this will invoke the method in the super class (Base) named methodTwo. This method prints the letter "B" and then returns. Next the execution returns from the call to the super.methodTwo and prints the letter "D". We return to the Base class methodOne and return from that to the Derived class methodOne and print the letter "C".)

 

[문제14 정답]

C

(I is true because Point2D does have a no-arg constructor. II is true because Point2D does have a constructor that takes x and y. III is true because Point2D does have a no-arg constructor which will be called before the first line of code is executed in this constructor. The fields x and y are public in Point2D and thus can be directly accessed by all classes.)

 

 


 

728x90
반응형

댓글