AP CSA Unit 1 Primitive Types
AP CSA Unit 1 Primitive Types
Note: Please retain the original question numbers as it is in your answer sheet.
II.
III.
(A) None
(B) I only
(C) II only
(D) III only
(E) I and III only
(D)
(E)
3. Consider the following code segment.
double answer = 13 / 5;
System.out.println("13 / 5 = " + answer);
The output is
13 / 5 = 2.0
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);
5. What value is stored in result if
int result = 13 - 3 * 6 / 4 % 3;
(A) −5
(B) 0
(C) 13
(D) −1
(E) 12
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 segment?
(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
13. Given that a, b, and c are integers, consider the boolean expression
(a < b) || !((c == a * b) && (c < a))
14. In the following code segment, you may assume that a, b, and n are
all type int.
15. Given that n and count are both of type int, which statement is true
about the following code segments?
I.
II.
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.
18. 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
Age Category
65 or above Senior
From 18 to 64 inclusive Adult
Below 18 Child
II.
III.
(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III
(A)
(B)
(C)
(D)
(E)
22. Which of the following program fragments will produce this output?
(Ignore spacing.)
I.
II.
III.
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III
(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III
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
Miscellaneous
• The System.out.println() statement displays information to the
console and moves the cursor to the next line.
• The System.out.print() statement displays information to the console
and does not move the cursor to the next line.
• Programs are documented using comments. There are three different
types of comments: inline, multiple line, and Javadoc.
• There are three main types of errors: compile-time, run-time, and logic.
• Compile-time errors are caused by syntax errors.
• Exception is another name for error.
• There are many types of run-time exceptions and they are based on the
type of error.
• Logic errors are difficult to fix because you have to read the code very
carefully to find out what is going wrong.
• Debugging is the process of removing errors from a program.
• Printing variables to the console screen can help you debug your
program.
Review Questions
1. Consider the following code segment.
Which statement will correctly calculate the average score of all the
games?
5. Consider the code segment.
(A) 20 20
(B) 20.0 30
(C) 20.7 31
(D) 20.7 30.7
(E) Nothing will be printed because of a compile-time error.
1 int a = 10;
2 double b = 10.7;
3 int d = a + b;
Line 3 will not compile in the code segment above. With which of the
following statements could we replace this line so that it compiles?
I. int d = (int) a + b;
II. int d = (int) (a + b);
III. int d = a + (int) b;
(A) I only
(B) II only
(C) III only
(D) I and III only
(E) II and III only
(A) 3, 2.75, 3
(B) 3, 2.75, 2.75
(C) 2, 3, 2
(D) 2, 2.75, 2.75
(E) Nothing will be printed because of a compile-time error.
(A) 0
(B) 10
(C) 25
(D) 40
(E) 50