Python Online Quiz for Experienced – Classes and Objects

If you enjoy getting through the coding problems, then you must attempt this Python online quiz.

It packages 21 coding snippets focussing on Python classes. With this quiz, you can quickly assess the technical depth of your Python programming ability.

Those who are beginners and learning object-oriented programming, they should attempt this quiz on Python classes for beginners that we delivered a while ago.

Next, we always wish to make you a better programmer. But it’s not possible until you get well-versed in all core Python features like OOPs. And even if you know Python classes but are you using them efficiently?

Also, there are many key programming decisions that you have to make as a programmer. For example –

  • When to use inheritance?
  • Define the scope and visibility of class members.
  • Whether to create an abstract class or use an interface?
  • How to utilize built-in class attributes?
  • Optimizing your class for performance.

So, it’s better to brainstorm the answers to the above points. Thinking in such a way would uplift your design and programming skills.

Before diving upon the quiz, let’s quickly know how to deal with Python classes. Though, you can use the below TOC to navigate to different sections of the post.

Quick Python Tutorial.

Create a class in Python.

Python entitles the class keyword to produce a new class definition. Within a class, you can add member variables and methods. Here are a few facts about the accessibility.

Public.

All members of a class are public by default in Python.

Now, you have a class ClipBoard. Following operations, you can do with it.

Protected.

Unlike C++/Java, which support protected members by design, Python allows implementing it by convention. Using underscore (_) as a prefix to any member, you mark it as protected.

Python doesn’t hold you from using a protected member. But it gives a symbolic way of doing that.

Private.

You can use name mangling technique to define a member as private in Python. It changes the way we usually access a class member. It states that if a member name begins with two underscores, then access the member in the following manner.

To access the private member, you can do the following.

If you use a private member differently, then Python will throw an error.

Next, Python provides a set of built-in class attributes which you should know.

Built-in Class Attributes.

Python inculcates a lot of other features in classes. It does so with the help of a no. of built-in attributes.

1. __doc__ – It returns None by default or the doc string if set already.

2. __module__ – It’s the name of target module containing the class definition.

3. __name__ – Name of the class.

4. __dict__ – Represents the dictionary referencing to the class’s namespace.

5. __bases__ – Mostly, it’s an empty tuple holding an ordered list of base classes.

Next, let’s have a look at the concept of inheritance in Python.

Inheritance in Python.

You don’t need a keyword to setup inheritance for a class in Python. See how easy it is to prepare a derived class.

Let’s now extend the ClipBoard class, please see the below example.

Now, use the below instructions to test our derived and base classes.

When you club the above coding snippets and run the code, it’ll return the following output.

Overriding.

Yes, you can override the parent class methods and modify their functionality. Also, you can override operators like +,- and a list of generic class methods.

Here is a list of generic methods that you can override.

We hope the brief intro of Python classes above would help. Now, it’s time to turn on the Python online quiz’s start button.

Move to the TOC.

Python Online Quiz – 21 Questions for Experienced Programmers.

Python Online Quiz - 21 Questions for Experienced Programmers

21 Python Coding Questions.

Q-1. What will be the output of the following code?

A. The program gives an error because there is no constructor for class Test.
B. Signature for the print method is incorrect, so an error is thrown.
C. The correct output is .
D. The above code will execute correctly on changing print(s) to print(self.s).

Click to check the answer.
Answer. D

Q-2. What will be the output of the following code?

A. The program has an error because class Test does not have a constructor.
B. The above code produces an error because the definition of print(s) does not include .
C. It executes successfully but prints nothing.
D. The program has an error because of the constructor call is made without an argument.

Click to check the answer.
Answer. D

Q-3. What will be the output of the following code?

A. The program has an error because the constructor is not present in class Test.
B. The above code produces an error because the definition of print(s) does not contain .
C. It executes successfully but prints nothing.
D. The program has an error because of the constructor call is made without an argument.
E. The program executes successfully and prints Welcome.

Click to check the answer.
Answer. E

Q-4. What will be the output of the following code snippet?

A. The program has an error because x is private and cannot be accessed outside of the class.
B. The program has an error because you cannot name a variable using .
C. The program runs fine and prints 1.
D. The program runs fine and prints nothing.

Click to check the answer.
Answer. C

Q-5. What will be the output of the following code snippet?

A. The program has an error because y is private and should not access it from outside the class.
B. The program has an error because you cannot name a variable using __y.
C. The program runs fine and prints 1.
D. The program runs fine and prints nothing.

Click to check the answer.
Answer. A

Explanation: Python protects those members by internally changing the name to include the class name. You can access such attributes as object._className__attrName. Thus replace the last line of the following code as <val._Test_y>.

Q-6. What will be the output of the following code snippet?

A. The program has an error because x is private and should not access it from outside the class.
B. The program has an error because you cannot name a variable using __y.
C. The program runs fine and prints 1.
D. The program runs fine and prints 45.

Click to check the answer.
Answer. D

Q-7. Which of the following is a private data field in the given code snippet?

A. __a
B. __b
C. __c__
D. __d__

Click to check the answer.
Answer. B

Q-8. What will be the output of the following code snippet?

A. The program has an error because y is private and should not access it from outside the class.
B. The program has an error because you cannot name a variable using __y.
C. The code runs fine and prints 1.
D. The code executes successfully and prints 45.

Click to check the answer.
Answer. A

Q-9. What will be the output of the following code snippet?

A. counter is 101 , number of times is 0
B. counter is 100, number of times is 0
C. counter is 100, number of times is 100
D. counter is 101, number of times is 101

Click to check the answer.
Answer. B

Q-10. What code can we put at the third line of the definition of class B to invoke its superclass’s constructor?

A. super().__init__(self)
B. super().__init__()
C. A.__init__()
D. A.__init__(self)

Click to check the answer.
Answer. B and D

Q-11. What will be the output of the following code snippet?

A. 0 0
B. 0 1
C. 1 2
D. 0 2
E. 2 1

Click to check the answer.
Answer. C

Q-12. What will be the output of the following code snippet?

A. 1 10
B. 1 20
C. 2 10
D. 2 20

Click to check the answer.
Answer. B

Q-13. What will be the output of the following code snippet?

A. 2 0
B. 3 1
C. 4 0
D. 3 0
E. 4 1

Click to check the answer.
Answer. B

Q-14. What will be the output of the following code snippet?

A. B’s __new__() invoked A’s __init__() invoked
B. B’s __new__() invoked A’s __new__() invoked
C. B’s __new__() invoked A’s __init__() invoked A’s __new__() invoked
D. A’s __init__() invoked A’s __new__() invoked

Click to check the answer.
Answer. C

Q-15. What will be the output of the following code snippet?

A. None None
B. None 11
C. 11 None
D. 11 11
E. AttributeError: ‘B’ object has no attribute ‘x’

Click to check the answer.
Answer. E

Q-16. What will be the output of the following code snippet?

A. 1
B. 2
C. 10
D. x is not accessible from the object of classB.

Click to check the answer.
Answer. B

Q-17. What will be the output of the following code snippet?

A. A C B
B. A B C
C. C B A
D. B B B

Click to check the answer.
Answer. C

Q-18. What will be the output of the following code snippet?

A. B B B
B. A B C
C. C B A
D. A A A

Click to check the answer.
Answer. D

Q-19. What will be the output of the following code snippet?

A. True
B. False
C. 2
D. 1

Click to check the answer.
Answer. A

Q-20. What will be the output of the following code snippet?

A. A’s getInfo is called A’s getInfo is called
B. A’s getInfo is called B’s getInfo is called
C. B’s getInfo is called A’s getInfo is called
D. B’s getInfo is called B’s getInfo is called

Click to check the answer.
Answer. B

Q-21. What will be the output of the following code snippet?

A. A’s getInfo is called A’s getInfo is called
B. A’s getInfo is called B’s getInfo is called
C. B’s getInfo is called A’s getInfo is called
D. B’s getInfo is called B’s getInfo is called

Click to check the answer.
Answer. A

Summary – Python Online Quiz for Experienced Programmers.

We are hopeful that you’ve liked the above Python online quiz and the idea to bring a brief tutorial of Python classes. You can also go through a no. of other Python quizzes on our blog that could be useful for you.

Before leaving, if you like to share a topic of your choice, then please write it in the comment box. We’ll add it to our roadmap and try to deliver a post at the earliest.

Best,

TechBeamers.

Leave a Reply


*