WorksheetsSankes Dance
Total questions: 47
Worksheet time: 2hrs 53mins
Q: What happens if a class has two constructors with the same signature in Python?
It’s okay, Python picks one randomly
Error! Python only allows one constructor per class
Both run simultaneously
Magic happen
What is the Output?
class MyClass:
@staticmethod
def add(x, y):
return x + y
print(MyClass.add(2, 3))
Error (self missing)
5
None
Requires object creation
class Dog:
def speak(self):
return "Woof"
class Cat:
def speak(self):
return "Meow"
for animal in [Dog(), Cat()]:
print(animal.speak(), end=" ")
Guess the Output?
Woof Meow
Meow Woo
Erro
None
What is The Output?
class Parent:
value = 100
class Child(Parent):
value = 200
c = Child()
print(c.value)
100
200
Error
None
Guess the Output
class A:
def init(self):
print("A")
class B(A):
pass
obj = B()
A
B
Error
None
Which of the following is true about a class in Python?
A class is a blueprint for creating objects
A class can’t have variables
A class can’t have methods
A class is executed like a function
What will be the output of this code?
class Test:
pass
obj = Test()
print(type(obj))
<class 'Test'>
<class '__main__.Test'>
Test
Which statement is true about objects?
Objects store data in class variables only
Objects are instances of a class
Objects can’t call class methods
Objects can’t have unique properties
Which of the following is the correct constructor in Python?
__init__()
__start__()
__construct__()
__new__()
What will this code print?
class B:
def init(self, x=5):
self.x = x
obj1 = B()
obj2 = B(10)
print(obj1.x, obj2.x)
5 5
10 10
5 10
Error
Can instance properties access class properties directly?
Yes, always
No, never
Only via self or class name
Only in constructor
Guess the output
class Pawan:
def attention(self):
return "Watching TV"
class Wife(Pawan):
def attention(self):
return "Complaining husband is not helping"
w = Wife()
print(w.attention())
Watching TV
Complaining husband is not helping
Error
Both
What will be the Output?
class Husband:
def dinner(self):
return "Order pizza"
class Wife:
def dinner(self):
return "Cook pasta"
class Son(Husband, Wife):
pass
s = Son()
print(s.dinner())
Order pizza
Cook pasta
Error
Both
What will Son print when he gives advice?
class Grandpa:
def advice(self):
return "Arrange marriage is the best! 💍"
class Father(Grandpa):
def advice(self):
return "Love marriage is the best! ❤️"
class Son(Father):
def advice(self):
return "Being single is more fun 😎"
s = Son()
print(s.advice())
Arrange marriage is the best! 💍
Love marriage is the best! ❤️
Being single is more fun 😎
Call grandma for advice 📞
what will be printed?
class Husband:
def init(self):
self.__secret_snack = "Chocolates 🍫"
def reveal_snack(self):
return "My secret snack is {self.__secret_snack}"
h = Husband()
print(h.reveal_snack())
My secret snack is Chocolates 🍫
secret snack cannot be accessed
My secret snack is {self.__secret_snack}
Error
If Lover class had another method @abstractmethod def gift(): and Girlfriend didn’t implement it, what happens?
Error on creating object
Prints default gift
Works fine
Runs only on weekends
Why is abstraction in programming like ordering food online? 🍔📱
You see the menu (interface), not the kitchen secrets 😄
You must implement your order (choose what you want)
Abstract methods are like “placeholders” for the actual dish
All of the above
from abc import ABC, abstractmethod
class Lover(ABC):
@abstractmethod
def surprise_plan(self):
pass
class Girlfriend(Lover):
def surprise_plan(self):
return "Plan a movie night 🎬"
g = Girlfriend()
print(g.surprise_plan())
Question: What is this an example of?
Encapsulation
Abstraction
Inheritance
Overriding
for i in range(3):
print(f"Husband asks: Are you hungry? {i}")
print("Wife says: I'm fine 😅")
Asks 3 times, prints wife’s answer each time
Prints only once
Error because of f-string
Wife gets angry
what will be the output?
for i in range(5)
print("Husband buys snacks 🍫")
Prints 5 times
Error due to missing colon
Prints nothing
Husband forgets snacks
for i in range(1, 4):
if i == 2:
break
print(f"Son eats slice {i} of cake 🍰")
Guess the Output?
Slice 1, Slice 2, Slice 3
Slice 1
Slice 1, Slice 2
Nothing
def husband_says():
print("I love coding ❤️ instead of loving wife")
husband_says
guess the Output?
I love coding ❤️ instead of loving wife
Error/Nothing
i hate Coding
Husband forgets
def Romeo():
print("Husband says: Let's watch movie 🍿")
def Juliet():
print("Wife says: Only if we eat popcorn 🍿")
Romeo()
Output??
Prints both Romeo and Juliet lines
Prints only Romeo line
Error
Prints nothing
Which of the following functions is not a built-in functional programming tool in Python?
map()
filter()
reduce()
sorted()
What is The map() function in Python
Filters elements based on a condition
Transforms each element using a function(apply on each element)
Reduces a sequence to a single value
Sorts a sequence
Which import is required to use reduce() in Python?
import collections
import itertools
from functools import reduce
from operator import reduce
What is the correct syntax of a lambda function?
Note:Without Storing in a variable
lambda (x, y): x + y
lambda x, y: x + y
def lambda(x, y): return x + y
def (x, y): return x + y
What will the following code output?
f = lambda x: x * 2
print(f(5))
5
10
25
error
Which of the following statements about lambda is TRUE?
lambda can contain multiple return statements.
lambda can take multiple arguments but only one expression.
lambda is faster than normal functions.
lambda must always be named.
What does the code return?
add = lambda a, b=10: a + b
print(add(5))
5
10
15
Error
Which of these is equivalent to the code below?
lambda x: x**2
def square(x):
return x**2
def square(x):
print(x**2)
def square():
return x**2
Both a & b
What will be the output?
nums = [1, 2, 3, 4]
print(list(map(lambda x: x+1, nums)))
[1, 2, 3, 4]
[2, 3, 4, 5]
[0, 1, 2, 3]
Error
Which is NOT true about lambda functions?
They can be used inside map(), filter(), and reduce().
They are anonymous functions.
They can have multiple expressions.
They can be assigned to variables.
Who will buy ice cream for Trainer?
from functools import reduce
students = ["Priyanshu", "Kishore", "Neha", "Vaishnavi"]
icecream = reduce(lambda a, b: a if a < b else b, students)
print(icecream, "will buy ice cream 🍦😂")
Priyanshu will buy ice cream 🍦😂
Kishore will buy ice cream 🍦😂
Neha will buy ice cream 🍦😂
Vaishnavi will buy ice cream 🍦😂
Secret Spy Student
students = ["Tom", "Jerry", "Doreamon", "sinchan"]
spy = [name[::-1] for name in students]
print("Secret codes:", spy)
Secret codes: ['Tom', 'Jerry', 'Doreamon', 'sinchan']
Secret codes: ['moT', 'yrreJ', 'nomaeroD', 'nahcnis']
Secret codes: ['mTo', 'Jyer', 'Doreamon', 'snachin']
Secret codes: ['TomJerryDoreamonSinchan']
Who will wash the dishes? 🍽️
students = ["Hitaishi", "Shusanth", "Pawan", "Priyanshu", "Kishore", "Harsha"]
result = [name for name in students if len(name) % 2 == 0]
print(result)
Who are they?
['Hitaishi', 'Pawan', 'Harsha']
['Shusanth', 'Priyanshu', 'Kishore']
['Hitaishi', 'Shusanth', 'Priyanshu', 'Kishore']
['Harsha', 'Priyanshu']
Your class went out for pizza. Everyone ate, but now you must decide who will pay the bill.
Here’s the rule:
Use reduce() to find the student with the longest name.
That unlucky student will pay the bill. 😜
from functools import reduce
students = ["Neha", "Keethana", "Vaishnavi", "Nayana", "Shruti", "Shusanth Yadav"]
payer = reduce(lambda a, b: a if len(a) > len(b) else b, students)
print(payer, "will pay the pizza bill 🍕")
Neha
Keethana
Shusanth Yadav
Vaishnavi
Nayana
What will the following dictionary comprehension produce?
d = {x: 'Even' if x % 2 == 0 else 'Odd' for x in range(1, 6)}
print(d)
{1: 'Odd', 2: 'Even', 3: 'Odd', 4: 'Even', 5: 'Odd'}
{1: 'Even', 2: 'Odd', 3: 'Even', 4: 'Odd', 5: 'Even'}
[1: 'Odd', 2: 'Even', 3: 'Odd', 4: 'Even', 5: 'Odd']
Error
What will be the output of the following code?
from functools import reduce
nums = [1, 2, 3, 4]
result = reduce(lambda x, y: x + y, nums)
print(result)
10
[1, 2, 3, 4]
24
TypeError
What will be the output of the following code?
nums = [1, 2, 3]
result = map(lambda x: x**2, nums)
print(list(result))
[1, 2, 3]
[2, 4, 6]
[1, 4, 9]
Error
def surprise(func):
def wrapper(*args):
print("Gift delivered 🎁")
return wrapper
@surprise
def birthday(name):
print(f"Happy Birthday {name} 🎂")
birthday("Nobita")
what Happens?
Gift delivered 🎁
Happy Birthday Nobita 🎂
Only Gift delivered 🎁
Error
Nothing
def snacks():
for i in ["🍫", "🍪", "🍩"]:
yield i
s = snacks
print(next(s))
what Will happen?
🍫
🍪
Error
Nothing
def husband_messages():
messages = ["I love puppy ❤️", "Dinner is ready 🍲", "Let's watch movie 🎬"]
for msg in messages:
yield msg
msg_gen = husband_messages()
print(next(msg_gen))
print(next(msg_gen))
print(next(msg_gen))
print(next(msg_gen))
what will happen?
Prints all messages plus error on last call ✅
Only first 2 messages
Prints nothing
Error immediately
What is a Python decorator?
A function that takes another function as an argument and returns a new function.
(adding new features)
A special type of loop that automatically iterates over a sequence.
A variable that stores multiple functions at once.
A built-in method to convert a function into a class.
Which statement about generators is FALSE?
Generators are memory efficient because they yield values one at a time.
Generators can be iterated only once.
Generators automatically store all generated values in a list.
You can use next() to get the next value from a generator.
What is a generator in Python?
A function that returns a list of all values at once
A function that uses yield to produce values one at a time.
A loop that can run infinitely without stopping.
A decorator that modifies a function’s output.
