Chapter 4 and 5 Review

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

4.27 What will be displayed by the following code? ch = 'F' if ch >= 'A' and ch <= 'Z': print(ch) A. F B. f C. nothing D. F f

A

5.19 How many times is the print statement executed? for i in range(10): for j in range(10): print(i * j) A. 100 B. 20 C. 10 D. 45

A

7. Suppose income is 4001. What will be displayed by the following code? if income > 3000: print("Income is greater than 3000") elif income > 4000: print("Income is greater than 4000") a. Income is greater than 3000 b. Income is greater than 3000 followed by Income is greater than 4000 c. Income is greater than 4000 d. Income is greater than 4000 followed by Income is greater than 3000

A

4.3 The word True is ________. A. a Python keyword B. a Boolean literal C. same as value 1 D. same as value 0

AB

4.19 Which of the Boolean expressions below is incorrect? A. True and 3 => 4 B. !(x > 0) and (x > 0) C. (x > 0) or (x < 0) D. (x != 0) or (x = 0) E. (-10 < x < 0) Explanation: a: (3 => 4) should be (3 >= 4), b: !(x > 0) should be not (x > 0) d: (x = 0) should be (x == 0)

ABD

4.22 Given |x - 2| >= 4, Which of the following is true? A. x - 2 >= 4 and x - 2 <= -4 B. x - 2 >= 4 or x - 2 <= -4 C. x - 2 >= 4 and x - 2 < -4 D. x - 2 >= 4 or x - 2 <= -4

B

4.23 Assume x = 4 and y = 5, Which of the following is true? A. x < 5 and y < 5 B. x < 5 or y < 5 C. x > 5 and y > 5 D. x > 5 or y > 5

B

4.30 What will be displayed by the following code? isCorrect = False print("Correct" if isCorrect else "Incorrect") A. Correct B. Incorrect C. nothing D. Correct Incorrect

B

4.6 random.random() returns ____________. A. a float number i such that 0 < i < 1.0 B. a float number i such that 0 <= i < 1.0 C. a float number i such that 0 <= i <= 1.0 D. a float number i such that 0 < i < 2.0

B

5.12 The following loop displays _______________. for i in range(1, 11): print(i, end = " ") A. 1 2 3 4 5 6 7 8 9 B. 1 2 3 4 5 6 7 8 9 10 C. 1 2 3 4 5 D. 1 3 5 7 9 E. 2 4 6 8 10

B

5.9 Which of the following function returns a sequence 0, 1, 2, 3? (choose-2) A. range(0, 3) B. range(0, 4) C. range(3) D. range(4)

BD

4. Which of the following code displays the area of a circle if the radius is positive? a. if radius != 0: print(radius * radius * 3.14159) b. if radius >= 0: print(radius * radius * 3.14159) c. if radius > 0: print(radius * radius * 3.14159) d. if radius <= 0: print(radius * radius * 3.14159)

C

4.29 Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if number % 2 == 0: even = True else: even = False Code 2: even = True if number % 2 == 0 else False Code 3: even = number % 2 == 0 A. Code 2 has a syntax error, because you cannot have True and False literals in the conditional expression. B. Code 3 has a syntax error, because you attempt to assign number to even. C. All three are correct, but Code 1 is preferred. D. All three are correct, but Code 2 is preferred. E. All three are correct, but Code 3 is preferred.

CE

4.12 Suppose x = 1, y = -1, and z = 1. What will be displayed by the following statement? if x > 0: _____if y > 0: ________print("x > 0 and y > 0") elif z > 0: _____print("x < 0 and z > 0") A. x > 0 and y > 0 B. x < 0 and z > 0 C. x < 0 and z < 0 D. nothing displayed

D

4.14 Analyze the following code: Code 1: if number % 2 == 0: ____even = True else: even = False Code 2: even = number % 2 == 0 A. Code 1 has compile errors. B. Code 2 has compile errors. C. Both Code 1 and Code 2 have compile errors. D. Both Code 1 and Code 2 are correct, but Code 2 is better.

D

"!= " comparison operator

not equal to

4.26 Which of the following is equivalent to x != y? A. not (x == y) B. x > y and x < y C. x > y or x < y D. x >= y or| x <= y

AC

16. What is the number of iterations in the following loop: upper range - lower range for i in range(1, n): # iteration a. 2*n b. n c. n - 1 d. n + 1 note(((2nd #) - (1st#))upper range - lower range)

C

12. The function range(5) return a sequence ______________. a. 1, 2, 3, 4, 5 b. 0, 1, 2, 3, 4, 5 c. 1, 2, 3, 4 d. 0, 1, 2, 3, 4

D

4.24 Assume x = 4 and y = 5, Which of the following is true? A. not (x == 4) B. x != 4 C. x == 5 D. x != 5

D

5.14 What is the output for y?range([start], stop[, step]) y = 0 for i in range(0, 10, 2): _____y += i print(y) A. 9 B. 10 C. 11 D. 20

D

2. What is round(3.52)? round (3.52, 1) = 3.5 a. 3.5 b. 3 c. 4 d. 5

C

3. random.randint(0, 1) returns a. 0 b. 1 c. 0 or 1 d. 2

C

4.13 The following code displays ___________. temperature = 50 if temperature >= 100: print("too hot") elif temperature <= 40: print("too cold") else: print("just right") A. too hot B. too cold C. just right D. too hot too cold just right

C

4.18 The __________ function immediately terminates the program. A. sys.terminate() B. sys.halt() C. sys.exit() D. sys.stop()

C

4.32 Which of the following operators are right-associative. A. * B. + C. % D. and E. =

E

5.15 What is the output for y? y = 0 for i in range(10, 1, -2): _____y += i print(y) A. 10 B. 40 C. 30 D. 20

C

5.5 What will be displayed when the following code is executed? number = 6 while number > 0: _______number -= 3 ______ print(number, end = ' ') A. 6 3 0 B. 6 3 C. 3 0 D. 3 0 -3 E. 0 -3

C

4.28 What is y after the following statement is executed? x = 0 y = 10 if x > 0 else -10 A. -10 B. 0 C. 10 D. 20 E. Illegal expression

A

14. Given the following four patterns, A B C D 1 1 2 3 4 5 6 1 1 2 3 4 5 6 1 2 1 2 3 4 5 2 1 1 2 3 4 5 1 2 3 1 2 3 4 3 2 1 1 2 3 4 1 2 3 4 1 2 3 4 3 2 1 1 2 3 1 2 3 4 5 1 2 5 4 3 2 1 1 2 1 2 3 4 5 6 1 6 5 4 3 2 1 1 Which of the pattern is produced by the following code? for i in range(1, 6 + 1): __for j in range(6, 0, -1): _____print(j if j <= i else " ", end = " ") __print() a. Pattern A b. Pattern B c. Pattern C d. Pattern D

C

4.33 What is the value of the following expression? True or True and False A. True B. Fals

A

5.18 To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy? A. add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0. B. add 1.00, 0.99, 0.98, ..., 0.02, 0.01 in this order to a sum variable whose initial value is 0.

A

4.34 Which of the following statements are True? A. (x > 0 and x < 10) is same as (x > 0 and x < 10) B. (x > 0 or x < 10) is same as (0 < x < 10) C. (x > 0 or x < 10 and y < 0) is same as (x > 0 or (x < 10 and y < 0)) D. (x > 0 or x < 10 and y < 0) is same as ((x > 0 or x < 10) and y < 0)

AC

4.4 To generate a random integer between 0 and 5, use ________________. A. random.randint(0, 5) B. random.randint(0, 6) C. random.randrange(0, 5) D. random.randrange(0, 6)

AD

5.10 Which of the following function is incorrect? A. range(0, 3.5) B. range(10, 4, -1) C. range(1, 3, 1) D. range(2.5, 4.5) E. range(1, 2.5, 4.5)

ADE

5.3 Analyze the following code. count = 0 while count < 100: # Point A print("Welcome to Python!") count += 1 # Point B # Point C A. count < 100 is always True at Point A B. count < 100 is always True at Point B C. count < 100 is always False at Point B D. count < 100 is always True at Point C E. count < 100 is always False at Point C

AE

15. Will the following program terminate? balance = 10 while True: if balance < 9: continue balance = balance - 9 a. Yes b. No

B

4.10 Analyze the following code: even = False if even = True: ____print("It is even!") A. The program has a syntax error in line 1 (even = False) B. The program has a syntax error in line 2 if even = True is not a correct condition. It should be replaced by if even == True: or if even:. C. The program runs, but displays nothing. D. The program runs and displays It is even!.

B

4.11 Analyze the following code. even = False if even: ___print("It is even!") A. The code displays It is even! B. The code displays nothing. C. The code is wrong. You should replace if even: with if even == True: D. The code is wrong. You should replace if even: with if even = True:

B

4.17 Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is the best? I: if age < 16: print("Cannot get a driver's license") if age >= 16: print("Can get a driver?s license") II: if age < 16: print("Cannot get a driver's license") else: print("Can get a driver's license") III: if age < 16: print("Cannot get a driver's license") elif age >= 16: print("Can get a driver's license") IV: if age < 16: print("Cannot get a driver's license") elif age == 16: print("Can get a driver's license") elif age > 16: print("Can get a driver's license") A. I B. II C. III D. IV

B

5. Analyze the following code. even = False if even: ___print("It is even!")? a. The code displays It is even! b. The code displays nothing c. The code is wrong. You should replace if even: with if even = True:

B

5.6 Analyze the following statement: sum = 0 for d in range(0, 10, 0.1): sum += sum + d A. The program has a syntax error because the range function cannot have three arguments. B. The program has a syntax error because the arguments in the range must be integers. C. The program runs in an infinite loop. D. The program runs fine.

B

11. Which of the following loops prints "Welcome to Python" 10 times? A:for count in range(1, 10): print("Welcome to Python") B:for count in range(0, 10): print("Welcome to Python") C:for count in range(1, 11): print("Welcome to Python") D:for count in range(1, 12): print("Welcome to Python") a. B and D b. A, B, and C c. A and C d. B and C ((2nd #) - (1st#))upper range - lower range

BC

8. To check whether a char variable ch is an uppercase letter, you write ___________. a. (ch >= 'A' and ch >= 'Z') b. (ch >= 'A' and ch <= 'Z') c. (ch >= 'A' or ch <= 'Z') d. ('A' <= ch <= 'Z')

BD

4.21 Given |x - 2| <= 4, Which of the following is true? A. x - 2 <= 4 and x - 2 >= 4 B. x - 2 <= 4 and x - 2 > -4 C. x - 2 <= 4 and x - 2 >= -4 D. x - 2 <= 4 or x - 2 >= -4

C

4.31 The order of the precedence (from high to low) of the operators +, *, and, or is: A. and, or, *, + B. *, +, and, or C. *, +, and, or D. *, +, or, and E. or, and, *, +

C

4.9 Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true. A. if isPrime = True: B. if isPrime == True: C. if isPrime: D. if not isPrime = False: E. if not isPrime == False:

C

5.11 Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100? 1:sum = 0 for i in range(1, 99): sum += i / (i + 1) print("Sum is", sum) 2:sum = 0 for i in range(1, 100): sum += i / (i + 1) print("Sum is", sum) 3:sum = 0 for i in range(1.0, 99.0): sum += i / (i + 1) print("Sum is", sum) 4:sum = 0 for i in range(1.0, 100.0): sum += i / (i + 1) print("Sum is", sum) A. 234 B. 1234 C. 2 D. 34

C

5.17 Analyze the following fragment: sum = d = 0 while d != 10.0: ______d += 0.1 ______sum += sum + d A. The program does not run because sum and d are not initialized correctly. B. The program never stops because d is always 0.1 inside the loop. C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers. D. After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9

C

6.What will be displayed by the following statement? x = 1 y = -1 z = 1. if x > 0: ___if y > 0: ______print("x > 0 and y > 0") elif z > 0: ___print("x < 0 and z > 0") a. x > 0 and y > 0 b. x < 0 and z > 0 c. nothing d. The code is wrong. You should replace if even: with if even = True:

C

9. Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if number % 2 == 0: even = True else: even = False Code 2: even = True if number % 2 == 0 else False Code 3: even = number % 2 == 0 a. Code 2 has a syntax error, because you cannot have True and False literals in the conditional expression. b. Code 3 has a syntax error, because you attempt to assign number to even. c. All three are correct, but Code 1 is preferred. d. All three are correct, but Code 2 is preferred. e. All three are correct, but Code 3 is preferred.

C

How many times will the following code print "Welcome to Python"? count = 0 while count < 10: print("Welcome to Python") count += 1 A. 8 B. 9 C. 10 D. 11 E. 0

C

4.16 Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is correct? I: if age < 16: print("Cannot get a driver's license") if age >= 16: print("Can get a driver's license") II: if age < 16: print("Cannot get a driver's license") else: print("Can get a driver's license") III: if age < 16: print("Cannot get a driver's license") elif age >= 16: print("Can get a driver's license") IV: if age < 16: print("Cannot get a driver's license") elif age == 16: print("Can get a driver's license") elif age > 16: print("Can get a driver's license") A. I and II B. II and III C. I, II, and III D. III and IV E. All correct

E

5.13 What is the output for y? y += i 0 += 0 0 += 1 1+= 2 3+=3 6+=4 10+=5 ... 36+=9 45 y = 0 for i in range(0, 10): y += i print(y) A. 10 B. 11 C. 12 D. 13 E. 45

E

5.2 What is the output of the following code? x = 0 while x < 4: ______x = x + 1 print("x is", x) A. x is 0 B. x is 1 C. x is 2 D. x is 3 E. x is 4 x = x + 1 1. x = 0 + 1 2. x = 1 + 1 3. x = 2 + 1 4. x = 3 + 1

E

5.4 How many times will the following code print "Welcome to Python"? count = 0 while count < 10: print("Welcome to Python") A. 8 B. 9 C. 10 D. 11 E. infinite number of times note- add "count += 1" to get an answer of 10

E

random.randrange (0,1)

always returns 0

1. The "equal" comparison operator is a. <> b. != c. == d. =

C


Kaugnay na mga set ng pag-aaral