CHAPTER 5 - Functions, Classes & Objects
I. Choose the correct answer :
1. A __________ is a set of statements that take inputs, do some specific computation and produce output.
a. function b. class c. array d. structure
2. The main advantage of using functions is that it reduces the size of the python program and induces __________
of code.
a. repetition b. reusability c. recreation d. reformation
3. In python there are ________ types of functions.
a. 4 b. 3 c. 2 d. none
4. _______________ functions are pre defined in python.
a. User defined b. Built - in c. Recursive d. Inline
5. __________ functions are the functions that the user create themselves.
a. User defined b. Built - in c. header file d. Inline
6. Lamda nd recursive functions come under __________ functions.
a. User defined b. Built - in c. header file d. Inline
7. To define a function in python ____________ keyword is used.
a. fun b. function c. def d. del
8. While defining a function in python a __________ mark the end of function header.
a. question mark (?) b. dot (.) c. double quotes( “) d. colon (:)
9. Indentation in python are usually _____________ spaces.
a. 3 b. 4 c. 5 d. 6
10. The _____________ statement is used to exit a function and go back to the place from where the function was
called.
a. exit b. goto c. return d. break
11. The __________ statement can contain expression which gets executed and the value is returned backj to
place where it was called.
a. exit b. goto c. return d. break
12. ___________ number of return statements are allowed in a function block.
a. 2 b. 3 c. 5 d. any
13. Any number of return statements are allowed in a function block, but __________ will be executed when the
program is run.
a. only one b. 2 c. 3 d. any
14. def are(base,height):
In the above function definition statement there are ___________ parameters.
a. 1 b. 2 c. 3 d. no
15. __________ are the variables used when defining a function, into which the arguments will be copied.
a. Tokens b. Identifier c. Parameter d. Operator
16. ____________ are the values which are passed to any function through the function call statement.
a. Arguments b. Identifier c. Parameter d. Operator
17. Default arguments can be given in a function prototype from ___________ only.
a. left to right b. top to bottom c. right to left c. bottom to top
18. We can use ____________ followed by a variable name to define variable length arguments.
a. plus (+) b. minus (-) c. asterisk (*) d. caret (^)
19. Lamda function are also called as ________ functions.
a. named b. unknown c. recursive d. anonymous
20. __________ functions can take any number of arguments as input, but must have only one expression to
evaluate.
a. Named b. Unknown c. Recursive d. Lamda
21. Pre defined functions are called as ___________ functions.
a. already defined b. user defined c. built in d. header file
22. Function that call themselves are called as __________ function.
a. inline b. recursive c. built in d. user defined
23. The recursion ends when the number gets reduced to ______.
a. 1 b. n-1 c. n d. 0
24. Every recursion function must have a __________ condition that stops the recursion.
a. high b. base c. relational d. expression
25. A complex task can be broken down into simpler sub-programs using __________.
a. inline b. recursive c. lamda d. operators
26. _____________ is the main building block in python.
a. class b. recursion c. inline d. lamda
27. _______________ is a collection of data and functions that act on those data.
a. class b. object c. functions d. structures
28. ___________ is a template for the object.
a. Recursion b. Function c. Class d. Constructors
29. __________ are also called as instance of a class or class variables.
a. Recursion b. Function c. Class d. Objects
30. In python, everything is an ___________.
a. recursion b. function c. class d. objects
31. All integer variables that we use in our python program are object of class __________.
a. numbers b. numerals c. int d. integers
32. All string variables that we use in our python program are object of class __________.
a. string b. character c. str d. char
33. In python, a class is defined by using the keyword ___________.
a. def b. lamda c. class d. struct
34. Every class has a unique name followed by a ______________.
a. semi colon(;) b. caret(^) c. colon(:) d. question mark(?)
35. Variables defined inside a class are called _______________.
a. attributes b. arguments c. parameters d. identifiers
36. Functions defined inside a class is called _____________.
a. methods b. lamda c. built in d. class functions
37. Class variable and methods are together known as __________ of a class.
a. arguments b. parameters c. methods d. members
38. The class members should be accessed through____________ of a class.
a. members b. object or instance c. arguments d. methods
39. The process of creation of objects for a class is called ____________.
a. instance b. instantiation c. object creation d. objectification
40. Any class members can be accessed by using the object name followed by a _______ operator.
a. semi colon(;) b. caret(^) c. colon(:) d. dot (.)
41. The class function must have the first argument named as __________.
a. self b. shelve c. shell d. sef
42. The ________ parameter is a reference to the current instance of the class.
a. self b. shelve c. shell d. sef
43. ___________ is the special built - in function that is automatically executed when an object of a class is created
a. Members b. Constructor c. Arguments d. Methods
44. In python, there is a special function called ___________ which acts as a constructor.
a. __init__() b. _init_() c. __del__() d. __cons__()
45. __________ keyword must be preceded and followed by double underscore.
a. init b. def c. lamda d. class
46. ___________ function is used to initialize the class variable.
a. Constructor b. Destructor c. Anonymous d. Class
47. Constructor allots memory space for the ________ created.
a. Recursion b. Function c. Class d. Objects
48. __________ is also a special built in method that gets executed when an object exits from its scope.
a. Constructor b. Destructor c. Anonymous d. Class
49. ___________ works just opposite to the constructor.
a. Constructor b. Destructor c. Anonymous d. Class
50. _____________ removes the memory space allotted to the object by the constructor when the object loses its
scope.
a. Functions b. Destructor c. Anonymous d. Class
51. In python, ___________ method is used as destructor.
a. __init__() b. _init_() c. __del__() d. __cons__()
52. ‘del’ keyword must be preceded and followed by _________.
a. plus(+) b. colon(.) c. double underscore(__) d. semi colon(;)
53. Destructor will be executed when the object is deleted using _________ command followed by the object
name.
a. del b. self c. cons d. def
54. There are ____________ types of access specifiers.
a. 1 b. 2 c. 3 d. 4
55. The ____________ are used to protect the data from being accessed by unauthorized persons or programs.
a. access specifiers b. constructors c. destructors d. classes
56. ____________ members of a class can be accessed only from within the class.
a. Private b. public c. protected d. all of these
57. _____________ members are denied access from outside the class in which they are declared.
a. Private b. public c. protected d. all of these
58. __________ members are accessible from outside the class, inside the class and its sub - classes.
a. Private b. public c. protected d. all of these
59. _____________ members of a class are accessible from within the class and are also available to its sub -
classes.
a. Private b. public c. protected d. all of these
60. All members in a python class are __________ by default.
a. Private b. public c. protected d. all of these
61. To declare a member as protected, we have to prefix a __________.
a. double underscore(__) b. single underscore(_) c. minus( - ) d. equal to (= )
62. Any attempt to access protected members from within the same class or its derived class will result in an
_________ error.
a. syntax b. logical c. runtime d. attribute
63. A member prefixed with ____________ makes it private.
a. double underscore(__) b. single underscore(_) c. minus( - ) d. equal to (= )
II. Fill in the blanks :
1. The statement that invokes the function is called __________. (function call statement)
2. In python, we can provide ____________ values for the parameters in the function header statement. (default)
3. _________________ allows the user to call the function with partial arguments or no arguments at all. (Default
arguments)
4. _____________ is a feature that allows a function to receive any number of arguments. (Variable length
arguments)
5. The ___________ function in python is an example for a function that supports variable length arguments.
(print())
6. To import math module in python, the line _________________ is used in the program. (import math)
7. ________ of a number is the product of all the integers from 1 to that number. (Factorial)
8. ____________ and ______________ are the key features of object oriented programming. (Classes, object)
III. Match the following:
1. Private members - a. returns the square root of the number x
2. Protected members - b. returns x to the power of y
3. Default access specifier - c. del
4. Constructor - d. lamda
5. Destructor - e. double underscore( __ )
6. Function keyword - f. single underscore ( _ )
7. Anonymous function - g. public
8. Function that call themselves - h. returns the cosine value of the degree x
9. pow(x,y) - i. __init__()
10. math.sqrt() - j. __del__()
11. math. cos(x) - k. returns the factorial of the number x
12. math.factorial(x) - l. recursive functions
ANSWER
1. e 2. f 3. g 4. i 5. j 6. c
7. d 8. l 9. b 10. a 11. h 12. k
IV. Say true or false:
1. User defined functions need not be defined before they can be used in the program. [False]
2. If all the arguments are provided in the function call statement, the default values are ignored. [True]
3. We can have default values for second parameter from right without giving default value for the first parameter
from right. [False]
4. Lamda functions are defined with keyword def and a function name. [False]
5. In python. it is possible to call a function by itself. [True]
6. Recursion functions make the code look clean and elegant. [True]
7. Recursive functions are easy to debug. [False]
8. Sometimes the logic behind recursion is hard to follow. [True]
9. While calling a class member function, we need to pass value for self argument. [ False]
10. Python automatically provides value to the self argument. [True]
11. Even if a function takes no argument, self parameter should be defined. [True]