Sie sind auf Seite 1von 6

AP Computer Science A

Lab 8 Instructor: J.L

All problems are supposed to and can be solved by hand. After enough thought has been given
to each problem, you can discuss with your classmate and verify your result by running the code.
Then you correct your original answers if necessary. Do NOT erase your original answer if it is
wrong.

1. Which of the following statements about a class that contains an abstract method is (are)
true?
I. You cant have any constructors in this class.
II. This class must be declared as abstract.
III. You cant declare any fields in this class.

(A) I and II only


(B) II only
(C) I, II and III
(D) I only
(E) III only

2. Which of the following is true about abstract classes?

(A) Abstract classes cannot be instantiated, but they can be sub-classed.


(B) Abstract classes can be instantiated, but they cannot be sub-classed.
(C) Abstract classes can only contain abstract methods. They can be sub-classed.
(D) Abstract classes can only contain abstract methods. They cannot be sub-classed.

3. Given the following class declarations, what is the output from Student s1 = new GradStu-
dent(); followed by s1.getInfo();?
public c l a s s Student {
public S t r i n g getFood ( ) {
return " P i z z a " ;
}
public S t r i n g g e t I n f o ( ) {
return t h i s . getFood ( ) ;
}
}

public c l a s s GradStudent extends Student {


public S t r i n g getFood ( ) {
return "Taco" ;
}
}

(A) Wont compile since GradStudent doesnt have a getInfo method


(B) Taco
(C) Pizza
(D) Wont compile since you are creating a GradStudent, not a Student

1
(E) Wont compile since you use this.getFood()

4. Given the following class declarations, and EnhancedItem enItemObj = new EnhancedItem();
in a client class, which of the following statements would compile?
public c l a s s Item
{
private int x ;

public void setX ( int theX )


{
x = theX ;
}
// . . . o t h e r methods not shown
}

public c l a s s EnhancedItem extends Item


{
private int y ;

public void setY ( int theY )


{
y = theY ;
}

// . . . o t h e r methods not shown


}

I . enItemObj . y = 3 2 ;
I I . enItemObj . setY ( 3 2 ) ;
I I I . enItemObj . setX ( 5 2 ) ;

(A) I only
(B) II only
(C) I and II only
(D) II and III only
(E) I, II, and III

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

public c l a s s Test1
{
public void method1 ( Test2 v1 , Test3 v2 )
{
// r e s t o f method not shown
}
}

public c l a s s Test2 extends Test1


{
}

public c l a s s Test3 extends Test2


{
}

2
//The f o l l o w i n g i n i t i a l i z a t i o n s appear i n a d i f f e r e n t c l a s s .
Test1 t 1 = new Test1 ( ) ;
Test2 t 2 = new Test2 ( ) ;
Test3 t 3 = new Test3 ( ) ;

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

6. 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 ( ) }

(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

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

public c l a s s Car
{
private int f u e l ;

public Car ( ) { f u e l = 0 ; }
public Car ( int g ) { f u e l = g ; }

public void addFuel ( ) { f u e l ++; }


public void d i s p l a y ( ) { System . out . p r i n t ( f u e l + " " ) ; }
}

public c l a s s RaceCar extends Car


{
public RaceCar ( int g ) { super ( 2 g ) ; }
}

What i s t h e r e s u l t when t h e f o l l o w i n g code i s c o m p i l e d and run ?

Car c a r = new Car ( 5 ) ;


Car f a s t C a r = new RaceCar ( 5 ) ;
car . display ()
c a r . addFuel ( ) ;
car . display ( ) ;
fastCar . display ( ) ;
f a s t C a r . addFuel ( ) ;
fastCar . display ( ) ;

(A) The code compiles and runs with no errors, the output is 5 6 5 6

3
(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 wont compile.
(E) You get a runtime error ClassCastException, when fastCar.addFuel() is executed.

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

public c l a s s Book
{
public S t r i n g getISBN ( )
{
// i m p l e m e n t a t i o n not shown
}

// c o n s t r u c t o r s , f i e l d s , and o t h e r methods not shown


}

public c l a s s D i c t i o n a r y extends Book


{
public S t r i n g g e t D e f i n i t i o n ( S t r i n g word )
{
// i m p l e m e n t a t i o n not shown
}

// c o n s t r u c t o r s , f i e l d s , and methods not shown


}

Assume t h a t t h e f o l l o w i n g d e c l a r a t i o n a p p e a r s i n a c l i e n t c l a s s .

Book b = new D i c t i o n a r y ( ) ;

Which o f t h e f o l l o w i n g s t a t e m e n t s would c o m p i l e w i t h o u t e r r o r ?
I . b . getISBN ( ) ;
I I . b . g e t D e f i n i t i o n ( " wonderful " ) ;
I I I . (( Dictionary ) b ) . g e t D e f i n i t i o n ( " wonderful " ) ;

(A) I only
(B) II only
(C) I and III only
(D) III only
(E) I, II, and III

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

public c l a s s Base
{
public void methodOne ( )
{
System . out . p r i n t ( "A" ) ;
methodTwo ( ) ;
}

public void methodTwo ( )


{

4
System . out . p r i n t ( "B" ) ;
}
}

public c l a s s De rive d extends Base


{
public void methodOne ( )
{
super . methodOne ( ) ;
System . out . p r i n t ( "C" ) ;
}

public void methodTwo ( )


{
super . methodTwo ( ) ;
System . out . p r i n t ( "D" ) ;
}
}

(A) ABDC
(B) AB
(C) ABCD
(D) ABC

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

public c l a s s Point2D {
public int x ;
public int y ;

public Point2D ( ) {}

public Point2D ( int x , int y ) {


this . x = x ;
this . y = y ;
}
// o t h e r methods
}

public c l a s s Point3D extends Point2D


{
public int z ;

// o t h e r code
}

I . public Point3D ( ) {}
I I . public Point3D ( int x , int y , int z )
{
super ( x , y ) ;
this . z = z ;
}
I I I . public Point3D ( int x , int y )
{
this . x = x ;
this . y = y ;

5
this . z = 0 ;
}

(A) II only
(B) III only
(C) I, II, and III
(D) I and II only
(E) I only

Das könnte Ihnen auch gefallen