Sample Test
Sample Test
Name:
First
Last
Student ID # _________________________________
Question #
Points
Available
1
2
3
4
5
6
7
Total:
10
10
10
10
20
10
30
100
Points
Earned
Directions: You have 53 minutes to complete this exam. Write all answers neatly in
the space provided. Please do not separate exam sheets. No additional exam sheets are
provided. Scrap paper is provided per request.
You must write with a pen.
Closed book exam: you are not allowed to use the textbook, lecture notes, cheat sheets
or anything else beside your brain during the exam.
Use well-named variables and include any documentation where you feel it is necessary
to understand your algorithms.
Question 1
a. (1 point) If you attempt to add an int, a byte, a long, and a double, the result will be a
__________ value.
A. double
B. byte
C. long
D. int
b. (1 point) 25 % 1 is _____
A. 3
B. 1
C. 2
D. 0
E. 4
c. (5 points) Multiple answers: To add number to sum, you write (Note: Java is case-sensitive)
A. number += sum;
B. number = sum + number;
C. sum = sum + number;
D. sum += number;
E. sum = Number + sum;
d. (1 point) Note that the Unicode for character 'A' is 65. The expression 'A' + 1 in the statement
System.out.print('A' + 1); evaluates to ________.
A. 66
B. "A1"
C. Illegal expression
D. 'B'
e. (1 point) The "less than or equal to" comparison operator in Java is __________.
A. <<
B. =<
C. <=
D. !=
E. <
Question 2
a (5 points) Which of the Boolean expressions below are incorrect?
A. (true) && (3 => 4)
B. (x != 0) || (x = 0)
C. !(x > 0) && (x > 0)
D. (x > 0) || (x < 0)
E. (-10 < x < 0)
Question 3
a. (4 point) Analyze the following code:
Code 1:
int number = 45;
boolean even;
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
int number = 45;
boolean even = (number % 2 == 0);
A. Code 2 has compile errors.
B. Code 1 has compile errors.
C. Both Code 1 and Code 2 have compile errors.
D. Both Code 1 and Code 2 are correct.
Question 4
a. (5 points) What is the output of the following code?
boolean even = false;
System.out.println((even ? "true" : "false"));
A. false
B. nothing
C. true
D. true false
b. (5 points) What is the value in count after the following loop is executed?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 9);
System.out.println(count);
A. 9
B. 8
C. 11
D. 10
E. 0
1 2
i
m(i ) ...
3 5
2i 1