Python Classes And Objects Quiz

Reviewed by Godwin Iheuwa
Godwin Iheuwa, MS (Computer Science) |
Database Administrator
Review Board Member
Godwin Iheuwa, a Database Administrator at MTN Nigeria, holds an MS in Computer Science, specializing in Agile Methodologies and Database Administration from the University of Bedfordshire and a Bachelor's in Computer Science from the University of Port Harcourt. His proficiency in SQL Server Integration Services (SSIS) and SQL Server Management Studio contributes to his expertise in database management.
, MS (Computer Science)
By TeacheMeIDEA
T
TeacheMeIDEA
Community Contributor
Quizzes Created: 6 | Total Attempts: 16,455
| Attempts: 10,191 | Questions: 10
Please wait...
Question 1 / 10
0 %
0/100
Score 0/100
1. 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)

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.

Submit
Please wait...
About This Quiz
Python Classes And Objects Quiz - Quiz

Want to test your coding prowess? Take this Python classes and objects quiz in order to see how good you actually are at Python and coding. Who knows,... see moreyou might even learn something new! Python is a high-level, general-purpose programming language made to aid in the creation of both large and small-scale projects. It also emphasizes code readability through the use of an extensive indentation format. Share the quiz with your friends once you are done taking it so they can test themselves as well! All the best! see less

2. Which of the following statements is true?

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.

Submit
3. What is the output of the following code?  
class Foo:
  def printLine(self, line='Python'):
    print(line)
  
o1 = Foo()
o1.printLine('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".

Submit
4. Which of the following codes uses the inheritance feature of Python?

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.

Submit
5. If you a class is derived from two different classes, it's called ______

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.

Submit
6. Which of the following statements is true?

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.

Submit
7. What does the __init__() the function do in Python?

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."

Submit
8. For the following code, which of the following statements is true?
 
def printHello():
    print("Hello")     
a = printHello()

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.

Submit
9. 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)

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".

Submit
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)

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".

Submit
View My Results
Godwin Iheuwa |MS (Computer Science) |
Database Administrator
Godwin Iheuwa, a Database Administrator at MTN Nigeria, holds an MS in Computer Science, specializing in Agile Methodologies and Database Administration from the University of Bedfordshire and a Bachelor's in Computer Science from the University of Port Harcourt. His proficiency in SQL Server Integration Services (SSIS) and SQL Server Management Studio contributes to his expertise in database management.

Quiz Review Timeline (Updated): Feb 13, 2024 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Feb 13, 2024
    Quiz Edited by
    ProProfs Editorial Team

    Expert Reviewed by
    Godwin Iheuwa
  • Mar 18, 2019
    Quiz Created by
    TeacheMeIDEA
Cancel
  • All
    All (10)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What is the output of the following code?...
Which of the following statements is true?
What is the output of the following code?...
Which of the following codes uses the inheritance feature of Python?
If you a class is derived from two different classes, it's called...
Which of the following statements is true?
What does the __init__() the function do in Python?
For the following code, which of the following statements is true?...
What is the output of the following program? ...
What is the output of the following code?...
Alert!

Advertisement