Assignment 3
Corresponds to: Session 3
Quiz
Instructions
Choose the correct option for each question.
1. What will be the type of x after execution?
A. int
B. str
C. float
D. Error
Answer
Correct answer: B
Explanation: The second statement assigns the value "hello" to x and that overwrites the previous value 10. The type of x afterthe second statement is str
2. Which of the following statements about Python lists is true?
A. Lists are immutable
B. Lists can contain elements of different data types
C. Lists cannot contain other lists
D. Lists have fixed size
Answer
Correct answer: Only B is true
Explanation:
- A is false because a list is mutable and the value of its elements can be changed.
- B is true because a list can contain elements of different types and there is no restriction that all elements must be of the same type as in the case of an array
- C is bfalse because a list can contain other lists
- D is false as its size is not fixed. It can be increased using
append()orinsert()methods and decreased using 1pop()orremove()` methods.
3. What will be the output?
A. 3
B. 4
C. 5
D. Error
Answer
Correct answer: B
Explanation: The length of a list returned by the function len() is the number of elements in a list. Elements of a list can be of differetnt types, and a list can contain other lists as its elements. The list after the a.append([4, 5]) is a = [1, 2, 3, [4, 5]]. The last element is a list [4, 5] and is counted as one element not two elements. Therefore the number of elements in a is 4. The length of a[3] is 2.
4. What does range(2, 10, 3) generate?
A. 2, 5, 8
B. 2, 5, 8, 11
C. 3, 6, 9
D. 2, 3, 4, 5
Answer
Correct answer: A
Explanation: The start value is 2, the stop value (the element generated by the range is less than this value) is 10 and step is 3. Thus the sequence is 2, 5, 8, 11, 14,.... Since the stop value is 10, the range consists of only those values less than the stop value. Thus the result is 2, 5, 8. Note: The stop valus itself is not included in the range.
5. What will be the output?
A. 1 2 3
B. 0 1 2
C. 0 1 2 3
D. Error
Answer
Correct answer: B
Explanation: Here start = 0 since it is not specified (for start to be specified, there must be at least two arguments to range()). stop = 3 (when only one argument is specified) and step = 1 (for step to be specified, there must be three arguments to range()). Thus the series is 0, 1, 2, 3, 4, .... Since the stop = 3, only the values less than 3 are included in the range. Thus the result is 0, 1, 2.
6. Which statement about while loops is correct?
A. They always execute at least once
B. They execute only a fixed number of times
C. They execute while a condition is True
D. They cannot be nested
Answer
Correct answer: C
Explanation:
- A is false. Depending on the values of different variables, it is possible that the logical expression at the start of the
whileloop may evaluate toFalse, in which case the loop may not be executed even once. - B is false. The number of times a
whileloop executes is determined by the initial values of variables, the logical expression evaluated at the start of the loop and thechanges made to the controlling variables within the loop. This does not guarantee that the loop executes a fixed number of times. - C is true. The
whileloop executes as long as the logical expression at the start of the loop evaluates toTrue. - D is false. There is no restriction on nesting a
whileloop within an outerwhileloop.
7. What will happen when the following code is executed?
A. Prints all values successfully
B. Raises TypeError
C. Raises ZeroDivisionError
D. Infinite loop
Answer
Correct answer: C
Explanation: On the second iteration of the for loop, x = 0 and 10 / 0 results in ZeroDivisionError exception and the loop is interrupted.
8. What is the purpose of the continue statement?
A. Exit the loop completely
B. Skip the current iteration and continue with the next
C. Restart the loop
D. Pause execution
Answer
Correct answer: B
Explanation: The continue skips the rest of the statements in the loop only for the current iteration and will continue to the next iteration.
9. What will be the output?
A. --abc
B. abc--
C. -abc-
D. abc
Answer
Correct answer: C
Explanation: The number of additional places in a field width of 5 is 5 - len(x) which is 2. Then there is one place on either side of the string x. Since the second argument is -, the places on either side of the string are filled with -, resulting in -abc-.
10. Which of the following is a valid function definition?
A.
B.
C.
D.
Answer
Correct answer: B
Explanation:
- A is invalid. The parentheses following the function name
funcare required, not optional. - B is valid.
- C is invalid. Keyword to start a function definition is
def, not function. - D is invalid. Name of the function
funcmust be floowed by parentheses, not square brackets.
11. What will be the output?
A. <class 'int'>
B. <class 'list'>
C. <class 'tuple'>
D. Error
Answer
Correct answer: C
Explanation: When a function requires to return multiple output values, they are bundled together into a single tuple and returned to the parent function. While on the surface it appears that the function is returning multiple items, it is in fact returning a single object with multiple elements contained in it. The result returned is a tuple and not a list because a tuple is immutable, that is, the values returned cannot be changed. They can be copied into other objects in order to change the copies but the original elements returned are immutable.
12. Which statement about function parameters is correct?
A. Parameter names must match argument names
B. Parameters must always have type annotations
C. Parameters act as placeholders for arguments
D. Functions must have at least one parameter
Answer
Correct answer: C
Explanation:
- A is incorrect. Parameter names need not match argument names although they can if you wish
- B is incorrect. Type annotations are optional and only indicate intent. The Python interpreter ignores the type annotations. Some code editors use them to prompt programmers to show the intent of the designer of the function.
- C is correct. Values of the arguements take the place of the parameters for each invocation of the function.
- D is incorrect. It is permitted to have zero parameters for a function.
13. What will be the output?
A. [1, 2, 3]
B. [10, 2, 3]
C. Error
D. [10]
Answer
Correct answer: B
Explanation: The statement b = a makes b an alias (alternate name) for a. This can be verified by checking that a is b evaluates to True. Thus, any change made to a automatically reflects in b, as it is merely another name by which we refer to a.
14. Which of the following is not a sequence type?
A. list
B. tuple
C. range
D. set
Answer
Correct answer: C
Explanation: A range is not a collection but what in Python is called a generator. While it generates a sequence of values, it does so one at a time. On the otherhand, a collection such a list, tuple or a set is collection of zero or more elements all stored in memory at the same time, not one at a time. We can create a list, tuple or a set from a range quite easily, using list(range(5)), tuple(range(5)) or set(range(5)).
15. What is the output?
A. True
B. False
C. Error
D. None
Answer
Correct answer: A
Explanation: This expression is evaluated by generating a (list(range(1, 5))), which is [1, 2, 3, 4] and testing if 3 is in it, which it does. Therefore it returns True.