Docsity

Java Quiz 5: Animal Class Hierarchy and Method Calls, Quizzes of Computer Science

Toggle sidebar
bg1
Student ID ________________________ CSE 8B Name ________________________
Quiz 5
Signature________________________ cs8w________
Spring 2003
This quiz is to be taken by yourself with closed books, closed notes, no calculators.
abstract class Animal {
private String name;
public Animal() { this( "Animal" ); }
public Animal( String name ) { this.name = name; }
public String getName() { return name; }
public String toString() { return getName(); }
public abstract String speak();
}
class Duck extends Animal {
public Duck() {}
public Duck( String name ) { super( name + " Duck" ); }
public String speak() { return "Quack"; }
}
final class Lion extends Animal {
public Lion() { super( "Lion" ); }
public Lion( String name ) { super( name + " Lion" ); }
public String speak() { return "Roar"; }
}
class LittleDuck extends Duck {
public LittleDuck() { this( "Little" ); }
public LittleDuck( String name ) { super( name ); }
}
class BigDuck extends Duck {
public String speak() { return "Big " + super.speak(); }
}
public class Q5 {
public static void main( String[] args ) {
Animal a;
a = new BigDuck();
System.out.println( a + " says " + a.speak() );
a = new LittleDuck( "Donald" );
System.out.println( a + " says " + a.speak() );
a = new Lion( "Cowardly" );
System.out.println( a + " says " + a.speak() );
a = new Duck();
System.out.println( a + " says " + a.speak() );
}
}
1) What gets printed?
(16 pts)
OVER
pf2

Partial preview of the text

Download Java Quiz 5: Animal Class Hierarchy and Method Calls and more Quizzes Computer Science in PDF only on Docsity!

Student ID ________________________ CSE 8B Name ________________________

Quiz 5

Signature________________________ cs8w________

Spring 2003

This quiz is to be taken by yourself with closed books, closed notes, no calculators.

abstract class Animal { private String name; public Animal() { this( "Animal" ); } public Animal( String name ) { this.name = name; } public String getName() { return name; } public String toString() { return getName(); } public abstract String speak(); }

class Duck extends Animal { public Duck() {} public Duck( String name ) { super( name + " Duck" ); } public String speak() { return "Quack"; } }

final class Lion extends Animal { public Lion() { super( "Lion" ); } public Lion( String name ) { super( name + " Lion" ); } public String speak() { return "Roar"; } }

class LittleDuck extends Duck { public LittleDuck() { this( "Little" ); } public LittleDuck( String name ) { super( name ); } }

class BigDuck extends Duck { public String speak() { return "Big " + super.speak(); } }

public class Q5 { public static void main( String[] args ) {

Animal a;

a = new BigDuck(); System.out.println( a + " says " + a.speak() );

a = new LittleDuck( "Donald" ); System.out.println( a + " says " + a.speak() );

a = new Lion( "Cowardly" ); System.out.println( a + " says " + a.speak() );

a = new Duck(); System.out.println( a + " says " + a.speak() ); } }

1) What gets printed?

(16 pts)

OVER

2) Fill in the blanks (11 points - 1 point each)

When a number of methods or constructors in the same class have the same name but differ in the number, order, or types of

parameters, the methods or constructors are said to be ___________________________.

The keyword ____________________ is used to denote inheritance of implementation and is used to inherit from concrete or abstract

classes..

The ____________________ reference is passed as a hidden parameter to every non-static method and constructor.

The access modifier to restrict access to subclasses and objects in the same package is ____________________.

In general, exception types listed in multiple catch blocks should be ordered from most ________________________ to

most ________________________.

An interface defines a new __________________ just the same as a class definition does.

Interfaces can contain only public ___________________ methods and public static __________________ constants.

To call a class's immediate superclass constructor passing the value 420, you use the statement ____________________.

What question would you like to see on the final exam (worth 1 point if you don't leave this blank)?