Python OOP Basics Quiz

Python
0 Passed
0% acceptance

A 35-question quiz covering the fundamentals of Object-Oriented Programming in Python, including classes, objects, attributes, and methods.

35 Questions
~70 minutes
1

Question 1

Which keyword is used to define a class in Python?

A
def
B
class
C
struct
D
object
2

Question 2

What is the correct syntax to define an empty class named `Robot`?

javascript

# Option A
class Robot:
    pass

# Option B
def Robot():
    return

# Option C
class Robot {}

# Option D
new class Robot:
                
A
Option A
B
Option B
C
Option C
D
Option D
3

Question 3

What is the standard naming convention for Python classes?

A
snake_case
B
camelCase
C
PascalCase (CapWords)
D
UPPER_CASE
4

Question 4

Which of the following best describes a 'class' in Python?

A
A built-in function for math operations.
B
A specific instance of data.
C
A blueprint or template for creating objects.
D
A list of variables.
5

Question 5

What happens if you define a class without a body (no code indented under it)?

javascript

class MyClass:
# No code here
                
A
It runs fine and creates an empty class.
B
It raises an IndentationError.
C
It creates a default constructor automatically.
D
It prints 'Empty Class'.
6

Question 6

What is the purpose of the docstring inside a class definition?

javascript

class User:
    """Represents a system user."""
    pass
                
A
It is required for the code to run.
B
It defines the class name.
C
It provides documentation accessible via help() or .__doc__.
D
It initializes the class variables.
7

Question 7

Which method is automatically called when a new object is instantiated?

A
__create__
B
__init__
C
__new__
D
__start__
8

Question 8

What will this code output?

javascript

class Cat:
    def __init__(self):
        print("Meow")

c = Cat()
                
A
Nothing
B
Meow
C
Error: __init__ takes no arguments
D
None
9

Question 9

What is the primary purpose of the `__init__` method?

A
To destroy the object.
B
To return a value from the class.
C
To initialize the object's attributes.
D
To define class-level methods.
10

Question 10

How do you correctly pass arguments to the constructor?

javascript

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

# Instantiation
                
A
p = Point()
B
p = Point.init(10, 20)
C
p = Point(10, 20)
D
p = new Point(10, 20)
11

Question 11

What is wrong with this constructor definition?

javascript

class Dog:
    def __init__(name):
        self.name = name
                
A
Nothing is wrong.
B
It is missing the 'self' parameter.
C
__init__ should not take arguments.
D
The method name should be 'init'.
12

Question 12

Can `__init__` return a value other than `None`?

A
Yes, it can return the new instance.
B
Yes, it can return True or False.
C
No, it must return None.
D
Yes, but only integers.
13

Question 13

How do you access an instance attribute named `color` inside a method?

A
color
B
this.color
C
self.color
D
Class.color
14

Question 14

What will be printed?

javascript

class Box:
    def __init__(self, size):
        self.size = size

b = Box(10)
print(b.size)
                
A
size
B
10
C
None
D
Error
15

Question 15

Instance attributes are...

A
Shared by all instances of the class.
B
Unique to each specific object.
C
Immutable once set.
D
Defined outside the class.
16

Question 16

What happens here?

javascript

class Player:
    def __init__(self, name):
        self.name = name

p1 = Player("Alice")
p2 = Player("Bob")
p1.name = "Charlie"
print(p2.name)
                
A
Alice
B
Bob
C
Charlie
D
Error
17

Question 17

Can you add new attributes to an object after it has been created?

javascript

class Car:
    pass

c = Car()
c.wheels = 4
print(c.wheels)
                
A
No, attributes must be defined in __init__.
B
Yes, Python allows dynamic attribute assignment.
C
Only if the class allows it explicitly.
D
No, it raises an AttributeError.
18

Question 18

What is the correct way to delete an instance attribute?

A
delete obj.attr
B
remove obj.attr
C
del obj.attr
D
obj.attr = None
19

Question 19

Where are class attributes defined?

A
Inside the __init__ method.
B
Inside the class body but outside any methods.
C
Outside the class definition.
D
Inside any method using 'self'.
20

Question 20

What is the output?

javascript

class Dog:
    species = "Canine"

d1 = Dog()
d2 = Dog()
print(d1.species)
                
A
Canine
B
None
C
Error
D
Dog
21

Question 21

What happens when you modify a class attribute via an instance?

javascript

class A:
    x = 10

a = A()
a.x = 20
print(A.x)
                
A
10
B
20
C
Error
D
None
22

Question 22

How should you modify a class attribute so it updates for all instances?

javascript

class Config:
    debug = False
                
A
Config.debug = True
B
self.debug = True
C
Config().debug = True
D
debug = True
23

Question 23

Class attributes are useful for...

A
Storing data unique to each user.
B
Defining constants or default values shared by all objects.
C
Temporary calculation results.
D
Private data hiding.
24

Question 24

What is the output?

javascript

class Counter:
    count = 0
    def __init__(self):
        Counter.count += 1

c1 = Counter()
c2 = Counter()
print(Counter.count)
                
A
0
B
1
C
2
D
Error
25

Question 25

What is the term for creating a specific object from a class?

A
Inheritance
B
Encapsulation
C
Instantiation
D
Polymorphism
26

Question 26

Which line correctly creates an object of class `Phone`?

A
p = Phone
B
p = new Phone()
C
p = Phone()
D
p = class Phone()
27

Question 27

What does `isinstance(obj, MyClass)` check?

A
If obj is a subclass of MyClass.
B
If obj is exactly MyClass (not a subclass).
C
If obj is an instance of MyClass or a subclass thereof.
D
If MyClass contains obj.
28

Question 28

Can you create multiple objects from a single class?

A
No, a class is a singleton by default.
B
Yes, you can create as many independent instances as needed.
C
Yes, but they will all share the same memory address.
D
Only up to 10 instances.
29

Question 29

What is the type of an object created from `class Demo:`?

javascript

class Demo:
    pass
d = Demo()
print(type(d))
                
A
<class 'object'>
B
<class '__main__.Demo'>
C
<type 'class'>
D
String
30

Question 30

What happens if you try to instantiate a class that requires arguments in `__init__` without passing them?

A
It creates the object with None values.
B
It raises a TypeError.
C
It prompts the user for input.
D
It skips initialization.
31

Question 31

What is the first parameter of every instance method in Python by convention?

A
this
B
obj
C
self
D
instance
32

Question 32

How do you call a method named `jump` on an object `player`?

A
Player.jump()
B
jump(player)
C
player.jump()
D
call player.jump
33

Question 33

What is the output?

javascript

class Greeter:
    def say_hello(self):
        return "Hello!"

g = Greeter()
print(g.say_hello())
                
A
Hello!
B
None
C
Error
D
<function say_hello>
34

Question 34

Why do we use `self` inside methods?

A
To refer to the global scope.
B
To access attributes and other methods of the current object.
C
To import modules.
D
It is optional and can be removed.
35

Question 35

How does a method call another method within the same class?

javascript

class Math:
    def double(self, x):
        return x * 2
    
    def quadruple(self, x):
        return ____.double(x) * 2
                
A
Math
B
self
C
this
D
super

QUIZZES IN Python