0% found this document useful (0 votes)
8 views

Tutorial-3

This document contains a series of questions related to Python programming concepts, including lambda functions, list operations, string methods, and data structures like sets and dictionaries. Each question presents multiple-choice answers, testing knowledge on outputs of code snippets, functionalities of built-in methods, and characteristics of Python data types. The questions cover a wide range of topics, making it a comprehensive tutorial for beginners in Python.

Uploaded by

sunilmachra2021
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Tutorial-3

This document contains a series of questions related to Python programming concepts, including lambda functions, list operations, string methods, and data structures like sets and dictionaries. Each question presents multiple-choice answers, testing knowledge on outputs of code snippets, functionalities of built-in methods, and characteristics of Python data types. The questions cover a wide range of topics, making it a comprehensive tutorial for beginners in Python.

Uploaded by

sunilmachra2021
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Tutorial-3

DAI-101

Q1 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

Q2 What is the output of the following code snippet?


func = lambda x: return x
print(func(2))
(a) 0
(b) 2
(c) 2.0
(d) SyntaxError

Q3 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

Q4 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) [2,4]
(c) [True, False, True, False, True]
(d) [0, 1, 0, 1, 0]

Q5 What is the output of the following code?


nums = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x % 2 == 0, nums))
print(result)
(a) [False, True, False, True, False]
(b) [1,3,5]
(c) [True, False, True, False, True]
(d) [2,4]

Q6 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))

Q7 What is the output of the following code?


def h(y):
x += 1
x=5
h(x)
print(x)
(a) 5
(b) 6
(c) Error
(d) 5.0

Q8 What is the output of the following code?


def h(y):
global x
x += 1
x=5
h(x)
print(x)

(a) 5
(b) 6
(c) Error
(d) 5.0

Q.9 What would be the output of this code?


y = ”stuff;thing;junk;”
z = y.split(‘;’)
len(z)
(a) 17
(b) 4
(c) 0
(d) 3

Q10 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()

Q11 What would be the output of this code?


​ s = "Python Programming"
print(s[::2])
print(s[::-1])
(a) "Pto rgamn", “gnimmargorP nohtyP”
(b) "PtoPormig", “gnimmargorP nohtyP”
(c) "ythonrga", “gnimmargorPnohtyP”
(d) "Pnhnrgamn", “gnimmargorP nohtyP”

Q12.. What will print("abc" + 3 * "xyz") output?


(a) "abcxyzxyzxyz"​
(b) "abcxyz3"​
(c) "abcxyz"​
(d) Error

Q13. What would be the output of this code?


​ s = “Hello, How are You?”
​ s.replace(‘H’, ’C’)

(a) "Cello, How are You?"​


(b) "Hello, Cow are You?"​
(c) "Cello, Cow are You?"​
(d) None of the above

Q14. 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')]

Q15. 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

Q.16 What built-in Python data type is best suited for implementing a queue?
(a) dictionary
(b) set
(c) List
(d) None. You can only build a queue from scratch

Q17. 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

Q18. What is the output of the following code?


a = [1, 2, 3]
b=a*2
b[0] = 0
print(sum(b))
a) 10
b) 11
c) 12
d) None of the above

Q19. What is the output of the following code?


def square(x):
return x*x
L = [1, 2, 3, 4, square]
L[-1](3)
a) 10
b) 11
c) 9
d) None of the above

Q20 For Given List, Perform the following Operations.


a=[1,4,7,0,34,23,14,7]
print(a[-2:-7:-2])
print(a[3:8:3])
print(a[-8:-2:2])

Q21. 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'>

Q22 What will be the output of the following code?


t = (1, 2, 3)
t[1] = 5
print(t)
(a) (1, 5, 3)
(b) (1, 2, 3, 5)
(c) TypeError
(d) (5, 2, 3)

Q23. What will be the output of the following code?


t = (1, [2, 3], 4)
t[1].append(5)
print(t)
(a) (1, [2, 3], 4)
(b) (1, [2, 3, 5], 4)
(c) TypeError
(d) (1, [5, 2, 3], 4)

Q24. 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) && ; ||

Q25. What will be the value of x after running this code?


x = {1,2,3,4,5}
x.add(5)
x.add(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}

Q26. 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
Q27. What is the output of the following Code?
x={2,10,1,4,5,7}
x[2]=6
print(x)
(a) {2,10,1,4,5,7}
(b) {2,10,6,1,4,5,7}
(c) Error
(d) {1,4,6,5,7,10,2}

Q28. Which of the following are correct representation of Set?


S1={1,3,4,[2,5,6],8}, S2={1,3,4,(2,5,6),8}
(a) Both are Correct
(b) S1 Correct and S2 Incorrect
(c) S1 Incorrect and S2 Correct
(d) Both are Incorrect

Q29. Which of the following represent an empty Set?


S1=set(), S2={}
(a) Both Represent Set
(b) S1 Represent Set and S2 Represent Dict.
(c) S1 Represent Dict and S2 Represent Set.
(d) None of the Above

Q30. Which of the following statements are Correct about Set?


(a) Elements of the sets are mutable and set itself are also mutable
(b) Elements of the sets are immutable but set itself are mutable
(c) Elements of the sets are immutable and set itself are also immutable
(d) Elements of the sets are mutable but set itself are immutable

Q31. 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

Q32. 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()

Q33. 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 fruits.keys() for x]
(b) fruit_names = for x in fruits.keys() *
(c) fruit_names = [x for x in fruits.keys()]
(d) fruit_names = x for x in fruits.keys()

Q34. Suppose d = {“peter”:40, “michel”:45}, to delete the entry for “peter” what commands can we use?
a) d.delete("peter":40)
b) d.pop("peter")
c) del d["peter"]
d) del d("peter":40)

Q35. 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

Q36. Output of the following Code?


def f():
def g():
print('inside function g')
f()
g()
print('inside function f')

a) inside function g
inside function f

b) inside function f
inside function g

c) Recursion Error

inside function g
d) inside function f
inside function g

Q37. Output of the following Code?


import functools
functools.reduce(lambda x,y:x if x>y else y,[23,11,45,10,1])
(a) It will Give Maximum Element in the List
(b) It will Give Minimum Element in the List
(c) It will Give Second Maximum Element in the List
(d) None of the Above

Q38. Output of the following Code?


L=[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

[L[i][i] for i in range(0,len(L))]


(a) [[2,3],
[5,6]]
(b) [1,4,7]
(c) [1,5,9]
(d) [[5,6],
[8,9]]

Q39. Which of the following are the characteristics of the Set (MSQ)
a) Unordered
b) Itself Mutable
c) No Duplicates
d) Can't contain mutable data types
e) Can Contain mutable data types

Q40. Which of the following are the characteristics of the Dict. (MSQ)
a) Mutable
b) keys can't be duplicated
c) keys can't be mutable items
d) Keys can be mutable
e) Maintained Order

You might also like