0% found this document useful (0 votes)
99 views12 pages

Quiz 1b - Logic and Conditionals

This document contains a quiz on logic and conditionals from an introduction to interactive programming in Python. It consists of 9 multiple choice questions testing concepts like if/elif statements, Boolean logic, print/return statements, computing digits of integers, random number generation, implementing mathematical functions, compound interest, and calculating regular polygon areas. The questions include examples to explain the concepts and the correct answers are provided with explanations.
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)
99 views12 pages

Quiz 1b - Logic and Conditionals

This document contains a quiz on logic and conditionals from an introduction to interactive programming in Python. It consists of 9 multiple choice questions testing concepts like if/elif statements, Boolean logic, print/return statements, computing digits of integers, random number generation, implementing mathematical functions, compound interest, and calculating regular polygon areas. The questions include examples to explain the concepts and the correct answers are provided with explanations.
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/ 12

An Introduction To Interactive Programing In Python (Part 1)

by Joe Warren, Scott Rixner, John Greiner, Stephen Wong

Quiz 1b – Logic and conditionals Skanda S Bharadwaj


Question 1
An if statement can have how many elif parts?

Your Answer Score Explanation

ü Unlimited, i.e., 0 or more Correct 10.00 Correct.

Total 10.00 / 10.00


Question 2
Consider the Boolean expression not (p or not q) . Give the four following values in order, separated only by spaces:

• the value of the expression when p is True , and q is True ,


• the value of the expression when p is True , and q is False ,
• the value of the expression when p is False , and q is True ,
• the value of the expression when p is False , and q is False ,

Remember, each of the four results you provide should be True or False with the proper capitalization.

Answer for Question 2


You entered:

Your Answer Score Explanation

False Correct 2.50

False Correct 2.50

True Correct 2.50

2
False Correct 2.50

Total 10.00 / 10.00

Question 3
A common error for beginning programmers is to confuse the behavior of print statements
and return statements.

• print statements can appear anywhere in your program and print a specified value(s) in the console. Note
that execution of your Python program continues onward to the following statement. Remember that
executing a print statement inside a function definition does notreturn a value from the function.
• return statements appear inside functions. The value associated with the return statement is substituted

for the expression that called the function. Note that executing a return statement terminates execution
of the function definition immediately. Any statements in the function definition following
the return statement are ignored. Execution of your Python code resumes with the execution of the
statement after the function call.

As an example to illustrate these points, consider the following piece of code:


def do_stuff():

print "Hello world"

return "Is it over yet?"

3
print "Goodbye cruel world!"

print do_stuff()

Note that this code calls the function do_stuff in the last print statement. The definition of do_stuff includes
two print statements and one return statement.

Which of the following is the console output that results from executing this piece of code? While it is trivial to solve this question by cutting
and pasting this code into CodeSkulptor, we suggest that you first attempt this problem by attempting to execute this code in your mind.

Your Answer Score Explanation

ü Hello world Correct 10.00


Is it over yet?

Hello world

Hello world
Is it over yet?
Goodbye cruel world!

Hello world
Is it over yet?

4
Goodbye cruel world!
Is it over yet?

Total 10.00 / 10.00

Question 4
Given a non-negative integer n , which of the following expressions computes the ten's digit of n ? For example, if n is 123, then we want the

expression to evaluate to 2.

Think about each expression mathematically, but also try each in CodeSkulptor.

Your Answer Score Explanation

ü (n // 10) % 10 Correct 4.00 This expression computes the ten's digit correctly.

ü ((n - n % 10) % Correct 4.00 This expression computes the ten's digit correctly. This is a relatively
100) / 10 complicated expression to accomplish the goal.

(n % 10) / 10 Correct 2.00 This expression does not compute the ten's digit correctly.
Remember, try the expression in CodeSkulptor.

5
Total 10.00 /
10.00

Question 5
The function calls random.randint(0, 10) and random.randrange(0, 10) generate random numbers in different ranges. What number can

be generated by one of these functions, but not the other? (Refer to the CodeSkulptor documentation.)

By the way, we (and most Python programmers) always prefer to use random.randrange() since it handles numerical ranges in a way that is

more consistent with the rest of Python.

Answer for Question 5


You entered:

Your Answer Score Explanation

10 Correct 10.00

Total 10.00 / 10.00

6
Question 6
Implement the mathematical function f(x) = -5 x5 + 69 x2 - 47 as a Python function. Then use Python to compute the function
values f(0),f(1), f(2), and f(3). Enter the maximum of these four values calculated.

Answer for Question 6


You entered:

Your Answer Score Explanation

69 Correct 10.00

Total 10.00 / 10.00

Question 7
When investing money, an important concept to know is compound interest. The equation FV = PV (1+rate)periods relates the following four
quantities.

• The present value (PV) of your money is how much money you have now.
• The future value (FV) of your money is how much money you will have in the future.
• The nominal interest rate per period (rate) is how much interest you earn during a particular length of
time, before accounting for compounding. This is typically expressed as a percentage.

7
• The number of periods (periods) is how many periods in the future this calculation is for.

Finish the following code, run it, and submit the printed number. Provide at least four digits of precision after the decimal point.

def future_value(present_value, annual_rate, periods_per_year, years):

rate_per_period = annual_rate / periods_per_year

periods = periods_per_year * years

# Put your code here.

print "$1000 at 2% compounded daily for 3 years yields $", future_value(1000, .02, 365, 3)

Before submitting your answer, test your function on the following example. future_value(500, .04, 10, 10) should

return745.317442824.

Answer for Question 7


You entered:

Your Answer Score Explanation

1061.83480113 Correct 10.00 Correct.

Total 10.00 / 10.00

8
Question 8
There are several ways to calculate the area of a regular polygon. Given the number of sides, n, and the length of each side, s, the polygon's area
is

¼ n s2 / tan(π/n).

For example, a regular polygon with 5 sides, each of length 7 inches, has area 84.3033926289 square inches.

Write a function that calculates the area of a regular polygon, given the number of sides and length of each side. Submit the area of a regular
polygon with 7 sides each of length 3 inches. Enter a number (and not the units) with at least four digits of precision after the decimal point.

Note that the use of inches as the unit of measurement in these examples is arbitrary. Python only keeps track of the numerical values, not the
units.

Answer for Question 8


You entered:

Your Answer Score Explanation

32.7052437114 Correct 10.00 Correct.

Total 10.00 / 10.00

9
Question 9
Running the following program results in the error SyntaxError: bad input on line 8 ('return'). Which of the following describes
the problem?

def max_of_2(a, b):

if a > b:

return a

else:

return b

def max_of_3(a, b, c):

return max_of_2(a, max_of_2(b, c))

Your Answer Score Explanation

Wrong number of arguments in


function call

Misspelled keyword

10
Missing colon

Missing parenthesis

Misspelled function name

ü Incorrect indentation Correct 10.00 Correct. The body of the function definition
for max_of_3() should be indented, but it is not.

Misspelled variable name

Extra parenthesis

Total 10.00 /
10.00

Question 10
The following code has a number of syntactic errors in it. The intended math calculations are correct, so the only errors are syntactic. Fix the
syntactic errors.

11
Once the code has been fully corrected, it should print out two numbers. The first should be 1.09888451159. Submit the secondnumber printed
in CodeSkulptor. Provide at least four digits of precision after the decimal point.

define project_to_distance(point_x point_y distance):

dist_to_origin = math.square_root(pointx ** 2 + pointy ** 2)

scale == distance / dist_to_origin

print point_x * scale, point_y * scale

project-to-distance(2, 7, 4)

Answer for Question 10


You entered:

Your Answer Score Explanation

3.84609579056 Correct 10.00 Correct.

Total 10.00 / 10.00


12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy