Quiz: Intro to Python
1. Which of the following is a correct way to name a variable and set its value to 5 in python? There may be more than one correct answer.
A. my_var = 5
- Correct. This is an acceptable variable name.
B. my var = 5
- Incorrect. Variable names cannot contain spaces.
C. myvar = 5
- Correct. This is an acceptable variable name.
D. 5 = my_var
- Incorrect. The variable name should be on the left with the assigned value on the right.
E. myvar: = 5
- Incorrect. ':' Has a purpose in python so it can't be used in a variable name.
F. myvar! = 5
- Incorrect. '!' Has a purpose in python so it can't be used in a variable name.
2. What is wrong with this 'if' statement that will keep it from working?
if y > 3
print(x)
print(x)
A. Incorrect symbol used for 'greater than'
- Incorrect. It is actually the correct symbol for 'greater than'.
B. Incorrect 'if' statement syntax
- Correct. There is a missing indentation and colon.
C. Incorrect order of 3 and y in the if statement
- Incorrect. They are in the correct order as shown
3. Which is the simplest correct way to program the following equation in python?
$$y = 5x^4 + 2x^3 + 14x^2 + 3x + 7$$
A. y = 5x4 + 2x3 + 14x2 + 3x + 7
- Incorrect. You will need to use the multiplication symbol in python as well as a symbol that will make an exponent.
B. y = 5x^4 + 2x^3 + 14x^2 + 3x + 7
- Incorrect. You will need to use a multiplication symbol and the correct symbol for creating an exponent.
C. y = 5*x^4 + 2*x^3 + 14*x^2 + 3*x + 7
- Incorrect. The correct symbol for making an exponent is **, not ^.
D. 5 = y = 5*x**4 + 2*x**3 + 14*x**2 + 3*x + 7
- Correct. The variable name should be on the left with the assigned value on the right.
E. y = 5*x*x*x*x + 2*x*x*x + 14*x*x + 3*x + 7
- Correct. This formula would work but takes longer to write than a simpler one.
4. What is the final value of z?
x = 3.897
y = 4.125
w = 8.796
z = x**2 + 3*y**3 + 2*w
z = z/z
y = 4.125
w = 8.796
z = x**2 + 3*y**3 + 2*w
z = z/z
A. 243.347
- Incorrect.
B. 1
- Correct.
C. 0
- Incorrect.
5. What is the final value of x?
y = 2
x = 3
#x = 4
z = 55
#y = 3
x = z + x + y
x = 3
#x = 4
z = 55
#y = 3
x = z + x + y
A. 60
- Correct. Ignore the lines with the comment character.
B. 61
- Incorrect. Ignore the lines with the comment character
C. 62
- Incorrect. Ignore the lines with the comment character
D. 63
- Incorrect. Ignore the lines with the comment character
6. Which of the following are valid functions in Python?
# A
def square_me(x)
return x^2
# B
def square_me(x)
return x**2
# C
function square_me(x)
return x**2
# D
function square_me(x) :
return x^2
# E
def square_me(x) :
return x**2
def square_me(x)
return x^2
# B
def square_me(x)
return x**2
# C
function square_me(x)
return x**2
# D
function square_me(x) :
return x^2
# E
def square_me(x) :
return x**2
A.
- Incorrect. There are 2 problems: no ":" and x^2 should be x**2
B.
- Incorrect. No, there needs to be a ":" at the end of the first line
C.
- Incorrect. The keyword is "def" not "function", and we need a ":"
D.
- Incorrect. The keyword is "def" not "function"
E.
- Correct.