1.
For the following code, which of the following statements is true?
def printHello():
print("Hello")
a = printHello()
Correct Answer
B. Both printHello() and a refer to the same object.
Explanation
Both printHello() and a refer to the same object because when the function printHello() is called and assigned to a variable a, the variable a becomes a reference to the function object. So, both printHello() and a point to the same function object in memory.
2.
What is the output of the following program?
def outerFunction():
global a
a = 20
def innerFunction():
global a
a = 30
print('a =', a)
a = 10
outerFunction()
print('a =', a)
Correct Answer
C. A = 20
Explanation
The output of the program is "a = 20". In this program, there are two functions: outerFunction and innerFunction. - The outerFunction sets the global variable "a" to 20. - The innerFunction also sets the global variable "a" to 30, but this change does not affect the value of "a" outside of the function. - The main program sets the global variable "a" to 10, then calls the outerFunction. - After the outerFunction is called, the value of "a" is still 20 because the innerFunction's change to "a" only affects its own scope. - Therefore, the final output is "a = 20".
3.
Which of the following statements is true?
Correct Answer
A. A class is blueprint for the object.
Explanation
The given answer is correct because a class in object-oriented programming serves as a blueprint or template for creating objects. It defines the properties and behaviors that an object of that class will have. Multiple objects can be created from a single class, so the second statement is not true. Therefore, the correct answer is that the first statement, "A class is a blueprint for the object," is true.
4.
What is the output of the following code?
class Foo:
def printLine(self, line='Python'):
print(line)
o1 = Foo()
o1.printLine('Java')
Correct Answer
C. Java
Explanation
The code defines a class called Foo with a method called printLine that takes an optional parameter called line with a default value of 'Python'. The method simply prints the value of the line parameter.
An instance of the Foo class is created and assigned to the variable o1. The printLine method of o1 is then called with the argument 'Java'.
Therefore, the output of the code will be "Java".
5.
What does the __init__() the function do in Python?
Correct Answer
B. This function is called when a new object is instantiated.
Explanation
The __init__() function in Python is used to initialize the class for use. It is automatically called when a new object is created from the class. This function allows us to set the initial state or attributes of the object. Therefore, the correct answer is "This function is called when a new object is instantiated."
6.
What is the output of the following code?
class Point:
def __init__(self, x = 0, y = 0):
self.x = x+1
self.y = y+1
p1 = Point()
print(p1.x, p1.y)
Correct Answer
B. 1 1
Explanation
The code defines a class called Point with an initializer method that takes two parameters, x and y, with default values of 0. Inside the initializer method, the values of x and y are incremented by 1 before assigning them to the instance variables self.x and self.y.
Then, an instance of the Point class is created using the default values, and the values of p1.x and p1.y are printed. Since the default values of x and y are both 0, and they are incremented by 1 inside the initializer method, the output will be 1 1.
7.
Which of the following codes uses the inheritance feature of Python?
Correct Answer
C.
class Foo:
pass
class Hoo(Foo):
pass
Explanation
The correct answer is the third code snippet, which uses the inheritance feature of Python. This is indicated by the line "class Hoo(Foo):", where class Hoo is inheriting from class Foo. Inheritance allows a class to inherit attributes and methods from another class, promoting code reuse and creating a hierarchical structure.
8.
If you a class is derived from two different classes, it’s called ______
Correct Answer
B. Multiple Inheritance
Explanation
Multiple inheritance is the correct answer because when a class is derived from two different classes, it is said to have multiple inheritance. This means that the derived class inherits attributes and methods from both parent classes, allowing for the combination of functionalities from multiple sources.
9.
Which of the following statements is true?
Correct Answer
D. All of the above.
Explanation
The given answer is correct because all of the statements mentioned are true in Python. In Python, the same operator may behave differently depending upon the operands. This is known as operator overloading. Additionally, you can change the way operators behave by defining special methods for the corresponding operators. For example, the special method __add__() is called when the + operator is used. Therefore, all of the statements mentioned in the options are true.
10.
What is the output of the following code?
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __sub__(self, other):
x = self.x + other.x
y = self.y + other.y
return Point(x,y)
p1 = Point(3, 4)
p2 = Point(1, 2)
result = p1-p2
print(result.x, result.y)
Correct Answer
B. 4 6
Explanation
The code defines a class called "Point" with a constructor that initializes the x and y coordinates. It also defines a subtraction method "__sub__" that calculates the sum of the x and y coordinates of two points and returns a new Point object with the resulting coordinates.
In the code, two Point objects p1 and p2 are created with coordinates (3, 4) and (1, 2) respectively. The subtraction operation p1-p2 is performed, which calls the __sub__ method and returns a new Point object with coordinates (4, 6).
Finally, the x and y coordinates of the resulting Point object are printed, which gives the output "4 6".