Sample exam 2

advertisement
Sample exam 2
1. Given the following declarations, indicate the truth values of expressions a
through e, below:
int x = 4, y = -5;
a. (x % 2) >= 0
b. (x < y) || (y < 0)
c. (x >= y) && (y >= (y * 2))
d. !(y > x) || !(y > 0)
e. !((x == y) && (x > y))
2. Examine the code fragment below, then answer questions a through c:
int x=0, y=0;
Scanner kb = new Scanner (System.in);
while (y >= 0)
{
System.out.print(“Enter a positive # (or negative to quit): ”);
y = kb.nextInt();
x++;
if (y > 0)
x = x * y;
}
System.out.println(“The result is: ” + x);
a) Which variable is the control variable?
b) Is the loop event controlled or count controlled?
c) Suppose the program is run, with the following values input (one value per
iteration): 2 3 0 5 -2 What is the output?
3. Rewrite the following switch/case structure as an if/else structure:
switch (x)
{
case 1: case 2:
x++;
break;
case 3: case 4:
x = x * 2;
break;
case 5:
x--;
break;
default:
x = y;
}
Computer Science I
Sheller
cs1s07sampEx2 - work
Spring 2007
Page 1
4. Describe the error(s) in the code fragments below and show how to correct them.
Assume that the comments accurately describe how the code is intended to
behave:
Code
// prints “hello” until user types 1 to quit
Scanner kb = new Scanner (System.in);
int quit = 0;
while (quit = 0)
System.out.println(“hello”);
System.out.print(“1=quit, 0=continue”);
System.out.print(“\nYour choice: ”);
quit = kb.nextInt();
Errors / corrections
// prints result if score is above MAX or
// below MIN; otherwise, prints score
if (score > MAX && score < MIN)
System.out.println(“result=” + result);
else;
System.out.println(“score=” + score);
// adds up the numbers between 1 & 10
int x=0;
while (x < 10)
{
x++;
int sum = 0;
sum = x;
}
// finds and prints the largest of a set of
// random numbers
Random rg = new Random();
Scanner kb = new Scanner (System.in);
int quit = 0;
int largest = rg.nextInt();
while (quit < 1);
{
int next = rg.nextInt();
if(largest > next)
next = largest;
System.out.print(“1=quit, 0=continue”);
System.out.print(“\nYour choice: ”);
quit = kb.nextInt();
Computer Science I
Sheller
cs1s07sampEx2 - work
Spring 2007
Page 2
5. Show the output from the following program if the program is run 10 times, with
the input for each run given in steps a through j, below:
import java.util.*;
public class RN {
public static void main (String [] args) {
int n;
Scanner kb = new Scanner (System.in);
System.out.print("Enter a number between 1 and 9 ");
String s = kb.next();
n = Integer.parseInt(s);
if (n == 9)
s = "JY";
else if (n >= 5)
{
s = "W";
if (n > 7)
s = s + "J";
if (n > 6)
s = s + "J";
if (n > 5)
s = s + "J";
}
else if (n > 1)
{
if (n == 4)
s = "JW";
else if (n == 3)
s = "JJJ";
else if (n == 2)
s = "JJ";
}
else
s = "J";
System.out.println(s);
}
}
inputs:
a. 7
b. 19
c. 9
d. –3
e. 4
f. 2
g. 8
h. 10
i. 0
j. 6
Computer Science I
Sheller
cs1s07sampEx2 - work
output:
Spring 2007
Page 3
6. Write a program that asks the user for a year and then reports whether or not the
year was a leap year. A leap year is a year with 366 days; generally, a year is a
leap year if:
 its value is divisible by 4 (e.g. 1996 and 2008 are leap years) unless
 it is also divisible by 100 (e.g. 1900 is NOT a leap year) unless
 it is also divisible by 400 (e.g. 2000 is a leap year)
7. Write a program that uses a loop to find and print the sum and average of 50
randomly generated numbers between 1 and 50.
Computer Science I
Sheller
cs1s07sampEx2 - work
Spring 2007
Page 4
Download