Practice Quiz 5 with Solutions for Intermediate Java Programming | COP 3804, Quizzes of Java Programming

Material Type: Quiz; Class: Intermediate Java Programming; Subject: Computer Programming; University: Florida International University; Term: Summer 2007;

Typology: Quizzes

Pre 2010

Uploaded on 03/11/2009

koofers-user-bj9
koofers-user-bj9 🇺🇸

5

(1)

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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

Partial preview of the text

Download Practice Quiz 5 with Solutions for Intermediate Java Programming | COP 3804 and more Quizzes Java Programming in PDF only on Docsity!

COP-3804 Intermediate Java Programming Quiz #5 Date: July, 19

th

2007 Grade: _____/

(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