Docsity

Java Class Definitions and Quiz, Exercises of Computer Science

Toggle sidebar
bg1
Signature _____________________
CSE 11
Name ________________________
Quiz 5
cs11f ____
Fall 2012
Student ID ____________________
This quiz is to be taken by yourself with closed books, closed notes, no calculators.
Given the following class definitions:
What is the output given the following code:
A _______________________ must have the same name as the type or class it is defined in, but must not
have a ___________________________ .
Inside a method the keyword used to refer to the object being sent the message is ________________________
which is passed as a hidden first argument to all __________________________ methods and constructors, but
is not passed as a hidden first argument to any __________________________ methods.
public class Snow {
public void method2() {
System.out.println("Snow 2");
}
public void method3() {
System.out.println("Snow 3");
}
}
public class Rain extends Snow {
public void method1() {
System.out.println("Rain 1");
}
public void method2() {
System.out.println("Rain 2");
}
}
public class Sleet extends Snow {
public void method2() {
method3();
super.method2();
System.out.println("Sleet 2");
}
public void method3() {
System.out.println("Sleet 3");
}
}
public class Fog extends Sleet {
public void method1() {
System.out.println("Fog 1");
}
public void method3() {
System.out.println("Fog 3");
}
}
Snow
method2
method3
method1
method2
(method3)
Rain
method1
(method2)
method3
Fog
method2
method3
Sleet
Snow var1 = new Fog();
var1.method2();
Put your answer here:
pf2

Partial preview of the text

Download Java Class Definitions and Quiz and more Exercises Computer Science in PDF only on Docsity!

Signature _____________________ CSE 11 Name ________________________

Quiz 5

cs11f ____ Fall 2012 Student ID ____________________

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

Given the following class definitions:

What is the output given the following code:

A _______________________ must have the same name as the type or class it is defined in, but must not

have a ___________________________.

Inside a method the keyword used to refer to the object being sent the message is ________________________

which is passed as a hidden first argument to all __________________________ methods and constructors, but

is not passed as a hidden first argument to any __________________________ methods.

public class Snow { public void method2() { System.out.println("Snow 2"); } public void method3() { System.out.println("Snow 3"); } }

public class Rain extends Snow { public void method1() { System.out.println("Rain 1"); } public void method2() { System.out.println("Rain 2"); } }

public class Sleet extends Snow { public void method2() { method3(); super.method2(); System.out.println("Sleet 2"); } public void method3() { System.out.println("Sleet 3"); } }

public class Fog extends Sleet { public void method1() { System.out.println("Fog 1"); } public void method3() { System.out.println("Fog 3"); } }

Snow

method

method

method

method

(method3)

Rain

method

(method2)

method

Fog

method

method

Sleet

Snow var1 = new Fog(); var1.method2();

Put your answer here:

Given the following class definitions for class Foo, class Fubar1, and class FubarTest:

public class Foo { public Foo() { this( 5, 10 ); System.out.println( "Foo ctor #1" ); }

public Foo( int x, int y ) { System.out.println( "Foo ctor #2" ); }

public String toString() { System.out.println( "Foo.toString" ); return "Foo"; } }

public class Fubar extends Foo { public Fubar() { System.out.println( "Fubar ctor #1" ); }

public Fubar( int x, int y ) { super( x, y ); System.out.println( "Fubar ctor #2" ); }

public String toString() { System.out.println( "Fubar.toString" ); return "Fubar" + " + "

  • super.toString(); } }

public class FubarTest { public static void main( String[] args ) { Foo ref = new Fubar();

System.out.println( "-----" );

System.out.println( ref.toString() ); } }

What is the output when we run FubarTest as in

java FubarTest

Given the following expressions, indicate whether

the expressions evaluate to true or false.

String s1 = new String( "CSE 11" ); String s2 = new String( "CSE 11" ); String s3 = s1;

s1 = "CSE 11";

s1 == s2 __________

s1 == s3 __________

s2 == s3 __________

s1.equals( s2 ) __________ s1.equals( s3 ) __________

s2.equals( s3 ) __________

s1 == "CSE 11" __________

s2 == "CSE 11" __________

s3 == "CSE 11" __________

What question would you like to see on the Final Exam?