wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

CS API Test

Total questions: 27

Worksheet time: 14mins

Name
Class
Date
1.
In the code segment below, assume that the int variable n has been properly declared and initialized. The code segment is intended to print a value that is 1 more than twice the value of n. /* missing code */ System.out.print(result); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? I. int result = 2 * n; result = result + 1; II. int result = n + 1; result = result * 2; III. int result = (n + 1) * 2;
a)
I only
b)
II only
c)
III only
d)
I and III
e)
II and III
2.
Consider the following code segment. int a = 5; int b = 8; int c = 3; System.out.println(a + b / c * 2); What is printed as a result of executing this code?
a)
2
b)
6
c)
8
d)
9
e)
14
3.
In the code segment below, assume that the int variables a and b have been properly declared and initialized. int c = a; int d = b; c += 3; d--; double num = c; num /= d; Which of the following best describes the behavior of the code segment?
a)
The code segment stores the value of (a + 3) / b in the variable num.
b)
The code segment stores the value of (a + 3) / (b - 1) in the variable num.
c)
The code segment stores the value of (a + 3) / (b - 2) in the variable num.
d)
The code segment stores the value of (a + 3) / (1 - b) in the variable num. The code segment causes a runtime error in the last line of code because num is type double and d is
e)
type int.
4.
Consider the following code segment, which is intended to find the average of two positive integers, x and y. int x; int y; int sum = x + y; double average = (double) (sum / 2); Which of the following best describes the error, if any, in the code segment?
a)
There is no error, and the code works as intended. In the expression (double) (sum / 2), the cast to double is applied too late, so the average will
b)
be less than the expected result for even values of sum. In the expression (double) (sum / 2), the cast to double is applied too late, so the average will
c)
be greater than the expected result for even values of sum. In the expression (double) (sum / 2), the cast to double is applied too late, so the average will
d)
be less than the expected result for odd values of sum. In the expression (double) (sum / 2), the cast to double is applied too late, so the average will
e)
be greater than the expected result for odd values of sum.
5.
Consider the following code segment. double num = 9 / 4; System.out.print(num); System.out.print(" "); System.out.print((int) num); What is printed as a result of executing the code segment?
a)
2 2
b)
2.0 2
c)
2.0 2.0
d)
2.25 2
e)
2.25 2.0
6.
Consider the following code segment. double x = (int) (5.5 - 2.5); double y = (int) 5.5 - 2.5; System.out.println(x - y); What is printed as a result of executing the code segment?
a)
-1
b)
-0.5
c)
0
d)
0.5
e)
1
7.
Consider the following code segment. int w = 1; int x = w / 2; double y = 3; int z = (int) (x + y); Which of the following best describes the results of compiling the code segment?
a)
The code segment compiles without error. The code segment does not compile, because the int variable x cannot be assigned the result of the
b)
operation w / 2. The code segment does not compile, because the integer value 3 cannot be assigned to the double
c)
variable y. The code segment does not compile, because the operands of the addition operator cannot be of different
d)
types int and double. The code segment does not compile because the result of the addition operation is of type double and
e)
cannot be cast to an int.
8.
Consider the following code segment. double x = 4.5; int y = (int) x * 2; System.out.print(y); What is printed as a result of executing the code segment?
a)
8
b)
8
c)
9
d)
9
e)
10
9.
The code segment below is intended to calculate the circumference c of a circle with the diameter d of 1.5. The circumference of a circle is equal to its diameter times pi. /* missing declarations */ c = pi * d; Which of the following variable declarations are most appropriate to replace /* missing declarations */ in this code segment? int pi = 3.14159;
a)
int d = 1.5; final int c; final int pi = 3.14159;
b)
int d = 1.5; int c; final double pi = 3.14159;
c)
double d = 1.5; double c; double pi = 3.14159;
d)
double d = 1.5; final double c = 0.0; final double pi = 3.14159;
e)
final double d = 1.5; final double c = 0.0;
10.
SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. Assume that the classes listed in the Java Quick Reference have been imported where appropriate. Unless otherwise noted in the question, assume that parameters in method calls are not nulland that methods are called only when their preconditions are satisfied. In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit. This question involves computing factorials and using factorials to compute the number of possible ways that items can be selected from a group of choices. You will write two methods in the Combinatoricsclass that follows. public class Combinatorics { /** Precondition: n is between 1 and 12, inclusive. * Returns the factorial of n, as described in part (a). */ public static int factorial(int n) { /* to be implemented in part (a) */ } /** Precondition: n and r are between 1 and 12, inclusive. * Determines the number of ways r items can be selected * from n choices and prints the result, as described in part (b). */ public static void numCombinations(int n, int r) { /* to be implemented in part (b) */ } } (a) In mathematics, the factorial of a positive integer n, denoted as n! , is the product of all positive integers less than or equal to n. The factorial of ncan be computed using the following rules. Case I: If nis 1, then the factorial of nis 1. Case II: If nis greater than 1, then the factorial of nis equal to ntimes the factorial of (n - 1). The factorialmethod returns the factorial of n, as determined by case I and case II. Write the factorialmethod below. You are encouraged to implement this method recursively. /** Precondition: n is between 1 and 12, inclusive. * Returns the factorial of n, as described in part (a). */ public static int factorial(int n) Please respond on separate paper, following directions from your teacher. (b) A combination is a selection of items from a group of choices when the order that the items are selected does not matter. For example, if there are four available choices (A, B, C, and D), there are six different ways that two items can be selected (A and B, A and C, A and D, B and C, B and D, C and D). The number of possible combinations of items from a group of choices can be calculated according to the following rules. If is greater than , then the number of possible combinations is . If is not greater than , then the number of possible combinations is equal to . The numCombinationsmethod is intended to calculate the number of possible combinations of ritems from a group of nchoices and print the result. Examples of the intended behavior of the method are shown in the table. Write the numCombinationsmethod below. Assume that factorialworks as specified, regardless of what you wrote in part (a). You must use factorialappropriately to receive full credit. /** Precondition: n and r are between 1 and 12, inclusive. * Determines the number of ways r items can be selected * from n choices and prints the result, as described in part (b). */ public static void numCombinations(int n, int r) Please respond on separate paper, following directions from your teacher.
4 lines
11.
Consider the following code segment. int a = 5; int b = 4; int c = 2; a *= 3; b += a; b /= c; System.out.print(b); What is printed when the code segment is executed?
a)
2
b)
4
c)
9
d)
9.5
e)
19
12.
Consider the following code segment. int x = 5; int y = 6; /* missing code */ z = (x + y) / 2; Which of the following can be used to replace /* missing code */ so that the code segment will compile? I. int z = 0; II. int z; III. boolean z = false;
a)
I only
b)
II only
c)
I and II only
d)
II and III only
e)
I, II, and III
13.
A code segment (not shown) is intended to determine the number of players whose average score in a game exceeds 0.5. A player’s average score is stored in avgScore, and the number of players who meet the criterion is stored in the variable count. Which of the following pairs of declarations is most appropriate for the code segment described? double avgScore;
a)
boolean count; double avgScore;
b)
double count; double avgScore;
c)
int count; int avgScore;
d)
boolean count; int avgScore;
e)
int count;
14.
Consider the following code segment. System.out.print("Hello System.out.println"); System.out.print("!!!"); What is printed as a result of executing the code segment?
a)
Hello!!!
b)
Hello System.out.println!!! Hello
c)
!!! Hello System.out.println
d)
!!!
e)
Nothing is printed because the text "System.out.println" cannot appear inside a print statement.
15.
The following code segment is intended to interchange the values of the int variables x and y. Assume that x and y have been properly declared and initialized. int temp = x; /* missing code */ Which of the following can be used to replace /* missing code */ so that the code segment works as intended? x = y;
a)
x = temp; x = y;
b)
y = temp; y = x;
c)
x = temp; y = x;
d)
temp = y; y = x;
e)
temp = x;
16.
Consider the following code segment. num += num; num *= num; Assume that num has been previously declared and initialized to contain an integer value. Which of the following best describes the behavior of the code segment?
a)
The value of num is two times its original value.
b)
The value of num is the square its original value.
c)
The value of num is two times the square of its original value.
d)
The value of num is the square of twice its original value.
e)
It cannot be determined without knowing the initial value of num.
17.
Consider the following code segment, which is intended to print the digits of the two-digit int number num in reverse order. For example, if num has the value 75, the code segment should print 57. Assume that num has been properly declared and initialized. /* missing code */ System.out.print(onesDigit); System.out.print(tensDigit); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? int onesDigit = num % 10;
a)
int tensDigit = num / 10; int onesDigit = num / 10;
b)
int tensDigit = num % 10; int onesDigit = 10 / num;
c)
int tensDigit = 10 % num; int onesDigit = num % 100;
d)
int tensDigit = num / 100; int onesDigit = num / 100;
e)
int tensDigit = num % 100;
18.
Which of the following expressions evaluate to 7 ? I. 9 + 10 % 12 II. (9 + 10) % 12 III. 9 – 2 % 12
a)
I only
b)
II only
c)
I and III
d)
II and III
e)
I, II, and III
19.
Consider the following code segment. int x = 5; x += 6 * 2; x -= 3 / 2; What value is stored in x after the code segment executes?
a)
-1.5
b)
1
c)
9
d)
15.5
e)
16
20.
Consider the following code segment, where k and count are properly declared and initialized int variables. k++; k++; count++; k--; count++; k--; Which of the following best describes the behavior of the code segment?
a)
The code segment leaves both k and count unchanged.
b)
The code segment increases both k and count by 2.
c)
The code segment increases k by 4 and count by 2.
d)
The code segment leaves k unchanged and increases count by 2.
e)
The code segment increases k by 2 and leaves count unchanged.
21.
Consider the following code segment. int a = 4; int b = 5; a++; b++; int c = a + b; a -= 1; System.out.println(a + c); What is printed when the code segment is executed?
a)
9
b)
10
c)
14
d)
15
e)
25
22.
Consider the following code segment. System.out.print("AP"); System.out.println(); System.out.println("CS"); System.out.print("A"); What is printed as a result of executing the code segment?
a)
APCSA APCS
b)
A AP
c)
CSA AP
d)
CS A AP
e)
CS A
23.
Consider the following code segment. System.out.print(I do not fear computers. ); // Line 1 System.out.println(I fear the lack of them.); // Line 2 System.out.println(--Isaac Asimov); // Line 3 The code segment is intended to produce the following output but may not work as intended. I do not fear computers. I fear the lack of them. --Isaac Asimov Which change, if any, can be made so that the code segment produces the intended output?
a)
In line 1, print should be changed to println.
b)
In lines 2 and 3, println should be changed to print.
c)
The statement System.out.println() should be inserted between lines 2 and 3.
d)
In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks.
e)
No change is needed; the code segment works correctly as is.
24.
Consider the following code segment. System.out.print(*); // Line 1 System.out.print("*"); // Line 2 System.out.println(); // Line 3 System.out.println("*"); // Line 4 The code segment is intended to produce the following output, but may not work as intended. ** * Which line of code, if any, causes an error?
a)
Line 1
b)
Line 2
c)
Line 3
d)
Line 4
e)
The code segment works as intended.
25.
Consider the following code segment. System.out.print("*"); System.out.println("**"); System.out.println("***"); System.out.print("****"); What is printed as a result of executing the code segment? * **
a)
*** **** *
b)
** ******* *
c)
***** **** ***
d)
*** **** ***
e)
*******
26.
Consider the following code segment. System.out.print("One"); // Line 1 System.out.print("Two"); // Line 2 System.out.print("Three"); // Line 3 System.out.print("Four"); // Line 4 The code segment is intended to produce the following output, but does not work as intended. OneTwo ThreeFour Which of the following changes can be made so that the code segment produces the intended output?
a)
Changing print to println in line 1 only
b)
Changing print to println in line 2 only
c)
Changing print to println in line 3 only
d)
Changing print to println in lines 2 and 3 only
e)
Changing print to println in lines 1, 2, 3, and 4
27.
The following code segment is intended to round val to the nearest integer and print the result. double val = -0.7; int roundedVal = (int) (val + 0.5); System.out.println(roundedVal); Which of the following best describes the behavior of the code segment?
a)
The code segment works as intended. The code segment does not work as intended because val and roundedVal should be declared as the
b)
same data type. The code segment does not work as intended because the expression (val + 0.5) should be cast to a
c)
double instead of an int. The code segment does not work as intended because val should be cast to an int before 0.5 is
d)
added to it. The code segment does not work as intended because the expression (int) (val + 0.5) rounds to
e)
the nearest integer only when val is positive.