ExamVeda
Login
Home
61
How can you create a static variable in a Python class?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
62
What will be the output of the following Python code?
>>> class demo():
	def __repr__(self):
		return '__repr__ built-in function called'
	def __str__(self):
		return '__str__ built-in function called'
>>> s=demo()
>>> print(s)
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
63
What is the purpose of the @property decorator in Python classes?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
64
What is the output of the following code:
class MyClass:
count = 0
def init(self):
MyClass.count += 1
obj1 = MyClass()
obj2 = MyClass()
print(obj1.count)
Discuss
Answer & Solution
Answer: Option D
Solution:
Let's break down this Python code step-by-step:

First, we have a class called `MyClass`:
This is like a blueprint for creating objects.

Inside the class, we have `count = 0`:
This is a class variable called `count`.
It's shared by all instances (objects) of the `MyClass`.
Initially, it's set to 0.

Next, we have `def __init__(self):`:
This is the constructor (initializer) of the class.
It's automatically called when you create a new object of the class.

Inside the constructor, we have `MyClass.count += 1`:
This line increments the `count` class variable by 1 every time a new object is created.
`MyClass.count` ensures we're updating the class variable, not an instance variable.

Now, let's look at the object creation:
`obj1 = MyClass()`
This creates the first object of the class. The `__init__` method is called, and `MyClass.count` becomes 1.

`obj2 = MyClass()`
This creates the second object of the class. The `__init__` method is called again, and `MyClass.count` becomes 2.

Finally, we have `print(obj1.count)`:
This prints the value of the `count` variable.
Since `count` is a class variable, `obj1.count` will access the class variable which is currently equal to 2.

Therefore, the output is 2.

So, the correct answer is Option A: 2
65
What will be the output of the following Python code?
>>> class demo():
	def __repr__(self):
		return '__repr__ built-in function called'
	def __str__(self):
		return '__str__  built-in function called'
>>> s=demo()
>>> print(s)
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
66
What will be the output of the following Python code?
class test:
     def __init__(self,a="Hello World"):
         self.a=a
 
     def display(self):
         print(self.a)
obj=test()
obj.display()
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
67
What is delattr(obj,name) used for?
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board