Solutions

advertisement
CSC 211 Winter 2002 Andrus
MIDTERM EXAM CSC 211
Page 1 of 4
Date: 2/12/02
Write all answers in your blue book. Please use a separate page for each of the six
questions. Please be neat. Point values are 15, 18, 20, 15, 16, 16, respectively.
1. Write a Java statement or set of statements to accomplish each of the following:
a Sum the odd integers between 1 and 99 using a for loop.
sum = 0;
for (int n = 1; n < 100; n += 2)
sum += n;
b
Print the integers from 1 to 20 using a while loop and the counter variable x.
Assume that the variable x has been declared but not initialized. Print only five
integers per line.
x = 1;
while (x <= 20) {
System.out.print(x + " ");
if (x % 5 == 0)
System.out.println( );
x++;
}
c
Using a do – while loop and the counter variable k, print the line
9, 8, 7, 6, 5, 4, 3, 2, 1
Assume the variable k has been declared but not initialized.
k = 9;
do {
if (k > 1)
System.out.print(k + ", ");
else
System.out.print(k);
k--;
} while (k > 0);
2.
What are the values of the following expressions? In each line assume that
double X = 2.5, Y = -1.5;
int M = 18, N = 4;
String S = "Hello", T = "World";
i)
X + N * Y - ( X + N ) * Y = 6.25
ii) M / N + M % N = 6
iii) 5 * X - N / 5 = 12.5
iv) S + T = "HelloWorld"
v)
S + N = "Hello4"
vi) 1 - ( 1 - ( 1 - ( 1 - ( 1 - N )))) = -3
3. Write a Java program that asks the user to input (from the keyboard)
 The number of gallons of gas in the tank
 The fuel efficiency in miles per gallon
CSC 211 Winter 2002 Andrus
Page 2 of 4
 The price of gas per gallon
Then print out how far the car can go with the gas in the tank and print the cost per
100 miles. For example, if the user entered: 12.6, 21, and 1.11, respectively, then
your program should print:
Distance car can travel: 264.6 miles
Cost per 100 miles: $5.29
import java.text.DecimalFormat;
import java.text.NumberFormat;
import cs1.Keyboard;
public class Car {
public static void main(String [] args)
{
double gallons, milesPerGallon, pricePerGallon;
// Get input
System.out.print("Gallons of gas in tank? ");
gallons = Keyboard.readDouble();
System.out.print("\nGas Mileage? ");
milesPerGallon = Keyboard.readDouble();
System.out.print("\nGas price? ");
pricePerGallon = Keyboard.readDouble();
// Print output
DecimalFormat fmt1 = new DecimalFormat("0.#");
NumberFormat fmt2 = NumberFormat.getCurrencyInstance();
System.out.println("Distance car can travel: " +
fmt1.format(gallons * milesPerGallon) + " miles");
System.out.println("Cost per 100 miles: " +
fmt2.format(pricePerGallon * 100 / milesPerGallon));
}
}
4. Consider the following Java code:
double shippingCharge = 5.00;
if (country.equals("USA"))
if (state.equals("IL"))
shippingCharge = 10.00;
else
shippingCharge = 20.00;
System.out.print ("Your shipping charge is: " +
shippingCharge);
CSC 211 Winter 2002 Andrus
Page 3 of 4
What gets printed if
a
b
c
country = "USA" and state = "IL" Your shipping charge is: 10.00
country = "USA" and state = "MI" Your shipping charge is: 20.00
country = "Afghanistan" and state = "OH"
Your shipping charge is: 5.00
5. What does the following program print?
public class Mystery2 {
public static void main (String [] args)
{
int count = 1;
while (count <= 10) {
if (count % 2 == 1)
System.out.println("****");
else
System.out.println("++++++++");
count++;
}
}
}
Output
****
++++++++
****
++++++++
****
++++++++
****
++++++++
****
++++++++
6. Suppose x, y, and z are int variables and have been assigned values. Write if – else
statements for the following
a Assign x the smaller of the values of y and z.
if (y < z) x = y;
else x = z;
b
Assign x the largest of the three values of x, y, and z.
if (x > y)
if (x > z)
;
else x = z;
else if (y > z) x = y;
else x = z;
c
Assign x the value 50 if both x and y are within the range 0 to 100 inclusive.
if (x >= 0 && x <= 100)
if (y >= 0 && y <= 100)
x = 50;
CSC 211 Winter 2002 Andrus
d
Page 4 of 4
Increment x by 1 if y is between 0 and 5 inclusive, increment x by 2 if y is between 6
and 25 inclusive, and increment x by 3 if y is between 26 and 100 inclusive. Your
code should not change x if the value of y is greater than 100 or if it is negative.
if (y >= 0 && y <= 5) x++;
if (y >= 6 && y <= 25) x += 2;
if (y >= 26 && y <= 100) x += 3;
Download