Python MCQ Answer Sheet #4
1.Answer: a
Explanation: If the class definition is class B(A): then class B inherits the methods of
class A. This is called inheritance.
2.Answer: c
Explanation: Any changes made to the private members of the class in the subclass
aren’t reflected in the original members.
3. Answer: c
Explanation: Since the invoking method, Test.__init__(self), isn’t present in the derived
class, variable x can’t be inherited.
4. Answer: b
Explanation: A subtype is something that be substituted for and behave as its parent
type. All subclass may not be a subtype in object-oriented programming.
5. Answer: b
Explanation: B is a subtype of B if instances of type B can substitute for instances of
type A without affecting semantics.
6. Answer: a
Explanation: To invoke the __init__ method in A from B, either of the following should be
written: A.__init__(self) or super().__init__(self).
7. Answer: b
Explanation: For example: >>> type((1,)) gives <class ‘tuple’>.
8. Answer: a
Explanation: Multiple, multi-level, single-level and hierarchical inheritance are all types of
inheritance.
9. Answer: c
Explanation: help() usually gives information of the class on any built-in type or function.
10. Answer: b
Explanation: obj1.two() invokes the method two() in class A which returns ‘A’ and
obj2.two() invokes the method two() in class B which returns ‘B’.
11. Answer: b
Explanation: In multiple inheritance, two or more subclasses are derived from the
superclass as shown in the above piece of code.
12. Answer: a
Explanation: In multi-level inheritance, a subclass derives from another class which itself
is derived from another class.
13. Answer: c
Explanation: In single-level inheritance, there is a single subclass which inherits from a
single superclass. So the class definition of the subclass will be: class B(A): where A is
the superclass.
14. Answer: c
Explanation: Any change made in variable i isn’t reflected as it is the private member of
the superclass.
15. Answer: c
Explanation: If the value of a private variable in a superclass is changed in the subclass,
the change isn’t reflected.
16. Answer: a
Explanation: isinstance(obj,class) returns True if obj is an object class.
17. Answer: c
Explanation: The __eq(other) method is called if any comparison takes place and it is
defined in the object class.
18. Answer: a
Explanation: Method issubclass() returns True if a class is a subclass of another class
and False otherwise.
19. Answer: d
Explanation: Private class members in the superclass can’t be accessed in the subclass.
20. Answer: c
Explanation: The class member x is protected, not private and hence can be accessed
by subclasses.
21. Answer: a
Explanation: The super() method re-assigns the variable x with value 5. Hence 5 is
printed.
22. Answer: a
Explanation: Functions are reusable pieces of programs. They allow you to give a name
to a block of statements, allowing you to run that block using the specified name
anywhere in your program and any number of times.
23. Answer: c
Explanation: The def keyword is used to create, (or define) a function in python
24. Answer: c
Explanation: Here, we define a function called printMax that uses two parameters called
a and b. We find out the greater number using a simple if..else statement and then print
the bigger number.
25. Answer: b
Explanation: The maximum function returns the maximum of the parameters, in this case
the numbers supplied to the function. It uses a simple if..else statement to find the
greater value and then returns that value.
26. Answer: d
Explanation: Python has a nifty feature called documentation strings, usually referred to
by its shorter name docstrings. DocStrings are an important tool that you should make
use of since it helps to document the program better and makes it easier to understand.
27. Answer: d
Explanation: None.
28. Answer: b
Explanation: Built-in functions and user defined ones. The built-in functions are part of
the Python language. Examples are: dir(), len() or abs(). The user defined functions are
functions created with the def keyword.
29. Answer: d
Explanation: Functions can be defined inside a module, a class or another function.
30. Answer: d
Explanation: None.
31. Answer: a
Explanation: Each object in Python has a unique id. The id() function returns the object’s
id.
32. Answer: a
Explanation: Functions that are always available for usage, functions that are contained
within external modules, which must be imported and functions defined by a programmer
with the def keyword.
Eg: math import sqrt
A sqrt() function is imported from the math module.
33. Answer: c
Explanation: A function is created to do a specific task. Often there is a result from such
a task. The return keyword is used to return values from a function. A function may or
may not return a value. If a function does not have a return keyword, it will send a none
value.
34. Answer: a
Explanation: Python supports the creation of anonymous functions (i.e. functions that
are not bound to a name) at runtime, using a construct called lambda. Lambda functions
are restricted to a single expression. They can be used wherever normal functions can
be used.
35. Answer: a
Explanation: The lambda keyword creates an anonymous function. The x is a parameter,
that is passed to the lambda function. The parameter is followed by a colon character.
The code next to the colon is the expression that is executed, when the lambda function
is called. The lambda function is assigned to the z variable.
The lambda function is executed. The number 8 is passed to the anonymous function
and it returns 48 as the result. Note that z is not a name for this function. It is only a
variable to which the anonymous function was assigned.
36. Answer: c
Explanation: None.
37. Answer: b
Explanation: lambda definition does not include a return statement. it always contains an
expression which is returned. Also note that we can put a lambda definition anywhere a
function is expected. We don’t have to assign it to a variable at all.
38. Answer: b
Explanation: lambda is an anonymous function in Python. Hence this statement is false.
39. Answer: b
Explanation: None.
40. Answer: a
Explanation: None.
41. Answer: b
Explanation: None.
42. Answer: c
Explanation: None.
43. Answer: b
Explanation: The value of a variable defined outside all function definitions is referred to
as a global variable and can be used by multiple functions of the program.
44. Answer: c
Explanation: The variable inside a function is called as local variable and the variable
definition is confined only to that function.
45. Answer: c
Explanation: Any change made in to an immutable data type in a function isn’t reflected
outside the function.
46. Answer: a
Explanation: The function a(b) creates a new list with b = b + [5], so it does not modify
the original list c. Since c remains unchanged with four elements, len(c) returns 4. If
b.append(5) were used instead, it would modify c.
47. Answer: d
Explanation: The values given during function call is taken into consideration, that is, i=2
and j=1.
48. Answer: b
Explanation: The parameter two is a variable parameter and consists of (2,3,4). Hence
the data type is tuple.
49. Answer: c
Explanation: A function can exist without a return statement and returns None if the
function doesn’t have a return statement.
50. Answer: a
Explanation: The loop runs three times and ‘z’ is printed each time.
51. Answer: c
Explanation: b combines the remaining parameters into a dictionary.