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

AP CSA Unit 1 Primitive Types

The document contains a series of multiple-choice questions focused on introductory Java programming concepts, including primitive types, boolean expressions, and error handling. It also includes review questions and explanations for answers related to code execution and output. The questions aim to assess understanding of Java syntax, operations, and programming logic.

Uploaded by

kaavya.majumder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

AP CSA Unit 1 Primitive Types

The document contains a series of multiple-choice questions focused on introductory Java programming concepts, including primitive types, boolean expressions, and error handling. It also includes review questions and explanations for answers related to code execution and output. The questions aim to assess understanding of Java syntax, operations, and programming logic.

Uploaded by

kaavya.majumder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Unit 1: Primitive Types

Note: Please retain the original question numbers as it is in your answer sheet.

Multiple-Choice Questions on Introductory Java Language


Features

1. Which of the following pairs of declarations will cause an error


message?
I.

II.

III.

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

2. What output will be produced by the following?


System.out.print("\\* This is not\n a comment *\\");

(A) * This is not a comment *


(B) \* This is not a comment *\
(C)

(D)

(E)
3. Consider the following code segment.

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.

4. Refer to the following code fragment.

double answer = 13 / 5;
System.out.println("13 / 5 = " + answer);

The output is
13 / 5 = 2.0

The programmer intends the output to be


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);
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

6. Suppose that addition and subtraction had higher precedence than


multiplication and division. Then the expression
2 + 3 * 12 / 7 - 4 + 8

would evaluate to which of the following?


(A) 11
(B) 12
(C) 5
(D) 9
(E) −4

7. Which is true of the following boolean expression, given that x is a


variable of type double?
3.0 == x * (3.0 / x)

(A) It will always evaluate to false.


(B) It may evaluate to false for some values of x.
(C) It will evaluate to false only when x is zero.
(D) It will evaluate to false only when x is very large or very
close to zero.
(E) It will always evaluate to true.
8. Let x be a variable of type double that is positive. A program
contains the boolean expression (Math.pow(x,0.5) ==
Math.sqrt(x)). Even though x1/2 is mathematically equivalent to
, the above expression returns the value false in a student’s
program. Which of the following is the most likely reason?
(A) Math.pow returns an int, while Math.sqrt returns a double.
(B) x was imprecisely calculated in a previous program
statement.
(C) The computer stores floating-point numbers with 32-bit
words.
(D) There is round-off error in calculating the pow and sqrt
functions.
(E) There is overflow error in calculating the pow function.

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

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)

12. Assume that a and b are integers. The boolean expression


!(a <= b) && (a * b > 0)

will always evaluate to true given that


(A) a = b.
(B) a > b.
(C) a < b.
(D) a > b and b > 0.
(E) a > b and b < 0.

13. Given that a, b, and c are integers, consider the boolean expression
(a < b) || !((c == a * b) && (c < a))

Which of the following will guarantee that the expression is true?


(A) c < a is false.
(B) c < a is true.
(C) a < b is false.
(D) c == a * b is true.
(E) c == a * b is true, and c < a is true.

14. In the following code segment, you may assume that a, b, and n are
all type int.

What will happen if a == b is false?


(A) /* statement 1 */ will be executed.
(B) /* statement 2 */ will be executed.
(C) Either /* statement 1 */ or /* statement 2 */ will be
executed.
(D) A compile-time error will occur.
(E) An exception will be thrown.

15. Given that n and count are both of type int, which statement is true
about the following code segments?
I.

II.

(A) I and II are exactly equivalent for all input values n.


(B) I and II are exactly equivalent for all input values n ≥ 1, but
differ when n ≤ 0.
(C) I and II are exactly equivalent only when n = 0.
(D) I and II are exactly equivalent only when n is even.
(E) I and II are not equivalent for any input values of n.

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.

The fragment is not correct. Which is a true statement?


(A) The sentinel gets processed.
(B) The last nonsentinel value entered in the list fails to get
processed.
(C) A poor choice of SENTINEL value causes the loop to
terminate before all values have been processed.
(D) The code will always process a value that is not on the list.
(E) Entering the SENTINEL value as the first value causes a run-
time error.

17. Consider this code segment.

What will be output after execution of this code segment?


(A)
(B)
(C)
(D)
(E)
Questions 18 and 19 refer to the following method, checkNumber, which
checks the validity of its four-digit integer parameter.

A program invokes method checkNumber with the statement


boolean valid = checkNumber(num);

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

19. What is the purpose of the local variable nRemaining?


(A) It is not possible to separate n into digits without the help of
a temporary variable.
(B) nRemaining prevents the parameter n from being altered.
(C) nRemaining enhances the readability of the algorithm.
(D) On exiting the method, the value of nRemaining may be
reused.
(E) nRemaining is needed as the left-hand side operand for
integer division.

20. For ticket-selling purposes, there are three categories at a certain


theater.

Age Category
65 or above Senior
From 18 to 64 inclusive Adult
Below 18 Child

Which of the following code segments will assign the correct


string to category for a given integer age?
I.

II.
III.

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

21. What output will be produced by this code segment? (Ignore


spacing.)

(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

23. Consider this program segment.


Which is a true statement about the segment?

I. If 100 ≤ num ≤ 1000 initially, the final value of newNum must


be in the range 10 ≤ newNum ≤ 100.
II. There is no initial value of num that will cause an infinite
while loop.
III. If num ≤ 10 initially, newNum will have a final value of 0.

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

24. Consider the method reverse.

Which of the following replacements for /* code segment */


would cause the method to work as intended?
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
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.

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


(A) 0
(B) 1
(C) 2
(D) 4
(E) 5
2. Consider the following code segment.
What is printed as a result of executing the code segment?
(A) 0
(B) 2.5
(C) 6
(D) 12.5
(E) 60
3. Consider the following code segment.

What is the value of z after the code segment is executed?


(A) 4.6
(B) 8.6
(C) 8.8
(D) 7.6
(E) 14.56
4. Consider the task of finding the average score in a game.

Which statement will correctly calculate the average score of all the
games?
5. Consider the code segment.

The segment compiles but has a run-time error. Which error is


generated?
(A) ArithmeticException
(B) DivisionByZeroException
(C) IndexOutOfBoundsException
(D) NullPointerException
(E) TypeMismatchException

Answers and Explanations


Bullets mark each step in the process of arriving at the correct solution.
1. The answer is D.
• The value of var begins at 12.
• The operation var % 7 finds the remainder after var is divided by 7.
Since 12 / 7 is 1 remainder 5, the value of var is now 5.
• var-- means subtract 1 from var, so the value of var is now 4, and
that’s what is printed.
2. The answer is A.
• count * multiplier = 12.5
• When we cast 12.5 to an int by putting (int) in front of it, we
truncate (or cut off) the decimals, so the value we assign to answer
is 12.
• 12 * 5 = 60. 60 % 10 = 0 because 60 / 10 has no remainder.
• So we print 0.
3. The answer is C.
• After the values of the variables are assigned, the first operation that
happens is the multiplication 2.0 * 13, which results in 26.0.
Remember, multiplication and division have higher precedence than
addition (PEMDAS).
• The next operation that happens is 26.0 / 5, which results in 5.2.
CHAPTER 3 REVIEW DRILL
Answers to the review questions can be found in Chapter 13.

1. Consider the following code segment:


1 int a = 10;
2 double b = 10.7;
3 double c = a + b;
4 int d = a + c;
5 System.out.println(c + “ ” + d);

What will be output as a result of executing 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.

2. Consider the following code segment:

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

3. Consider the following code segment.


1 int a = 11;
2 int b = 4;
3 double x = 11;
4 double y = 4;
5 System.out.print(a / b);
6 System.out.print(“, ”);
7 System.out.print(x / y);
8 System.out.print(“, ”);
9 System.out.print(a / y);

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

(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.

4. Consider the following statement:


int i = x % 50;

If x is a positive integer, which of the following could NOT be the


value of i after the statement above executes?

(A) 0
(B) 10
(C) 25
(D) 40
(E) 50

You might also like