Question 1:
What is the output of the following code?
public class Question02b{ public static void main(String[] args){ SuperClass superA = new SuperClass(); SuperClass superB = new SubClass(); SubClass sub = new SubClass(); System.out.println(superA.method()); System.out.println(superB.method()); System.out.println(sub.method()); System.out.println(superB.method(1)); System.out.println(superB.method(1.4)); }} class SuperClass { public static String method(){ return "SuperClass"; } public String method(double a){ return "SuperClass"; } } class SubClass extends SuperClass{ public static String method(){ return "SubClass"; } public String method(int a){ return "SubClass"; } } Question 3 is on page 4
Question 2:
What are the possible types of objects at runtime that veg in the method omnivore may be pointing
to? And what would the output be?
public class Question03 { void omnivore(Vegetable veg){ veg.eat(); } } abstract class Vegetable { protected int roots = 7; public void eat(){ slice(); saute(); } public static void saute(){ System.out.println("Sizzle!"); } public abstract void slice(); } class Tuber extends Vegetable { Tuber(){ mash(); } public void slice(){ System.out.println("Chop!"); mash();} public void mash(){ System.out.println("Glop! " + roots); } } class Potato extends Tuber { Potato(){ roots = 59; } public void eat(){ if(this.equals(new Potato())) System.out.println("Spud!"); else System.out.println("Spudless!"); super.eat(); } public static void saute(){ System.out.println("Crisps!"); } public void mash(){ System.out.println("Creamy! " + roots); } }
Question 3:
There are two errors in this class, what are they and how can they be fixed?
public class Question04c extends OtherClass{ protected int answer(int i) throws DumbQuestionException { if(i < 0){ throw new DumbQuestionException(); } else return 6; } } class OtherClass{ public int answer(int i){ return i; } } class DumbQuestionException extends Exception{ }