Python Syntax Based Questions
Python Syntax Based Questions
x = [1, 2, 3]
y = x
y[0] = 10
print(x)
Output: [10, 2, 3]
Output: 3
print(f(1))
print(f(2, []))
print(f(3))
Output:
[1]
[2]
[1, 3]
Explanation: The default list y is shared across function calls.
print(g(1, y=3))
print(g(1))
Output:
4
3
Explanation: The function uses the default value for y if not explicitly passed.
Output: False
Explanation: y is a new list created from x, so they are not the same object.
Output: cba
Output:
Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)
Explanation: * replicates the tuple.
Output: hello
Output: Pto
Output: [1, 4, 5, 3]
Output: [1, 3]
Explanation: Replacing the slice with an empty list removes the element.
Output: {1, 3}
print(f(1))
print(f(2, []))
print(f(3))
Output:
[1]
[2]
[1, 3]
Explanation: The default list y persists across function calls.
Output: 4
Output: yth
Output: (1, 2, 3, 4, 5)
Output: axc
Output: -1
Output: n
Output: [3, 2, 1]
Output: b
Output: Pytho
Output: PYTHON
Output: [1, 2, 3]
Output: [1, 2, 3, 1, 2, 3]
Output: Hello
Explanation: capitalize returns the string with the first letter capitalized.
Output: nohtyP
Output: 2
Output: {1, 2, 3, 4, 5}
Output: [2, 3]
Output: 124
Output: hello*****
Output: 4
Output: Pythan
Output:
[1, 2, 3]
[1, 2, 3, 4]
Output: 3
Output: yth
Output: {1}
Explanation: y is a reference to x.
Output: 3
Output: ytho
Output: [1, 2, 3, 4, 5]
class B(A):
def __init__(self):
super().__init__()
self.y = 20
obj = B()
print(obj.x, obj.y)
Output: 10 20
obj = B()
print(obj.value)
Output: 2
obj1 = A(10)
obj2 = A(20)
obj3 = obj1 + obj2
print(obj3.x)
Output: 30
def __repr__(self):
return f'A(value={self.value})'
obj = A()
print(obj)
Output: A(value=1)
class B(A):
def __init__(self):
super().__init__()
self.x = 20
obj = B()
print(obj.x)
Output: 20
obj = A()
print(obj(5))
Output: 15
Explanation: __call__ allows instances to be called like functions.
def method(self):
return self.x
class B(A):
def method(self):
return super().method() + 10
obj = B()
print(obj.method())
Output: 20
class B(A):
def __init__(self):
super().__init__()
self.x = 10
obj = B()
print(hasattr(obj, 'x'))
Output: True
.x = 10
@property
def value(self):
return self.x
obj = A()
print(obj.value)
Output: 10
def __del__(self):
print("Object deleted")
obj = A()
del obj
obj1 = A(10)
obj2 = A(10)
print(obj1 == obj2)
Output: True
def __str__(self):
return f'A(x={self.x})'
obj = A()
print(str(obj))
Output: A(x=10)
Output: 10
obj = A()
obj.x = 5
print(obj.x)
Output: 5
def __iter__(self):
yield self.x
obj = A()
print(list(obj))
Output: [10]
@classmethod
def set_x(cls, value):
cls.x = value
A.set_x(10)
print(A.x)
Output: 10
obj1 = A()
obj2 = A()
print(obj1 == obj2)
Output: True
obj = A()
obj.x = 5
print(obj.x)
Output: 10
def __del__(self):
print("Deleting object")
def __repr__(self):
return f"A(x={self.x})"
obj = A()
del obj
obj = A()
print(obj(5))
Output: 15
obj = A()
print(obj[0])
Output: 1
obj = A()
print(10 in obj)
Output: True
def __enter__(self):
return self
Output: 10
Explanation: __enter__ and __exit__ allow the object to be used in a with statement.
def __iter__(self):
return iter([self.x])
obj = A()
print(next(iter(obj)))
Output: 10
Explanation: __iter__ returns an iterator over [self.x].
@property
def value(self):
return self.x
@value.setter
def value(self, new_value):
self.x = new_value
obj = A()
obj.value = 20
print(obj.value)
Output: 20
def __str__(self):
return f'A(x={self.x})'
obj = A()
print(obj)
Output: A(x=10)
obj = A()
obj.x = 5
print(obj.x)
Output: 10
obj1 = A()
obj2 = A()
print(obj1 == obj2)
Output: True
obj = A()
print(obj[0])
Output: 10
def __iter__(self):
return iter([self.x])
obj = A()
print(list(iter(obj)))
Output: [10]
@property
def value(self):
return self.x
@value.setter
def value(self, new_value):
self.x = new_value
obj = A()
obj.value = 20
print(obj.value)
Output: 20
obj1 = A()
obj2 = A()
print(obj1 == obj2)
Output: True
def __del__(self):
print("Object deleted")
obj = A()
Output: Object deleted
def __repr__(self):
return f'A(x={self.x})'
obj = A()
print(repr(obj))
Output: A(x=10)
def __str__(self):
return f'A(x={self.x})'
obj = A()
print(obj)
Output: A(x=10)
def __iter__(self):
return iter([self.x])
obj = A()
print(next(iter(obj)))
Output: 10
def __enter__(self):
return self
Output: 10
Explanation: __enter__ and __exit__ allow the object to be used in a with statement.
obj = A()
print(10 in obj)
Output: True
obj = A()
print(obj[0])
Output: 10
obj = A()
obj.x = 5
print(obj.x)
Output: 10
obj = A()
obj.x = 4
print(obj.x)
Output: 4
obj = A()
print(obj(3) + obj(4))
Output: 17
obj1 = A()
obj2 = A()
print(obj1 + obj2)
Output: 20
Explanation: __add__ defines behavior for the + operator with another A instance.
def __len__(self):
return len(self.x)
obj = A()
print(len(obj))
Output: 3
Explanation: __len__ provides the length of the object.
obj = A()
print(10 in obj)
print(5 in obj)
Output:
True
False
obj1 = A()
obj2 = A()
print(obj1 == obj2)
print(obj1 == 10)
Output:
True
False
obj = A()
obj += 5
print(obj.x)
Output: 15
obj = A()
print(obj[5])
Output: 15
Explanation: __getitem__ defines behavior for item access with [].
obj = A()
obj[0] = 20
print(obj.x)
Output: 20
def __enter__(self):
return self
Output: 10
Explanation: __enter__ sets up the context, and __exit__ can modify the object when
the context ends.
51. What is the result of this code?
class A:
def __init__(self):
self.x = 10
def __str__(self):
return f'A({self.x})'
def __repr__(self):
return f'A(x={self.x})'
obj = A()
print(str(obj))
print(repr(obj))
Output:
A(10)
A(x=10)
obj = A()
print(obj.y)
Explanation: __getattr__ is called when an attribute is accessed that does not exist.
53. What is the result of this code?
class A:
def __init__(self):
self.x = 10
obj = A()
obj.x = 5
print(obj.x)
Output: 6
obj = A()
print(obj(1, 2, 3))
Output: 6
def __del__(self):
print("Object deleted")
obj = A()
obj = None
obj = A()
print(10 in obj)
print(20 in obj)
Output:
True
False
obj = A()
print(obj[1])
Output: 2
obj = A()
obj.x = 5
print(obj.x)
Output: 25
Output: False
def __repr__(self):
return f'A({self.x})'
obj = A()
print(obj)
Output: A(10)
def __len__(self):
return len(str(self.x))
obj = A()
print(len(obj))
Output: 2
def __str__(self):
return f'Value is {self.x}'
obj = A()
print(str(obj))
Output: Value is 10
obj = A()
print(obj.x)
print(obj.y)
Output:
20
y not found
def __iter__(self):
yield self.x
obj = A()
print(list(iter(obj)))
Output: [10]
obj = A()
print(10 in obj)
print(20 in obj)
Output:
True
False
Explanation: __contains__ checks for membership.
obj = A()
print(obj(1, 2, 3))
print(obj())
Output:
3
0
obj = A()
print(obj[0])
print(obj[1])
Output:
10
11
Explanation: __getitem__ supports item access with indices.
obj = A()
obj[0] = 20
print(obj.x)
Output: 20
obj = A()
del obj[0]
print(hasattr(obj, 'x'))
Output: False
obj1 = A()
obj2 = A()
print(obj1 != obj2)
Output: False
obj1 = A()
obj2 = A()
print(obj1 < obj2)
Output: False
obj = A()
print(5 + obj)
Output: 15
obj = A()
print(obj * 5)
Output: 50
obj = A()
print(obj % 3)
Output: 1
obj = A()
print(obj // 3)
Output: 3
obj = A()
print(obj & 3)
Output: 2
Explanation: __and__ defines behavior for the bitwise & operator.
obj = A()
print(obj | 3)
Output: 11
obj = A()
print(obj ^ 3)
Output: 9
obj = A()
print(obj << 2)
Output: 40
obj = A()
print(obj >> 2)
Output: 2
def __neg__(self):
return -self.x
obj = A()
print(-obj)
Output: -10
Explanation: __neg__ defines behavior for the unary negation operator (-).
def __pos__(self):
return +self.x
obj = A()
print(+obj)
Output: 10
Explanation: __pos__ defines behavior for the unary positive operator (+).
obj = A()
print(10 in obj)
print(20 in obj)
Output:
True
False
def __repr__(self):
return f'Value: {self.x}'
def __str__(self):
return f'{self.x}'
obj = A()
print(repr(obj))
print(str(obj))
Output:
Value: 10
10
obj = A()
print(obj + 5)
Output: 15
obj = A()
print(obj - 5)
Output: 5
obj = A()
print(divmod(obj, 3))
Output: (3, 1)
def __reversed__(self):
return reversed([self.x])
obj = A()
print(list(reversed(obj)))
Output: [10]
def __enter__(self):
return self
Output: 10
def __hash__(self):
return hash(self.x)
obj = A()
print(hash(obj))
def __iter__(self):
return iter([self.x])
obj = A()
print(list(obj))
Output: [10]
obj = A()
print(obj(1, 2, 3, a=4, b=5))
Output: 5
obj = A()
print(obj.__get__(None, None))
Output: 10
obj = A()
obj.__set__(None, 20)
print(obj.x)
Output: 20
def __del__(self):
print("Deleted")
obj = A()
del obj
Output: Deleted
def __str__(self):
return f'A with x={self.x}'
obj = A()
print(f'The object is: {obj}')
obj = A()
print(f'{obj!r}')
Output: A(10)
obj = A()
print(10 in obj)
print(20 in obj)