APCS Week #1 Quiz Set

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

12

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

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

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

II and III only

Consider the method reverse: //Precondition: n > 0. //Postcondition: returns n with its digits reversed. //Example: If n = 234, method reverse returns 432. int reverse(int n) { int rem, revNum = 0; /* code segment */ return revNum; } Which of the following replacements for /* code segment */ would cause the method to work as intended? I. for (int i = 0; i <= n; i++) { rem = n % 10; revNum = revNum * 10 + rem n /= 10; } II. while (n != 0) { rem = n % 10; revNum = revNum * 10 + rem n /= 10; } III. for (int i = n; i != 0; i /= 10) { rem = i % 10; revNum * 10 + rem; } ANSWERS: II and III only I and III only I only I and II only II only

4 12

Consider this code segment: int x = 10, y = 0; while (x > 5) { y = 3; while (y < x) { y *= 2; if (y % x == 1) y += x; } x -= 3; } System.out.println(x + " " + y); What will be output after execution of this code segment? ANSWERS: 7 12 -3 6 4 12 -3 12 1 6

111101bin

Suppose that base-2(binary) numbers and base-16(hexadecimal) numbers can be denoted with subscripts, as shown below: 2Ahex = 101010bin Which is equal to 3Dhex? ANSWERS: 101111bin 110100bin 10011bin 111101bin 101101bin

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

11

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

\* 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 *\

#0E14FF

A common use of hexadecimal numerals is to specify colors on web pages. Every color has a red, green, and blue component. In decimal notation, these are denoted with an ordered triple (x, y, z), where x, y, and z are the three components, each an int from 0 to 255. For example, a certain shade of red, whose red, green, and blue components are 238, 9, and 63, is represented as (238, 9, 63). In hexadecimal, a color is represented in the format #RRGGBB, where RR, GG, and BB are hex values for the red, green, and blue. Using this notation, the color (238, 9, 63) would be coded as ###093F. Which of the following hex codes represents the color (14, 20, 255)? ANSWERS: 0FE5FE #0E14FF #0D14FF #1418FE #0E20FE

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.

II and III only

Consider this program segment: int newNum = 0, temp; int num = k; // k is some predefined integer value >= 0 while (num > 10) { temp = num % 10; num /= 10; newNum = newNum * 10 + temp; } System.out.print(newNum); 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. ANSWERS: I, II, and III only II and III only III only I only II only

The code will always process a value that is not on the list.

The following fragment intends that a user will enter a list of positive integers at the keyboard and terminate the list with a sentinel: int value = 0; final int SENTINEL = -999; while (value != SENTINEL) { //code to process value ... value = IO.readInt(); //read user input } The fragment is not correct. Which is a true statement? ANSWERS: The sentinel gets processed. A poor choice of SENTINEL value causes the loop to terminate before all values have been processed. Entering the SENTINEL value as the first value causes a run-time error. The last nonsentinel value entered in the list fails to get processed. The code will always process a value that is not on the list.

9 7 5 3 1 7 5 3 1 5 3 1 3 1 1

What output will be produced by this code segment? (Ignore spacing.) for (int i = 5; i >= 1; i--) { for (int j = i; j >= 1; j--) System.out.print(2 * j - 1); System.out.println( ); } ANSWERS: 9 7 5 3 1 9 7 5 3 9 7 5 9 7 9 1 3 5 7 9 1 3 5 7 1 3 5 1 3 1 1 1 3 1 3 5 1 3 5 7 1 3 5 7 9 9 7 5 3 1 7 5 3 1 -1 5 3 1 -1 -3 3 1 -1 -3 -5 1 -1 -3 -5 -7 9 7 5 3 1 7 5 3 1 5 3 1 3 1 1

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.

I, II, and III

Which of the following program fragments will produce this output? (Ignore spacing.) 2 - - - - - - 4 - - - - - - 6 - - - - - - 8 - - - - - - 10 - - - - - - 12 I. for(int i = 1; i <= 6; i++) { for(int k = 1; k <= 6; k++) if(k == 1) System.out.print(2 * k); else System.out.print("-"); System.out.println(); } II. for(int i = 1; i <= 6; i++) { for(int k = 1; k <= 1 - 1; k++) System.out.print("-"); System.out.print(2 * i); for(int k = 1; k <= 6 - 1; k++) System.out.print("-"); System.out.print(); } III. for(int i = 1; i <= 6; i++) { for(int k = 1; k <= i - 1; k++) System.out.print("-"); System.out.print(2 * i); for(int k = i + 1; k <= 6; k++) System.out.print("-"); System.out.println(); } ANSWERS: I and II only I, II, and III III only II only I only

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.

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

Given that n and count are both of type int, which statement is true about the following code segments? I for (count = 1; count <= n; count++) System.out.println(count); II count = 1; while (count <= n) { System.out.println(count); count++; } ANSWERS: I and II are exactly equivalent for all input values n. I and II are exactly equivalent for all input values n is greater than or equal to 1, but differ when n is less than or equal to 0. I and II are exactly equivalent only when n = 0. I and II are not equivalent for any input values of n. I and II are exactly equivalent only when n is even.

231 - 1

In Java, a variable of type int is represented internally as a 32-bit signed integer. Suppose that one bit stores the sign, and the other 31 bits store the magnitude of the number in base 2. In this scheme, what is the largest value that can be stored as type int? ANSWERS: 230 232 - 1 231 232 231 - 1

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.

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.


Set pelajaran terkait

Combo with Amsco APUSH Chapter 19 +18 and 1 other

View Set

Budowa i działanie narządu wzroku

View Set

Taylor's Fundametnals PrepU Ch. 45 Sensory Functioning

View Set

U.S. Government Final Exam review

View Set

Workforce Planning & Employment section 2-7

View Set

Microbiology Chapter 9 Physical and Chemical Control of Microbes

View Set

Chapter 10 - Interest in Real Estate - Mini Quiz

View Set

Federal Tax Considerations for Accident and Health Insurance

View Set