Programming With Python - PGDBDA - Feb20
Programming With Python - PGDBDA - Feb20
Contents
PYTHON ................................................................................................................................................................ 1
BASIC OPERATOR ..................................................................................................................................................4
WHILE AND FOR LOOP...........................................................................................................................................5
DICTIONARY .......................................................................................................................................................... 6
FILES......................................................................................................................................................................8
FUNCTION ........................................................................................................................................................... 12
ARGUMENT ......................................................................................................................................................... 15
EXCEPTION HANDLING ........................................................................................................................................16
CORE DATA TYPES ...............................................................................................................................................18
CLASSES & OBJECT...............................................................................................................................................20
INHERITANCE ...................................................................................................................................................... 22
EXTRA MCQ ......................................................................................................................................................... 25
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 = " ")
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.
Q.15) Which of the following statement is true about the pass statement?
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
PG DBDA Feb 20 Programming with Python Question Bank
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
1. Which is the correct operator for power(x^y)?
a) X^y b) X**y c) X^^y d) None of the mentioned
Explanation: In python, power operator is x**y i.e. 2**3=8.
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.
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?
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
c) (‘CheckCheckCheck’) d) Syntax error
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.
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.
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.
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
PG DBDA Feb 20 Programming with Python Question Bank
4. What is the type of sys.argv?
a) set b) list c) tuple d) string View Answer
Explanation: It is a list of elements.
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”.
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.
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.
6. What is the output of the following piece of code when executed in the Python shell?
>>> class A: pass >>> class B(A):
pass
>>> obj=B()
>>> isinstance(obj,A)
a) True
b) False
c) Wrong syntax for isinstance() method
d) Invalid method for classes
Explanation: isinstance(obj,class) returns True if obj is an object class.
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,
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
PG DBDA Feb 20 Programming with Python Question Bank
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