OOPs in Python MCQs Exercise I


Ques 1 OOPs


What is the output of the following Python Code?


class SmallCode:
    def __init__(self,name):
        self.name=name
    def show(self):
        print(self.name)
object=SmallCode("Vicky")
object.show()

  a) Vicky
  b) Error
  c) No output
  d) no 'name' variable is found


a is the correct option

Code Analysis:
A class SmallCode is defined with an __init__ method that initializes an instance variable self.name.
The show method of the class prints the name attribute of the object.
An object object of the class SmallCode is created, passing the string "Vicky" to the __init__ method.
The show method is called on the object.
Execution:
During object initialization, self.name is set to "Vicky".
The show method accesses self.name and prints "Vicky".
Output:
The program outputs Vicky as a result of the print(self.name) statement inside the show method.





Ques 2 OOPs


What is the output of the following Python Code?


class SmallCode:
    def __init__(self,name):
        self.name=name
    def show(self):
        print(self.name)
object=SmallCode()
object.show()

  a) Vicky
  b) Error
  c) No output
  d) no 'name' variable is found


b is the correct option

Code Analysis:
The __init__ method in the SmallCode class is defined to accept a name parameter during object creation.
When creating an object (object = SmallCode()), no arguments are provided, but the __init__ method requires one positional argument (name).
Error Details:
Python raises a TypeError when the number of arguments passed to a function or method does not match its definition.
The error message will look like:
TypeError: __init__() missing 1 required positional argument: 'name'.





Ques 3 OOPs


What is the output of the following Python Code?


class smallcode:
    def __init__(self):
        self.sm= 'smallcode'
    def Change(self):
        sm='SM'
obJ=smallcode()
print(obj.sm)
obj.Change()
print(obj.sm)

  a) smallcode
  b) smallcode
smallcode
  c) error
  d) None of these


b is the correct option

The Change method modifies only a local variable, not the instance variable self.sm.





Ques 4 OOPs


We can access a private variables outside the class?
  a) Yes
  b) No
  c) NA
  d) NA


b is the correct option

Private variables in Python (declared with a double underscore, e.g., __var) can be accessed outside the class using name mangling, where the variable is accessed as _ClassName__var.





Ques 5 OOPs


We can access private variables outside the class in python.
  a) Yes
  b) No
  c) NA
  d) NA


a is the correct option

Private variables in Python (defined with a double underscore, e.g., __var) can still be accessed outside the class using name mangling: _ClassName__var.





Ques 6 OOPs


What is the output of the following Python Code?


class SmallCode:
    def __init__(self,owner):
        self.owner = owner
object1=SmallCode("Vikas")
object1.yt="SmallCode"
object1.insta="SmallCode__"
print(len(object1.owner)+len(object1.__dict__))

  a) Error
  b) SmallCode
  c) 8
  d) None of these.


c is the correct option

Code Execution:
The class SmallCode initializes an instance variable owner with the value "Vikas".
Two additional instance variables yt and insta are added to object1 with values "SmallCode" and "SmallCode__", respectively.
The __dict__ attribute of an object contains a dictionary of all its instance variables.
Breakdown:
len(object1.owner): The length of "Vikas" is 5.
len(object1.__dict__): The dictionary contains owner, yt, and insta (3 keys), so len(object1.__dict__) is 3.
Total: 5 + 3 = 8.





Ques 7 OOPs


What is the output of the following Python Code?


class student:
    def __init__(self, roll_no,college):
        self.roll_no=roll_no
        self.college=college
    def display (self):
        print("Roll no : ", self.roll_no, ", Name: ",self.college)
student1 = student(18345354332,"PSIT COE")
student1.branch="Information Technology"
boolean=hasattr(student1, 'branch')
print(boolean)

  a) True
  b) False
  c) 18345354332
  d) PSIT COE


a is the correct option

The 'hasattr()' function checks if an object has a specified attribute.
An attribute branch with the value "Information Technology" is dynamically added to the student1 object.
The 'hasattr(student1, 'branch')' checks for the existence of the branch attribute in student1.
Since the attribute branch exists, 'hasattr()' returns 'True'