0% found this document useful (0 votes)
7 views

AP CS Java Basics MCQs Answers

The document contains a series of multiple-choice questions and answers related to programming concepts, particularly in Java. Each question is followed by an explanation of the correct answer, detailing the reasoning behind it. The content focuses on topics such as type casting, boolean expressions, arithmetic operations, and algorithm behavior.

Uploaded by

Shreyas Parekh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

AP CS Java Basics MCQs Answers

The document contains a series of multiple-choice questions and answers related to programming concepts, particularly in Java. Each question is followed by an explanation of the correct answer, detailing the reasoning behind it. The content focuses on topics such as type casting, boolean expressions, arithmetic operations, and algorithm behavior.

Uploaded by

Shreyas Parekh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

I.

II.

III.

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

Answer Key
1. B
2. E
3. D
4. E
5. E
6. C
7. B
8. D
9. D
10. A
11. C
12. D
13. A
14. C
15. A
16. D
17. D
18. B
19. C
20. C
21. B
22. E
23. D
24. D

Answer Explanations
1. (B) When x is converted to an integer, as in segment I, information is
lost. Java requires that an explicit cast to an int be made, as in
segment II. Note that segment II will cause x to be truncated: The
value stored in y is 14. By requiring the explicit cast, Java doesn’t let
you do this accidentally. In segment III, y will contain the value 14.0.
No explicit cast to a double is required since no information is lost.
2. (E) The string argument contains two escape sequences: ‘\\’, which
means print a backslash (\), and ‘\n’, which means go to a new line.
Choice E is the only choice that does both of these.
3. (D) Short-circuit evaluation of the boolean expression will occur. The
expression (n != 0) will evaluate to false, which makes the entire
boolean expression false. Therefore the expression (x / n > 100)
will not be evaluated. Hence no division by zero will occur, which
would have caused an ArithmeticException to be thrown. When the
boolean expression has a value of false, only the else part of the
statement, statement2, will be executed.
4. (E) For this choice, the integer division 13/5 will be evaluated to 2,
which will then be cast to 2.0. The output will be 13/5 = 2.0. The
compiler needs a way to recognize that real-valued division is
required. All the other options provide a way.
5. (E) The operators *, /, and % have equal precedence, all higher than
-, and must be performed first, from left to right.

6. (C) The expression must be evaluated as if parenthesized like this:


(2 + 3) * 12 / (7 - 4 + 8)

This becomes 5 * 12 / 11 = 60 / 11 = 5.

7. (B) Although the expression is always algebraically true for nonzero


x, the expression may evaluate to false. This could occur because of
round-off error in performing the division and multiplication
operations. Whether the right-hand side of the expression evaluates to
exactly 3.0 depends on the value of x. Note that if x is zero, the
expression will be evaluated to false because the right-hand side will
be assigned a value of Infinity.
8. (D) Any time arithmetic operations are done with floating-point
numbers, round-off error may occur. The Math class methods (see p.
182) such as pow and sqrt use various approximations to generate
their answers to the required accuracy. Since they do different
internal arithmetic, however, the round-off will usually not result in
exactly the same answers. Note that choice A is not correct because
both Math.pow and Math.sqrt return type double. Choice B is wrong
because no matter how x was previously calculated, the same x is
input to pow and sqrt. Choice C is wrong since round-off error occurs
no matter how many bits are used to represent numbers. Choice E is
wrong because if x is representable on the machine (i.e., hasn’t
overflowed), then its square root, x1/2, will not overflow.
9. (D) Each else gets paired with the nearest unpaired if. Thus when
the test (22 % 5 == 0) fails, the else part indicating that 22 is
negative will be executed. This is clearly not the intent of the
fragment, which can be fixed using delimiters:

10. (A) Since the first test (x >= 0) is true, the matching else part, y =
-x, will not be executed. Since (x <= 100) is true, the matching
else part, y = x * 2, will not be executed. The variable y will be set
to x * 3 (i.e., 90) and will now fail the test y < 50. Thus, x will
never be altered in this algorithm. Final values are x = 30 and y =
90.

11. (C) In order for !(A || B || C) to be true, (A || B || C) must


evaluate to false. This will happen only if A, B, and C are all false.
Choice A evaluates to true when A and B are false and C is true. In
choice B, if any one of A, B, or C is false, the boolean expression
evaluates to true. In choice D, if any one of A, B, or C is false, the
boolean expression evaluates to true since we have !(false). All
that’s required for choice E to evaluate to true is for A to be false.
Since true||(any) evaluates to true, both B and C can be either true
or false.
12. (D) To evaluate to true, the expression must reduce to true &&
true. We therefore need !(false) && true. Choice D is the only
condition that guarantees this: a > b provides !(false) for the left-
hand expression, and a > b and b > 0 implies both a and b positive,
which leads to true for the right-hand expression. Choice E, for
example, will provide true for the right-hand expression only if a <
0. You have no information about a and can’t make assumptions
about it.
13. (A) If (c < a)is false, ((c == a*b) && (c < a)) evaluates to
false irrespective of the value of c == a*b. In this case, !(c ==
a*b && c < a) evaluates to true. Then (a < b) || true evaluates
to true irrespective of the value of the test (a < b). In all the other
choices, the given expression may be true. There is not enough
information given to guarantee this, however.
14. (C) If a == b is false, then a != b is true. Thus, the second piece of
the compound test must be evaluated before the value of the whole
test is known. Since a == b is false, a - b is not equal to zero. Thus,
there is no division by zero, and no exception will be thrown. Also,
since the relative values of a, b, and n are unknown, the value of the
test n / (a - b) > 90 is unknown, and there is insufficient
information to determine whether the compound test is true or false.
Thus, either /* statement 1 */ or /* statement 2 */ will be executed.
15. (A) If n ≥ 1, both segments will print out the integers from 1 through
n. If n ≤ 0, both segments will fail the test immediately and do
nothing.
16. (D) The (value != SENTINEL) test occurs before a value has been
read from the list. This will cause 0 to be processed, which may
cause an error. The code must be fixed by reading the first value
before doing the test:
17. (D) Here is a trace of the values of x and y during execution. Note
that the condition (y % x == 1) is never true in this example.

The while loop terminates when x is 4 since the test while (x > 5)
fails.
18. (B) The algorithm finds the remainder when the sum of the first three
digits of n is divided by 7. If this remainder is equal to the fourth
digit, checkDigit, the method returns true, otherwise false. Note
that (6+1+4) % 7 equals 4. Thus, only choice B is a valid number.
19. (C) As n gets broken down into its digits, nRemaining is the part of n
that remains after each digit is stripped off. Thus, nRemaining is a
self-documenting name that helps describe what is happening.
Choice A is false because every digit can be stripped off using some
sequence of integer division and mod. Choice B is false because num
is passed by value and therefore will not be altered when the method
is exited (see p. 111). Eliminate choice D: When the method is
exited, all local variables are destroyed. Choice E is nonsense.
20. (C) Segment III works because if you enter an age of 90, say,
category will correctly be assigned "Senior", and none of the other
else pieces of code will be executed. Similarly, if you enter an age
corresponding to an adult or a child, only the correct assignment is
made. Segment I fails because if you enter an age of 90, category
will be assigned "Senior", but then will be changed to "Adult"
when the age passes the second test. Segment II uses incorrect
syntax. The segment will work if you change the second test to
if (age >= 18 && age <= 64)

21. (B) The outer loop produces five rows of output. Each pass through
the inner loop goes from i down to 1. Thus five odd numbers
starting at 9 are printed in the first row, four odd numbers starting at
7 in the second row, and so on. You can picture what is happening in
a chart.

i j Steps
5 5
4 Print 5 odd numbers starting at
3 9:
2 9 7 5 3 1
1

4 4 Print 4 odd numbers starting at


3 7:
2 7 5 3 1
1 Stop here! Answermust be B!
3 3
.
.
.

22. (E) All three algorithms produce the given output. The outer for
(int i …) loop produces six rows, and the inner for (int k …)
loops produce the symbols in each row.
23. (D) Statement I is false, since if 100 ≤ num ≤ 109, the body of the
while loop will be executed just once. (After this single pass through
the loop, the value of num will be 10, and the test if (num > 10) will
fail.) With just one pass, newNum will be a one-digit number, equal to
temp (which was the original num % 10). Note that statement II is
true: There cannot be an infinite loop since num /= 10 guarantees
termination of the loop. Statement III is true because if num ≤ 10,
the loop will be skipped, and newNum will keep its original value of 0.
24. (D) The algorithm works by stripping off the rightmost digit of n
(stored in rem), multiplying the current value of revNum by 10, and
adding that rightmost digit. When n has been stripped down to no
digits (i.e., n == 0 is true), revNum is complete. Both segments II
and III work. Segment I fails to produce the right output whenever
the input value n has a first digit less than (number of digits − 1). For
these cases, the output has the first digit of the original number
missing from the end of the returned number.

You might also like