CPSC 231 Tutorial
description
Transcript of CPSC 231 Tutorial
![Page 1: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/1.jpg)
Xin Liu
Feb 11, 2013
*CPSC 231 Tutorial
![Page 2: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/2.jpg)
*Midterm exam review
*
![Page 3: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/3.jpg)
Part 11. What value does s have, after this code is run?s = '*'s = s + ss = s + s + s(A) '**'(B) '***'(C) '****'(D) '*****'(E) '******'
![Page 4: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/4.jpg)
2. What does this code print when run?sum = 0for i in range(3): for j in range(2): sum = sum + jprint(sum) (A) 1 (B) 2 (C) 3 (D) 4 (E) 5
![Page 5: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/5.jpg)
3. What does this code print when run?sum = 0for i in range(3): for j in range(i): sum = sum + jprint(sum) (A) 0 (B) 1 (C) 2 (D) 3 (E) 4
![Page 6: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/6.jpg)
4. What does this code print when run?print(12 + 3 * 3) (A) 17 (B) 18 (C) 21 (D) 39 (E) 45 5. What does this code print when run?print('1' * 2) (A) 1 (B) 2 (C) 11 (D) 12 (E) Nothing; there is an error
![Page 7: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/7.jpg)
6. What is the value of x after this code is run?x = 1 / 2 (A) 0 (B) 1 (C) 0.0 (D) 0.5 (E) 1.0
7. What is the value of x after this code is run?x = 1 // 2 (A) 0 (B) 1 (C) 0.0 (D) 0.5 (E) 1.0
![Page 8: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/8.jpg)
8. Which of the following are valid variable names?_fooif4everMO0S3 (A) _ and MO0S3 (B) All except 4ever (C) All except _ (D) foo and if (E) foo , if , and 4ever
![Page 9: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/9.jpg)
9. Real numbers are the same as floating point numbers.(A) True(B) False
10. How many of the following statements evaluate to False ?'ab' not in 'abc''ab' in 'abc''9' < '10'5.0 > -2(A) 0(B) 1(C) 2(D) 3(E) 4
![Page 10: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/10.jpg)
11. How many of the following statements evaluate to True ?2 < 43 >= 542 == 42'foo' != 'bar’(A) 0(B) 1(C) 2(D) 3(E) 4
![Page 11: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/11.jpg)
12. How many times is 1 printed when this code is run?for i in range(2): for j in range(4): x = 1 print(x)(A) 0(B) 2(C) 4(D) 6(E) 8
![Page 12: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/12.jpg)
13. How many times is 1 printed when this code is run?for i in range(2): for j in range(4): x = 1 print(x)(A) 0(B) 2(C) 4(D) 6(E) 8
![Page 13: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/13.jpg)
14. How many times is X printed when this code is run?for i in range(5): if i != 2: print('X')(A) 0(B) 1(C) 2(D) 3(E) 4
15. How many times is X printed when this code is run?for i in range(5): if (i == 2) or (i-1 < 1): print('X')(A) 0(B) 1(C) 2(D) 3(E) 4
![Page 14: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/14.jpg)
16. What input would cause this program to print X ?s = input()b = 'e' not in s if not b: print('X') (A) Batman (B) hobbit (C) orc (D) elf (E) Gandalf
![Page 15: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/15.jpg)
17. Under what conditions is baz printed? Assume A and B are Boolean variables that have been assigned a value.if A: print('foo')elif B: print('bar')else: print('baz') (A) One of A or B is True (B) A is True (C) B is True (D) Both A and B are True (E) Neither A nor B are True
![Page 16: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/16.jpg)
18. What does this code draw when run?import turtleturtle.lt(90)turtle.fd(75)turtle.rt(45)turtle.bk(100)turtle.fd(50)turtle.rt(45)turtle.fd(40)(A) Nothing listed here(B) A triangle(C) The letter “N”(D) The letter “A”(E) Nothing; there is an error when it is run
![Page 17: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/17.jpg)
19. Just as dividing by zero in math is somewhat problematic, an error is caused if a Python program tries to divideby zero. Does this program cause an error when it is run?b = True or 1 / 0(A) Yes(B) No
![Page 18: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/18.jpg)
Part 2There is a Python module called foo. In that module is a function named bar that you can call. bar takes one integeras an argument. Starting with the following:AAABBBs = '42'CCC
20. What should AAA be replaced with?(A) import math(B) import turtle(C) Nothing
21. What should BBB be replaced with? (A) import foo (B) import time (C) Nothing
![Page 19: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/19.jpg)
22. What should CCC be replaced with? (A) foo.bar(s) (B) bar(s) (C) foo.bar(int(s)) (D) foo.bar(integer(s)) (E) bar(int(s))
![Page 20: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/20.jpg)
Part 3Write a loop to print the numbers 1 through 10, inclusive.Starting with the following:n = 10 for i in AAA: BBB23. What should AAA be replaced with?(A) range(n)(B) n(C) range(len(n))(D) range(1, n)(E) range(1, n+1)24. What should BBB be replaced with?(A) print i(B) print n(C) print(i)(D) print(n)
![Page 21: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/21.jpg)
Part 4Write code to draw a horizontal row of six boxes. Starting with the following:AAABBBfor i in CCC: turtle.pu() turtle.fd(75) turtle.pd() for j in DDD: turtle.fd(50) turtle.lt(90)25. What should AAA be replaced with?(A) import math(B) import turtle(C) Nothing26. What should BBB be replaced with?(A) n = 5(B) n = 6(C) Nothing
![Page 22: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/22.jpg)
27. What should CCC be replaced with?(A)4(B)(B) n(C) n-1(D) range(n)(E) range(n-1)
28. What should DDD be replaced with? (A) 4 (B) n (C) range(4) (D) range(n)
![Page 23: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/23.jpg)
Part 5Convert a2 - 2ab + cπ-1 to Python code. Starting with the following:import mathAAA - BBB + CCC29. What should AAA be replaced with?(A) a * 2(B) a ^ 2(C) a ** 230. What should BBB be replaced with?(A) 2ab(B) 2 * ab(C) 2a * b(D) 2 * a * b31. What should CCC be replaced with?(A) c * pi-1(B) c * math.pi-1(C) c ** (pi-1)(D) c ^ (math.pi-1)(E) c ** (math.pi-1)
![Page 24: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/24.jpg)
Part 6The input to the programs in this section is the following:924732. What does the following program print when it is run with the above input?input()a = float(input())input()b = int(input())print(a * b)
(A) 8.0(B) 14(C) 14.0(D) 18(E) 18.0
![Page 25: CPSC 231 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062807/568150f3550346895dbf1225/html5/thumbnails/25.jpg)
33. What does the following program print when it is run with the above input?x = 0for i in range(3): n = int(input()) x = x + nprint(x) (A) 11 (B) 15 (C) 22 (D) Nothing; there is an error
34. What does the following program print when it is run with the above input?x = 0for i in range(4): n = int(input()) x = x * nprint(x) (A) 0 (B) 18 (C) 72 (D) 504 (E) Nothing; there is an error