iGET

Python Programming - MCQ Practice Questions

Core Python MCQs — syntax, data types, OOP, libraries for placements & IT exams.

188 questions | 100% Free

Q.121Medium

What will be the output of the following code? class Counter: count = 0 def __init__(self): Counter.count += 1 a = Counter() b = Counter() c = Counter() print(Counter.count)

Q.122Medium

What will be the output of the following code? class MyClass: def __init__(self, x): self.__x = x obj = MyClass(10) print(obj.__x)

Q.123Medium

Which of the following correctly defines a class method in Python?

Q.124Medium

What is the output of the following code? class Shape: def area(self): return 0 class Circle(Shape): def __init__(self, r): self.r = r def area(self): return 3.14 * self.r * self.r shapes = [Shape(), Circle(7)] for s in shapes: print(s.area())

Q.125Medium

What will be the output of the following code? class A: def greet(self): return 'Hello from A' class B(A): def greet(self): return super().greet() + ' and B' obj = B() print(obj.greet())

Q.126Medium

What will be the output of the following code? class Vehicle: def __init__(self, brand): self.brand = brand class Car(Vehicle): def __init__(self, brand, model): super().__init__(brand) self.model = model c = Car('Toyota', 'Camry') print(c.brand, c.model)

Q.127Medium

What is the output of the following code? class Box: def __init__(self, vol): self.vol = vol def __gt__(self, other): return self.vol > other.vol b1 = Box(10) b2 = Box(20) print(b1 > b2)

Q.128Medium

What will be the output of the following code? class Foo: def __init__(self): self.x = 10 def bar(self): self.x += 5 return self.x obj1 = Foo() obj2 = Foo() obj1.bar() print(obj1.x, obj2.x)

Q.129Medium

Which of the following modes opens a file for both reading and writing without truncating the file, and raises an error if the file does not exist?

Q.130Medium

What will be the output of the following code? with open('test.txt', 'w') as f: f.write('Hello') f.write(' World') with open('test.txt', 'r') as f: print(f.read())

Q.131Medium

Which method reads all lines of a file and returns them as a list of strings, with each string ending in a newline character?

Q.132Medium

What does the seek(0) method do when called on an open file object?

Q.133Medium

What will be the output of the following code? f = open('data.txt', 'w') f.write('Line 1\nLine 2\nLine 3') f.close() f = open('data.txt', 'r') print(len(f.readlines())) f.close()

Q.134Medium

Which of the following is the correct way to open a file in binary write mode in Python?

Q.135Medium

What is the advantage of using the 'with' statement when working with files in Python?

Q.136Medium

What will the following code print? with open('sample.txt', 'w') as f: f.write('Python\nFile Handling') with open('sample.txt', 'r') as f: line = f.readline() print(line.strip())

Q.137Medium

What exception is raised when you try to open a file that does not exist using open('missing.txt', 'r')?

Q.138Medium

What will be the output of the following code? with open('test.txt', 'w') as f: f.write('Hello World') with open('test.txt', 'a') as f: f.write('!!!') with open('test.txt', 'r') as f: print(f.read())

Q.139Medium

What will be the output of the following code? def greet(name, msg='Hello'): return f'{msg}, {name}!' print(greet('Alice')) print(greet('Bob', 'Hi'))

Q.140Medium

What will be the output of the following code? def func(*args, **kwargs): print(len(args), len(kwargs)) func(1, 2, 3, a=4, b=5)