

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A java quiz where the task is to identify valid java statements based on given definitions and variable definitions. The quiz includes statements with compiler errors and valid statements. The document also includes a hint about what the compiler knows about reference variables at compile time.
Typology: Exercises
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Given the following definitions:
And the following variable definitions:
private Puppy puppy; private Kitty kitty; private Speakable speakable;
Indicate which are valid Java statements. Consider each statement executed sequentially in the order it appears.
A) Invalid Java statement – Compiler Error B) Valid Java statement – No Compiler Error
kitty = new Kitty(); _______
puppy = new Puppy(); _______
puppy.speak(); _______
puppy.wag(); _______
puppy.sleep( 1000 ); _______
kitty.speak(); _______
kitty.wag(); _______
kitty.sleep( 2000 ); _______
speakable = kitty; _______
speakable.speak(); _______
speakable.wag(); _______
speakable = puppy; _______
speakable.speak(); _______
speakable.sleep( 3000 ); _______
puppy = kitty; _______
speakable = new Speakable(); _______
public interface Speakable { public String speak(); }
public class Puppy implements Speakable { private static final String PUPPY_SPEAK = "Bark";
public Puppy() { // ctor initialization here }
public String speak() { return PUPPY_SPEAK; }
public void sleep( int time ) { // puppy sleeps for time seconds } }
public class Kitty implements Speakable { private static final String KITTY_SPEAK = "Meow";
public Kitty() { // ctor initialization here }
public String speak() { return KITTY_SPEAK; }
public void wag() { // kitty wags its tail } }
Hint: What does the compiler know about any reference variable at compile time (vs. run time)?