DAI-101 Tutorial 1 Solution
Q.1 What would be the output?
college_years = ['Freshman', 'Sophomore', 'Junior', 'Senior']
list(enumerate(college_years, 2019))
(a) [('Freshman', 2019), ('Sophomore', 2020), ('Junior', 2021), ('Senior', 2022)]
(b) [(2019, 2020, 2021, 2022), ('Freshman', 'Sophomore', 'Junior', 'Senior')]
(c) [('Freshman', 'Sophomore', 'Junior', 'Senior'), (2019, 2020, 2021, 2022)]
(d) [(2019, 'Freshman'), (2020, 'Sophomore'), (2021, 'Junior'), (2022, 'Senior')]
Answer: d
Q.2 In Python, when using sets, you use...........to calculate the intersection between two sets
and ..................to calculate the union.
(a) Intersect ; union
(b) |;&
(c) &;|
(d) && ; ||
Answer: c
Q.3 Which of the following is the most memory-efficient way to create a list of squares of
numbers from 1 to 1000?
(a) [x**2 for x in range(1, 1001)]
(b) list(map(lambda x: x**2, range(1, 1001)))
(c) (x**2 for x in range(1, 1001))
(d) list(x**2 for x in range(1, 1001))
(e)
Answer: c
Q.4 What will this statement return?
{x : x*x for x in range(1,100)}
(a) a dictionary with x as a key, and x squared as its value; from 1 to 100
(b) a dictionary with x as a key, and x squared as its value; from 1 to 99
(c) a set of tuples, consisting of (x, x squared); from 1 to 99
(d) a list with all numbers squared from 1 to 99
Answer: b
Q.5 This code provides the ............ of the list of numbers.
num_list = [21, 13, 19, 3, 11, 5, 18]
num_list.sort()
num_list[len(num_list) // 2]
(a) mode
(b) average
(c) mean
(d) Median
Answer: d
Q.6 How would you access and store all the keys in this dictionary at once?
fruit_info = {'fruit': 'apple', 'count': 4, 'price': 90}
(a) my_keys = fruit_info.to_keys()
(b) my_keys = fruit_info.all_keys()
(c) my_keys = fruit_info.keys
(d) my_keys = fruit_info.keys()
Answer: d
Q.7 What will be the value of x after running this code?
x = {1,2,3,4,5}
[Link](5)
[Link](6)
(a) {1, 2, 3, 4, 5, 5, 6}
(b) {5, 6, 1, 2, 3, 4, 5, 6}
(c) {6, 1, 2, 3, 4, 5}
(d) {1, 2, 3, 4, 5, 6}
Answer: d
Q.8 What built-in Python data type can be used as a hash table?
(a) set
(b) list
(c) tuple
(d) Dictionary
Answer: d
Q.9 Which mode is not a valid way to access a file from within a Python script?
(a) write('w')
(b) scan('s')
(c) append('a')
(d) read('r')
Answer: b
Q.10 Suppose you have a string variable defined as y=”stuff;thing;junk;”. What would be
the output from this code?
z = [Link](‘;’)
len(z)
(a) 17
(b) 4
(c) 0
(d) 3
Answer: b
Q.11 what will this command return?
{x for x in range(100) if x%3 == 0}
(a) a set of all the multiples of 3 less then 100
(b) a set of all the number from 0 to 100 multiplied by 3
(c) a list of all the multiples of 3 less then 100
(d) a set of all the multiples of 3 less then 100 excluding 0
Answer: a
Q.12 What is the correct syntax for replacing the string apple in the list with the string
orange?
my_list = ['kiwi', 'apple', 'banana']
(a) orange = my_list[1]
(b) my_list[1] = 'orange'
(c) my_list['orange'] = 1
(d) my_list[1] == orange
Answer: b
Q.13 What is the proper way to write a list comprehension that represents all the keys in
this dictionary?
fruits = {'Apples': 5, 'Oranges': 3, 'Bananas': 4}
(a) fruit_names = [x in [Link]() for x]
(b) fruit_names = for x in [Link]() *
(c) fruit_names = [x for x in [Link]()]
(d) fruit_names = x for x in [Link]()
Answer: c
Q.14 What built-in Python data type is best suited for implementing a queue?
(a) dictionary
(b) set
(c) None. You can only build a queue from scratch.
(d) List
Answer: d
Q.15 Which of the following are the modes of both writing and reading in binary format in
file?
a) wb+
b) w
c) wb
d) w+
Answer: a
Q.16 Suppose d = {“peter”:40, “michel”:45}, to delete the entry for “peter” what command
do we use?
a) [Link]("peter":40)
b) [Link]("peter")
c) del d["peter"]
d) del d("peter":40)
Answer: c
Q.17 What should be the output
a={1:"A",2:"B",3:"C"}
print([Link](5,4))
a) Error, invalid syntax
b) A
c) 5
d) 4
Answer: d)
Q.18 If a is a dictionary with some key-value pairs, what does [Link]() do?
a) Removes an arbitrary element
b) Removes all the key-value pairs
c) Removes the key-value pair for the key given as an argument
d) Invalid method for dictionary
Answer: a)
Q.19 What is the purpose of the 'seek()' method when working with files?
(a) To search for a specific string in the file
(b) To move the file cursor to a specific position
(c) To read a specific number of bytes from the file
(d) To write at a specific position in the file
Answer: b)
Q.20 Which of the following is true about Python dictionaries?
(a) They maintain the order of insertion of keys
(b) They can have mutable objects as keys
(c) They allow duplicate keys
(d) They can be nested
Answer: d)
Q.21 Which of the following is a valid use case for a lambda function?
(a) Sorting a list of tuples by the second element
(b) Defining a class method
(c) Handling file I/O operations
(d) Performing matrix multiplication
Answer: (a)
Q.22 What is the purpose of using lambda functions with map() and filter()
functions in Python?
(a) To apply the lambda function to all elements of an iterable
(b) To remove all elements from an iterable
(c) To sort the elements of an iterable
(d) To create a new iterable with selected elements
Answer: (a)
Option (a) correct for map() and filter() and option d can be considered for filter().
• Option (a): Applies to map() and filter() because both functions apply the lambda
function to all elements of an iterable.
• Option (d): Specifically applies to filter(), as it creates a new iterable with selected
elements based on the lambda function condition.
filter(): This function applies a given function (typically a lambda function) to all elements of an
iterable and returns a new iterable with only those elements that satisfy the condition defined by
the function.
Q.23 What is the output of the following code?
nums = [1, 2, 3, 4, 5]
result = list(map(lambda x: x % 2 == 0, nums))
print(result)
(a) [False, True, False, True, False]
(b) [1, 0, 1, 0, 1]
(c) [True, False, True, False, True]
(d) [0, 1, 0, 1, 0]
Answer: (a)
Q.24 What is the output of the following code?
from collections import deque
d = deque([1, 2, 3, 4, 5], maxlen=5)
[Link](0)
[Link](6)
print(d)
(a) deque([1, 2, 3, 4, 5, 0, 6])
(b) deque([0, 1, 2, 3, 4, 6])
(c) deque([0, 1, 2, 3, 4, 5])
(d) None
Answer: (d) deque([1, 2, 3, 4, 6], maxlen=5)
Q.25 What is the output of the following code snippet?
func = lambda x: return x
print(func(2))
(a) x
(b) 2
(c) 2.0
(d) SyntaxError
(e) 0
Answer d:
Q.26 Which of the following is true about Python's sorted() function compared to the .sort()
method of lists?
(a) sorted() modifies the original list, while .sort() returns a new list
(b) sorted() returns a new list, while .sort() modifies the original list
c) Both sorted() and .sort() modify the original list
d) Both sorted() and .sort() return a new list
Answer: b)
Q.27 What is the output when you run the code?
from functools import reduce
numbers = [1, 2, 3,4,5,6]
reduce(lambda x, y: x + y, map(lambda x: x**2, filter(lambda x: x % 2 == 0, range(1, 6))))
(a) 20
(b) 40
(c) 10
(d) 30
Answer: a
Q.28 Consider the following list as an input: numbers = [1, 2, 3] Which of the following
would produce the result: [2] Select all that apply:
(a) list(filter(lambda x: (x + 1) * 3 / 3 % 3 == 0, numbers))
(b) list(filter(lambda x: x > 1, numbers))
(c) list(filter(lambda x: 2, numbers))
(d) list(filter(lambda x: x % 2 == 0, numbers))
Answer: a,d
Q. 29 What is the output of the following code?
with open('[Link]', 'w+') as f:
[Link]('Hello\nWorld')
[Link](0)
print([Link](5))
print([Link]())
a) Hello 5 b) Hello 0 c) Hell 4 d) Hello 10
Answer: (a)
Q.30 What is the purpose of the __iter__() method in a custom iterator class?
a) To initialize the iterator
b) To return the next item in the iteration
c) To return the iterator object itself
d) To signal the end of iteration
Answer: c
Q.31 Which of the following is not a characteristic of a stack?
a) LIFO (Last In First Out)
b) Push operation adds an element
c) Pop operation removes the most recently added element
d) Elements can be accessed in any order
Answer:d
Q.32 Which method is used to remove and return an arbitrary element from a set?
(a) [Link]()
(b) [Link]()
(c) [Link]()
(d) [Link]()
Answer:a
Q.33 Which of the following is not a valid queue operation?
(a) Enqueue
(b) Dequeue
(c) Peek
(d) Sort
Answer: d
Q.34 What is the purpose of the 'with' statement when working with files?
(a) To create a new file
(b) To ensure the file is properly closed after operations
(c) To read the entire file at once
(d) To write to multiple files simultaneously
Answer: (b)
Q.35 What will be the output of the following code?
numbers = [1, 2, 3, 4, 5]
squares = (x**2 for x in numbers)
print(type(squares))
(a) <class 'list'>
(b) <class 'tuple'>
(c) <class 'generator'>
(d) <class 'set'>
Answer: (c)
Q.36 What is the output of the following code?
a = [1, 2, 3]
b=a*2
b[0] = 0
print(a)
print(b)
Output:______________________________________________
Answer: [1,2,3]
[0,2,3,1,2,3]
Q.37 What is the output of the following code?
d = {'a': 1, 'b': 2, 'c': 3}
print([Link]('b', 0) + [Link]('d', 4))
a) 2
b) 4
c) 6
d) KeyError
Answer: c
Q.38 Which of the following string methods returns a new string with the first character of
each word capitalized?
(a) capitalize()
(b) title()
(c) upper()
(d) swapcase()
Answer: (b)
Q.39 Which of the following is true about Python's dictionary views?
a) They are static and don't reflect changes to the dictionary
b) They can be indexed like lists
c) They are dynamic and reflect changes to the dictionary
d) They can contain duplicate elements
Answer: c)
Q.40 Which of the following string methods would you use to split a string into a list,
keeping the delimiters?
a) split()
b) partition()
c) [Link]()
d) splitlines()
Answer: c)