Python Programming Question Bank eDBDA Sept 21
Python Programming Question Bank eDBDA Sept 21
Contents
PYTHON ...................................................................................................................................................................... 1
BASIC OPERATOR ........................................................................................................................................................ 4
WHILE AND FOR LOOP................................................................................................................................................ 5
DICTIONARY ................................................................................................................................................................ 7
FILES ............................................................................................................................................................................ 9
FUNCTION ................................................................................................................................................................. 14
ARGUMENT............................................................................................................................................................... 17
EXCEPTION HANDLING ............................................................................................................................................. 19
CORE DATA TYPES .................................................................................................................................................... 20
CLASSES & OBJECT .................................................................................................................................................... 22
INHERITANCE ............................................................................................................................................................ 24
EXTRA MCQ .............................................................................................................................................................. 28
PYTHON
Q.1) What is the output of the following?
i = 1 while True: if i%0O7 == 0:
break print(i) i += 1
a) 1 2 3 4 5 6 b) 1 2 3 4 5 6 7 c) error d) none of the entioned
Q.4) What is the output of the following? x = "abcdef" i = "a" while i in x[1:]:
print(i, end = " ")
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Python Programming Question Bank eDBDA Sept 21
a) a a a a a a b) a c) no output d) error
Q.7) The if...elif...else executes only one block of code among several blocks.
a) True b) False c) It depends on expression used. d) There is no elif statement in Python.
Q.9)In Python, for and while loop can have optional else statement?
a) Only for loop can have optional else statement
b) Only while loop can have optional else statement
c) Both loops can have optional else statement
d) Loops cannot have else statement in Python
Q.10) What is the output of the following code? i = sum = 0 while i <= 4: sum += i i = i+1
print(sum)
a) 0 b) 10 c) 4 d) None of the above
Q.12) Is it better to use for loop instead of while if you are iterating through a sequence (like: list)? a)
No, it’s better to use while loop.
b) Yes, for loop is more pythonic choice.
c) No, you cannot iterate through a sequence using while loop.
d) No, you cannot iterate through a sequence using loops.
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Python Programming Question Bank eDBDA Sept 21
Q.15) Which of the following statement is true about the pass statement?
a) The Python interpreter ignores the pass statement like comments.
b) The pass statement terminates the loop containing it.
c) It is used as a placeholder for future implementation of functions, loops etc d) All of the above.
Q.21) What is the output when the following code is executed ? "Welcome to Python".split()
a) [“Welcome”, “to”, “Python”]. b) (“Welcome”, “to”, “Python”)
c) {“Welcome”, “to”, “Python”} d) “Welcome”, “to”, “Python”
BASIC OPERATOR
i) arentheses ii)
xponential iii)
Multiplication
iv) Division
v) Addition
vi) Subtraction
a) i,ii,iii,iv,v,vi b) ii,i,iii,iv,v,vi c) ii,i,iv,iii,v,vi d) i,ii,iii,iv,vi,v Explanation: For order of precedence,
just remember this PEMDAS (similar to BODMAS)
9. The expression Int(x) implies that the variable x is converted to integer. State whether true or false.
a) True b) False
10. Which one of the following have the highest precedence in the expression?
a) Exponential b) Addition c) Multiplication d) Parentheses
Explanation: Just remember: PEDMAS, that is, Parenthesis, Exponentiation, Division,
Multiplication, Addition, Subtraction. Note that the precedence order of Division and Multiplication is the
same. Likewise, the order of Addition and Subtraction is also the same.
a) [‘ab’, ‘cd’]. b) [‘AB’, ‘CD’]. c) [None, None]. d) none of the mentioned View
Explanation: The function upper() does not modify a string in place, it returns a new string which isn’t
being stored anywhere.
Explanation: The numbers 2 and 4 are printed. The next value of i is 6 which is divisible by 3 and hence control
exits the loop.
DICTIONARY
1. Which of these about a dictionary is false?
a) The values of a dictionary can be accessed using keys
b) The keys of a dictionary can be accessed using values
c) Dictionaries aren’t ordered
d) Dictionaries are mutable View Answer
Explanation: The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be
accessed using values.
FILES
1. To open a file c:\scores.txt for reading, we use
a) infile = open(“c:\scores.txt”, “r”) b) infile = open(“c:\\scores.txt”, “r”)
c) infile = open(file = “c:\scores.txt”, “r”) d) infile = open(file = “c:\\scores.txt”, “r”) Explanation: Execute
help(open) to get more details.
6. To read the entire remaining contents of the file as a string from a file object infile, we use
a) infile.read(2) b) infile.read() c) infile.readline() d) infile.readlines() View
Explanation: read function is used to read all the lines in a file.
8. To read the next line of the file from a file object infile, we use
a) infile.read(2) b) infile.read() c) infile.readline() d) infile.readlines()
Explanation: Execute in the shell to verify.
9. To read the remaining lines of the file from a file object infile, we use
a) infile.read(2) b) infile.read() C) infile.readline() d) infile.readlines()
View Answer
Explanation: Execute in the shell to verify.
TUPLES
Q.1) Which of the following is a Python tuple?
a) [1, 2, 3]. b) (1, 2, 3) c) {1, 2, 3} d) {}
>>>t[1:-1]
Q.12)What is the output of the following piece of code when executed in Python shell? >>>
a=("Check")*3
>>> a
a) (‘Check’,’Check’,’Check’) b) * Operator not valid for tuples
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Python Programming Question Bank eDBDA Sept 21
(a,) print(a)
a) Error, tuples are immutable b) ((‘check’,),) (((‘check’,),),).
c) ((‘check’,)’check’,) d) ((‘check’,)’check’,) (((‘check’,)’check’,)’check’,)
Q.22) What is the output of the following piece of code when executed in Python shell?
>>> a=(1,2) >>> b=(3,4)
>>> c=a+b
>>> c
a) (4,6) b) (1,2,3,4) c) Error as tuples are immutable d) None
FUNCTION
1. Which of the following is the use of function in python?
a) Functions are reusable pieces of programs
b) Functions don’t provide better modularity for your application
c) you can’t also create your own functions
d) All of the mentioned View Answer
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.
4.
5. sayHello() # call the function
6. sayHello() # call the function again
Explanation: For some functions, you may want to make some parameters optional and use default values
in case the user does not want to provide values for them. This is done with the help of default argument
values. You can specify default argument values for parameters by appending to the parameter name in
the function definition the assignment operator (=) followed by the default value.
The function named say is used to print a string as many times as specified. If we don’t supply a value, then
by default, the string is printed just once. We achieve this by specifying a default argument value of 1 to
the parameter times.
In the first usage of say, we supply only the string and it prints the string once. In the second usage of say,
we supply both the string and an argument 5 stating that we want to say the string message 5 times.
In the first usage, func(3, 7), the parameter a gets the value 3, the parameter b gets the value 7 and c gets
the default value of 10.
In the second usage func(25, c=24), the variable a gets the value of 25 due to the position of the argument.
Then, the parameter c gets the value of 24 due to naming i.e. keyword arguments. The variable b gets the
default value of 5.
In the third usage func(c=50, a=100), we use keyword arguments for all specified values. Notice that we are
specifying the value for parameter c before that for a even though a is defined before c in the function
definition.
ARGUMENT
1. What is the output of the following code? def
foo(k): k = [1] q = [0] foo(q) print(q)
a) [0]. b) [1] c) [1, 0]. d) [0, 1].
Explanation: A new list object is created in the function and the reference is lost. This can be checked by
comparing the id of k before and after k = [1].
3. Which module in the python standard library parses options received from the command line?
a) getopt b) os c) getarg d) main
Explanation: getopt parses options received from the command line.
9. Where are the arguments received from the command line stored?
a) sys.argv b) os.argv c) argv d) none of the mentioned
Explanation: Refer documentation.
EXCEPTION HANDLING
Q.1) How many except statements can a try-except block have?
a) zero b) one c) more than one d) more than zero
Q.10) Syntax errors are also known as parsing errors. Is this statement true or false?
a) True b) False
Q.11) Which of the following blocks will be executed whether an exception is thrown or not?
a)except b)else c)finally d)assert
3. Given a function that does not return any value, What value is thrown by default when executed in shell.
a) int b) bool c) void d)None
Explanation: Python shell throws a NoneType object back.
4. Following set of commands are executed in shell, what will be the output? .>>>str="hello"
>>>str[:2]
>>>
a) he b) lo c) olleh d) hello View Answer
Explanation: We are printing only the 1st two bytes of string and hence the answer is “he”.
Explanation: Execute help(round) in the shell to get details of the parameters that are passed into the
round function.
7. In python we do not specify types,it is directly interpreted by the compiler, so consider the following
operation to be performed.
1. >>>x = 13 ? 2 objective is to make sure x has a integer value, select all that
apply (python 3.xx) a) x = 13 // 2
b) x = int(13 / 2)
c) x = 13 % 2
d) All of the mentioned
Explanation: // is integer operation in python 3.0 and int(..) is a type cast operator.
Q.10 In order to store values in terms of key and value we use what core datatype.
a) list b) tuple c) class d) dictionary
Explanation: Dictionary stores values in terms of keys and values.
3. harry
Select all of the function calls that result in this output
a) print(”’tom \ndick \nharry”’)
b) print(”’tomdickharry”’)
c) print(‘tom\ndick\nharry’)
d) print(‘tom dick harry’) View Answer Explanation: The \n adds a new line.
13. What is the average value of the code that is executed below ?
1. >>>grade1 = 80
2. >>>grade2 = 90
3. >>>average = (grade1 + grade2) / 2
a) 85 b) 85.1 c) 95 d) 95.1
Explanation: Cause a decimal value to appear as output.
INHERITANCE
1. What type of inheritance is illustrated in the following piece of code?
class A(): pass class B(A): pass class C(B): pass
a) Multi-level inheritance b) Multiple inheritance
c) Hierarchical inheritance d) Single-level inheritance
Explanation: In multi-level inheritance, a subclass derives from another class which itself is derived from
another class.
a) A subclass derives from a class which in turn derives from another class
b) A single superclass inherits from multiple subclasses
c) A single subclass derives from a single superclass
d) Multiple base classes inherit a single derived class View Answer
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.
6. What is the output of the following piece of code when executed in the Python shell?
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Python Programming Question Bank eDBDA Sept 21
c) 5
d) Error, private class member can’t be accessed in a subclass
Explanation: The class member x is protected, not private and hence can be accessed by subclasses.
print(self._x)
def main():
obj = B()
obj.display()
main() a) 5
b) Error, class member x has two values
c) 3
d) Error, protected class member can’t be accessed in a subclass View Answer
Explanation: The super() method re-assigns the variable x with value 5. Hence 5 is printed.
EXTRA MCQ
1. The value of a in the following example is?
>>> a = [1,2,3,4]
>>> a = a.append(5)
>>> print a
A. [1,2,3] B. [1,2,3,4] C. [1,2,3,4,5] D. None of the Above
2. In computer programming, _______________ is the term used to describe sections of code that have to
be included in many places with little or no alteration.
A. shebang B. REPL C. boilerplate D. header
4. When a python file is run directly, the special variable "__name__" is set to .
A. "__main_void__" B. "__void_main__" C. "__main__" D. "__void__"
5. Suppose the file "binky.py" contains a "def foo()". The fully qualified name of that foo function is
_________________.
A. "main.binky" B. "binky.main" C. "boo.binky" D. “binky.foo"
6. Inside a python interpreter, the ________ command gives a quick list of the defined symbols in python.
A. snapshot B. view C. help D. dir
10. A "raw" string literal is prefixed by an '______' and passes all the chars through without special
treatment of backslashes,
A. r B. R C. \r D. \R
14. In computer programming, __________________ is a method by which individual units of source code
are tested to determine if they are fit for use.
A. Load Testing B. Integration Testing C. Stress Testing D. Unit Testing