0% found this document useful (0 votes)
42 views10 pages

CS 1114 Midterm 1 2019

Uploaded by

diyahegdebhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views10 pages

CS 1114 Midterm 1 2019

Uploaded by

diyahegdebhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

NYU – Tandon School of Engineering

Brooklyn, NY 11201

CS-UY 1114 / Python


First Midterm Exam – 22 October 2019

Name: __________________________________________________

NetID(first part of email):___________________________________

● Duration: 1 hour, 15 minutes


● DO NOT WRITE ON THE BACK OF ANY PAGE!
● Do not separate any page.
● Please do not use pencil, if you must, write darkly, these pages will be scanned
● If you write an answer other than in the space provided, please indicate in the space
provided, where we can find that answer
● This is a closed book exam, no calculators are allowed
● You can expect that the user inputs the appropriate values (int/float/etc where required).
● Comments are not required
● Anyone found cheating on this exam will receive a zero for the exam.
● Anyone who is found writing after time has been called will receive a zero for this exam.
● Do not open this test booklet until you are instructed to do so.
● If you have a question please ask the proctor of the exam!
● Descriptive variable names are required
● Answers that are unnecessarily convoluted will result in a point deduction.
● You may use ONLY the following Python constructs and functions!
math operators: +, -, /, //, %, *, ** conditional statements: if, elif, else

boolean operators: and, or, not loops: while and for something
in range

all functions in modules random, turtle, coercion functions: int, str, float
and math
assignment operator (=) and augmented print, input
assignment
operators: +=, -= , *=, /=, //=, %=, **=

comparison operators: ==, !=, <, <=, >, >=


Name _________________________________________ Net ID: ___________________

Question 1 (15 points; 5 points each):


a) Consider the following code typed into a python shell
>>> a_string = “basketball”
>>> print(a_string[6:] + a_string[4:6])
What would be the output from this code?
i) baseball
ii) ballet
iii) bball
iv) baskets
v) ball

b) Consider the following code typed into a python shell


>>> print(a_string[0:len(a_string) - 2] + a_string[::-1])
What would be the output from this code?
i) basketballlabteksab
ii) basketballbasketball
iii) basketballabteksab
iv) llabteksabbasketball
v) Basketballabteksab

c) Consider the following code typed into a python shell


>>> a_string = “basketball”
>>>index = a_string.find("b", a_string.find("b") + 1)
>>>print(a_string[0] + a_string[index:])

i) bball
ii) bbasketball
iii) basketball
iv) ball
v) basket

1
Name _________________________________________ Net ID: ___________________

Question 2 (15 points):


Trace the output of the following code. If you find an error, please write “ERROR,” if you find an
infinite loop, please write “INFINITE LOOP.”

In your output, be sure visually represent any space with a ◡ character.


for i in range(1,5,2):
for j in range(i+1, 10, 3):
if j%2!=0:
print("i=",i," j=",j);

Output:
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________

Question 3 (15 points; 3pts each):


Given the following variables and values, print out the results of the following boolean
expressions (in the result column, write “True”, “False” or “Error”)

alpha = True

beta = True

gamma = False

delta= False

Expression Result

alpha and beta and gamma

gamma or beta and delta

(alpha and beta) and (not beta or alpha)

(beta and alpha) or (not gamma and beta)

delta or (beta and alpha) or delta

2
Name _________________________________________ Net ID: ___________________

Question 4 (25 points):


We are designing an optical recognition system for a cheese manufacturing plant. The plant
makes four types of cheeses “Cheddar,” “Feta,” “Swiss,” or “Gouda.” A camera will take a
picture of a block of cheese and automatically measure the side lengths and angles of the block
of cheese. The automated program will pass, to your program, the length of two adjacent sides
(decimal real numbers) and two adjacent angles (always integers), you should output what type
of cheese the block is.

For the purposes of this problem, you may use the following definitions
● A “Cheddar” block has two 90 degree angles and all sides are the same length
● A “Feta” block has two 90 degree angles and two adjacent sides have different lengths
● A “Swiss” block has adjacent sides with different lengths and the sum of the two adjacent
angles is not 180 degrees but the block is not Feta.
● A “Gouda” block has adjacent sides with the same length and the sum of the angles is
180 degrees but the block is not Cheddar.

What is the length of side 1:10 What is the length of side 1:5
What is the first angle:90 What is the first angle:90
What is the length of side 2:10 What is the length of side 2:10
What is the second angle:90 What is the second angle:90
CHEDDAR FETA

What is the length of side 1:10 What is the length of side 1:10
What is the first angle:90 What is the first angle:50
What is the length of side 2:5 What is the length of side 2:10
What is the second angle:30 What is the second angle:130
SWISS GOUDA

What is the length of side 1:10 What is the length of side 1:10
What is the first angle:30 What is the first angle:120
What is the length of side 2:5 What is the length of side 2:10
What is the second angle:150 What is the second angle:120
Not sure Not sure

(Please start your answer on page 4)

3
Name _________________________________________ Net ID: ___________________

Solution to Question 4:

4
Name _________________________________________ Net ID: ___________________

(Additional space for Question 4)

5
Name _________________________________________ Net ID: ___________________

Question 5 (30 points):


A digital cash register totals an amount that a person should pay based on what they purchase
in the store. Our items are simple, but we have some special sales that need to be addressed
in the software that totals the amount a customer should pay. You should write a program that
asks the user what product they purchased (all of our products are single letter items) and when
the user enters a zero, print on the screen, the total amount due.

Item code Price Special

A $1.75 Buy one, get one free

B $2.25 (none)

C $4.00 Half price if customer also purchased “B”


● Note: Item A is full price if the customer only purchases one. The second one is free.
Also note that the customer could buy more than 2, in which case all additional items are
paired (1= $1.75, 2= $1.75, 3 = $3.50, 4=$3.50, etc)
● Note: Any number of Item C are half price ($2.00) if the customer purchases item B. The
purchase of B doesn’t have to appear first, it may come after the customer purchases C
in which case the cost of C should be reduced appropriately.
● Note: the user will only, ever, input the letters A,B,C, or the number 0.
Please make your program look exactly as below:

Welcome to the cash register Welcome to the cash register


Which item?A Which item?B
Which item?A Which item?C
Which item?0 Which item?0
Your total is: 1.75 Your total is: 4.25

Welcome to the cash register Welcome to the cash register


Which item?C Which item?A
Which item?C Which item?A
Which item?B Which item?A
Which item?0 Which item?C
Your total is: 6.25 Which item?0
Your total is: 7.5

(Please start your answer on page 7)

6
Name _________________________________________ Net ID: ___________________

Solution to Question 5:

7
Name _________________________________________ Net ID: ___________________

(Additional space for Question 5)

8
Name _________________________________________ Net ID: ___________________

(SCRAP)

You might also like