3/2/2012CS 110 Exam 2Page 4
Computer Science I
Spring 2012, Friday, 3/2/12
100 points
Name _____________________________
1. True/false on while and for loop structures.
[10 pts]
_____ A while loop is a control structure that repeats a block of code zero or more times.
_____ The loop control variable associated with the while or for loop cannot be used inside the loop body.
_____ The condition controlling the while or for loop continues the looping when it evaluates to false.
_____ A do-while loop executes the loop body at least once before the condition is checked.
_____ The condition of the while or for loop is re-evaluated only after all of the loop body is executed.
_____ The loop control variable value should be altered at some point during execution of the loop.
_____ For loops are merely a syntactic variation of while loops and provide no additional functionality.
_____ For loops are generally used when we do not know the number of iterations ahead of the loop.
_____ Loop control variables can only be incremented by one.
_____ 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++;
}
for(int count = 5; count>=0; count--){
System.out.print(count+” “);
}
int count;
double fact = 1;
for(count=1; count <= 5; count++){
fact = fact * count;
System.out.print(fact+” “);
}
System.out.println(count);
int count=10;
do{
System.out.print((count*2)+” “);
count = count - 2;
} while (count<5);
____________________________________________________________________________
(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+”)”);
3. True/false on arrays.
[10 pts]
_____ An array has a fixed length that cannot be changed after it is created.
_____ An array’s elements are all of the same type.
_____ An array element reference can occur anywhere a variable can occur.
_____ An array element is referenced by the array name followed by only an integer constant inside [ ].
_____ An array of size 25 has indexes or subscripts ranging from 0 to 25.
_____ An array may have a logical size that is larger than its physical size.
_____ A for loop is the preferred looping structure to manipulate each array element.
_____ A negative subscript retrieves elements just accesses the zeroth element.
_____ Parallel arrays share the same length but must have separate subscript variables.
_____ 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:
Purpose of variables ‘s’ and ‘t’:
Output from this sample input stream: Spring break is for relaxation and fun.
__________________________________________________________________
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]
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 __________[_____];
equiv[0] = 0.0;
for(int i=1; i<________; _______){
equiv[i] = ((__________)i) / (_________);
}
for(int i=0; i<_________; _________){
System.out.printf(“ %_____._____f”,equiv[_______]);
}
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( ______ i=___; i<__________ . _________ ; i++){
System.out.println(names [____]+” gpa=“+gpa[_____]);
}
//find the position of the highest gpa
int hi = _____;
for( _______k=____; k____ _______ . ______________; ________ ){
if( gpa [______] ___ gpa [hi]){
_________ = ________;
}
}
//display who had the highest gpa
System.out.println( ____________________________ +” has the highest gpa”);