Sample exam
Answers
Question One (15 points)
Rewrite this method so that it uses a while-loop instead of a for-loop:
public void count()
{
for (int i = 1; i < 10; i = i +1 )
{
System.out.println(i);
}
}
Question Two (25 points)
What does this class print out when compiled and run?
public class Thing
{
private int number;
public Thing(int number)
{ this.number = number; }
public void double()
{ number = number * 2; }
public void addOne()
{ number = number + 1; }
public void printDouble()
{ System.out.println(number * 2); }
public static void main(String args[])
{
Thing thing1 = new Thing(10);
Thing thing2 = new Thing(10);
Thing thing3 = thing1;
thing1.double();
thing2.double();
thing3 = thing2;
thing3.addOne();
thing1.printDouble();
thing2.printDouble();
thing3.printDouble();
System.out.println("thing 1: " + thing1.number);
System.out.println("thing 2: " + thing2.number);
System.out.println("thing 3: " + thing3.number);
}
}
Question 3: (15 points)
public class Dog
{ public void speak()
{ System.out.println("Woof!"); }
public void hunt()
{ System.out.println("Bark! Chase squirrels!"); }
}
Write a class called Snoopy that extends class Dog. Over-ride the
hunt method so that it prints out "Curse you, Red Baron!" instead of
"Bark! Chase squirrels!". Overload the speak method so that it accepts
a String as a parameter, prints out that String, and then runs the speak()
method from the super-class Dog.
Question 4: (10 points)
Some of these classes should extend others of these classes.
Write next to each class what other class or classes it should extend.
class Object (remember: class Object doesn't extend anything!)
class Airplane
class FilingCabinet
class Car
class Biplane
class Vehicle
class Porsche
class Submarine
class Motorbike
class House
Question 5 (15 points)
Declare and write a method that takes no parameters, and which returns an
array of ints. The array that is returned should have space for exactly
100 ints, and it should contain the first 100 even numbers (starting
with 0 and going up from there).
We expect you to use a loop in this method.
Question 6 (15 points)
What does is printed when the Quotes class is compiled and run?
public class Hero
{
boolean HasSecretIdentity;
String quote = "My name is not important";
public void rescueDamsel()
{ System.out.println("Don't worry, I shall save you!"); }
public void defyEvil()
{ System.out.println("You'll never get away with this!"); }
}
public class SuperHero extends Hero
{
boolean canFly;
String quote = "Do what is right.";
public void defyEvil()
{ System.out.println("I shall save the Earth!"); }
}
public class Quotes
{
public static void main (String[] args)
{
Hero zorro = new Hero();
Hero batman = new SuperHero();
SuperHero superman = new SuperHero();
zorro.defyEvil();
System.out.println(zorro.quote);
batman.defyEvil();
System.out.println(batman.quote);
superman.defyEvil();
System.out.println(superman.quote);
}
}
Question 7
(5 points)
This class contains an error. What is it?
public class Spider
{ private int legs = 8;
public static void main(String[] args)
{
Spider charlotte = new Spider();
System.out.println( legs );
}
}