APCS U2Q1

Ace your homework & exams now with Quizwiz!

22 is negative

What will the output be for the following poorly formatted program segment, if the input value for num is 22? int num = call to a method that reads an integer; if (num > 0) if (num % 5 == 0) System.out.println(num); else System.out.println(num + " is negative"); ANSWERS: 4 2 is negative 22 22 is negative Nothing will be output.

a > b and b > 0

Assume that a and b are integers. The boolean expression !(a <= b) && (a * b > 0) will always evaluate to true given that ANSWERS: a > b and b > 0 a > b and b < 0 a = b a > b a < b

statement 2, but not statement 1, will be executed.

Consider the following code segment if (n != 0 && x / n > 100) statement 1; else statement 2; If n is of type int and has a value of 0 when the segment is executed, what will happen? ANSWERS: An ArithmeticException will be thrown. statement 1, but not statement 2, will be executed. statement 2, but not statement 1, will be executed. Neither statement 1 nor statement 2 will be executed; control will pass to the first statement following the if statement. A syntax error will occur.

Either /* statement 1 */ or /* statement 2 */ will be executed.

In the following code segment, you may assume that a, b, and n are all type int. if (a != b && n / (a - b) > 90) { /* statement 1 */ } else { /* statement 2 */ } /* statement 3 */ What will happen if a == b is false? ANSWERS: Either /* statement 1 */ or /* statement 2 */ will be executed. /* statement 2 */ will be executed. /* statement 1 */ will be executed. A compile-time error will occur. An exception will be thrown.

12

What is the value stored in result if int result = 13 - 3 * 6 / 4 % 3; ANSWERS: 12 13 -1 0 -5

\* This is not a comment *\

What output will be produced by: System.out.print("\\* This is not\n a comment *\\"); ANSWERS: * This is not a comment * * This is not a comment * \* This is not a comment *\ \\* This is not a comment *\\ \* This is not a comment *\

x = 30 y = 90

What values are stored in x and y after execution of the following program segment? int x = 30, y = 40; if (x >= 0) { if (x <= 100) { y = x * 3; if (y < 50) x /= 10; } else y = x * 2; } else y = -x; ANSWERS: x = 30 y = 90 x = 3 y = -3 x = 30 y = 40 x = 30 y = 60 x = 30 y = -30

I only

Which of the following pairs of declarations will cause an error message? I. double x = 14.7; int y = x; II. double x = 14.7; int y = (int) x; III. int x = 14; double y = x; ANSWERS: None II only I and III only I only III only

!(A || B || C)

Which of the following will evaluate to true only if boolean expressions A, B, and C are all false? ANSWERS: !A && !(B && !C) !(A || B || C) !(A && B && C) !A || !(B || !C) !A || !B || !C

11

The expression: 2 + 3 * 12 / 7 - 4 + 8 evaluates to which of the following? ANSWERS: 5 11 -4 12 9

c < a is false.

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? ANSWERS: c < a is true. a < b is false. c < a is false. c == a * b is true, and c < a is true. c == a * b is true.

There is round-off error in calculating the pow and sqrt functions.

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 √x, the above expression returns the value false in a student's program. Which of the following is the most likely reason? ANSWERS: Math.pow returns an int, while Math.sqrt returns a double. The computer stores floating-point numbers with 32-bit words. There is overflow error in calculating the pow function. There is round-off error in calculating the pow and sqrt functions. x was imprecisely calculated in a previous program statement.

double answer = (double) (13 / 5);

Refer to the following code fragment: double answer = 13 / 5; System.out.print("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? ANSWERS: double answer = (double) 13 / 5; double answer = (double) (13 / 5); double answer = 13 / (double) 5; double answer = 13.0 / 5; double answer = 13 / 5.0

6144

Refer to the following method, checkNumber, which checks the validity of its four-digit integer parameter. //Precondition: n is a 4-digit integer. //Postcondition: Returns true if n is valid, false otherwise. boolean checkNumber(int n) { int d1,d2,d3, checkDigit,nRemaining,rem; //strip off digits checkDigit = n % 10; nRemaining = n / 10; d3 = nRemaining % 10; nRemaining /= 10; d2 = nRemaining % 10; nRemaining /= 10; d1 = nRemaining % 10; //check validity rem = (d1 + d2 + d3) % 7; return true == checkDigit; } A program invokes method checkNumber with the statement boolean valid = checkNumber(num); Which of the following values of num will result in valid having a value of true? ANSWERS: 6143 6144 6145 6147 6146

nRemaining enhances the readability of the algorithm.

Refer to the following method, checkNumber, which checks the validity of its four-digit integer parameter. //Precondition: n is a 4-digit integer. //Postcondition: Returns true if n is valid, false otherwise. boolean checkNumber(int n) { int d1, d2,d3, checkDigit,nRemaining,rem; //strip off digits checkDigit = n % 10; nRemaining = n / 10; d3 = nRemaining % 10; nRemaining /= 10; d2 = nRemaining % 10; nRemaining /= 10; d1 = nRemaining % 10; //check validity rem = (d1 + d2 + d3) % 7; return true == checkDigit; } A program invokes method checkNumber with the statement boolean valid = checkNumber(num); What is the purpose of the local variable nRemaining? ANSWERS: It is not possible to separate n into digits without the help of a temporary variable. nRemaining is needed as the left-hand side operand for integer division. nRemaining enhances the readability of the algorithm. On exiting the method, the value of nRemaining may be reused. nRemaining prevents the parameter num from being altered.


Related study sets

BYU Fitness for Living Well (Online)

View Set

debt (money market debt) SIE EXAM

View Set

Describing Things---Adjectives (page 23): Picture Dictionary

View Set

World Geography A End of Semester Post Test

View Set

Med Serg: Ch 14-Neurologic Disorder study guide

View Set