Quiz: Python Loops and Lists
Main.PythonQuiz8 History
Hide minor edits - Show changes to markup
4. Which of the following are valid functions in Python?
(:source lang=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
(:sourceend:)
(:div id=q4a:)
- Incorrect. There are 2 problems: no ":" and x^2 should be x**2
(:divend:)
(:div id=q4b:)
- Incorrect. No, there needs to be a ":" at the end of the first line
(:divend:)
(:div id=q4c:)
- Incorrect. The keyword is "def" not "function", and we need a ":"
(:divend:)
(:div id=q4d:)
- Incorrect. The keyword is "def" not "function"
(:divend:)
(:div id=q4e:)
- Correct.
(:divend:)
(:title Quiz: Python Loops and Lists:) (:keywords quiz, test, Python, list, array, loop, introduction, course:) (:description Learning assessment on Introduction to Python with Loops and Lists.:)
1. A "for" loop is typically used when the number of loop cycles is known.
(:div id=q1a:)
- Correct. Use a "for" loop to cycle through a specified number of times.
(:divend:)
(:div id=q1b:)
- Incorrect. Use a "while" loop when the number of cycles is based on a condition, not number of cycles.
(:divend:)
2. To skip an individual cycle of a for loop, the following command is used:
(:div id=q2a:)
- Incorrect. break terminates the loop
(:divend:)
(:div id=q2b:)
- Correct. continue initiates the next cycle of the loop
(:divend:)
(:div id=q2c:)
- Incorrect. Use a "continue" statement
(:divend:)
(:div id=q2d:)
- Incorrect. Use a "continue" statement
(:divend:)
3. What will the following code print as a result for sum?
(:source lang=python:) sum = 0 for i in range(5):
if i==3: continue sum = sum + i
print(sum) (:sourceend:)
(:div id=q3a:)
- Incorrect. Skip the 3 for 0+1+2+4 = 7
(:divend:)
(:div id=q3b:)
- Incorrect. Skip the 3 for 0+1+2+4 = 7
(:divend:)
(:div id=q3c:)
- Correct. Skip the 3 for 0+1+2+4 = 7
(:divend:)
(:div id=q3d:)
- Incorrect. Skip the 3 for 0+1+2+4 = 7
(:divend:)
4. Which of the following are valid functions in Python?
(:source lang=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
(:sourceend:)
(:div id=q4a:)
- Incorrect. There are 2 problems: no ":" and x^2 should be x**2
(:divend:)
(:div id=q4b:)
- Incorrect. No, there needs to be a ":" at the end of the first line
(:divend:)
(:div id=q4c:)
- Incorrect. The keyword is "def" not "function", and we need a ":"
(:divend:)
(:div id=q4d:)
- Incorrect. The keyword is "def" not "function"
(:divend:)
(:div id=q4e:)
- Correct.
(:divend:)