CPSC 217 Midterm (Python 3 version)

Similar documents
CPSC 217 L01 Midterm

CPSC 217 Midterm (Python 3 version)

Due to the number of people in the room, you must stay for the entire exam.

ENGR 101 Engineering Design Workshop

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

Modules and Programs 1 / 14

CSCI 101 Midterm Sample Questions

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

Sequence Types FEB

Python 2 Conditionals and loops Matthew Egbert CS111

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CMSC 201 Spring 2017 Project 1 Number Classifier

Overview of List Syntax

(f) d={ alchemist :( a, t ), shaman : ( s, n ), wizard : ( w, z )} d[ shaman ][1]

Functionally Modular. Self-Review Questions

Spring 2017 CS 1110/1111 Exam 1

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

Structure and Interpretation of Computer Programs Spring 2016 Test 1

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

Working with Lists 4

Variable and Data Type I

Python: common syntax

Introduction to Python (All the Basic Stuff)

Comp Exam 1 Overview.

Computer Science 217

Lecture 3. Input, Output and Data Types

Fundamentals of Programming (Python) Getting Started with Programming

Jython. secondary. memory

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

1 Fall 2017 CS 1110/1111 Exam 3

Midterm Exam 2B Answer key

Introduction to Python

ENGR 102 Engineering Lab I - Computation

What Would Python Print? Tuples, Lists, Dictionaries

Midterm #1a Fall minutes

Python I. Some material adapted from Upenn cmpe391 slides and other sources

7. (2 pts) str( str( b ) ) str '4' will not compile (single, double, or triple quotes

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

ANS:

ECE 364 Software Engineering Tools Lab. Lecture 3 Python: Introduction

Announcements. Project 2 due next Monday. Next Tuesday is review session; Midterm 1 on Wed., EE 129, 8:00 9:30pm

CS 1301 Exam 1 Fall 2010

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

Midterm #2a Spring minutes DO NOT WRITE IN THIS AREA

Getting Started Values, Expressions, and Statements CS GMU

nostarch.com/pfk For bulk orders, please contact us at

Variable and Data Type I

Structure and Interpretation of Computer Programs

Math Circles: Pigeons and Rams(ey)

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Introduction to Python! Lecture 2

Test #2 October 8, 2015

Fundamentals of Programming. Lecture 1: Introduction + Basic Building Blocks of Programming

egrapher Language Reference Manual

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Programming to Python

Structure and Interpretation of Computer Programs

Midterm #2a Spring minutes

CS1114 Spring 2015 Test ONE ANSWER KEY. page 1 of 8 pages (counting the cover sheet)

Structure and Interpretation of Computer Programs

Chapter 2. Python Programming for Physicists. Soon-Hyung Yook. March 31, Soon-Hyung Yook Chapter 2 March 31, / 52

Python: Short Overview and Recap

Introduction to Computer Programming for Non-Majors

Visualize ComplexCities

CMPT-166: Sample Midterm

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Comp 150 Exam 2 Overview.

CS 1110 Final, December 8th, Question Points Score Total: 100

Teaching London Computing

VLC : Language Reference Manual

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

Spring 2017 CS 1110/1111 Exam 2

The Big Python Guide

cs1114 REVIEW of details test closed laptop period

CSE413 Midterm. Question Max Points Total 100

PYTHON NUMPY TUTORIAL CIS 581

Control Structures 1 / 17

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

Introduction to Programming in Python (1)

Lists, loops and decisions

CS1 Lecture 5 Jan. 25, 2019

Chapter 5 Conditional and Iterative Statements (Part-II) To carry out repetitive task, python provides following iterative/looping statements:

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

roboturtle Documentation

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Final Exam Practice Questions

CIS192 Python Programming. Robert Rand. August 27, 2015

CS150 Sample Final Solution

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Getting Started with Python

Transcription:

CPSC 217 Midterm (Python 3 version) Duration: 60 minutes 7 March 2011 This exam has 81 questions and 14 pages. This exam is closed book. No notes, books, calculators or electronic devices, or other assistance may be used. Mark your answers on the supplied answer sheet. If you think there are multiple correct answers to a question, select the best answer. Part I 1. An algorithm is (A) a set of instructions in a computer language (B) a series of steps followed to solve a problem (C) a file whose name ends with.py (D) something that implements the IPO model 2. A program is (A) a set of instructions in a computer language (B) a series of steps followed to solve a problem (C) a file whose name ends with.py (D) something that implements the IPO model 3. TRUE/FALSE: There are many different algorithms to solve a given problem. 4. TRUE/FALSE: Algorithms specify a lot of implementation details. 5. TRUE/FALSE: There are many ways to implement an algorithm in a given programming language. 6. Executing a program means (A) to run a program (B) to write a program (C) to stop a program from running (D) to kill a process belonging to a program 7. The following are all whitespace characters: (A) space, tab, and # (B) backslash, space, and tab (C) space only (D) space and tab 1

8. Constructing a list of a million integers from zero to 999,999 takes 45 seconds on a small laptop. The program probably built it using (A) tuples (B) list addition (C) the append list method 9. What will the following code print? x = (97,) print(type(x)) (A) <class int > (B) <class float > (C) <class tuple > (D) <class list > (E) There is an error in this code 10. TRUE/FALSE: foo and "foo" represent different strings. 11. len( ) is (B) 1 (C) 2 12. len( foo foo") is (A) 3 (B) 6 (C) 7 13. 42 + 3 is (A) 45 (B) 45 (C) 424242 14. 42 * 3 is (A) 126 (B) 126 (C) 424242 15. TRUE/FALSE: type( ) == type( True ) 16. TRUE/FALSE: Floating point numbers are the same as real numbers. 17. TRUE/FALSE: 6. has no number after the decimal point, so it is an integer. 2

18. In the code below, to check if the integer assigned to foo is odd, what must XXX be? if XXX: print( foo is odd ) (A) foo % 1 == 0 (B) foo % 2 == 0 (C) foo % 2 == 1 (D) foo // 2 == 0 (E) foo // 2 == 1 19. What does the following code print? Python 3 handles division differently. print(2 + 3 * 5 // 2) (A) 9 (B) 10 (C) 11 (D) 12 (E) 13 20. TRUE/FALSE: If foo is a positive integer, the expression foo % N always gives a result between 0 and N-1, inclusive. 21. TRUE/FALSE: type(type(42))!= type(type(type(42))) 22. 12 + 3 is (A) 15 (B) 15 (C) 123 (D) 123 (E) 121212 23. After the command below is run, what type does foo have? Python 3 handles integers differently. foo = 42 * long(3) + 3.2 - int( 32 ) (A) floating point (B) integer (C) long integer (D) string 24. TRUE/FALSE: Adding one string onto another is called concatenation. 25. Once initialized, a variable may change (A) its value (B) its type (C) its value and type (D) neither its value nor its type 3

26. Which of the following are valid variable names? Python 3 handles print differently. (A) foo and 2plus3 (B) for and foo (C) _foo and name (D) _ and print 27. What does the following code print? foo = 1 Foo = 3 Foo = foo foo = foo + 1 print(foo) (A) 1 (B) 2 (C) 3 (D) 4 28. What shape does the following program draw? import turtle turtle.left(45) for i in range(4): turtle.forward(90) turtle.right(120) (A) triangle (B) rhombus (C) diamond (D) square (E) none of the above 29. What does the following code print? L = [3, 6, 9, 12, 15] for i in range(len(l)): sum = L[i] + L[i-1] print(sum) (A) 27 (B) 45 (C) 90 (D) There is an error 4

30. Assuming N is a positive integer, what value will this code print? x = 0 for i in range(n): x = x + 1 print(x) (A) N (B) N - 1 (C) N + 1 31. What does the following code print? x = 1 for i in range(4): x = x * i print(x) (B) 6 (C) 24 (D) 120 32. What does the following code print? i = 0 while i < 100: i = i + 1 print(i) (A) 99 (B) 100 (C) 101 (D) There is an error 33. list(range(5)) is (A) [0, 1, 2, 3, 4] (B) [0, 1, 2, 3, 4, 5] (C) [1, 2, 3, 4] (D) [1, 2, 3, 4, 5] 34. list(range(1, 5)) is (A) [2, 3, 4] (B) [0, 1, 2, 3, 4] (C) [0, 1, 2, 3, 4, 5] (D) [1, 2, 3, 4] (E) [1, 2, 3, 4, 5] 5

35. list(range(5, 2, -3)) is (A) [] (B) [5] (C) [5, 2] (D) [2, 5] (E) an error 36. How many times is eek printed by the following code? for i in range(2): for j in range(3): if i == j: print( eek ) (B) 1 (C) 2 (D) 3 (E) 6 37. bm is a square 2D black-and-white bitmap image, stored as a list of lists. What does the following code do? for i in range(len(bm)): for j in range(len(bm[i])): bm[i][j] = 1 (A) Sets all bits to 1 (B) Sets all bits in the first row to 1 (C) Sets all bits along the diagonal to 1 (D) Sets all bits in the first columnn to 1 38. Which is the best conversion of the formula f = e 2n + k? Assume that n and k are already defined. (A) e = 2.7182818284490451 f = e ** 2 * n + k (B) e = 2.7182818284490451 f = e ** (2 * n) + k (C) import math f = math.e ** 2 * n + k (D) import math f = math.e ** (2 * n) + k 39. TRUE/FALSE: Computing the first ten values of a function correctly is sufficient to test it. 40. TRUE/FALSE: Programs crash when given incorrect input. 41. TRUE/FALSE: IPO is a program design that stands for input-processing-output. 6

42. Which implementation of f = k (i + 2) is correct? Assume k is already defined. i=1 (A) f = 0 for i in range(1, k): f = f * (i + 2) (B) f = 1 for i in range(1, k): f = f * (i + 2) (C) f = 0 for i in range(1, k+1): f = f * (i + 2) (D) f = 1 for i in range(1, k+1): f = f * (i + 2) 43. Consider the following program, which is called foo.py: import sys print(sys.argv) The program is run by typing the following. What will the program print? python foo.py bar baz < datafile (A) [ bar, baz ] (B) [ foo.py, bar, baz ] (C) [ python, foo.py, bar, baz ] (D) [ foo.py, bar, baz, <, datafile ] (E) [ python, foo.py, bar, baz, <, datafile ] 44. A recursive function is a function (A) that calls another function (B) that calls itself (C) that has multiple return statements (D) that is nested inside another function 45. What does the following code print? def foo(): x = 123 x = 42 foo() print(x) (A) 42 (B) 123 (C) There is an error 7

46. What does the following code print? def foo(p, q): p = q q = 12 p = 34 q = 78 foo(p, q) print(p, q) (A) 34 12 (B) 34 78 (C) 78 12 (D) 78 78 (E) There is an error 47. TRUE/FALSE: Bottom-up design decomposes a problem into smaller and smaller pieces. 48. How many lines does the following code print? def A(): print( A ) def B(): A() A() def C(): A() C() (B) 1 (C) 2 (D) 4 (E) 5 49. A string is a sequence of zero or more characters. What would you expect this code to do? xs = abcde for singer in xs: print(singer) (A) Print the letters a through e, inclusive (B) Print abcde five times (C) Print nothing (D) Print False (E) Give an error 50. TRUE/FALSE: The Monte Carlo method yields an exact result. 51. TRUE/FALSE: Lists and tuples are mutable. 8

52. TRUE/FALSE: False!= True 53. TRUE/FALSE: True and False 54. TRUE/FALSE: True or False 55. TRUE/FALSE: True and not False 56. TRUE/FALSE: 9 > 10 and 9 < 10 57. TRUE/FALSE: True!= 1 or False!= ( in foo ) 58. What does the following code print? i = 0 while i < 5: print(i) i = i + 1 while i < 5: print(i) i = i + 1 1 2 3 4 (B) 0 1 2 3 4 5 (C) 0 1 2 3 4 0 1 2 3 4 (D) 0 1 2 3 4 5 0 1 2 3 4 5 (E) 0 1 2 3 4 5 6 7 8 9 10 59. What does the following code print? i = 0 while i < 3: break i = i + 1 print(i) (B) 1 (C) 2 (D) 3 (E) 4 60. What does the following code print? for i in range(5): if i == 2: continue print(i) 1 (B) 0 1 3 4 (C) 0 1 3 4 5 (D) 0 1 2 3 4 (E) 0 1 2 3 4 5 9

61. The Unix password file has the following format, to quote the manual page: There is one entry per line, and each line has the format: account:password:uid:gid:gecos:directory:shell In the following code, what would XXX be replaced with, to print the account and GECOS fields for the first 100 password entries read from the standard input? for i in range(100): line = input() fields = line.split( : ) XXX (A) print(fields[0], fields[4]) (B) print(fields[0], fields[5]) (C) print(fields[1], fields[4]) (D) print(fields[1], fields[5]) 62. After the command T = (1,2,3) is run, what is T[-2] * T[2]? (A) -4 (B) 3 (C) 4 (D) 6 Part II (E) An error Use the following code to answer the questions in this part. L = XXX for i in L: if i == 0: x = 0 elif i == 1: x = x + 1 elif i == 2: x = x + 2 else: print(x) 63. What will the code above print, if XXX is changed to [0, 99] (B) 99 (C) Nothing 10

64. What will the code above print, if XXX is changed to [0, 2, 1, 2, -1] (B) 1 (C) 2 (D) 5 (E) Nothing 65. What will the code above print, if XXX is changed to [0, 1, 1, 2, 1] (B) 1 (C) 2 (D) 5 (E) Nothing 66. What will the code above print, if XXX is changed to [0, 1, 1, 3, 1] + list(range(4)) (A) 2 (B) 3 (C) 2 3 (D) 2 6 (E) Nothing Part III Use the following code for the questions in this section. L = [4, 2, 7, 9, 13] 67. TRUE/FALSE: 9 not in L 68. L[2] + L[1] is (A) 6 (B) 9 (C) 16 69. len(l[1:4]) is (A) 1 (B) 3 (C) 4 (D) 5 11

70. L[-3:] is (A) [2, 7, 9, 13] (B) [7, 9, 13] (C) [9, 13] 71. L[L[2]] is (A) 2 (B) 4 (C) 9 72. L[L[1]] is (A) 2 (B) 4 (C) 7 Part IV Use the following code for the questions in this section. D = {5: 3, 13: 2, 3: 7, 8: 12} 73. len(d) is (A) 1 (B) 4 (C) 8 74. D[3] is (A) 5 (B) 7 (C) 8 (D) 12 (E) an error 75. What would the following code print? s = 0 for x in D: s = s + x print(s) (A) 24 (B) 29 (C) 53 12

Part V A program reads a sequence of input that only contains the strings foo and bar. For example, one input sequence might be foo bar bar bar foo foo bar foo The following finite state machine should model the logic necessary for this program to determine when it has read foo bar foo consecutively and in that order. AAA BBB CCC DDD EEE FFF 76. What label should be given to the edge marked AAA? (A) foo (B) bar 77. What label should be given to the edge marked BBB? (A) foo (B) bar 78. What label should be given to the edge marked CCC? (A) foo (B) bar 79. What label should be given to the edge marked DDD? (A) foo (B) bar 80. What label should be given to the edge marked EEE? (A) foo (B) bar 81. What label should be given to the edge marked FFF? (A) foo (B) bar 13

Answer Key Q1: B; Q2: A; Q3: true; Q4: false; Q5: true; Q6: A; Q7: D; Q8: B; Q9: C; Q10: false; Q11: A; Q12: D; Q13: D; Q14: C; Q15: true; Q16: false; Q17: false; Q18: C; Q19: A; Q20: true; Q21: false; Q22: D; Q23: n/a; Q24: true; Q25: C; Q26: C and D; Q27: A; Q28: A; Q29: A; Q30: A; Q31: A; Q32: B; Q33: A; Q34: D; Q35: B; Q36: C; Q37: A; Q38: D; Q39: false; Q40: false; Q41: true; Q42: D; Q43: B; Q44: B; Q45: A; Q46: B; Q47: false; Q48: B; Q49: A; Q50: false; Q51: false; Q52: true; Q53: false; Q54: true; Q55: true; Q56: true; Q57: true; Q58: A; Q59: A; Q60: B; Q61: A; Q62: D; Q63: A; Q64: D; Q65: E; Q66: C; Q67: false; Q68: B; Q69: B; Q70: B; Q71: D; Q72: C; Q73: B; Q74: B; Q75: B; Q76: B; Q77: A; Q78: A; Q79: B; Q80: A; Q81: B. 14