Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Which of the following blocks will be executed whether an exception is thrown or not?(a) except(b) else(c) finally(d) assertThis question was addressed to me in unit test.Origin of the question is Exception Handling in chapter Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The correct option is (c) finally

Easiest explanation - The STATEMENTS in the finally BLOCK will always be executed, WHETHER an exception is THROWN or not. This clause is USED to close the resources used in a code.

2.

_______________________ exceptions are raised as a result of an error in opening a particular file.(a) ValueError(b) TypeError(c) ImportError(d) IOErrorThis question was addressed to me in an online interview.Origin of the question is Exception Handling in chapter Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Correct answer is (d) IOERROR

Easiest EXPLANATION - IOError exceptions are RAISED as a result of an ERROR in opening or CLOSING a particular file.

3.

An exception is ____________(a) an object(b) a special function(c) a standard module(d) a moduleI have been asked this question during an interview for a job.This interesting question is from Exception Handling topic in chapter Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The CORRECT option is (a) an OBJECT

The BEST explanation: An exception is an object that is raised by a FUNCTION signaling that an unexpected situation has OCCURRED, that the function itself cannot handle.

4.

Syntax errors are also known as parsing errors.(a) True(b) FalseThe question was asked in examination.This question is from Exception Handling topic in division Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Correct ANSWER is (a) TRUE

The BEST explanation: Syntax errors are known as PARSING errors. Syntax errors are raised when there is a deviation from the rules of a language. Hence the STATEMENT is true.

5.

Which of the following is not a standard exception in Python?(a) NameError(b) IOError(c) AssignmentError(d) ValueErrorThis question was posed to me in my homework.My question is from Exception Handling topic in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Correct option is (C) AssignmentError

To explain: NAMEERROR, IOError and VALUEERROR are standard EXCEPTIONS in Python whereas Assignment error is not a standard exception in Python.

6.

Which of the following statements is true?(a) The standard exceptions are automatically imported into Python programs(b) All raised standard exceptions must be handled in Python(c) When there is a deviation from the rules of a programming language, a semantic error is thrown(d) If any exception is thrown in try block, else block is executedThe question was asked in an interview for job.I need to ask this question from Exception Handling in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Correct choice is (a) The standard exceptions are AUTOMATICALLY imported into Python programs

Best explanation: When any exception is THROWN in try block, except block is executed. If exception in not thrown in try block, else block is executed. When there is a deviation from the rules of a PROGRAMMING LANGUAGE, a syntax error is thrown. The only true statement above is: The standard exceptions are automatically imported into Python programs.

7.

What happens when ‘1’ == 1 is executed?(a) we get a True(b) we get a False(c) an TypeError occurs(d) a ValueError occursThis question was posed to me in an interview for job.My question is taken from Exception Handling in portion Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Right option is (b) we GET a FALSE

Easy explanation - It SIMPLY evaluates to False and does not raise any exception.

8.

When is the finally block executed?(a) when there is no exception(b) when there is an exception(c) only if some condition that has been specified is satisfied(d) alwaysThe question was asked in an international level competition.The origin of the question is Exception Handling in division Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The correct ANSWER is (d) always

For explanation: The FINALLY block is always EXECUTED.

9.

Can one block of except statements handle multiple exception?(a) yes, like except TypeError, SyntaxError [,…](b) yes, like except [TypeError, SyntaxError](c) no(d) none of the mentionedThe question was posed to me in an internship interview.I'd like to ask this question from Exception Handling in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The correct option is (a) yes, like except TYPEERROR, SYNTAXERROR [,…]

EXPLANATION: Each type of exception can be specified directly. There is no need to PUT it in a LIST.

10.

When will the else part of try-except-else be executed?(a) always(b) when an exception occurs(c) when no exception occurs(d) when an exception occurs in to except blockI have been asked this question in unit test.My doubt is from Exception Handling in division Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Right answer is (C) when no exception occurs

Best explanation: The ELSE part is EXECUTED when no exception occurs.

11.

How many except statements can a try-except block have?(a) zero(b) one(c) more than one(d) more than zeroI got this question in an interview for internship.Asked question is from Exception Handling topic in chapter Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer» RIGHT CHOICE is (d) more than zero

To explain: There has to be at LEAST one except STATEMENT.
12.

Which of the following is false about protected class members?(a) They begin with one underscore(b) They can be accessed by subclasses(c) They can be accessed by name mangling method(d) They can be accessed within a classI got this question by my school principal while I was bunking the class.The question is from Encapsulation in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Correct ANSWER is (C) They can be accessed by name MANGLING method

Explanation: Protected class MEMBERS can’t be accessed by name mangling.

13.

The purpose of name mangling is to avoid unintentional access of private class members.(a) True(b) FalseThe question was posed to me in an interview.My doubt stems from Encapsulation in chapter Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Correct choice is (a) True

Best explanation: NAME mangling prevents unintentional access of private members of a CLASS, while STILL allowing access when NEEDED. Unless the variable is accessed with its MANGLED name, it will not be found.

14.

Private members of a class cannot be accessed.(a) True(b) FalseThis question was addressed to me by my college professor while I was bunking the class.Asked question is from Encapsulation topic in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Correct OPTION is (b) False

Easiest explanation - Private members of a class are accessible if written as follows: obj._Classname__privatemember. Such renaming of IDENTIFIERS is CALLED as name MANGLING.

15.

Methods of a class that provide access to private members of the class are called as ______ and ______(a) getters/setters(b) __repr__/__str__(c) user-defined functions/in-built functions(d) __init__/__del__This question was posed to me in class test.This question is from Encapsulation topic in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer» RIGHT choice is (a) getters/setters

To EXPLAIN I would say: The PURPOSE of getters and setters is to get(RETURN) and SET(assign) private instance variables of a class.
16.

Which of the following is the most suitable definition for encapsulation?(a) Ability of a class to derive members of another class as a part of its own definition(b) Means of bundling instance variables and methods in order to restrict access to certain class members(c) Focuses on variables and passing of variables to functions(d) Allows for implementation of elegant software that is well designed and easily modifiedThe question was asked in a job interview.I would like to ask this question from Encapsulation in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The correct CHOICE is (b) Means of bundling instance VARIABLES and methods in order to restrict access to certain class MEMBERS

To EXPLAIN: The values assigned by the constructor to the class members is used to create the object.

17.

Which of these is not a fundamental features of OOP?(a) Encapsulation(b) Inheritance(c) Instantiation(d) PolymorphismThis question was posed to me in exam.This interesting question is from Encapsulation topic in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer» CORRECT choice is (C) Instantiation

To EXPLAIN: Instantiation simply refers to creation of an instance of class. It is not a FUNDAMENTAL feature of OOP.
18.

Which of the following statements is true?(a) A non-private method in a superclass can be overridden(b) A subclass method can be overridden by the superclass(c) A private method in a superclass can be overridden(d) Overriding isn’t possible in PythonThe question was asked during an interview.My doubt stems from Polymorphism in portion Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Right OPTION is (a) A non-private method in a superclass can be overridden

Explanation: A PUBLIC method in the base CLASS can be overridden by the same named method in the subclass.

19.

Overriding means changing behaviour of methods of derived class methods in the base class.(a) True(b) FalseThis question was posed to me at a job interview.I would like to ask this question from Polymorphism in portion Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The CORRECT option is (b) False

The BEST I can EXPLAIN: Overriding means if there are two same methods present in the superclass and the subclass, the contents of the subclass method are EXECUTED.

20.

A class in which one or more methods are only implemented to raise an exception is called an abstract class.(a) True(b) FalseI have been asked this question in examination.This intriguing question originated from Polymorphism topic in chapter Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The correct choice is (a) True

The EXPLANATION is: A class in which ONE or more METHODS are unimplemented or implemented for the methods throw an EXCEPTION is CALLED an abstract class.

21.

What is the use of duck typing?(a) More restriction on the type values that can be passed to a given method(b) No restriction on the type values that can be passed to a given method(c) Less restriction on the type values that can be passed to a given method(d) Makes the program code smallerI had been asked this question in an international level competition.My question is based upon Polymorphism in portion Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The correct choice is (C) Less restriction on the type values that can be passed to a given method

The explanation: In PYTHON, any set of classes with a COMMON set of methods can be treated similarly. This is called duck TYPING. Hence duck typing imposes less restrictions.

22.

What is the biggest reason for the use of polymorphism?(a) It allows the programmer to think at a more abstract level(b) There is less program code to write(c) The program will have a more elegant design and will be easier to maintain and update(d) Program code takes up less spaceI had been asked this question in exam.Query is from Polymorphism in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Right CHOICE is (c) The program will have a more elegant DESIGN and will be EASIER to maintain and update

To explain: Polymorphism ALLOWS for the implementation of elegant SOFTWARE.

23.

Which of the following best describes polymorphism?(a) Ability of a class to derive members of another class as a part of its own definition(b) Means of bundling instance variables and methods in order to restrict access to certain class members(c) Focuses on variables and passing of variables to functions(d) Allows for objects of different types and behaviour to be treated as the same general typeThis question was posed to me in homework.My question is based upon Polymorphism topic in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Right option is (d) Allows for OBJECTS of different TYPES and behaviour to be treated as the same general type

For EXPLANATION: Polymorphism is a feature of object-oriented programming languages. It allows for the implementation of elegant software that is well designed and EASILY MODIFIED.

24.

What does built-in function help do in context of classes?(a) Determines the object name of any value(b) Determines the class identifiers of any value(c) Determines class description of any built-in type(d) Determines class description of any user-defined built-in typeI had been asked this question in a national level competition.This is a very interesting question from Inheritance topic in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The correct choice is (c) DETERMINES class description of any built-in TYPE

Easy EXPLANATION - HELP() usually GIVES information of the class on any built-in type or function.

25.

Which of the following is not a type of inheritance?(a) Double-level(b) Multi-level(c) Single-level(d) MultipleI got this question during an interview for a job.I need to ask this question from Inheritance topic in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The CORRECT answer is (a) Double-level

The BEST I can explain: MULTIPLE, multi-level, single-level and HIERARCHICAL inheritance are all types of inheritance.

26.

What does built-in function type do in context of classes?(a) Determines the object name of any value(b) Determines the class name of any value(c) Determines class description of any value(d) Determines the file name of any valueI got this question during an online interview.This interesting question is from Inheritance topic in portion Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer» RIGHT answer is (B) Determines the class name of any value

Easiest explanation - For EXAMPLE: >>> type((1,)) gives .
27.

Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you should write?(a) A.__init__(self)(b) B.__init__(self)(c) A.__init__(B)(d) B.__init__(A)The question was asked in homework.This is a very interesting question from Inheritance topic in portion Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The CORRECT choice is (a) A.__init__(self)

EASY explanation - To INVOKE the __init__ method in A from B, either of the FOLLOWING should be written: A.__init__(self) or super().__init__(self).

28.

What does print(Test.__name__) display (assuming Test is the name of the class)?(a) ()(b) Exception is thrown(c) Test(d) __main__I got this question in unit test.Asked question is from Classes and Objects topic in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Right answer is (c) Test

Easy EXPLANATION - __name__ built-in CLASS attribute is USED to DISPLAY the class name.

29.

__del__ method is used to destroy instances of a class.(a) True(b) FalseThe question was posed to me during an interview for a job.The query is from Classes and Objects in division Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The correct option is (a) True

The best explanation: ___del__ METHOD acts as a destructor and is USED to destroy OBJECTS of CLASSES.

30.

What is Instantiation in terms of OOP terminology?(a) Deleting an instance of class(b) Modifying an instance of class(c) Copying an instance of class(d) Creating an instance of classI got this question during an interview.This interesting question is from Classes and Objects in division Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer» RIGHT choice is (d) Creating an INSTANCE of class

Explanation: Instantiation REFERS to creating an object/instance for a class.
31.

What is getattr() used for?(a) To access the attribute of the object(b) To delete an attribute(c) To check if an attribute exists or not(d) To set an attributeI got this question in semester exam.The above asked question is from Classes and Objects in chapter Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The correct CHOICE is (a) To access the ATTRIBUTE of the OBJECT

Easy explanation - getattr(OBJ,name) is used to GET the attribute of an object.

32.

What is setattr() used for?(a) To access the attribute of the object(b) To set an attribute(c) To check if an attribute exists or not(d) To delete an attributeThis question was addressed to me in exam.The doubt is from Classes and Objects topic in chapter Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The CORRECT option is (B) To SET an attribute

For explanation: setattr(obj,name,value) is used to set an attribute. If attribute doesn’t exist, then it would be CREATED.

33.

_____is used to create an object.(a) class(b) constructor(c) User-defined functions(d) In-built functionsThis question was addressed to me during a job interview.Query is from Classes and Objects topic in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Correct answer is (b) constructor

The best I can explain: The values assigned by the constructor to the class MEMBERS is used to CREATE the OBJECT.

34.

_____ represents an entity in the real world with its identity and behaviour.(a) A method(b) An object(c) A class(d) An operatorThe question was asked by my school teacher while I was bunking the class.The doubt is from Classes and Objects topic in section Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Right OPTION is (B) An OBJECT

Explanation: An object represents an entity in the real world that can be DISTINCTLY identified. A class may DEFINE an object.

35.

Which function overloads the // operator?(a) __div__()(b) __ceildiv__()(c) __floordiv__()(d) __truediv__()This question was addressed to me in an interview.I need to ask this question from Operator Overloading topic in division Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The CORRECT CHOICE is (C) __floordiv__()

For EXPLANATION: __floordiv__() is for //.

36.

Which operator is overloaded by the __or__() function?(a) ||(b) |(c) //(d) /I have been asked this question in an interview for job.Question is from Operator Overloading in portion Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The correct answer is (B) |

The BEST I can EXPLAIN: The FUNCTION __or__() overloads the bitwise OR operator |.

37.

Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?(a) __add__(), __str__()(b) __str__(), __add__()(c) __sum__(), __str__()(d) __str__(), __sum__()I got this question in an interview.My question is from Operator Overloading in portion Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Right OPTION is (a) __add__(), __str__()

For EXPLANATION: The FUNCTION __add__() is called first since it is within the bracket. The function __str__() is then called on the object that we received after ADDING A and B.

38.

Which function overloads the >> operator?(a) __more__()(b) __gt__()(c) __ge__()(d) none of the mentionedI have been asked this question at a job interview.This question is from Operator Overloading in chapter Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

Right ANSWER is (d) NONE of the mentioned

Easy explanation - __rshift__() overloads the >> OPERATOR.

39.

Which operator is overloaded by __lg__()?(a) (c) !=(d) none of the mentionedI had been asked this question in an interview for internship.The above asked question is from Operator Overloading topic in division Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The CORRECT ANSWER is (d) NONE of the mentioned

Easiest explanation - __lg__() is invalid.

40.

Which function overloads the == operator?(a) __eq__()(b) __equ__()(c) __isequal__()(d) none of the mentionedThis question was posed to me in a national level competition.Question is taken from Operator Overloading topic in chapter Classes and Objects, Inheritance, Polymorphism, Encapsulation and Exception Handling of Python

Answer»

The correct choice is (a) __eq__()

The EXPLANATION is: The other two do not EXIST.