Quiz4.fa08

# Quiz4.fa08 - many sorting algorithms, two array elements...

This preview shows pages 1–2. Sign up to view the full content.

Signature _____________________ CSE 11 Name ________________________ Quiz 4 cs11f ____ Fall 2008 Student ID ____________________ This quiz is to be taken by yourself with closed books, closed notes, no calculators. 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( 5 ) ); } private int mystery( int a ) { int b = a + 2; if ( b >= 5 ) { 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; } } Output

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Write the equivalent for loop for the following code without using any integer constant (like 101) other than 0 to set the loop variant i . int[] a = new int[101]; int i; i = 0; while ( i < 101 ) { a[i] = i * i; ++i; } What is the value of i when the loop terminates? _________ What is the value of a[20] ? __________ A typical operation performed on arrays is sorting the elements in the array in a particular order. As part of
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: many sorting algorithms, two array elements must be swapped with each other. Complete the following swap() method to swap two elements in the array locArray indexed by a and b using a temporary local variable as an intermediate storage location. void swap( Location locArray, int a, int b ) { _______________ tmp; tmp = _______________________; ____________________ = locArray[b]; ____________________ = tmp; } Given the following code fragment, rewrite it to use a do-while loop instead. Use the same comments to indicate various actions/tests. Don't forget the semicolon at the end of the do-while statement. System.out.println( "Enter an int between 1 and 10" ); // read input from user while ( /* User input is not valid */ ) { System.out.println( "Enter an int between 1 and 10" ); // read input from user } int a = new int[101]; int i; // Do not change any code above this comment!...
View Full Document

## This note was uploaded on 01/07/2011 for the course CSE cs11 taught by Professor Cs during the Spring '10 term at UCSD.

### Page1 / 2

Quiz4.fa08 - many sorting algorithms, two array elements...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online