Docsity

Java Quiz: Identifying Valid Java Statements, Exercises of Computer Science

Toggle sidebar
bg1
Signature _____________________ CSE 11 Name ________________________
Quiz 3
cs11f ____ Fall 2009 Student ID ____________________
This quiz is to be taken by yourself with closed books, closed notes, no calculators.
What is the output produced by this program? (Hint: Draw Stack Frames!)
public class Mystery
{
public static void main( String[] args )
{
Mystery ref = new Mystery();
System.out.println( ref.mystery( 5 ) );
}
private int mystery( int a )
{
int b = a + 3;
if ( b >= 6 )
{
System.out.println( b );
b = a + mystery( a - 1 );
System.out.println( a );
}
else
{
System.out.println( "Whoa!" );
b = a;
System.out.println( a );
}
return b;
}
}
(over)
Output
pf2

Partial preview of the text

Download Java Quiz: Identifying Valid Java Statements and more Exercises Computer Science in PDF only on Docsity!

Signature _____________________ CSE 11 Name ________________________

Quiz 3

cs11f ____ Fall 2009 Student ID ____________________

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

What is the output produced by this program? (Hint: Draw Stack Frames!)

public class Mystery

public static void main( String[] args )

Mystery ref = new Mystery();

System.out.println( ref.mystery( 5 ) );

private int mystery( int a )

int b = a + 3;

if ( b >= 6 )

System.out.println( b );

b = a + mystery( a - 1 );

System.out.println( a );

else

System.out.println( "Whoa!" );

b = a;

System.out.println( a );

return b;

(over)

Output

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)?