iGET

Python Programming - MCQ Practice Questions

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

188 questions | 100% Free

Q.61Medium

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

Q.62Medium

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.63Medium

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.64Medium

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.65Medium

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.66Medium

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.67Medium

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.68Medium

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.69Medium

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.70Medium

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

Q.71Medium

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.72Medium

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

Q.73Medium

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

Q.74Medium

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.75Medium

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

Q.76Medium

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.77Medium

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.78Medium

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)

Q.79Medium

Which of the following statements about lambda functions in Python is CORRECT?

Q.80Medium

What will be the output of the following code? def outer(): x = 10 def inner(): nonlocal x x += 5 inner() return x print(outer())