Download Array Declaration - Computer Science - Quiz and more Exercises Computer Science in PDF only on Docsity!
Signature _____________________ CSE 11 Name ________________________
Quiz 4
cs11f ____ Fall 2011 Student ID ____________________
This quiz is to be taken by yourself with closed books, closed notes, no electronic devices.
What is the output produced by the following program? (Hint: draw stack frames)
public class Swap { private int a;
public Swap( int a ) { this.a = a; }
public void swap( int a, int b ) { int tmp;
tmp = a; a = b; b = tmp; }
public void swap( Swap ref ) { int tmp;
tmp = this.a; this.a = ref.a; ref.a = tmp; }
public static void main( String[] args ) { int a = 15; int b = 20;
Swap ref1 = new Swap(5); Swap ref2 = new Swap(10);
ref1.swap( a, b ); System.out.println( a ); System.out.println( b );
ref1.swap( ref2 ); System.out.println( ref1.a ); System.out.println( ref2.a ); } }
What is the initial value of each array element in the following arrays?
int[] a = new int[5]; __________ Integer c = new Integer[7]; __________
boolean[] b = new boolean[3]; __________ double d = new double[4]; __________
Output
Given the following array declaration:
int[] a = { 1, 1, 2, 3, 5, 8, ... }; // You do not know how many values are in the initializer list
Fill in the blanks to print out each element:
for ( int i = ___________ ; i < ___________ ; ___________ )
System.out.println( ___________ );
Now do the same using a foreach (enhanced for) loop:
___________ ( int i ___________ ___________ )
System.out.println( ___________ );
Now do the same using a while loop:
_______________
while ( ___________________ )
_______________________________________________
_______________________________________________
In general, if you override the equals() method you should also override the ______________________ method.
a b
Given the following diagram of an array of array:
What is the output from the following:
System.out.println( a == b ); ___________
System.out.println( a[0] == b[0] ); ___________
System.out.println( a.equals(b) ); ___________
System.out.println( a[0].equals(b[0]) ); ___________
System.out.println( b.length ); ___________
System.out.println( b[1].length ); ___________
Write the code to check for exact type equivalence between two objects referenced by variables o1 and o2:
if ( _________________________________________________________________________________ )
System.out.println( "o1 and o2 reference the exact same type of object" );
else
System.out.println( "o1 and o2 reference different types of objects" );