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

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

cs1114 REVIEW of details test closed laptop period

1 Docstrings. COSC 101 Lecture #14 Handout: Defining Functions, Part 4 Spring 2015

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

CS 111X - Fall Test 1

CS 1301 Exam 1 Answers Fall 2009

Programming for Engineers in Python. Autumn

CS 111X - Fall Test 1 - KEY KEY KEY KEY KEY KEY KEY

CS 1301 Exam 1 Fall 2009

Python for Non-programmers

Spring 2017 CS 1110/1111 Exam 1

ENGR 101 Engineering Design Workshop

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

Programming for Engineers in Python. Recitation 1

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

CSC A20H3 S 2011 Test 1 Duration 90 minutes Aids allowed: none. Student Number:

The current topic: Python. Announcements. Python. Python

Strings and Testing string methods, formatting testing approaches CS GMU

Getting Started with Python

Introduction to computers and Python. Matthieu Choplin

Python Games. Session 1 By Declan Fox

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

MITOCW watch?v=0jljzrnhwoi

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

Introduction to Python (All the Basic Stuff)

CS 1301 CS1 with Robots Summer 2007 Exam 1

CS150 Sample Final. Name: Section: A / B

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Algorithms and Programming I. Lecture#12 Spring 2015

CS 1110, LAB 1: PYTHON EXPRESSIONS.

The Big Python Guide

Lecture 2: Variables & Assignments

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

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

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

The following content is provided under a Creative Commons license. Your support

Name Feb. 14, Closed Book/Closed Notes No electronic devices of any kind! 3. Do not look at anyone else s exam or let anyone else look at yours!

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Beyond Blocks: Python Session #1

Scripting Languages. Python basics

PREPARING FOR PRELIM 1

CPSC 217 Midterm (Python 3 version)

CS 2316 Exam 1 Spring 2014

Python for Non-programmers

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Copied from: on 3/20/2017

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

Try typing the following in the Python shell and press return after each calculation. Write the answer the program displays next to the sums below.

Conditionals and Recursion. Python Part 4

1) Log on to the computer using your PU net ID and password.

CSCE 110: Programming I

CS 2316 Exam 1 Spring 2013

UNIVERSITY OF TECHNOLOGY SYDNEY FACULTY OF ENGINEERING AND IT. Let's code with! DOCUMENTATION, MATERIAL, RESOURCES. (version 2)

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

Overview of List Syntax

Fundamentals of Programming (Python) Getting Started with Programming

Getting Started Values, Expressions, and Statements CS GMU

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.

Part III Appendices 165

Chapter 2 Input, Processing and Output. Hong Sun COSC 1436 Spring 2017 Jan 30, 2017

CSC148 Fall 2017 Ramp Up Session Reference

CS150 Sample Final Solution

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

CS1 Lecture 3 Jan. 22, 2018

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

CSCE 110 Programming I

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Python Intro GIS Week 1. Jake K. Carr

Question 1. Part (a) Part (b) December 2013 Final Examination Marking Scheme CSC 108 H1F. [13 marks] [4 marks] Consider this program:

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

ECS Baruch Lab 3 Spring 2019 Name

PROGRAMMING FUNDAMENTALS

CS 115 Lecture 4. More Python; testing software. Neil Moore

CS 1301 Exam 1 Fall 2014

CS 1301 Exam 1 Spring 2014

CS 1301 Exam 1 Fall 2014

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

R in Linguistic Analysis. Week 2 Wassink Autumn 2012

Tic Tac Toe Game! Day 8

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

Chapter 2 C++ Fundamentals

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

CS 1301 Exam 1 Spring 2014

Programming in Python 3

CS8 Final Exam E03, 09M, Phill Conrad, UC Santa Barbara 09/10/2009

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

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

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Computer and Programming: Lab 1

Introduction to Java Unit 1. Using BlueJ to Write Programs

Student Number: Instructor: Brian Harrington

ICS 31 UC IRVINE. ############## YOUR UCINET #### K E Y #### ############## Second Midterm YOUR NAME YOUR STUDENT ID (8 DIGITS)

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

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

AP Computer Science Unit 1. Programs

CS1 Lecture 3 Jan. 18, 2019

Lists, loops and decisions

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

Transcription:

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

For the following questions, use these variable definitions a = 36 b = 3 c = 12 d = '3' What is the type and value of each of the following expressions or, if it won't compile, circle that answer type value circle if will not compile 1 (2 pts) a // b int 12 will not compile 2 (2 pts) a % b int 0 will not compile 3 (2 pts) (c * 8) / 12 float 80 will not compile 4 (2 pts) str( b + 2 ) * int( c ) str '5' will not compile 5 (2 pts) hex( 17 ) str '0x10' will not compile must have quotes of some sort 6 (2 pts) 5 if 4 == 3 else 2 int 2 will not compile 7 (2 pts) None NoneType None will not compile 8 (2 pts) a < c and b == 1 bool False will not compile spelling counts and caps count 9 (2 pts) b + c float 42 will not compile 10 (2 pts) b < 4 or not will not compile 11 (2 pts) b > c >= 0 will not compile 12 (2 pts) (on the next several lines hi lo str 'hi\nlo' will not compile must be str and have newline

13 (5 pts) Given: p = hello i = 'grail' print( p, end='' ) print( p, i, sep = '22' ) Show what will be on the screen after these statements are executed If there will be a runtime error or this will not compile, state that here hellohello22grail A programmer has been hired by a company to write a function that returns a random value between minimum and maximum values set by the company These are inclusive Note that this question is NOT about branching Do NOT write this function Do NOT write any code for this question ONLY answer this question: 14 (10 pts ) What are the pieces of information this programmer needs to solve this subproblem? For each, state the source of information and how each would be implemented in python (If there are no needed pieces of information, just state none) Consider both good design as well as the fact that the company has a very specific minimum length INFORMATION NEEDED SOURCE HOW TO IMPLEMENT random valuecaller (2 pt) calculated locally assignment or random call (2 pt) cannot be known (-2 pt) cannot be device input (-8!) the writer of this function cannot need to know where their caller got the information passed in minimum (2 pt) known (2pt) OR caller (2 pt) global constants or parameters maximum (2 pt) [could be either] [caller would probably pass in the global] 15 (3 pts ) What design issue does this programmer not understand?? def amtuserowesisdivisibleby( amountuserowes, divisor ): returns True when the amount the user owes is evenly divisible by the passed in divisor, otherwise False Both parameters must be ints The user can't owe any value not evenly divisble by divisor return amountuserowes % divisor == 0 The designer of a function cannot need to know how the caller will use their function (and there is no user)

16 (5 pts ) When you want to call a function, what must you NOT NEED TO READ? (circle only one letter) a) the def line (the first line of the function definition) b) the docstring c) the body of the function d) the call of the function e) c) and d) only f) a) and c) only g) a), b) and c) only h) b), c), and d) only 17 (3 pts ) else: is a statement (circle your answer) YES NO 18 (10 pts ) In a raffle, there are 20 possible prizes and a consolation prize The winning user's prize has already been chosen Now it's time to print a line on the screen telling the winning user which prize they won Which problem form is controlling the output? If you can't answer which of the problem forms, for fewer points you may answer by stating which python statement should be used to control the screen display one of many, one must or for a maximum of 4 points: if-elif-elfi- else 19 (5 pts ) If defensive programming, were implemented, how would the code for #18 be different? Do NOT write any code The last else must be changed to an elif and an else must be added (with the defensive handling code, like exit or something)

CS1114 Test ONE Spr15 ANSWER KEY 20 PARTS ONE, TWO and THREE (18 pts together, 6 pts each) def callthee( ): 0 print( 'THEE' ) 1 x = 5 2 p = callme( x ) 3 print x 4 return( p ) 5 def callme( p ): 6 print( 'ME' ) 7 p = 3 8 x = p + 2 9 return 4 PART FOUR (7 pts ) For example: This arrow shows where x refers to after line 1 x PART ONE (6 pts) Draw the arrow showing where p refers after line 5 p PART TWO (6 pts) Draw the arrow showing where p refers after line 7 p PART THREE (6pts) Draw the arrow showing where x refers after line 8 x Write the values that will be in RAM 6 5 4 3 2 1 What is the output when main is called? def main(): write your output here print( callthee( ) ) THEE ME main() 5 4

CS1114 Test ONE Spr15 Print your name legibly: circle your last name Sign: Poly-ID: 21 (20 pts) You are given the randylines module which contains these functions (shown with their docstrings) def drawlineofranddigit( howlong ): displays a line howlong wide using a single randomly chosen digit and positions the cursor onto the next line on the screen assumes howlong is an interger value For example: drawlineofranddigit( 7 ) would produce this output: 4444444 if the randomly chosen digit were 4 # do NOT write the body of this function def drawfourranddigits( ): Displays a four digit long line where each digit is chosen randomly For example drawfourranddigits( ) might display: 5232 or 7744 or even 5555 # do NOT write the body of this function def drawlineofdigit( digit, width ): displays a line width wide using the digit passed in and positions the cursor onto the next line on the screen assumes width and digit are interger values For example drawlineofdigit( 3, 4 ) would display: 3333 # do NOT write the body of this function This question continues on the next page WRITE ANSWERS TO THESE QUESTIONS IN THIS DOCUMENT page 6 of 8 pages (counting the cover sheet)

CS1114 Test ONE Spr15 Print your name legibly: circle your last name Sign: Poly-ID: (21 cont) Calling only the functions in the randylines module and the random module and the print function, create these two functions: (CORRECTION ANNOUNCED DURING TEST) --- a function that displays a 4 x 4 square made of random digits 2287 For example calling this function might produce this pattern: 5388 --- a function that displays a line made of a single random digit that is of random length - between 5 and 31 digits (incl) For example calling this function might produce this pattern: 88888888888888888 (You may not use looping in these two functions) 7760 2704 DO write the docstrings for the these two functions Write only these two defs no main, no program level docstrings You do not have to write from future import print_function, we'll assume it's written import randylines import random def draw4x4squareofrandomdigits( ): displays a 4 x 4 square made of random digits randylinesdrawlineofranddigit( 4 ) randylinesdrawlineofranddigit( 4 ) randylinesdrawlineofranddigit( 4 ) randylinesdrawlineofranddigit( 4 ) def drawlineofsingleranddigitbtwn5and31long( ): displays a line made of a single random digit that is of random length - between 5 and 31 digits, incl randylinesdrawlineofranddigit( randomrandint( 5, 31 ) ) OR randylinesdrawlineofdigit ( randomrandint( 0, 9 ), randomrandint( 5, 31 ) ) WRITE ANSWERS TO THESE QUESTIONS IN THIS DOCUMENT page 7 of 8 pages (counting the cover sheet)

22 (15 pts ) There is a builtin function you have never used before named omytomm Here is a SnakePit session showing the help for that function IDLE 271 >>> help( omytomm ) Help on built-in function omytomm in module builtin : omytomm() omytomm(object, object, object) -> str The first two parameters must be strings, third must be int Returns the str made of the first and second parameters concatenated together and repeated the third parameter number of times Eg: "Hi", "Lo", 3 yields the string: "HiLoHiLoHiLo" (3 concatenated "HiLo"'s) Obviously you cannot define this function because it is built in Your job is to complete drawfigurewithstrs by writing the needed parameters and python statements that make the function do what the docstring says it will do Even though it's not the best, the only functions you can call are omytomm and other builtin functions You do not need to show any testing for your function Do NOT write a main function (obviously) or a complete program (obviously) ONLY complete the definition for drawfigurewithstrs (don't forget the parameters!) def drawfigurewithstrs( first, second ): Given two strings, draws a pattern on the screen made of the strings jammed together in a nice way Here is an example of the output when the strings are "John" and "Mary" There are always these five lines with each str as shown on each line: JohnMaryJohnMaryJohnMary Mary-John John---Mary Mary-----John MaryJohnMaryJohnMaryJohnMaryJohn Assume both parameters are strs print( omytomm( first, second, 3 ) ) print( second, first, sep='-') print( first, second, sep='---') print( second, first, sep='-----') print( omytomm( second, first, 4 ) ) [points off for missing sep-] [points off for missing sep---] [points off for missing sep-----] Can also use '%s' % Not sure how?! Ok if replicate the sep: sep='-'*3 and sep='-'*5 Write your answer to this question in your BLUEBOOK page 8 of 8 pages (counting the cover sheet)