Docsity

Compiler Error - Computer Science - Quiz, Exercises of Computer Science

Toggle sidebar
bg1
Signature _____________________
CSE 11
Name ________________________
Quiz 3
cs11f ____
Fall 2012
Student ID ____________________
This quiz is to be taken by yourself with closed books, closed notes, no calculators.
What gets printed when the following program is run?
public class While
{
public static void main( String[] args )
{
final int MAX = 5;
int i = 3, j = -1;
while ( i < MAX )
{
j = i;
while ( j <= MAX )
{
--j;
System.out.println( i + " " + j );
j += 2;
}
i++;
}
System.out.println( i + " " + j );
}
}
Trace the following program and specify its output.
public class Trace
{
public static void main( String[] args )
{
foo2();
System.out.println( "main1" );
foo3();
System.out.println( "main2" );
foo1();
System.out.println( "main3" );
}
public static void foo1()
{
System.out.println( "A" );
}
public static void foo2()
{
System.out.println( "B" );
foo1();
}
public static void foo3()
{
foo2();
System.out.println( "C" );
}
}
An object which is defined to handle action events must implement the ______________________
interface and provide a body/implementation for the abstract method named _______________________.
A GUI __________________ has a layout manager which defines how GUI __________________ are laid out.
pf2

Partial preview of the text

Download Compiler Error - Computer Science - Quiz and more Exercises Computer Science in PDF only on Docsity!

Signature _____________________ CSE 11 Name ________________________

Quiz 3

cs11f ____ Fall 2012 Student ID ____________________

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

What gets printed when the following program is run?

public class While { public static void main( String[] args ) { final int MAX = 5; int i = 3, j = -1;

while ( i < MAX ) { j = i; while ( j <= MAX ) { --j; System.out.println( i + " " + j ); j += 2; } i++; }

System.out.println( i + " " + j ); } }

Trace the following program and specify its output.

public class Trace { public static void main( String[] args ) { foo2(); System.out.println( "main1" ); foo3(); System.out.println( "main2" ); foo1(); System.out.println( "main3" ); }

public static void foo1() { System.out.println( "A" ); }

public static void foo2() { System.out.println( "B" ); foo1(); }

public static void foo3() { foo2(); System.out.println( "C" ); } }

An object which is defined to handle action events must implement the ______________________

interface and provide a body/implementation for the abstract method named _______________________.

A GUI __________________ has a layout manager which defines how GUI __________________ are laid out.

Given the following definitions:

And the following variable definitions:

Thing1 thing1; Thing2 thing2; Speakable speakable;

Indicate which are valid Java statements. Consider each statement executed sequentially in the order it appears.

1) Invalid Java statement โ€“ Compiler Error

2) Valid Java statement โ€“ No Compiler Error

public interface Speakable { public abstract void speak(); }

class Thing1 implements Speakable { private static final String SPEAK = "Me";

public Thing1() { // ctor initialization here }

public String speak() { return SPEAK; }

public void doit( String s ) { // Thing1 does its thing with s } }

class Thing2 implements Speakable { public static final String SPEAK = "No, Me";

public Thing2() { // ctor initialization here }

public String speak() { return SPEAK; }

public void doit() { // Thing2 does its thing } }

Hint: What does the compiler know about

any reference variable at compile time (vs.

run time)?

thing1 = new Thing1(); _______

thing1.speak(); _______

thing1.doit(); _______

thing1.doit( "Yours" ); _______

String s1 = Thing1.SPEAK; _______

thing2 = new Thing2(); _______

thing2.doit( "Theirs" ); _______

thing2.speak(); _______

thing2.doit(); _______

String s2 = Thing2.SPEAK; _______

speakable = new Speakable();_______

thing1 = thing2; _______

thing1 = speakable; _______

speakable = new Thing1(); _______

speakable.speak(); _______

speakable.doit( "Mine" ); _______

speakable = thing2; _______

speakable.speak(); _______

speakable.doit(); _______