Python Programming (MCQS)
Python Programming (MCQS)
in
1|P a g e
www.aktutor.in
UNIT 1
2|P a g e
www.aktutor.in
3|P a g e
www.aktutor.in
17. Why are local variable names beginning with an underscore discouraged?
a) they are used to indicate a private variables of a class
b) they confuse the interpreter
c) they are used to indicate global variables
d) they slow down execution
24. 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
4|P a g e
www.aktutor.in
d) None
>>>str="hello"
>>>str[:2]
a) he
b) lo
c) olleh
d) hello
28. In python we do not specify types, it is directly interpreted by the compiler, so consider the
following operation to be performed.
>>>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
29. What error occurs when you execute the following Python code snippet?
apple = mango
a) SyntaxError
b) NameError
c) ValueError
d) TypeError
30. What will be the output of the following Python code snippet?
def example(a):
a = a + '2'
a = a*2
return a
>>>example("hello")
a) indentation Error
5|P a g e
www.aktutor.in
32. In order to store values in terms of key and value we use what core data type.
a) list
b) tuple
c) class
d) dictionary
34. The following is displayed by a print function call. Select all of the function calls that result
in this output.
1. tom
2. dick
3. harry
a) print('''tom
\ndick
\nharry''')
b) print(”’tomdickharry”’)
c) print(‘tom\ndick\nharry’)
d) print('tom
dick
harry')
35. What is the average value of the following Python code snippet?
>>>grade1 = 80
>>>grade2 = 90
>>>average = (grade1 + grade2) / 2
a) 85.0
b) 85.1
c) 95.0
d) 95.1
6|P a g e
www.aktutor.in
38. How we can convert the given list into the set ?
a) list.set()
b) set.list()
c) set(list)
d) None of the above
42. Which of the following can convert the string to float number ?
a) str(float,x)
b) float(str,int)
c) int(float(str))
d) float(str)
43. Which of the following function are used to convert the string into the list ?
a) map()
b) convertor()
c) split()
d) lambda
7|P a g e
www.aktutor.in
a) int
b) list
c) dictionary
d) All of the above
ANSWERS
1.a 2.c 3.a 4.b 5.a 6.b 7.d 8.b 9.c 10.b 11.b 12.c 13.a 14.d 15.d
16.b 17.a (As Python has no concept of private variables, leading underscores are used to indicate
variables that must not be accessed from outside the class.) 18.a (eval can be used as a variable)
19.d 20.a 21b 22.b 23.d 24.d 25.a 26.a 27.a 28.d
29.b 30.a 31.a 32.d 33.c 34.c 35.a 36.c 37.a 38.c
39.c 40.d 41.c 42.d 43.c 44.d 45.d 46.d
8|P a g e
www.aktutor.in
10. Which one of the following has the highest precedence in the expression?
a) Exponential
b) Addition
c) Multiplication
d) Parentheses
9|P a g e
www.aktutor.in
21. What will be the output of the following Python code snippet if x=1?
x<<2
a) 8
b) 1
c) 2
d) 4
23. What will be the value of x in the following Python expression, if the result of
10 | P a g e
www.aktutor.in
that expression is 2?
x>>2
a) 8
b) 4
c) 2
d) 1
24. What will be the output of the following Python expression if x=15 and y=12?
x&y
a) b1101
b) 0b1101
c) 12
d) 1101
26. Bitwise _________ gives 1 if either of the bits is 1 and 0 when both of the bits are
1.
a) OR
b) AND
c) XOR
d) NOT
28. What will be the output of the following Python code if a=10 and b =20?
a=10
b=20
a=a^b
b=a^b
a=a^bprint(a,b)
a) 10 20
b) 10 10
c) 20 10
d) 20 20
11 | P a g e
www.aktutor.in
~100?
a) 101
b) -101
c) 100
d) -100
30. The value of the expressions 4/(3*(2-1)) and 4/3*(2-1) is the same.
a) True
b) False
33. Which of the following operators has its associativity from right to left?
a) +
b) //
c) %
d) **
12 | P a g e
www.aktutor.in
b) %
c) //
d) |
42. Which of the following expressions involves coercion when evaluated in Python?
a) 4.7 – 1.5
b) 7.9 * 6.3
c) 1.7 % 2
d) 3.4 + 4.6
13 | P a g e
www.aktutor.in
44. Which among the following list of operators has the highest precedence?
+, -, **, %, /, <<, >>, |
a) <<, >>
b) **
c) |
d) %
14 | P a g e
www.aktutor.in
ANSWERS:
1.b 2.b 3.a 4.b 5.b 6.a 7.c 8.a 9.a 10.d 11.b 12.c
13.c 14.a 15.a 16.d 17.a 18.d 19.d 20.d 21.d 22.b
23.a 24.c 25.b 26.c 27.c 28.c 29.b 30.a 31.b 32.b
33.d 34.b 35.a 36.c 37.d 38.a 39.b 40.b 41.d 42.c
43.a 44.b 45.c 46.a 47.d 48.b 49.b
15 | P a g e
www.aktutor.in
UNIT II
a. Yes
b. No
c. if/else not used in python
d. None of the above
16 | P a g e
www.aktutor.in
a. pq
b. rs
c. pqrs
d. pq12
a. if a = b:
b. if a == b:
c. if a === c:
d. if a == b
11. What will be the output of given Python code?
str1="hello"
c=0
for x in str1:
if(x!="l"):
c=c+1
else:
pass print(c)
a. 2
b. 0
c. 4
d. 3
12. What does the following code print to the console?
if 5 > 10:
print("fan")
elif 8 != 9:
print("glass")
else:
print("cream")
a. Cream
B. Glass
c. Fan
d. No output
17 | P a g e
www.aktutor.in
a. usa b. ireland
c.colombia d. None of the above
18 | P a g e
www.aktutor.in
print("Audi")
a. Nissan b. Ford
c. BMW d. Audi
1.a 2.a 3.a 4.b 5.c 6.c 7.a (Yes, we can write if/else in one
line. For eg i = 5 if a > 7 else 0. So, option A is correct.) 8.b 9.a ( If condition
is true so pq will be the output. So, option A is correct.) 10.b 11.d 12.b
13.c 14.a 15.d 16.c 17.b (The first elif that is True will be
executed when multiple elif statements are True.) 18.a (1 does not equal True, but it's
considered True in a boolean context. Values that are considered True in a boolean
context are called "truthy" and values that are considered False in a boolean context are
called "falsy".) 19. c 20. D
19 | P a g e
www.aktutor.in
A. For
B. while
C. do while
D.1 and 2
7. What is the final value of the i after this, for i in range(3): pass
A. 1
B. 2
C. 3
D. 0
20 | P a g e
www.aktutor.in
A. a b c d
B. A B C D
C. a B C D
D. error
13. What will be the output of the following Python code snippet?
x = 'abcd'
for i in range(len(x)):
x = 'a'
print(x)
A. a
B. abcd abcd abcd
C. a a a a
D. None of the mentioned
21 | P a g e
www.aktutor.in
17. What will be the output of the following Python code snippet?
for i in [1, 2, 3, 4][::-1]:
print (i)
A. 1234
B. 4321
C. error
D. none of the mentioned
A. 0.0 1.0
B. 0 1
C. error
D. none of the mentioned
19. What will be the output of the following Python code snippet?
x=2
for i in range(x):
x += 1
print (x)
A. 01234
B. 01
C. 3 4
D. 0 1 2 3
20. What will be the output of the following Python code snippet?
x=2
for i in range(x):
x -= 2
22 | P a g e
www.aktutor.in
print (x)
A. 01234
B. 0 -2
C. 30
D. Error
A. 0 1 2 3 4 Here
B. 0 1 2 3 4 5 Here
C. 0 1 2 3 4
D. 0 1 2 3 4 5
ANSWERS :
1. D 2. C 3. A 4. A 5. D 6. C 7. B 8. D 9. B 10.
A 11. B 12.D ( The loop does not terminate as new elements are being added to the list in
each iteration.) 13.C ( range() is computed only at the time of entering the loop.) 14.D
15.C (Objects of type int are not iterable.) 16. C ( Object of type float cannot be
interpreted as an integer.) 17.B 18.B 19.C 20.B 21.C
23 | P a g e
www.aktutor.in
B. 1
C. 3
D. infinite
A. no output
B. i i i i i i …
C. a b c d e f
D. abcdef
A. 123456
B. 1234567
C. error
D. none of the mentioned
A. True
24 | P a g e
www.aktutor.in
B. False
C. None
D. none of the mentioned
A. 0120
B. 012
C. None
D. none of the mentioned
A. a b c d e f
B. Abcdef
C. i i i i i i...
D. Error
A. a a a a a a
B. a a a a
C. i i i i i i
D. None of the mentioned
A. 1 2
B. 1 2 3
C. Error
D. None of the mentioned
25 | P a g e
www.aktutor.in
i=1
while True: if i%O.7 == 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 mentioned
A. 5 6 7 8 9 10
B. 5 6 7 8
C. 5 6
D. Error
A. 1
B. 1 2
C. 1 2 3 4 5 6 ....
D. 1 3 5 7 9 11 ....
A. 2 4 6 8 10 ....
B. 2 4
C. 2 3
D. Error
26 | P a g e
www.aktutor.in
A. 1
B. 1 3 5 7 ...
C. 1 2 3 4 ....
D. none of the mentioned
A. true
B. false
C. error
D. none of the mentioned
A. 0 1 2 0
B. 0 1 2
C. error
D. none of the mentioned
A. 0 1 2 3 0
B. 0 1 2 0
C. 0 1 2
D. error
A. a a a a a a
27 | P a g e
www.aktutor.in
B. a
C. no output
D. error
ANSWERS :
A. 4.5
B. 5
C. 4
D. 4.6
A. (x**y)**z
B. (x**y)/z
C. (x**y)%z
D. (x**y)*z
A. Error
B. True
C. False
D. 0
A. Error
28 | P a g e
www.aktutor.in
B. True
C. False
D. 4>2
A. Error
B. -5
C. 5
D. 5.0
A. Error,6
B. 12, Error
C. 12,6
D. Error, Error
A. 2
B. False
C. -3
D. -4
A. a Error
B. ‘a’ Error
C. Error a
D. Error Error
A. Error
B. 1
C. 2j
D. 1+2j
11. The function divmod(a,b), where both ‘a’ and ‘b’ are integers is evaluated as:
A. (a%b,a//b)
B. (a//b,a%b)
C. (a//b,a*b)
D. (a/b,a%b)
29 | P a g e
www.aktutor.in
A. Error
B. [(1,2),(2,3)]
C. [(0,2),(1,3)]
D. [(2,3)]
13. Which of the following functions does not necessarily accept only iterables as arguments?
A. enumerate()
B. all()
C. chr()
D. max()
A. ord()
B. min()
C. chr()
D. any()
15. Which of the following functions will not result in an error when no arguments are passed
to it?
A. min()
B. divmod()
C. all()
D. float()
A. f
B. OxF
C. OXf
D. Oxf
A. 4
B. 3
C. Error
D. 6
30 | P a g e
www.aktutor.in
B. Define
C. Def
D. Function
def sayHello():
print('Hello World!')
sayHello()
sayHello()
A. 3
B. 4
C. 4 is maximum
D. None of the mentioned
A. x is now 50
B. x is now 2
C. x is now 100
D. None of the mentioned
31 | P a g e
www.aktutor.in
say('Hello')
say('World', 5)
A. Hello WorldWorldWorldWorldWorld
B. Hello World 5
C. Hello World,World,World,World,World
D. Hello HelloHelloHelloHelloHello
32 | P a g e
www.aktutor.in
def cube(x):
return x * x * x
x = cube(3)
print x
A. 9
B. 3
C. 27
D. 30
def C2F(c):
return c * 9/5 + 32
print C2F(100)
print C2F(0)
A. 212
32
B. 314
24
C. 567
98
D. None of the mentioned
33 | P a g e
www.aktutor.in
A. 212
32
B. 9
27
C. 567
98
D. None of the mentioned.
37. How many keyword arguments can be passed to a function in a single function call?
A. zero
B. one
C. zero or more
D. one or more
34 | P a g e
www.aktutor.in
return total
total = 0print(foo())
A. 0
B. 1
C. error
D. none of the mentioned
35 | P a g e
www.aktutor.in
x+=1
print(x)
x=12print("x")
A. Error
B. 13
C. 13
x
D. X
36 | P a g e
www.aktutor.in
D. 5 10 15 40
53. Read the following Python code carefully and point out the global variables?
y, z = 1, 2def f():
global x
x = y+z
A. x
B. y and z
C. x, y and z
D. Neither x, nor y, nor z
54. Which of the following data structures is returned by the functions globals() and locals()?
37 | P a g e
www.aktutor.in
A. list
B. set
C. dictionary
D. tuple
56. What happens if a local variable exists with the same name as the global variable you want to
access?
A. Error
B. The local variable is shadowed.
C. Undefined behavior.
D. The global variable is shadowed.
ANSWERS :
38 | P a g e
www.aktutor.in
"abcd"[2:]
a)a
b)ab
c)cd
d) dc
Answer: c
3. The output of executing string.ascii_letters can also be achieved by:
a) string.ascii_lowercase_string.digits
b) string.ascii_lowercase+string.ascii_upercase
c) string.letters
d) string.lowercase_string.upercase
Answer: b
a) olleh
b) hello
c) h
d) o
Answer: d
5. What arithmetic operators cannot be used with strings?
a)+
b)*
c) –
d) All of the mentioned
Answer: c
6. What will be the output of the following Python code?
>>>print (r"\nhello")
a) a new line and hello
b) \nhello
c) the letter r and then hello
d) error
Answer: b
7. What will be the output of the following Python statement?
>>>print('new' 'line')
a) Error
b) Output equivalent to print ‘new\nline’
c) newline
d) new line
Answer: c
8. What will be the output of the following Python code?
>>>str1="helloworld"
>>>str1[::-1]
a) dlrowolleh
b) hello
c) world
d) helloworld
Answer: a
9. print(0xA + 0xB + 0xC):
39 | P a g e
www.aktutor.in
a) 0xA0xB0xC
b) Error
c) 0x22
d) 33
Answer: d
10. What will be the output of the following Python code?
>>>example = "snow world"
>>>print("%s" % example[4:7])
a) wo
b) world
c) sn
d) rl
Answer: a
11. What will be the output of the following Python code?
>>>example = "snow world"
>>>example[3] = 's'
>>>print example
a) snow
b) snow world
c) Error
d) snos world
Answer: c
40 | P a g e
www.aktutor.in
>>example="helloworld"
>>example[::-1].startswith("d")
a) dlrowolleh
b) True
c) -1
d) None
Answer: b
17. To concatenate two strings to a third what statements are applicable?
a)s3=s1s2
b)s3=s1.add(s2)
c)s3=s1.__add__(s2)
d) s3 = s1 * s2
Answer: c
Explanation: __add__ is another method that can be used for concatenation.
18. Which of the following statement prints hello\example\test.txt?
a)print(“hello\example\test.txt”)
b)print(“hello\\example\\test.txt”)
c)print(“hello\”example\”test.txt”)
d) print(“hello”\example”\test.txt”)
Answer: b
Explanation: \is used to indicate that the next \ is not an escape sequence.
19. Suppose s is “\t\tWorld\n”, what is s.strip()?
a)\t\tWorld\n
b)\t\tWorld\n
c)\t\tWORLD\n
d) World
Answer: d
20. The format function, when applied on a string returns ___________
a) Error
b) int
c) bool
d) str
Answer: d
21. What will be the output of the “hello” +1+2+3?
a) hello123
b) hello
c) Error
d) hello6
Answer: c
22. What will be the output of the following Python code?
>>>print("D", end = ' ')
>>>print("C", end = ' ')
>>>print("B", end = ' ')
>>>print("A", end = ' ')
a) DCBA
b) A,B,C,D
c) DCBA
d) D, C, B, A will be displayed on four lines
Answer: c
23. What will be displayed by print(ord(‘b’) – ord(‘a’))?
a)0
b)1
c)-1
41 | P a g e
www.aktutor.in
d) 2
Answer: b
Explanation: ASCII value of b is one more than a. Hence the output of this code is 98-97, which is
equal to 1.
24. Say s=”hello” what will be the return value of type(s)?
a) int
b) bool
c) str
d) String
Answer: c
25. What is “Hello”.replace(“l”, “e”)?
a) Heeeo
b) Heelo
c) Heleo
d) None
26. To retrieve the character at index 3 from string s=”Hello” what command do we execute
(multiple answers allowed)?
a) s[]
b) s.getitem(3)
c) s.__getitem__(3)
d) s.getItem(3)
27. To return the length of string s what command do we execute?
a) s.__len__()
b) len(s)
c) size(s)
d) s.size()
28. Suppose i is 5 and j is 4, i + j is same as ________
a) i.__add(j)
b) i.__add__(j)
c) i.__Add(j)
d) i.__ADD(j)
42 | P a g e
www.aktutor.in
43 | P a g e
www.aktutor.in
d) Error
40. What will be the output of the following Python code snippet?
print('for'.isidentifier())
a) True
b) False
c) None
d) Error
41. What will be the output of the following Python code snippet?
print('abc'.islower())
a) True
b) False
c) None
d) Error
42. What will be the output of the following Python code snippet?
print('11'.isnumeric())
a) True
b) False
c) None
d) Error
43. What will be the output of the following Python code snippet?
print('HelloWorld'.istitle())
a) True
b) False
c) None
d) Error
44. What will be the output of the following Python code snippet?
print('abcdef12'.replace('cd', '12'))
a) ab12ef12
b) abcdef12
c) ab12efcd
d) none of the mentioned
45. What will be the output of the following Python code snippet?
print('Ab!2'.swapcase())
a) AB!@
b) ab12
c) aB!2
d) aB1@
ANSWERS :
1.d 2.c 3.b 4.d 5.c 6.b 7.c 8.a 9.d 10.a 11.c 12.d 13.a
14.c 15.b 16.b 17.c 18.b 19.d 20.d 21.c 22.c 23.b
24.c 25.a 26.c 27.a 28.b 29.a 30.c 31.a 32.c 33.b
34.b 35.a 36.a 37.a 38.b 39.b 40.a 41.a 42.a 43.b
44.a 45.c
44 | P a g e
www.aktutor.in
45 | P a g e
www.aktutor.in
d) “peter”
46 | P a g e
www.aktutor.in
d) Error, < operator is valid for tuples but not if there are sub-tuples
47 | P a g e
www.aktutor.in
c)(0,1)
d) (0,2)
48 | P a g e
www.aktutor.in
1.b 2.b 3.c 4.c 5.c 6.a 7.a 8.b 9.d(Tuples are immutable and
don’t have an append method. An exception is thrown in this case.) 10.b 11.d
12.a 13.c 14.d 15.c 16.c 17.b 18.c 19.a 20.c(A tuple
needn’t be enclosed in parenthesis.) 21.c (For unpacking to happen, the number of
values of the right hand side must be equal to the number of variables on the left hand
side.) 22.b 23.c 24.d 25.d (A tuple is immutable thus it doesn’t have
a sort attribute.) 26.c (Tuple doesn’t have any update() attribute because it is
immutable.) 27.a (A list of tuples is a list itself. Hence items of a list can be
sorted.)
49 | P a g e
www.aktutor.in
8. Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct syntax for slicing
operation?
a) print(list1[0])
b) print(list1[:2])
c) print(list1[:-2])
d) all of the mentioned
50 | P a g e
www.aktutor.in
10. Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?
a) [2, 33, 222, 14]
b) Error
c) 25
d) [25, 14, 222, 33, 2]
51 | P a g e
www.aktutor.in
19. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?
a) [3, 4, 5, 20, 5, 25, 1, 3]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [25, 20, 5, 5, 4, 3, 3, 1]
d) [3, 1, 25, 5, 20, 5, 4, 3]
20. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34,
5])?
a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]
b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5]
c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5]
d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]
21. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop(1)?
a) [3, 4, 5, 20, 5, 25, 1, 3]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]
22. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop()?
a) [3, 4, 5, 20, 5, 25, 1]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]
52 | P a g e
www.aktutor.in
indexOfMax = 0
for i in range(1, len(myList)):
if myList[i] > max:
max = myList[i]
indexOfMax = i
print(indexOfMax)
a) 1
b) 2
c) 3
d) 4
29. which of the following the “in” operator can be used to check if an item is in it?
a) Lists
b) Dictionary
c) Set
d) All of the mentioned
53 | P a g e
www.aktutor.in
c) 5
d) 8
54 | P a g e
www.aktutor.in
print(lst)
a) [3, 7, 8, 6, 1, 2]
b) Syntax error
c) [3,[7,8],6,1,2]
d) [3,4,6,7,8]
ANSWERS :
1.d 2.a 3.a 4.c 5.d 6.c 7.c 8.d 9.c 10.a 11.d 12.c 13.b 14.b 15.b 16.a
17.d 18.d 19.d 20.a 21.c 22.a 23.a 24.a
25.a 26.b 27.c 28.b 29.d 30.d 31.d 32.d 33.c 34.b
35.c 36.a 37.b 38.a 39.b
55 | P a g e
www.aktutor.in
56 | P a g e
www.aktutor.in
10. Which of the following is the same as list(map(lambda x: x**-1, [1, 2, 3]))?
a) [x**-1 for x in [(1, 2, 3)]]
b) [1/x for x in [(1, 2, 3)]]
c) [1/x for x in (1, 2, 3)]
d) error
l1=[1,2,3]
l2=[4,5,6]
[x*y for x in l1 for y in l2]
a) [4, 8, 12, 5, 10, 15, 6, 12, 18]
b) [4, 10, 18]
c) [4, 5, 6, 8, 10, 12, 12, 15, 18]
d) [18, 12, 6, 15, 10, 5, 12, 8, 4]
12. Write the list comprehension to pick out only negative integers from a
given list ‘l’.
57 | P a g e
www.aktutor.in
a) [x<0 in l]
b) [x for x<0 in l]
c) [x in l for x<0]
d) [x for x in l if x<0]
13. Write a list comprehension for number and its cube for l=[1, 2, 3, 4, 5, 6, 7, 8, 9].
a) [x**3 for x in l]
b) [x^3 for x in l]
c) [x**3 in l]
d) [x^3 in l]
14. Read the information given below carefully and write a list comprehension such
that the output is: [‘e’, ‘o’]
w="hello"
v=('a', 'e', 'i', 'o', 'u')
a) [x for w in v if x in v]
b) [x for x in w if x in v]
c) [x for x in v if w in v]
d) [x for v in w for x in w]
t=32.00
[round((x-32)*5/9) for x in t]
a) [0]
b) 0
c) [0.00]
d) Error
16. Write a list comprehension for producing a list of numbers between 1 and 1000
that are divisible by 3.
a) [x in range(1, 1000) if x%3==0]
b) [x for x in range(1000) if x%3==0]
c) [x%3 for x in range(1, 1000)]
d) [x%3=0 for x in range(1, 1000)]
58 | P a g e
www.aktutor.in
18. What will be the output of the following Python list comprehension?
20. Write a list comprehension equivalent for the Python code shown below.
1.c 2.b 3.c 4.c 5.b 6.b 7.a 8.a 9.c 10.c 11.c 12.d 13.a
14.b 15.d 16.b 17.a 18.c 19.b 20.b
2. Which of the following is not the correct syntax for creating a set?
a) set([[1,2],[3,4]])
b) set([1,2,2,3,4])
59 | P a g e
www.aktutor.in
c) set((1,2,3,4))
d) {1,2,3,4}
60 | P a g e
www.aktutor.in
61 | P a g e
www.aktutor.in
62 | P a g e
www.aktutor.in
c) 6
d) Nothing is displayed
27. Which of the following functions will return the symmetric difference between two sets, x
and y?
a) x|y
b) x^y
c) x&y
d) x – y
28. What will be the output of the following Python code snippet?
z=set('abc$de')
63 | P a g e
www.aktutor.in
'a' in z
a) True
b) False
c) Nooutput
d) Error
29. What will be the output of the following Python code snippet?
s=set([1, 2, 3])
s.union([4, 5])
s|([4, 5])
a) {1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}
b) Error
{1, 2, 3, 4, 5}
c) {1, 2, 3, 4, 5}
Error
d) Error
Error
30. What will be the output of the following Python code snippet?
for x in set('pqr'):
print(x*2)
a) pp
qq
rr
b) pqr
pqr
c) ppqqrr
d) pqrpqr
31. What will be the output of the following Python code snippet?
{a**2 for a in range(4)}
a) {1,4,9,16}
b) {0,1,4,9,16}
c) Error
d) {0, 1, 4, 9}
32. The ____________ function removes the first element of a set and the last element of a list.
a) remove
b) pop
c) discard
d) dispose
33. The difference between the functions discard and remove is that:
a) Discard removes the last element of the set whereas remove removes the first element of the set
b) Discard throws an error if the specified element is not present in the set whereas remove does
not throw an error in case of absence of the specified element
c) Remove removes the last element of the set whereas discard removes the first element of the
set
d) Remove throws an error if the specified element is not present in the set whereas discard does
not throw an error in case of absence of the specified element
34. If we have two sets, s1 and s2, and we want to check if all the elements of s1 are present in s2
or not, we can use the function:
64 | P a g e
www.aktutor.in
a) s2.issubset(s1)
b) s2.issuperset(s1)
c) s1.issuperset(s2)
d) s1.isset(s2)
35. What will be the output of the following Python code, if s1= {1, 2, 3}?
s1.issubset(s1)
a) True
b) Error
c) Nooutput
d) False
1.d 2.a 3.c 4.b 5.b 6.d 7.b 8.c 9.a 10.d 11.a 12.d 13.c
14.a 15.b 16.a 17.c 18.d 19.b 20.c 21.b 22.a 23.a 24.c
25.b 26.d 27.b 28.a 29.c 30.a 31.d 32.b 33.d
34.b 35.a
65 | P a g e
www.aktutor.in
7. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we
use?
a) d.delete(“john”:40)
b) d.delete(“john”)
c) del d[“john”]
d) del d(“john”:40)
10. Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using
the expression d[“susan”]?
a) Since “susan” is not a value in the set, Python raises a KeyError exception
b) It is executed fine and no exception is raised, and it returns None
c) Since “susan” is not a key in the set, Python raises a KeyError exception
d) Since “susan” is not a key in the set, Python raises a syntax error
66 | P a g e
www.aktutor.in
13. What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
for i,j in a.items():
print(i,j,end=" ")
a) 1A2B3C
b) 123
c) ABC
d) 1:”A” 2:”B” 3:”C”
14. What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
print(a.get(1,4))
a) 1
b) A
c) 4
d) Invalid syntax for get method
67 | P a g e
www.aktutor.in
24. What will be the output of the following Python code snippet?
>>> a={1:"A",2:"B",3:"C"}
>>> del a
a) method del doesn’t exist for the dictionary
68 | P a g e
www.aktutor.in
25. If a is a dictionary with some key-value pairs, what does a.popitem() do?
a) Removes an arbitrary element
b) Removes all the key-value pairs
c) Removes the key-value pair for the key given as an argument
d) Invalid method for dictionary
69 | P a g e
www.aktutor.in
b) An exception is thrown
c) {‘a’:‘b’:‘c’:}
d) {1: ‘a’, 2: ‘b’, 3: ‘c’}
ANSWERS :
1.d 2.b 3.a 4.b 5.c 6.a 7.c 8.b 9.a 10.c 11.b 12.c 13.a 14.b 15.c 16.b
17.d 18.c 19.b 20.d 21.a 22.d 23.c 24.c 25.a 26.a 27.d 28.b 29.c
30.a 31.d 32.a
1.a2.b3.4.5.
70 | P a g e
www.aktutor.in
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()
f = None
for i in range (5):
with open("data.txt", "w") as f:
if i > 2:
break
print(f.closed)
a) True
b) False
c) None
d) Error
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()
9. To read the remaining lines of the file from a file object infile, we use ____________
a) infile.read(2)
71 | P a g e
www.aktutor.in
b) infile.read()
c) infile.readline()
d) infile.readlines()
11. Which are the two built-in functions to read a line of text from standard input, which by default
comes from the keyboard?
a) Raw_input & Input
b) Input & Scan
c) Scan & Scanner
d) Scanner
a) closed
b) softspace
c) rename
d) mode
72 | P a g e
www.aktutor.in
a) rename(current_file_name, new_file_name)
b) rename(new_file_name, current_file_name,)
c) rename(()(current_file_name, new_file_name))
d) none of the mentioned
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name
a) Compilation Error
b) Syntax Error
c) Displays Output
d) None of the mentioned
73 | P a g e
www.aktutor.in
import sys
sys.stdout.write(' Hello\n')
sys.stdout.write('Python\n')
a) Compilation Error
b) Runtime Error
c) Hello Python
d) Hello
Python
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
fo.flush()
fo.close()
74 | P a g e
www.aktutor.in
a) Compilation Error
b) Runtime Error
c) No Output
d) Flushes the file when closing them
30. In file handling, what does this terms means “r, a”?
a) read, append
b) append, read
c) write, append
d) none of the mentioned
75 | P a g e
www.aktutor.in
39. Which of the following are the modes of both writing and reading in binary format in file?
a) wb+
b) w
c) wb
d) w+
42. How do you get the name of a file from a file object (fp)?
a) fp.name
b) fp.file(name)
c) self.__name__(fp)
d) fp.__name__()
43. Which of the following is not a valid attribute of a file object (fp)?
a) fp.name
b) fp.closed
c) fp.mode
d) fp.size
76 | P a g e
www.aktutor.in
c) fp.close()
d) fp.__close__()
45. How do you get the current position within the file?
a) fp.seek()
b) fp.tell()
c) fp.loc
d) fp.pos
48. How do you change the file position to an offset value from the start?
a) fp.seek(offset, 0)
b) fp.seek(offset, 1)
c) fp.seek(offset, 2)
d) none of the mentioned
77 | P a g e
www.aktutor.in
78 | P a g e
www.aktutor.in
c) an TypeError occurs
d) a ValueError occurs
10. The following Python code will result in an error if the input value is entered as -5.
assert False, 'Spanish'
a) True
b) False
79 | P a g e
www.aktutor.in
l2=[4, 5, 6]
l=itertools.chain(l1, l2)print(next(l1))
a) ‘list’ object is not iterator
b) ‘tuple’ object is not iterator
c) ‘list’ object is iterator
d) ‘tuple’ object is iterator
17. What happens if the file is not found in the following Python code?
a=Falsewhile not a:
try:
f_n = input("Enter file name")
i_f = open(f_n, 'r')
except:
print("Input file not found")
a) No error
b) Assertion error
c) Input output error
d) Name error
20. What will be the output of the following Python code, if the time module has already been
imported?
4 + '3'
a) NameError
b) IndexError
c) ValueError
d) TypeError
80 | P a g e
www.aktutor.in
22. What will be the output of the following Python code if the input entered is 6?
valid = Falsewhile not valid:
try:
n=int(input("Enter a number"))
while n%2==0:
print("Bye")
valid = True
except ValueError:
print("Invalid")
a) Bye (printed once)
b) No output
c) Invalid (printed once)
d) Bye (printed infinite number of times)
29. Which of the following blocks will be executed whether an exception is thrown or not?
a) except
81 | P a g e
www.aktutor.in
b) else
c) finally
d) assert
1.d 2.c 3.b 4.d 5.a 6.d 7.b 8.a 9.b 10.a 11.c 12.b
13.c 14.a 15.b 16.c 17.a 18.c 19.b 20.d 21.b 22.d
23.b 24.a 25.c 26.a 27.a 28.d 29.c
3. Program code making use of a given module is called a ______ of the module.
a) Client
b) Docstring
c) Interface
d) Modularity
4. ______ is a string literal denoted by triple quotes for providing the specifications of certain
program elements.
a) Interface
b) Modularity
c) Client
d) Docstring
82 | P a g e
www.aktutor.in
10. Which of the following is false about “import modulename” form of import?
a) The namespace of imported module becomes part of importing module
b) This form of import prevents name clash
c) The namespace of imported module becomes available to importing module
d) The identifiers in module are accessed as: modulename.identifier
14. What is the order of namespaces in which Python looks for an identifier?
a) Python first searches the global namespace, then the local namespace and finally the built-in
namespace
b) Python first searches the local namespace, then the global namespace and finally the built-in
namespace
c) Python first searches the built-in namespace, then the global namespace and finally the local
namespace
d) Python first searches the built-in namespace, then the local namespace and finally the global
namespace
83 | P a g e
www.aktutor.in
1.b 2.c 3.a 4.d 5.c 6.b 7.b 8.d 9.b 10.a 11.b 12.c
13.d 14.b
1. _____ represents an entity in the real world with its identity and behaviour.
a) A method
b) An object
c) A class
d) An operator
def display(self):
print(self.a)
obj=test()
obj.display()
a) The program has an error because constructor can’t have default arguments
b) Nothing is displayed
c) “Hello World” is displayed
d) The program has an error display function doesn’t have parameters
x = change(1,2,3)
y = getattr(x, 'a')setattr(x, 'a', y+1)print(x.a)
84 | P a g e
www.aktutor.in
a) 6
b) 7
c) Error
d) 0
def display(self):
print(self.a)
obj=test()
obj.display()
a) Runs normally, doesn’t display anything
b) Displays 0, which is the automatic default value
c) Error as one argument is required while creating the object
d) Error as display function requires additional argument
85 | P a g e
www.aktutor.in
obj.quantity=10
obj.bags=2
print(obj.quantity+len(obj.__dict__))
a) 12
b) 52
c) 13
d) 60
12. The assignment of more than one function to a particular operator is _______
a) Operator over-assignment
b) Operator overriding
c) Operator overloading
d) Operator instance
15. What are the methods which begin and end with two underscore characters called?
a) Special methods
b) In-built methods
c) User-defined methods
d) Additional methods
86 | P a g e
www.aktutor.in
21. What does print(Test.__name__) display (assuming Test is the name of the class)?
a) ()
b) Exception is thrown
c) Test
d) __main__
ANSWERS :
1.b 2.b 3.c 4.b 5.a 6.b 7.c 8.a 9.c 10.d 11.c 12.c
13.a 14.b 15.a 16.b 17.c 18.c 19.b 20.a 21.c 22.a
87 | P a g e
www.aktutor.in
d) Allows for implementation of elegant software that is well designed and easily modified
88 | P a g e
www.aktutor.in
pass
obj = B()
obj.disp()
a) Invalid syntax for inheritance
b) Error because when object is created, argument must be passed
c) Nothing is printed
d) A disp()
7. When defining a subclass in Python that is meant to serve as a subtype, the subtype Python
keyword is used.
a) True
b) False
8. 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)
89 | P a g e
www.aktutor.in
def two(self):
return 'A'
class B(A):
def two(self):
return 'B'
obj1=A()
obj2=B()print(obj1.two(),obj2.two())
a) A A
b) A B
c) B B
d) An exception is thrown
90 | P a g e
www.aktutor.in
c) Hierarchical inheritance
d) Single-level inheritance
def display(self):
print(self.__i, self.j)class B(A):
def __init__(self):
super().__init__()
self.__i = 2
self.j = 7
c = B()
c.display()
a) 2 7
b) 1 5
c) 1 7
d) 2 5
91 | P a g e
www.aktutor.in
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
26. What will be the output of the following Python code?
class A:
def test1(self):
print(" test of A called ")class B(A):
def test(self):
print(" test of B called ")class C(A):
def test(self):
92 | P a g e
www.aktutor.in
a) test of B called
test of C called
test of A called
b) test of C called
test of B called
c) test of B called
test of C called
d) Error, all the three classes from which D derives has same method test()
1.a 2.c 3.b 4.c 5.d 6.b 7.b 8.a 9.c 10.d 11.b 12.a 13.c 14.b
15.b 16.a 17.c 18.c 19.c 20.a 21.c 22.a 23.d 24.c 25.a 26.c
27.a
93 | P a g e
www.aktutor.in
UNIT V
2. Only problems that are recursively defined can be solved using recursion.
94 | P a g e
www.aktutor.in
a)True
b) False
4. Fill in the line of the following Python code for calculating the factorial of a number.
def fact(num):
if num == 0:
return 1
else:
return _____________________
a) num*fact(num-1)
b) (num-1)*(num-2)
c) num*(num-1)
d) fact(num)*fact(num-1)
a) 13
b) 7
c) Infinite loop
d) 17
a) 50
95 | P a g e
www.aktutor.in
b) 100
c) 74
d) Infinite loop
10. What happens if the base condition isn’t defined in recursive programs?
a) Program gets into an infinite loop
b) Program runs once
c) Program runs n number of times where n is the argument given to the function
d) An exception is thrown
a) 0 1 2 3
b) An exception is thrown
c) 0 1 1 2 3
d) 0 1 1 2
14. Suppose the first fibonnaci number is 0 and the second is 1. What is the sixth
fibonnaci number?
a) 5
b) 6
c) 7
d) 8
96 | P a g e
www.aktutor.in
17. Which of the following recurrence relations can be used to find the nth fibonacci
number?
a) F(n) = F(n) + F(n – 1)
b) F(n) = F(n) + F(n + 1)
c) F(n) = F(n – 1)
d) F(n) = F(n – 1) + F(n – 2)
18. How many times will the function fibo() be called when the following code is executed?
int fibo(int n){
if(n == 1)
return 0;
else if(n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);}int main(){
int n = 5;
int ans = fibo(n);
printf("%d",ans);
return 0;}
a) 5
b) 6
c) 8
d) 9\
19. What is the output of the following code?
int fibo(int n){
if(n == 1)
return 0;
else if(n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);}int main(){
int n = 10;
int ans = fibo(n);
printf("%d",ans);
return 0;}
a) 21
b) 34
c) 55
d) 13
97 | P a g e
www.aktutor.in
23. The time complexity of the solution tower of hanoi problem using recursion is _________
a) O(n2)
b) O(2n)
c) O(n log n)
d) O(n)
24. Recurrence equation formed for the tower of hanoi problem is given by _________
a) T(n) = 2T(n-1)+n
b) T(n) = 2T(n/2)+c
c) T(n) = 2T(n-1)+c
d) T(n) = 2T(n/2)+n
25. Minimum number of moves required to solve a tower of hanoi problem with n disks is
__________
a) 2n
b) 2n-1
c) n2
d) n2-1
98 | P a g e
www.aktutor.in
d) O(n log n)
29. Minimum time required to solve tower of hanoi puzzle with 4 disks assuming one
move takes 2 seconds, will be __________
a) 15 seconds
b) 30 seconds
c) 16 seconds
d) 32 seconds
ANSWERS :
1.b 2.b 3.c 4.a 5.d 6.d 7.d 8.b 9.b 10.a 11.c 12.b
13.d 14.a 15.d 16.d 17.d 18.d 19.b 20.c 21.a 22.c
23.b 24.c 25.b 26.b 27.d 28.a 29.b
99 | P a g e
www.aktutor.in
d) O(1)
4. What is the best case and worst case complexity of ordered linear search?
a) O(nlogn), O(logn)
b) O(logn), O(nlogn)
c) O(n), O(1)
d) O(1), O(n)
10. The array is as follows: 1,2,3,6,8,10. At what time the element 6 is found? (By
100 | P a g e
www.aktutor.in
11. The array is as follows: 1,2,3,6,8,10. Given that the number 17 is to be searched.
At which call it tells that there’s no such element? (By using linear search(recursive)
algorithm)
a) 7th call
b) 9th call
c) 17th call
d) The function calls itself infinite number of times
13. Can linear search recursive algorithm and binary search recursive algorithm be
performed on an unordered list?
a) Binary search can’t be used
b) Linear search can’t be used
c) Both cannot be used
d) Both can be used
14. What is the recurrence relation for the linear search recursive algorithm?
a) T(n-2)+c
b) 2T(n-1)+c
c) T(n-1)+c
d) T(n+1)+c
16. Given an input arr = {2,5,7,99,899}; key = 899; What is the level of recursion?
a) 5
b) 2
c) 3
d) 4
101 | P a g e
www.aktutor.in
17. Given an array arr = {45,77,89,90,94,99,100} and key = 99; what are the mid
values(corresponding array elements) in the first and second levels of recursion?
a) 90 and 99
b) 90 and 94
c) 89 and 99
d) 89 and 94
18. What is the worst case complexity of binary search using recursion?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
19. What is the average case time complexity of binary search using recursion?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
22. Given an array arr = {5,6,77,88,99} and key = 88; How many iterations are done
until the element is found?
a) 1
b) 3
c) 4
d) 2
23. Given an array arr = {45,77,89,90,94,99,100} and key = 100; What are the mid
values(corresponding array elements) generated in the first and second iterations?
a) 90 and 99
b) 90 and 100
c) 89 and 94
d) 94 and 99
102 | P a g e
www.aktutor.in
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
ANSWERS :
1.d 2.d 3.c 4.d 5.b 6.c 7.a 8.a 9.a 10.a 11.a 12.a 13.a 14.c
15.b 16.c 17.a 18.b 19.b 20.d 21.b 22.d 23.a 24.b
103 | P a g e
www.aktutor.in
7. The given array is arr = {3,4,5,2,1}. The number of iterations in bubble sort and
selection sort respectively are,
a) 5 and 4
b) 4 and 5
c) 2 and 4
d) 2 and 5
8. The given array is arr = {1,2,3,4,5}. (bubble sort is implemented with a flag
variable)The number of iterations in selection sort and bubble sort respectively are,
a) 5 and 4
b) 1 and 4
c) 0 and 4
d) 4 and 1
10. Merge sort uses which of the following technique to implement sorting?
a) backtracking
b) greedy algorithm
c) divide and conquer
d) dynamic programming
104 | P a g e
www.aktutor.in
b) O(n2)
c) O(n2log n)
d) O(n log n2)
15. Which of the following method is used for sorting in merge sort?
a) merging
b) partitioning
c) selection
d) exchanging
16. What will be the best case time complexity of merge sort?
a) O(n log n)
b) O(n2)
c) O(n2 log n)
d) O(n log n2)
18. Choose the incorrect statement about merge sort from the following?
a) it is a comparison based sort
b) it is an adaptive algorithm
c) it is not an in place algorithm
d) it is stable algorithm
21. Which of the following stable sorting algorithm takes the least time when applied
to an almost sorted array?
a) Quick sort
b) Insertion sort
c) Selection sort
d) Merge sort
105 | P a g e
www.aktutor.in
23. Which of the following sorting algorithm makes use of merge sort?
a) tim sort
b) intro sort
c) bogo sort
d) quick sort
24. Which of the following sorting algorithm does not use recursion?
a) quick sort
b) merge sort
c) heap sort
d) bottom up merge sort
ANSWERS :
1.a 2.c 3.d 4.a 5.d 6.b 7.a 8.d 9.d 10.c
11.a 12.c 13.a 14.a 15.a 16.a 17.a 18.b 19.a
20.a 21.d 22.b 23.a 24.d
106 | P a g e