Download Java Programming Exercises and Questions and more Exams Java Programming in PDF only on Docsity!
Signature _____________________ Name ________________________
cs11f ____ Student ID ____________________
CSE 11
Final
Fall 2009
Page 1 ___________ (24 points)
Page 2 ___________ (7 points)
Page 3 ___________ (13 points)
Page 4 ___________ (34 points)
Page 5 ___________ (13 points)
Page 6 ___________ (15 points)
Page 7 ___________ (23 points)
Page 8 ___________ (18 points)
Page 9 ___________ (19 points)
Total ___________ (166 points = 158 base points + 8 points EC [5%])
(Partial) Operator Precedence Table
Operators Associativity
! ++ -- (pre & post inc/dec) right to left
* / % left to right
+ - left to right
< <= > >= left to right
== != left to right
&& left to right
|| left to right
= right to left
1) Which of the following are not valid Java identifiers? (Circle your answer(s).)
1stJavaClass My_First_Java_Class Java1 CSE11Is#
CSE11 CSE-11 My1stJavaClass double
2) Using the operator precedence table above, evaluate each expression and state what gets printed. Remember
short-circuit evaluation with && and ||.
int i = 1, j = 2, k = 3, m = 2;
System.out.println( !( k >= m ) ); ________
System.out.println( j <= i || j == m && k <= m ); ________
System.out.println( i >= 1 && !(j != 4) ); ________
System.out.println( !(i > 4 && j <= 6) == i >= 4 || j > 6 ); ________
3) What gets printed?
int a = 3, b = 6;
System.out.println( -1 + ++a * 5 + 17 % 5 ); _________
System.out.println( 6 + b++ - 5 / 9 + 4 ); _________
4) What gets printed?
public class Question
public static void main( String[] args )
final int MAX = 9;
int i = 4, j = 8;
for (i = 6; i <= MAX; ++i )
j = i;
while ( j < MAX )
--j;
System.out.println( i + " " + j );
j += 2;
System.out.println( i + " " + j );
7) Given the following class definitions:
public class Person { public Person() { ... } public void print() { System.out.println( "Person" ); } public void printAll( Person[] list ) { for ( int i = 0; i < list.length; ++i ) list[i].print(); } }
public class Student extends Person { public void print() { System.out.println( "Student" ); } }
Assume the method printAll() is called with an array of length 5, and than none of the five elements of the array
is null. Which of the following statements best describes what will happen, and why? Circle correct answer.
A. The word Person will be printed five times since the type of the array parameter is Person.
B. The word Person will be printed five times since printAll is a method of the Person class.
C. The word Student will be printed five times since the print method was overridden by the Student class.
D. For each of the five objects in the array, either the word Person or the word Student will be printed,
depending on the type of the objects in the array list.
E. If the array actually contains objects of type Person, then the word Person will be printed five times;
otherwise, a runtime error will occur.
8) Complete the following method which is intended to return the index of the last occurrence of value in the
array numbers or -1 if value is not in the array numbers.
public static int findLastOccurrence( int[] numbers, int value ) { for ( int i = _________________________________________; ____________________________; --i ) { if ( ______________________________________________________ ) { return ____________ ; } } return ____________; }
9) What does the following method print as a result of the call F9( 10 )?
public void F9( int x ) { for ( int y = 0; y <= x; y = y + 2 ) { System.out.print( y + " " ); }
System.out.println();
if ( x > 0 ) { F9( x โ 2 ); }
System.out.print( x + " " ); }
10) Indicate whether each of the following parts of a Java program is (A-H) and where in the Java Runtime
Environment each part lives (1-3)
A) Class (static) variable 1) The Class Area
B) Instance variable 2) The Heap
C) Static method 3) Stack Frame in the Runtime Stack
D) Instance method
E) Local variable
F) Formal Parameter
G) Constructor
H) Class definition Java program part Java Runtime area
(Answer A-H in this column) (Answer 1-3 in this column)
public class F10 ________ F { private char actor; ________ actor ________
public F10() { } ________ F10 ________
public void setActor( char ch ) { actor = ch; } ________ ch ________
________ setActor ________
public static int cling; ________ cling ________ }
public class SomeOtherClass ________ SomeOtherClass { private int cling; ________ cling ________
public static void main( String[] args ) ________ args ________ { ________ main ________
char toon = '?'; ________ toon ________
F10 ref1; ________ ref1 ________
ref1 = new F10(); (where ref1 is pointing) ________
SomeOtherClass ref2 = new SomeOtherClass(); ________ ref2 ________
// Other Code ... possibly changes the value in toon (where ref2 is pointing) ________ ... **// *** Location 1 ***** }
public char fubar( char tester ) { ... } ________ tester ________
________ fubar ________ }
Write a single statement that could appear above at the line marked //*** Location 1 *** that passes the value of
toon to fubar and puts the return value of fubar into the variable actor in the object referenced by ref1.
Write a single statement that could appear above at the line marked //*** Location 1 *** that puts the value of
cling in class F10 into the variable cling in the SomeOtherClass object referenced by ref.
13) Given the following definitions:
And the following variable definitions:
Puppy puppy; Kitty kitty; MyPet pet;
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(); _______
pet = kitty; _______
pet.speak(); _______
pet.wag(); _______
pet.sleep( 3000 ); _______
kitty = pet; _______
pet = new MyPet(); _______
pet = puppy; _______
pet.speak(); _______
( (Puppy) pet).wag(); _______
( (Puppy) pet).sleep( 3000 ); _______
puppy = pet; _______
puppy = kitty; _______
kitty.wag(); _______
public abstract class MyPet { public abstract String speak(); }
public class Puppy extends MyPet { 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 extends MyPet { 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)?
14) What is the output produced by the following program? (Hint: draw stack frames)
public class Mystery
public static void main( String[] args )
Mystery ref = new Mystery();
System.out.println( ref.mystery( 9 ) );
public int mystery( int a )
int b = a + 3;
int c = a - 3;
if ( c > 0 )
System.out.println( a + " " + b + " " + c );
c = b + mystery( a - 2 );
System.out.println( a + " " + b + " " + c );
else
c = a + b;
System.out.println( "Stop!" );
System.out.println( a + " " + b + " " + c );
return c;
Output
Given the following class definitions for class Foo, class Fubar, and class FubarTest:
18) What is Rick's favorite beer? _____________________________________
Java supports single inheritance of _________________________ using the keyword __________________.
Composition provides a(n) __________ relationship while inheritance provides a(n) __________ relationship.
When assigning a variable of type double to a variable of type int, Java requires you to use a ______________
on the double variable.
A(n) _____________________ can contain only public abstract methods and public static final constants.
Java supports multiple inheritance of _______________________ using the keyword ____________________.
To check for exact type equivalence, call ___________________ on the two objects and check if the resulting
references are the same with ==.
public class Foo { public Foo( int x, int y ) { this(); System.out.println( "Foo ctor #1" ); }
public Foo() { System.out.println( "Foo ctor #2" ); }
public String toString() { System.out.println( "Foo.toString" ); return "Foo.toString"; } }
public class Fubar1 extends Foo { public Fubar1( int x, int y, int z ) { super( x, y ); System.out.println( "Fubar ctor #1" ); }
public Fubar1( int x, int y ) { this( x, y, -99 ); System.out.println( "Fubar ctor #2" ); }
public String toString() { System.out.println( "Fubar.toString" ); return super.toString() + " + " + "Fubar.toString"; } }
public class FubarTest { public static void main( String[] args ) { Foo ref = new Fubar1( 5, 10 );
System.out.println( "-----" );
System.out.println( ref.toString() ); } }
17) What is the output when we run FubarTest as in
java FubarTest
Scratch Paper