Multiple choice quiz about Python - Part 2
To help readers add to their interesting knowledge of Python objects and classes, below the Network Administrator will send you multiple choice questions on this topic. Please try.
Question 1: Which assertion is correct when talking about the following code:
def printHello(): print("Hello") a = printHello()
printHello () is a function and a is a variable. Both are not objects.
Both printHello () and a represent the same object.
printHello () and a are two different objects.
Syntax error. Cannot assign functions to a variable in Python.
Question 2: What is the output of the program below?
def outerFunction(): global a a = 20 def innerFunction(): global a a = 30 print('a =', a) a = 10 outerFunction() print('a =', a)
Question 3: Which of the following statements is true?
Class (class) is a detailed plan for the object.
Only one object can be created from a given class.
Both answers are correct.
There is no exact answer.
Question 4: What is the result of the code below?
class Foo: def printLine(self, line='Python'): print(line)
o1 = Foo() o1.printLine('Java')
Question 5: What is the meaning of __init __ () function in Python?
Called when a new object is initialized.
Initialize and bring all data attributes to 0 when called.
There is no right answer.
Question 6: What is the output of the program below?
class Point: def __init__(self, x = 0, y = 0): self.x = x+1 self.y = y+1
p1 = Point() print(p1.x, p1.y)
Question 7: Which of the following code uses Python's inheritance feature?
class Foo(object): pass class Hoo(object) pass
class Foo: pass class Hoo(Foo): pass
There is no exact answer.
Question 8: What is a class inherited from two different classes of attributes?
Multi-level inheritance (Multilevel Inheritance)
Question 9: Which of the following statements is true?
In Python, an operator may have different operations depending on the operand used.
You can change the way operators work in Python.
__add () __ is called when the '+' operator is used.
Question 10: The results of the program below are:
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)