Intro Java Language Concepts MC
Intro Java Language Concepts MC
(A) None
(B) I only
(C) II only
(D) III only
(E) I and III only
If n is of type int and has a value of 0 when the segment is executed, what will
happen?
(A) An ArithmeticException will be thrown.
(B) A syntax error will occur.
(C) statement1, but not statement2, will be executed.
(D) statement2, but not statement1, will be executed.
(E) Neither statement1 nor statement2 will be executed; control will pass to the
first statement following the if statement.
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 78 — #92
✐ ✐
double answer = 13 / 5;
System.out.println("13 / 5 = " + answer);
The output is
13 / 5 = 2.0
13 / 5 = 2.6
Which of the following replacements for the first line of code will not fix the
problem?
(A) double answer = (double) 13 / 5;
(B) double answer = 13 / (double) 5;
(C) double answer = 13.0 / 5;
(D) double answer = 13 / 5.0;
(E) double answer = (double) (13 / 5);
int result = 13 - 3 * 6 / 4 % 3;
(A) −5
(B) 0
(C) 13
(D) −1
(E) 12
6. Suppose that addition and subtraction had higher precedence than multiplication
and division. Then the expression
2 + 3 * 12 / 7 - 4 + 8
3.0 == x * (3.0 / x)
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 79 — #93
✐ ✐
9. What will the output be for the following poorly formatted program segment, if
the input value for num is 22?
(A) 22
(B) 4
(C) 2 is negative
(D) 22 is negative
(E) Nothing will be output.
10. What values are stored in x and y after execution of the following program seg-
ment?
(A) x = 30 y = 90
(B) x = 30 y = -30
(C) x = 30 y = 60
(D) x = 3 y = -3
(E) x = 30 y = 40
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 80 — #94
✐ ✐
11. Which of the following will evaluate to true only if boolean expressions A, B, and
C are all false?
(A) !A && !(B && !C)
(B) !A || !B || !C
(C) !(A || B || C)
(D) !(A && B && C)
(E) !A || !(B || !C)
13. Given that a, b, and c are integers, consider the boolean expression
14. In the following code segment, you may assume that a, b, and n are all type int.
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 81 — #95
✐ ✐
15. Given that n and count are both of type int, which statement is true about the
following code segments?
I for (count = 1; count <= n; count++)
System.out.println(count);
II count = 1;
while (count <= n)
{
System.out.println(count);
count++;
}
16. The following fragment intends that a user will enter a list of positive integers at
the keyboard and terminate the list with a sentinel:
int value = 0;
final int SENTINEL = -999;
while (value != SENTINEL)
{
//code to process value
...
value = IO.readInt(); //read user input
}
17. Suppose that base-2 (binary) numbers and base-16 (hexadecimal) numbers can be
denoted with subscripts, as shown below:
2Ahex = 101010bin
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 82 — #96
✐ ✐
18. A common use of hexadecimal numerals is to specify colors on web pages. Ev-
ery color has a red, green, and blue component. In decimal notation, these are
denoted with an ordered triple (x, y, z), where x, y, and z are the three compo-
nents, each an int from 0 to 255. For example, a certain shade of red, whose red,
green, and blue components are 238, 9, and 63, is represented as (238, 9, 63).
In hexadecimal, a color is represented in the format #RRGGBB, where RR,
GG, and BB are hex values for the red, green, and blue. Using this notation, the
color (238, 9, 63) would be coded as #EE093F.
Which of the following hex codes represents the color (14, 20, 255)?
(A) #1418FE
(B) #0E20FE
(C) #0E14FF
(D) #0FE5FE
(E) #0D14FF
19. In Java, a variable of type int is represented internally as a 32-bit signed integer.
Suppose that one bit stores the sign, and the other 31 bits store the magnitude of
the number in base 2. In this scheme, what is the largest value that can be stored
as type int?
(A) 232
(B) 232 − 1
(C) 231
(D) 231 − 1
(E) 230
int x = 10, y = 0;
while (x > 5)
{
y = 3;
while (y < x)
{
y *= 2;
if (y % x == 1)
y += x;
}
x -= 3;
}
System.out.println(x + " " + y);
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 83 — #97
✐ ✐
Questions 21 and 22 refer to the following method, checkNumber, which checks the
validity of its four-digit integer parameter.
21. Which of the following values of num will result in valid having a value of true?
(A) 6143
(B) 6144
(C) 6145
(D) 6146
(E) 6147
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 84 — #98
✐ ✐
23. What output will be produced by this code segment? (Ignore spacing.)
(A) 9 7 5 3 1
9 7 5 3
9 7 5
9 7
9
(B) 9 7 5 3 1
7 5 3 1
5 3 1
3 1
1
(C) 9 7 5 3 1
7 5 3 1 -1
5 3 1 -1 -3
3 1 -1 -3 -5
1 -1 -3 -5 -7
(D) 1
1 3
1 3 5
1 3 5 7
1 3 5 7 9
(E) 1 3 5 7 9
1 3 5 7
1 3 5
1 3
1
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 85 — #99
✐ ✐
24. Which of the following program fragments will produce this output? (Ignore
spacing.)
2 - - - - -
- 4 - - - -
- - 6 - - -
- - - 8 - -
- - - - 10 -
- - - - - 12
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 86 — #100
✐ ✐
✐ ✐
✐ ✐
✐ ✐
“ap” — 2014/11/4 — 11:10 — page 87 — #101
✐ ✐
/* code segment */
return revNum;
}
Which of the following replacements for /* code segment */ would cause the
method to work as intended?
I for (int i = 0; i <= n; i++)
{
rem = n % 10;
revNum = revNum * 10 + rem;
n /= 10;
}
II while (n != 0)
{
rem = n % 10;
revNum = revNum * 10 + rem;
n /= 10;
}
III for (int i = n; i != 0; i /= 10)
{
rem = i % 10;
revNum = revNum * 10 + rem;
}
(A) I only
(B) II only
(C) I and II only
(D) II and III only
(E) I and III only
✐ ✐
✐ ✐