Download 44-141 Computer Programming I Name: Fall 2010 Exam 2 Instructor

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
44-141 Computer Programming I
Fall 2010 Exam 2
Name:___________________
Instructor: Siever Spradling
Matching (2 points each)
Match the best answer with the best response below.
a)
do…while
b)
for loop
loop
c)
while loop
__c__1.
Assume that we want to accept a number as input and repeatedly add the number to a
total only if the total is greater than 50. Which loop is the best choice when a particular
condition will be checked before the loop body is executed?
__b__2.
Assume that we want to accept input for 100 exam scores and add the scores to a total.
Which loop is the best choice when the programmer knows the number of times that a
loop should be executed?
__a__3.
Assume that we want to display a list of options at least once and then repeat this list of
options as long as the option chosen is not 0. Which loop is the best choice when the
loop body needs to be executed at least once?
Multiple Choice (2 points each)
Choose the best answer and write the corresponding letter in the space provided.
__a_4.
price in the Java code below is used to pass additional information that is needed for the
method to complete its task and is referred to as a(n) _________.
displayPrice( price
a.
b.
c.
d.
__c_5.
)
argument
formal parameter
method header
method signature
The Java statement below is an example of a(n) __________.
public static double setAge( int ageIn )
a.
b.
c.
d.
argument
formal parameter
method header
method signature
_a__6.
_______ is the keyword that is used to indicate that a method does not return a value or
result.
a. void
b. return
c. public
d. noReturn
__a__7.
The scope of the variable priceIn in the Java code below is the setPrice method.
public static void setPrice( double priceIn ){ …
a. True
b. False
__b_8.
Select the statement below that is used to print the fourth element of an array named price.
a) System.out.println( price[4] );
b) System.out.println( price[3] );
c) System.out.println( price<4> );
d) System.out.println( price<3> );
__b_9.
double in the Java statement below is referred to as a(n) _________________.
public static double setAge( int ageIn )
a.
b.
c.
d.
__d_10.
argument type
return type
formal parameter type
method signature
The Java statement below is an example of a(n) __________.
setPrice( double )
a.
b.
c.
d.
argument
formal parameter
method header
method signature
__a__11. The + operator below in the Java statement below ______.
System.out.println( Price:  + price );
a. joins or concatenates the string (“Price: ”) with the contents of the variable price
b. sums the string (“Price: “) with the contents of the variable price
c. is an illegal operator and therefore is an illegal statement.
__b__12. The statement below
double[] price = new double[20];
reserves memory for _____ doubles.
a) 0
b) 20
c) 19
d) 21
__a__13. Each element in the array is specified by a(n) _______ that denotes the element’s location in the
array.
a) index or subscript
b) location
c) boundary
Page 2
__a__ 14.
Given the Java code below, which statement is correct?
for(int n = 1; n <= 5; n++ )
{
System.out.println(n + " " + n*n);
}
a.
b.
c.
d.
The int n = 1 statement is executed exactly once at the top of the loop.
The n <= 5 statement is executed exactly once at the bottom of the loop.
The n++ statement is executed at the beginning of each execution of the loop body.
The Java statements are invalid because they do not contain an increment to n.
Scope Question (2 points each)
15. Give the following code:
Line #
Code
1
static void doSomething(int a) {
int b;
2
for(b=0;b<10;b++) {
3
int c=2*a;
b+=c;
4
}
System.out.println(a);
5
}
Using the line numbers (given to the left of the code) identify:
1. Which number indicates the beginning of a’s scope:
___1___
2. Which number indicates the end of a’s scope:
___5___
3. Which number indicates the beginning of b’s scope:
___2___
4. Which number indicates the end of b’s scope:
___5___
5. Which number indicates the end of c’s scope:
___4___
Page 3
Tracing (15 points)
16. For each segment of code trace the values of the variables a and b:
Show how each variable changes as the code runs.
Part 1: (6 points)
int a
int b
for(a
{
b =
}
= 1;
= 0;
= 0;a < 5;a++)
b + a;
Part 2: (3 points)
int a = 5;
int b = 0;
do
{
a--;
}
while(a > 0 && a < 5);
Part 3: (6 points)
int a = 100;
int b;
while(a > 80)
{
for(b = 0; b < 2; b++)
a -= 10;
a = a + a / 10;
}
a’s value
1
0
1
2
3
4
5
b’s value
0
0
1
3
6
10
a’s value
5
4
3
2
1
0
b’s value
0
a’s value
100
90
80
88
78
68
74
b’s value
0
1
2
0
1
2
Page 4
Tracing (10 points)
17. Trace the program code shown below. Write all values that are stored in the myList array
during the program run. Also show the output in the output box provided.
int[] myList = new int[7];
myList[0] = 1;
myList[1] = 1;
for( int count = 2; count < myList.length; count++)
{
myList[count] = myList[count - 2] + myList[count - 1];
}
for( int count = 0; count < myList.length; count++)
{
System.out.println( myList[count] );
}
myList
0
1
1
1
2
2
3
3
4
5
Output
1
1
2
3
5
8
13
21 (optional)
Page 5
5
8
6
13
7
(OPTIONAL)
21
Writing Code (14 points)
18. Complete the program on the next page. This program repeatedly prompts a user to enter
integers (int). The program should continue until the user enters a sentinel value of 0. The
program should sum all numbers entered and display the average. (Note: The sentinel value of 0
should not be used in the calculation of the average.) Your program should match the sample
runs below:
Sample run 1 (input is shown in bold):
Enter an integer or 0 to quit: 100
Enter an integer or 0 to quit: 90
Enter an integer or 0 to quit: 80
Enter an integer or 0 to quit: -10
Enter an integer or 0 to quit: 0
The average of the numbers is 65.0
Sample run 2 (input is shown in bold)
Enter an integer or 0 to quit: 2
Enter an integer or 0 to quit: 4
Enter an integer or 0 to quit: 6
Enter an integer or 0 to quit: 10
Enter an integer or 0 to quit: -1
Enter an integer or 0 to quit: 100
Enter an integer or 0 to quit: 0
The average of the numbers is 20.166666666666668
Sample run 3 (input is shown in bold)
Enter an integer or 0 to quit: 10
Enter an integer or 0 to quit: 20
Enter an integer or 0 to quit: 0
The average of the numbers is 15.0
Complete the program on the next page.
Page 6
import java.util.Scanner;
public class AverageDriver
{
public static void main( String[] args)
{
Scanner keyboardInput = new Scanner( System.in );
+3 declare appropriate variables
int number;
int total = 0;
int count = 0;
double average = 0;
+2 prompt and get input
System.out.print( "Enter a number or 0 to quit: " );
number = keyboardInput.nextInt();
+2
while ( number != 0 )
{
+1
+1
total += number;
count++;
+2 prompt and get input
System.out.print( "Enter a number or 0 to quit: " );
number = keyboardInput.nextInt();
}
+2
+1
average = (double) total / count;
System.out.println( "The average of the numbers is " + average );
-1 for int division
-1 for count being off by 1 (counting the 0)
}
} //===== end method main(String[]) =====
//======= end class AverageDriver =======
Page 7
Writing Code (13 points)
19. Write a static method named findMax(). The findMax() method should receive two
integer arguments and return the greater of the two.
For example, if the numbers 6 and 10 were passed to the method, it would return 10. If, instead,
the numbers were 20 and 10, the method would return 20. If the numbers were 10 and 10, the
method would return 10.
Note: You may not call an external method to accomplish this task. Write only the
findMax() method below.
+6
+1
+1
+1
+1
+1
public static int findMax(int number1, int number2)
{
if (number1 > number2)
return number1;
else
return number2;
}
Page 8
Writing Code (10 points)
20. A person’s weight (in kilograms) can be estimated by combining their height and width (both in
centimeters) and their age (in years). Don’t worry about how to calculate this because you will
assume that someone has already written a method that can do this computation for you and it has
the following method heading:
/** returns the estimated weight for a human */
double estimateWeight(double height, double width, int age)
Complete the method named weightTable() that will use (call) the above
estimateWeight()method to print a table showing how the estimated weight changes based on
the age of the person. The method should use the height and width variables and print a table of
how the estimated weight differs from ages 10 through 60 (in increments of 10).
The method should be able to match the sample run below (bold indicates numbers that are
somehow calculated by the estimatedWeight() method):
Sample run 1: If estimateWeight is given a height of 180 and a width of 40:
For a height of 180cm and a width of 40cm:
10 year old: 145 kg
20 year old: 149 kg
30 year old: 154 kg
40 year old: 159 kg
50 year old: 162 kg
60 year old: 165 kg
static void weightTable(double height, double width) {
static void weightTable(double height, double width) {
+2
System.out.println(“For a height of “ + height +
“cm and a width of “ + width + “cm:”);
int i;
+2
for(i=10;i<=60;i++) {
+1
System.out.print(i + “ year old: “);
+4
System.out.print(estimatedWeight(height, width, i));
+1
System.out.println(“kg”);
}
}
-1 for no loop (bad style)
Page 9