CS 1301 CS1 with Robots Summer 2007 Exam 1

Similar documents
CS 1301 Exam 1 Answers Fall 2009

CS 1301 Exam 1 Fall 2009

Conditionals and Recursion. Python Part 4

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

CS 1301 Exam 1 Fall 2014

CSI33 Data Structures

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

CS 1301 Exam 1 Fall 2014

CS 1301 Exam 1 Fall 2010

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

CS 1301 Final Exam Review Guide

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

DM536 Introduction to Programming. Peter Schneider-Kamp.

Python for Informatics

Decisions, Decisions. Testing, testing C H A P T E R 7

Your Name: Your TA's Name: 1 / 10 CS 1301 CS1 with Robots Fall 2008 Exam 3

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

DM502 Programming A. Peter Schneider-Kamp.

CSCE 110: Programming I

CS 1301 Exam 1 Fall 2011

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Introduction to Programming Using Java (98-388)

ENGR 101 Engineering Design Workshop

CS 1301 Exam 1 Fall 2010

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

APCS Semester #1 Final Exam Practice Problems

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

PREPARING FOR PRELIM 1

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Functions and Recursion

First name (printed): a. DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Visualize ComplexCities

FORM 2 (Please put your name and form # on the scantron!!!!)

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 4. Conditionals and recursion. 4.1 The modulus operator. 4.2 Conditional execution

Programming for Engineers Iteration

Information Science 1

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

Iteration. Chapter 7. Prof. Mauro Gaspari: Mauro Gaspari - University of Bologna -

What is an algorithm?

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

DM536 Introduction to Programming. Peter Schneider-Kamp.

CS 1301 Exam 1 Spring 2011

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

CS 1301 Exam 1 Spring 2014

Spring Semester 10 Exam #1 Dr. Dillon. (02/18)

Simple Java Programming Constructs 4

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

CS 6353 Compiler Construction Project Assignments

CS 1301 Exam 2 A Fall 2015

CS 111X - Fall Test 1

Chapter 2: Functions and Control Structures

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

CS 6353 Compiler Construction Project Assignments

Text Input and Conditionals

UVa ID: NAME (print): CS 4501 LDI Midterm 1

G Programming Languages - Fall 2012

Spring Semester 09 Exam #2 Dr. Dillon. (04/02)

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Spoke. Language Reference Manual* CS4118 PROGRAMMING LANGUAGES AND TRANSLATORS. William Yang Wang, Chia-che Tsai, Zhou Yu, Xin Chen 2010/11/03

Structure and Interpretation of Computer Programs

Information Science 1

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Conditional Expressions and Decision Statements

Spring Semester 13, Dr. Punch. Exam #1 (2/14), form 1 A

CS1 Lecture 5 Jan. 25, 2019

CS 1301 Exam 1 Spring 2014

cs1114 REVIEW of details test closed laptop period

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

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Test #2 October 8, 2015

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

Lecture 3 Tao Wang 1

Fundamentals of Programming (Python) Getting Started with Programming

CS 1301 Exam 1 Spring 2015

C ONTROL AND H IGHER O RDER F UNCTIONS

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Unit 3. Operators. School of Science and Technology INTRODUCTION

Variables, expressions and statements

Structure and Interpretation of Computer Programs

CS Exam 2 Name: Your Grading TA: This exam has 7 pages including the title page. Please check to make sure all pages are included.

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Structure and Interpretation of Computer Programs

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS 1301 Exam 1 Fall 2013

8. Control statements

The SPL Programming Language Reference Manual

Spring Semester 08, Dr. Punch. Exam #1 (2/12), form 1 B

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

Pupil Name. Year. Teacher. Target Level. Key Stage 3 Self-Assessment Year 9 Python. Spelling Test No 3. Spelling Test No 2. Spelling Test No 1

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS 1301 Exam 2 A Fall 2015

Getting Started with Python


Transcription:

Your Name: 1 / 6 CS 1301 CS1 with Robots Summer 2007 Exam 1 1. Vocabulary Matching: (15 points) Write the number from the correct definition in the blank next to each term on the left: _12_Print statement 1_Program 3_Runtime error _14_Semantic error _11_Syntax error 8_Floating point 6_Integer 4_Integer division 2_Keyword 7_Operator 5_Variable _10_Function _13_function call 9_Type conversion _15_Frame One point for each correct answer. 1. A sequence of instructions that specifies to a computer actions and computations to be performed. 2. A reserved word that is used by the compiler to parse a program; you cannot use things like if, def, and while as variable names. 3. An error that does not occur until the program has started to execute but that prevents the program from continuing. 4. An operation that divides one integer by another and yields an integer. It yields only the whole number of times that the numerator is divisible by the denominator and discards any remainder. 5. A name that refers to a value. 6. A Python data type that holds positive and negative whole numbers. 7. A special symbol that represents a simple computation like addition, multiplication, or string concatenation. 8. A format for representing numbers with fractional parts. 9. An explicit statement that takes a value of one type and computes a corresponding value of another type. 10. A named sequence of statements that performs some useful operation. They may or may not take parameters and may or may not produce a result. 11. An error in a program that makes it impossible to parse (and therefore impossible to interpret). 12. An instruction that causes the Python interpreter to display a value on the screen. 13. A statement that executes a function. It consists of the name of the function followed by a list of arguments enclosed in parentheses. 14. An error in a program that makes it do something other than what the programmer intended. 15. A box in a stack diagram that represents a function call. It contains the local variables and parameters of the function.

Your Name: 2 / 6 2. Vocabulary Matching Part 2 (15 points) Write the number from the correct definition in the blank next to each word: _11_Modulus operator 1_Boolean expression 6_Conditional statement 3_Comparison operator 7_Block _13_Recursion 9_Base case _12_Temporary variable _10_None 8_Guardian 2_Incremental development _14_Multiple assignment _15_Encapsulate 4_Generalize 5_Iteration One point per correct answer. 1. An expression that is either true or false. 2. A program development plan intended to avoid debugging by adding and testing only a small amount of code at a time. 3. One of the operators that compares two values: ==,!=, >, <, >=, and <=. 4. To replace something unnecessarily specific (like a constant value) with something appropriately general (like a variable or parameter). 5. Repeated execution of a set of statements using either a recursive function call or a loop. 6. A statement that controls the flow of execution depending on some condition. 7. A group of consecutive statements with the same indentation. 8. A condition that checks for and handles circumstances that might cause an error. 9. A branch of the conditional statement in a recursive function that does not result in a recursive call. 10. A special Python value returned by functions that have no return statement, or a return statement without an argument. 11. An operator, denoted with a percent sign (%), that works on integers and yields the remainder when one number is divided by another. 12. A variable used to store an intermediate value in a complex calculation. 13. The process of calling the function that is currently executing. 14. Making more than one assignment to the same variable during the execution of a program. 15. To divide a large complex program into components (like functions) and isolate the components from each other (by using local variables, for example).

Your Name: 3 / 6 3. Write Code ( 5 points ) Write a function get_number that prompts the user to enter a number and returns a floating point value. You do NOT need to check for errors. (Assume the user always enters a valid number). def get_number(): num_str = raw_input("enter a number!") num_float = float(num_str) return(num_float) +1 pts = Function prompts user for number +2 pts = Function returns the value +2 pts = Function converts string to float. 4. Write Code (5 points) Write a function return_largest that accepts 3 parameters (x,y,z) and returns the largest of the three. For example, return_largest(7, 34, 23.8) should return 23.8. def return_largest(x,y,z): if (X >= Y) and (X >= Z): return(x) elif (Y >= X) and (Y >= Z): return(y) else: return(z) +2pts = function takes 3 parameters +2pts = correct value is selected +1pts = value is returned. Test cases to try out on your code: return_largest(4,4,5) return_largest(5,5,4) 5. Write Code (5 points) Write a function draw_a_square() that will drive your scribbler robot in a square (polygon with 4 equal sides and four 90 degee corners), and beep at each of the four corners. You may assume that turnright(1, 0.5) will turn your scribbler exactly 90 degrees. You do not need to include the from myro import * or initialize() calls, and you may assume they have already been done for you. Do NOT use a loop, and do NOT use recursion. def draw_a_square(): forward(1,1) turnright(1,0.5) beep(1,800) <above repeated 3 more times> +2 = does a forward,turn, and beep each time, in any order (but order must be consistant) +2 = repeats 4 times without for/while loops +1 = in a function

Your Name: 4 / 6 def n_lines(n): if n > 0: print L ine! n_lines(n 1) 6a. Program Comprehension (1 point) How many times will the string Line! be printed when n_lines is called with n=4? Number Four Note that 0 > 0 is False, so the print statement does not execute when N=0. 6b. Simple Stack Diagram (4 points) Draw a stack diagram for the function n_lines called with n = 4. (i.e. n_lines(4) ) Include the value of any local variables, and remember to start with main. main N = 4 N = 3 N = 2 N = 1 N = 0 + 2 points stack has 5 function calls of n_lines. +1 point starts with main +1 point N = 4,3,2,1,0 in the 5 calls. 7. Write Code (4 points) Write a function with infinite recursion named run_forever. Your function should have no parameters, and it should run forever when called (on an ideal computer, in a real computer it would eventaully run out of memory.) You may add a print statement if you wish. def run_forever() run_forever() # This is a recursive call +2 points it's a function +2 points that calls itself, without a termination clause.

Your Name: 5 / 6 8. Write Code (6 points) Rewrite the function n_lines from question 6 using a for loop instead of recursion. def n_lines(n): +1 pt = it's in a function for i in range(n): +1 pt = function accepts N as a parameter print "Line!" +2 pt = The for statement is correct (executs N times) +2 pt = Function has the same behavior as the original: Prints Line! N times. 9. Write Code (8 points) Rewrite the function n_lines from question 6 and 8 using a while loop instead of recursion or a for loop. You may use n as your looping variable. def n_lines(n) while N > 0: print "Line!" N = N 1 +1 pt = it's in a function +1 pt = Function accepts N as a parameters +2 pts = While statement is correct +2 pts = Looping Variable is decremented +2 pts = Function has same behavior as the original: Prints Line! N times. 10. Stack Diagram ( 4 points) Draw a stack diagram for the code you wrote for problem 9 when n_lines is called with n=4 (e.g. n_lines(4)). Remember to start with main and to show all local variables. If local variables change durring execution, strike them out and show the new value each time they change. Main N = 4 N = 3 N = 2 N = 1 N = 0 + 2 = has 2 frames, main and n_lines +2 = Shows N (or looping variable) decrementing as the while loop runs. ( 1 point if they forget the last value of the looping variable that makes the continuation condition turn false.)

Your Name: 6 / 6 11. Python Expression Evaluation (14 points) Pretend that you are the Python Interpreter (IDLE window). What do you print or return when each of the following statements are entered? Example: (7+4) / 2 Result: 5 Example: range(4) Result: _ [0, 1, 2, 3]_ 1. (7.0 + 4 ) / 2 Result: 5.5 2. 7 + 3 / 2 Result: 8 3. range( 4, 8) Result: [4,5,6,7] 4. range(4, 8, 2) Result: [4,6] 5. 7.0 > 5.0 Result: True 6. 7 + 3 / 2 > 8 Result: False 7. print "Pumpkin %.3f" %3.1459 Result: Pumpkin 3.146 Grading: 2 points for each correct answer. 12. Extra Credit (1 point) What is the name you gave your robot? Grading: Any name gets a point! 13. Extra Credit (2 points) Where does Python get it's name? Monty Python's Flying Circus, a British comedy TV show. 14. Extra Credit ( 3 points) What are Isaac Asimov's 3 laws of robotics? 1. A robot may not injure a human being or, through inaction, allow a human being to come to harm. 2. A robot must obey orders given to it by human beings except where such orders would conflict with the First Law. 3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Law. Grading: 1 point for each law that is close.