Thursday, September 12, 2013

Learn to Program: The Fundamentals - Week 1 Exercise


Score of 15.00 out of 15.00.
Answer the following questions. Some of the questions will require you to run code in IDLE's Python shell. (You should do this anyway to get used to using IDLE.)

Question 1

Select the function call(s) that run without error. Determine the answer using the description given by help(round), not by running the code.
Your Answer
Score Explanation
round(45.345, 2) Correct 0.20
round(45.345, 2, 5) Correct 0.20
round(45.8) Correct 0.20
round(45) Correct 0.20
round() Correct 0.20
Total
1.00 / 1.00
Question ExplanationThe function round takes one argument, as well as a second optional argument.
Help on built-in function round in module builtins:

round(...)
    round(number[, ndigits]) -> number
    
    Round a number to a given precision in decimal digits (default 0 digits).
    This returns an int when called with one argument, otherwise the
    same type as the number. ndigits may be negative.

Question 2

What type of value does built-in function id return? Determine the answer using the description given by help(id).
Your Answer
Score Explanation
int Correct 1.00
float


Total
1.00 / 1.00
Question ExplanationHere is the output of help(id). The return type is to the right of the arrow:
id(...)
    id(object) -> integer
    
    Return the identity of an object.  This is guaranteed to be unique among
    simultaneously existing objects.  (Hint: it's the object's memory address.)

Question 3

Consider this code:
x = 8 / 4
What value does x refer to?

You entered:

Your Answer
Score Explanation
2.0 Correct 1.00
Total
1.00 / 1.00
Question ExplanationThe division operation (/) produces a float. Note that the question just asks for a value, so don't include any other information other than the value.

Question 4

Consider this code:
x = 12 // 3
What value does x refer to?

You entered:

Your Answer
Score Explanation
4 Correct 1.00
Total
1.00 / 1.00
Question ExplanationThe integer division operation (//) produces an int. Note that the question just asks for a value, so don't include any other information other than the value.

Question 5

Consider this code:
x = 12 / 5
What value does x refer to?

You entered:

Your Answer
Score Explanation
2.4 Correct 1.00
Total
1.00 / 1.00
Question ExplanationThe division operation (/) produces a float. Note that the question just asks for a value, so don't include any other information other than the value.

Question 6

Consider this code:
x = 13 / 7
What value does x refer to?

You entered:

Your Answer
Score Explanation
1.8571428571428572 Correct 1.00
Total
1.00 / 1.00
Question ExplanationThe division operation (/) evaluates to a float. Note that the question just asks for a value, so don't include any other information other than the value.

Question 7

Consider this code:
x = 3
y = 5
x = y 
After the code above has executed, what value does x refer?

You entered:

Your Answer
Score Explanation
5 Correct 1.00
Total
1.00 / 1.00
Question ExplanationNote that the question just asks for a value, so don't include any other information other than the value.

Question 8

Consider this code:
x = 3
y = 5
x = y 
After the code above has executed, what value does y refer?

You entered:

Your Answer
Score Explanation
5 Correct 1.00
Total
1.00 / 1.00

Question 9

Consider this code:
apple = banana
When the code above is executed, what type of error occurs?
Your Answer
Score Explanation
NameError Correct 1.00
SyntaxError


Total
1.00 / 1.00
Question ExplanationThe name banana does not exist, so a NameError occurs.

Question 10

Select the legal Python name(s) below.
Your Answer
Score Explanation
18happy_day Correct 0.25
happy_day Correct 0.25
happy_45day Correct 0.25
_happy Correct 0.25
Total
1.00 / 1.00
Question Explanation
  1. Names must start with a letter or _.
  2. Names must contain only letters, digits, and _.

Question 11

Consider this code:
def f(data):
    return data * 0.5
Select the phrase that describes data.
Your Answer
Score Explanation
a function name


a parameter Correct 1.00
an argument


Total
1.00 / 1.00
Question ExplanationReview this terminology in the "Defining Functions" video and lecture summary.

Question 12

Consider this code:
def g(a, b):
    c = a + b
    return c
How many parameters does function g have?
Your Answer
Score Explanation
3


2 Correct 1.00
0


1


Total
1.00 / 1.00
Question ExplanationThe parameters are a and b. Review this terminology in the "Defining Functions" video and lecture summary.

Question 13

Consider this code:
data = 3
data2 = 7.5
result = min(data, data2)
Select the phrase that describes data in the third line.
Your Answer
Score Explanation
a function name


an argument Correct 1.00
a parameter


Total
1.00 / 1.00
Question ExplanationThe value of data is passed as an argument to function min.

Question 14

Consider this code:
round(45.342, 2)
What value does the expression above produce?

You entered:

Your Answer
Score Explanation
45.34 Correct 1.00
Total
1.00 / 1.00
Question ExplanationNote that the question just asks for a value, so don't include any other information other than the value.

Question 15

Consider this code:
def even_bigger(x):
    return (2 * x) ** x

even_bigger(12)
Which value does even_bigger(12) produce?
Your Answer
Score Explanation
2481152873203736576


36520347436056576 Correct 1.00
584318301411328


10240000000000


Total
1.00 / 1.00