Beruflich Dokumente
Kultur Dokumente
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.
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 ( ) ;
}
}
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 ;
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
}
}
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 ( ) }
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 ; }
(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
}
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 ( ) ;
}
4
System . out . p r i n t ( "B" ) ;
}
}
(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 ( ) {}
// 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