Solutions to Midterm Exam

advertisement
Midterm Exam 10/18/05
Name ___________________
For multiple choice/true-false questions circle exactly one correct answer. Except
for the three questions marked otherwise, all questions are worth 3 points.
1. What command is used to create an object?
_______new___________
2. Match the results of the following computations:
39 % 4 __4__
1. 9.75
39 / 4 __5__
2. 0
39.0 / 4 __1__
3. 4
4 % 39 __3__
4. 3
45 % 5 __2__
5. 9
3. Constants in Java are declared with the modifier ______final__________ .
4. The showInputDialog method returns a String object.
T*
F
5. It is necessary to declare an object before you use it.
T*
F
6. An argument is a value we pass to a method, and a parameter is
a placeholder for arguments.
T*
F
7. Which one of the following code fragments adds the even numbers between 0 and 21?
A. int sum = 0, num = 0;
while (num <= 20) {
sum = sum + num;
num++;
}
C. int sum = 0, num = 0;
while (num < 20) {
sum += num;
num += 2;
}
B*. int sum = 0, num = 0;
while (num < 21) {
sum += num;
num += 2;
}
D. int sum = 0, num = 0;
while (num <= 20) {
sum += num;
num++;
}
8. How many times will the following loop execute?
for (int j = 0; j <= 25; j += 2)
A.
B.
C.
D.
j++;
9*
10
11
None of the above
9. (7) Write a for loop that prints the numbers between 100 and 0, inclusive, to the screen
decreasing by 5 each time (100, 95, 90, etc.).
for (int i = 100; i >= 0; i -= 5)
System.out.println(i);
10. (8) Write a for loop that goes from x = 0 to 256 by 4 (0, 4, 8, etc.) and keeps track of
how many times x is evenly divisible by 8, and prints out the result.
int count = 0;
for (int x = 0; x <= 256; x += 4)
if (x % 8 == 0) count++;
System.out.println(count);
11. Which of the following is a valid Java statement that will generate a random integer
between 1 and 35 (inclusive)?
A.
B.
C.
D.
int num = Math.random(17);
*int num = (int) Math.floor(Math.random() * 35) + 1;
int num = Math.random(0, 35);
int num = (int) Math.ceil(Math.random() * 35) + 1;
12. What is the output after executing the following code?
int sum = 0; count = 1;
while (count < 10) {
sum += count;
count++;
}
System.out.println(sum);
A.
B.
C.
D.
43
44
45*
None of the above
13. Which of the following loops does not terminate?
A.
B.
C.
D.
*for (int k = 0; k < 100; k = 10) { . . . }
for (int k = 10; k != 1000; k *= 10) { . . . }
for (int k = 100; k > 100; k -= 10) { . . . }
for (int k = 0; k < 10; k += 1) { . . . }
14. Choose the correct statement to complete a code fragment that asks the user to input
a number from 1 to 100 (inclusive), and returns that numbers square root. If the
number is not in the specified range, the code prints out an error message.
int num = Integet.parseInt(JOptionPane.showInputDialog(null,
"Enter a number between 1 and 100")
if ( ____________ ) {
JOptionPane.showMessageDialog(null, "Invalid data");
2
else
A.
B.
C.
D.
JOptionPane.showMessageDialog(null, "Sqrt of num = " +
Math.sqrt(num));
1 <= num <= 100
num < 1 && num < 100
num < 1 || num > 100*
num < 1 && num > 100
15. List the Boolean operators for
A. and ___&&_____
B. not ___!_____
C. or
___||_____
16. (10) Using the Scanner class write a code fragment that asks the user to input his or
her age. If the age is under 16, the program should respond that the user is not old
enough to drive. If the user is at least 16 but not 18, the program will respond that the
user is old enough to drive, but not old enough to vote. If the age is at least 18, the
program should respond that the user is old enough to drive and to vote. If the age is
older than 65, the response should be that the user is old enough to be on Medicare.
Scanner scan = new Scanner (System.in);
int age;
System.out.print("Enter your age: ");
age = scan.nextInt();
if (age < 16)
System.out.println("Not old enough to drive.");
else
if (age < 18)
System.out.println("Old enough to drive, not to vote.");
else {
System.out.println("Old enough to drive and to vote.");
if (age > 65)
System.out.println("Old enough to be on Medicare.");
}
17. Evaluate the following Boolean expressions (circle T or F):
A. 1 < 2 || 4 <= 3
B. !((2 + 7) == (3 + 6)) && 4 < 6
C. 8 % 12 * 4 > 9 * 8 / 5 && !(15 + 19 == 8 * 15 + 6)
T
T*
T*
F*
F
F
18. What is the output from the following switch statement.
A. Otis
int number = 45;
switch (number) {
case 35: System.out.println("Otis");
case 45: System.out.println("Good");
case 55: System.out.println("Dog");
}
B. Good
C. Dog
3
D.* Good
Dog
E.
Otis
Good
Dog
19. A class must have a main method (T or F)?
T
F*
20. A private variable can only be used by methods of the same class (T or F)?
T*
F
21. Define a class constant MAX_VALUE.
A.
B.
C.
D.
private final int MAX_VALUE = 100;
private static int MAX_VALUE = 100;
*private static final int MAX_VALUE = 100;
None of the above
22. Consider the following method
public int kissoff(int dump) {
int loser = dump * 8;
return loser;
}
How would you characterize loser?
A.
B.
C.
D.
*A local variable
An argument
A return type
A global variable
23. A Java program must include at least one class that has the main method.
T*
F
24. Arguments and parameters must have the same name.
T
F*
25. The data type of a value returned from a method must be assignment-compatible
with the declared return type of the method.
T*
F
26. The following method in the class, TissueBox:
public TissueBox(){
}
is _____________.
A.
B.
C.
D.
*a constructor
a null method
a mutator
None of the above
27. The practice of writing multiple constructors to handle different sets of inputs is
known as _____________.
A. inheritance
B. polyinputting
4
C. *overloading
D. multiplicity
28. Assume we wish to include a class constant to be shared by all objects of a specific
class type, as in:
private final double temp = 32.0;
What is wrong with this?
A.
B.
C.
D.
E.
There is nothing wrong with this.
It should be declared public
temp is a reserved word
*It is missing the keyword, static.
temp must be capitalized
5
Download