You are on page 1of 5

1. Assume that x and y are boolean variables and have been properly initialized.

(x && y) || (!(x && y))

The result of evaluating the expression above is best described as

(A) always true


(B) always false
(C) true only when x is true and y is true
(D) true only when x and y have the same value
(E) true only when x and y have different values

2. At a certain high school students receive letter grades based on the following scale.
Integer Score Letter Grade
93 or above A
From 84 to 92 inclusive B
From 75 to 83 inclusive C
Below 75 F

Which of the following code segments will assign the correct string grade for a given integer score?

I. if (score >= 93)


grade = "A";
if (score >= 84 && score <= 92)
grade = "B";
if (score >= 75 && score <= 83)
grade = "C";
if (score < 75)
grade = "F";
II. if (score >= 93)
grade = "A"; (A) II only
(B) III only
if (84 <= score <= 92)
(C) I and II only
grade = "B"; (D) I and III only
if (75 <= score <= 83) (E) I, II and III

grade = "C";
if (score < 75)
grade = "F";
III. if (score >= 93)
grade = "A";
else if (score >= 84)
grade = "B";
else if (score >= 75)
grade = "C";
else
grade = "F";
3. Consider the following code segment
int x = //some number greater than 0
int n = 100;
if(x < 1000)
{
if (x > 1000)
{ n = 200; }
else
{ n = 300; }
}
else
{
if ( x < 1000)
{ n = 400; }
else
{ n = 300; }
}
System.out.println(n);

What is printed as a result of executing the code segment?

4. What is printed as a result of executing the following statement?

System.out.println(15 / 2 * 4.0 + 15.0 / 2.0 + "!");


A.
B. 35.0!
C. 37.5!
D. 35.5!
E. 28.0 7.5!
F. The statement does not compile.

5. Assume that a and b are variables of type int. The expression

(a < b) && !(a < b)

is equivalent to which of the following?

(A) true
(B) false
(C) a == b
(D) a != b
(E) !(a < b) && (a > b)
6. Consider the following code segment which uses properly defined and initialized int variables x and y.

String result = "";


if (x < 5)
{
if (y > 0)
{ result += "a"; }
else
{ result += "b"; }
}
else if (x > 10)
{
if (y < 0)
{ result += "c"; }
else if (y < 10)
{ result += "d"; }
result += "e";
}
result += "f";

What is the value of result after the code segment is executed if x has the value 15 and y has the value 5 ?

(A) Ad
(B) Adf
(C) d
(D) def
(E) ef

7. Consider the following method.

public static double compute(int x, int y)


{
double r = 0;
if(y == 0 || x / y <= 2)
{
r = 1 / (x * y – 2);
}

return r;
}

Which of the following combinations of x and y will cause this code segment to produce an error message?
I. x = 0, y = 0
II. x = 1, y = 2
III. x = 10, y = 2

(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III

if(num = 13
8. Write the method fixTeen which takes 1 integer parameter and returns its fixTeen value. For a fixTeen value,
all “teens” (13 – 19) count as 0 excepts for 15 or 16 which count double their original value (30 and 32 respectively.
-19)
All non-teen numbers count as their original value.
For example:
if(num =
fixTeen(10) returns 10
fixTeen(35) returns 35
15||16)
fixTeen(13) returns 0
fixTeen(15) returns 30
{
public static
{
Return
int fixTeen(int num)

num*2
}
else
{
return
0
9. Write the method noTeenSum which takes 3 int values and returns their fixTeen sum.
For Example
} 2, 3) returns 6
noTeenSum(1, 2, 3) returns 6
noTeenSum(1,
else1, 16) returns 35
noTeenSum(2, 13, 1) returns 3
noTeenSum(2,

You MUST{ usereturn


the method you num
created in question 8 as part of your answer. You can assume the method
works as}intended regardless of what you wrote for question 8.

public static int noTeenSum(int a, int b, int c)


{

10. Write the method blackjack, given 2 int values greater than 0, return whichever value is nearest to 21
without going over. Return 0 if they both go over.
For Example
blackjack(19, 21) returns 21
blackjack(21, 19) returns 21
blackjack(19, 22) returns 19
blackjack(23, 22) returns 0

public static int blackjack(int a, int b)


{

You might also like