Test Series - python

Test Number 39/108

Q: What will be the output of the following Python code?

def f(i, values = []):
    values.append(i)
    return values
 
f(1)
f(2)
v = f(3)
print(v)
A. [1] [2] [3]
B. [1] [1, 2] [1, 2, 3]
C. [1, 2, 3]
D. 1 2 3
Solution: Execute in the shell to verify
Q: What will be the output of the following Python code?

numbers = [1, 2, 3, 4]
 
numbers.append([5,6,7,8])
 
print(len(numbers))
A. 4
B. 5
C. 8
D. 12
Solution: A list is passed in append so the length is 5.
Q: To which of the following the “in” operator can be used to check if an item is in it?
A. Lists
B. Dictionary
C. Set
D. All of the mentioned
Solution: In can be used in all data structures.
Q: What will be the output of the following Python code?

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
 
print(len(list1 + list2))
A. 2
B. 4
C. 5
D. 8
Solution: + appends all the elements individually into a new list.
Q: What will be the output of the following Python code?

def addItem(listParam):
    listParam += [1]
 
mylist = [1, 2, 3, 4]
addItem(mylist)
print(len(mylist))
A. 1
B. 4
C. 5
D. 8
Solution: + will append the element to the list.
Q: What will be the output of the following Python code?

def increment_items(L, increment):
    i = 0
    while i < len(L):
        L[i] = L[i] + increment
        i = i + 1
 
values = [1, 2, 3]
print(increment_items(values, 2))
print(values)
A. None [3, 4, 5]
B.  None [1, 2, 3]
C. [3, 4, 5] [1, 2, 3]
D.  [3, 4, 5] None
Solution: Execute in the shell to verify.

You Have Score    /6