
COP-3804 Intermediate Java Programming Quiz #5 Date: July, 19th 2007 Grade: _____/100
(This quiz consists of one page)
Name: _______________________________________________ Last 4 digits PantherID: __________
1. Given the following code, what’s printed on the screen when Quiz5’s main function executes?
public class Quiz5{
public static void main( String[] args ){
String[] arr = new String[]{"Florida","Georgia","Montana","California"};
String s;
s = printRec1( arr, 0 );
System.out.println( "Result of printRec1 is: " + s );
s = printRec2( arr, 0 );
System.out.println( "Result of printRec2 is: " + s );
}
/**
* Print recursively an array of Strings.
* @param strs the array of Strings
* @param i the index for the String to print
* @return a string
*/
private static String printRec1( String[] strs, int i ){
String res;
if ( i == strs.length - 1 ){
System.out.println( strs[i] );
res = strs[i];
}
else{
System.out.println( strs[i] );
res = printRec1( strs, i+1 );
}
return res;
}
/**
* Print recursively an array of Strings.
* @param strs the array of Strings
* @param i the index for the String to print
* @return a string
*/
private static String printRec2( String[] strs, int i ){
String res;
if ( i == strs.length - 1 ){
System.out.println( strs[i] );
res = strs[i];
}
else{
res = printRec2( strs, i+1 );
System.out.println( strs[i] );
}
return res;
}
}
Output is: Florida
Georgia
Montana
California
Result of printRec1 is California
California
Montana
Georgia
Florida
Result of printRec2 is California