3/2/2012CS 110 Exam 2Page 3
Computer Science I
Spring 2012, Friday, 3/2/12
100 points
Name ______KEY______
1. True/false on while and for loop structures.
[10 pts]
__T__ A while loop is a control structure that repeats a block of code zero or more times.
__F__ The loop control variable associated with the while or for loop cannot be used inside the loop body.
__ F __ The condition controlling the while or for loop continues the looping when it evaluates to false.
__ T __ A do-while loop executes the loop body at least once before the condition is checked.
__ T __ The condition of the while or for loop is re-evaluated only after all of the loop body is executed.
__ T __ The loop control variable value should be altered at some point during execution of the loop.
__ T __ For loops are merely a syntactic variation of while loops and provide no additional functionality.
__ F __ For loops are generally used when we do not know the number of iterations ahead of the loop.
__ F __ Loop control variables can only be incremented by one.
__ T __ Variables declared in a loop body { } are no longer accessible after the loop finishes.
2. Show the output of the following independent code segments.
[5 pts each = 25]
int count = 1;
while(count 7){
System.out.print(count+” “);
count++;
}
1 2 3 4 5 6
for(int count = 5; count>=0; count--){
System.out.print(count+” “);
}
5 4 3 2 1 0
int count;
double fact = 1;
for(count=1; count = 5; count++){
fact = fact * count;
System.out.print(fact+” “);
}
System.out.println(count);
1.0 2.0 6.0 24.0 120.0 6
int count=10;
do{
System.out.print((count*2)+” “);
count = count - 2;
} while (count<5);
___20______
(2. continued)
String phrase = “Spring break, here we come!”;
int count = 0;
for(int i=0; i<phrase.length(); i++){
if(phrase.charAt(i) == ‘r‘){
count++;
System.out.print(i+” “);
}
}
System.out.println(“(r=”+count+”)”);
2 8 16 (r=3)
3. True/false on arrays.
[10 pts]
__T__ An array has a fixed length that cannot be changed after it is created.
__ T __ An array’s elements are all of the same type.
__ T __ An array element reference can occur anywhere a variable can occur.
__F__ An array element is referenced by the array name followed by only an integer constant inside [ ].
__ F __ An array of size 25 has indexes or subscripts ranging from 0 to 25.
__ F __ An array may have a logical size that is larger than its physical size.
__ T __ A for loop is the preferred looping structure to manipulate each array element.
__ F __ A negative subscript retrieves elements just accesses the zeroth element.
__ F __ Parallel arrays share the same length but must have separate subscript variables.
__ F __ Searching an array results in reporting only if the search value is in the array.
4. Show what is displayed on the canvas by the following graphics commands in the paint() routine of a Java applet. Just label the different colors.
[5 pts]
String [] words = {“Can’t”,”wait for”,”BREAK!”};
Color [] colors = {Color.BLUE,Color.BLACK,Color.RED};
canvas.drawRect(0,0,250,250);
canvas.setFont(new Font(“Courier New”, Font.PLAIN, 12));
for(int c=0; c<words.length; c++){
canvas.setColor(colors[c]);
canvas.drawString(words[c], 10*c, 50*c);
}
5. Explain what the following code segment does. Explain what the purposes of variables ‘s’ and ‘t’ are. Show the output, given the same input sequence below.
[15 pts]
Scanner kb = new Scanner(System.in);
String [] words = new String[100];
int k = 0;
int s = 0;
int t = 0;
while(kb.hasNext()){
words[k] = kb.next();
if(words[k].length() > s){
s = words[k].length();
t = k;
}
k++;
}
System.out.println (“k=“+k+” and the word is “+words[t]);
Description of the code segment:
Read a series of words into an array, counting the words, tracking the longest and its position and printing the number of words and the longest one.
Purpose of variables ‘s’ and ‘t’:
S is the length of the longest word seen and t is the position
Output from this sample input stream: Spring break is for relaxation and fun.
k=7 and the word is relaxation
______
6. Develop a Java code segment to input lines of text using keyboard.nextLine() concatenating them together (like summing a list), until the total length exceeds 140 characters. You need not use an array. Print the resulting string. You do not have to give me a complete program, just the code segment to do these tasks.
[10 pts]
String full = “”;
do {
String line = keyboard.nextLine();
full = full + line;
} while (full.length()<140);
System.out.println(full);
7. Fill in the blanks to accomplish to input a number n and store in an array the decimal equivalents of the sequence of 0 ½, 2/3, ¾, 4/5, 5/6,… (n-1)/n. So if you put in 6 as n, then you should store 0.000, 0.500, 0.667, 0.750, 0.800, 0.833. Then print the array using an appropriate printf( “% __ . __ f”, var) to format the output.
[10 pts]
1. .
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
double [] equiv = new __double_[_n__];
equiv[0] = 0.0;
for(int i=1; i<___ n ____; _i++___){
equiv[i] = ((__double __)i) / (__i+1___);
}
for(int i=0; i<___ n _____; __ i++_____){
System.out.printf(“ %__5__.__3__f”,equiv[__i____]);
}
8. Assume two arrays are declared and “loaded” as shown. Fill in the blanks for the code segment to perform the manipulations as given in the comments.
Write the code to properly compute results regardless of the contents or size of the arrays. The data is supplied for you to have something to consider as you fill in the code.
[15 pts]
String [] names = {“Allen”, ”Erica”, ”Hans”, ”Hunter”, ”Phil”, ”Pam”, ”Sue”};
double [] gpa = {3.5, 2.9, 2.5, 2.0, 3.9, 3.8, 2.7};
//print the table with two vertical columns
for( __int_ i=_0_; i<__names___ . _length_ ; i++){
System.out.println(names [_i__]+” gpa=“+gpa[__i __]);
}
//find the position of the highest gpa
int hi = __0__;
for( __ int __k=_1__; k___ names _.__ length __; ___k++_____ ){
if( gpa [_k ___] __ gpa [hi]){
__hi_____ = ___ k ___;
}
}
//display who had the highest gpa
System.out.println( ___names[hi]______+” has the highest gpa”);