Python Classes And Objects Quiz

Reviewed by Godwin Iheuwa
Godwin Iheuwa, MS, Computer Science |
Computer Expert
Review Board Member
Godwin is a proficient Database Administrator currently employed at MTN Nigeria. He holds as MS in Computer Science from the University of Bedfordshire, where he specialized in Agile Methodologies and Database Administration. He also earned a Bachelor's degree in Computer Science from the University of Port Harcourt. With expertise in SQL Server Integration Services (SSIS) and SQL Server Management Studio, Godwin's knowledge and experience enhance the authority of our quizzes, ensuring accuracy and relevance in the realm of computer science.
, MS, Computer Science
Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By TeacheMeIDEA
T
TeacheMeIDEA
Community Contributor
Quizzes Created: 6 | Total Attempts: 15,815
Questions: 10 | Attempts: 9,891

SettingsSettingsSettings
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, you 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!


Questions and Answers
  • 1. 

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

    • A.

      PrintHello() is a function and a is a variable. None of them are objects.

    • B.

      Both printHello() and a refer to the same object.

    • C.

      PrintHello() and a refer to different objects.

    • D.

      Syntax error! You cannot assign function to a variable in Python.

    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.

    Rate this question:

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

    • A.

      A = 10  a = 30

    • B.

      A = 10

    • C.

      A = 20

    • D.

      A = 30

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

    Rate this question:

  • 3. 

    Which of the following statements is true?

    • A.

      A class is blueprint for the object.

    • B.

      You can only make a single object from the given class.

    • C.

      Both statements are true.

    • D.

      Neither statement 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.

    Rate this question:

  • 4. 

    What is the output of the following code?   class Foo: def printLine(self, line='Python'): print(line) o1 = Foo() o1.printLine('Java')

    • A.

      Python

    • B.

      Line

    • C.

      Java

    • D.

      Java Python

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

    Rate this question:

  • 5. 

    What does the __init__() the function do in Python?

    • A.

      Initializes the class for use.

    • B.

      This function is called when a new object is instantiated.

    • C.

      Initializes all the data attributes to zero when called.

    • D.

      None of the above.

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

    Rate this question:

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

    • A.

      0 0

    • B.

      1 1

    • C.

      None None

    • D.

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

    Rate this question:

  • 7. 

    Which of the following codes uses the inheritance feature of Python?

    • A.

      class Foo: Pass

    • B.

      class Foo(object): pass class Hoo(object): pass

    • C.

      class Foo: pass class Hoo(Foo): pass

    • D.

      None of the above code.

    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.

    Rate this question:

  • 8. 

    If you a class is derived from two different classes, it’s called ______

    • A.

      Multilevel Inheritance

    • B.

      Multiple Inheritance

    • C.

      Hierarchical Inheritance

    • D.

      Python Inheritance

    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.

    Rate this question:

  • 9. 

    Which of the following statements is true?

    • A.

      In Python, the same operator may behave differently depending upon operands.

    • B.

      You can change the way operators behave in Python.

    • C.

      Special method __add()__ is called when + operator is used.

    • D.

      All of the above.

    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.

    Rate this question:

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

    • A.

      2 2

    • B.

      4 6

    • C.

      0 0

    • D.

      1 1

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

    Rate this question:

Godwin Iheuwa |MS, Computer Science |
Computer Expert
Godwin is a proficient Database Administrator currently employed at MTN Nigeria. He holds as MS in Computer Science from the University of Bedfordshire, where he specialized in Agile Methodologies and Database Administration. He also earned a Bachelor's degree in Computer Science from the University of Port Harcourt. With expertise in SQL Server Integration Services (SSIS) and SQL Server Management Studio, Godwin's knowledge and experience enhance the authority of our quizzes, ensuring accuracy and relevance in the realm of computer science.

Quiz Review Timeline +

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

Related Topics

Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.