Tuesday, September 10, 2019

Python MCQ's


1. Which of these in not a core data type?
a) Lists
b) Dictionary
c) Tuples
d) Class

2. 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

3. Following set of commands are executed in shell, what will be the output?
1.     >>>str="hello"
2.     >>>str[:2]
3.     >>> 
a) he
b) lo
c) olleh
d) hello

4. Which of the following will run without errors ?
a) round(45.8)
b) round(6352.898,2,5)
c) round()
d) round(7463.123,2,1)
5. What is the return type of function id?
a) int
b) float
c) bool
d) dict

6. 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

7. What error occurs when you execute?
apple = mango
a) SyntaxError
b) NameError
c) ValueError
d) TypeError

8. Carefully observe the code and give the answer.
1.     def example(a):
2.         a = a + '2'
3.          a = a*2
4.         return a
5.     >>>example("hello")
a) indentation Error
b) cannot perform mathematical operation on strings
c) hello2
d) hello2hello2

9. What data type is the object below ?
L = [1, 23, ‘hello’, 1].
a) list
b) dictionary
c) array
d) tuple

10. 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

11. Which of the following results in a SyntaxError ?
a) ‘”Once upon a time…”, she said.’
b) “He said, ‘Yes!'”
c) ‘3\’
d) ”’That’s okay”’
12. The following is displayed by a print function call:
1.     tom
2.     dick
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(‘tomdickharry’)
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.0
b) 85.1
c) 95.0
d) 95.1

14. Select all options that print
hello-how-are-you
a) print(‘hello’, ‘how’, ‘are’, ‘you’)
b) print(‘hello’, ‘how’, ‘are’, ‘you’ + ‘-‘ * 4)
c) print(‘hello-‘ + ‘how-are-you’)
d) print(‘hello’ + ‘-‘ + ‘how’ + ‘-‘ + ‘are’ + ‘you’)

15. What is the return value of trunc() ?
a) int
b) bool
c) float
d) None

16. Which is the correct operator for power(xy)?
a) X^y
b) X**y
c) X^^y
d) None of the mentioned

17. Which one of these is floor division?
a) /
b) //
c) %
d) None of the mentioned

18. What is the order of precedence in python?
i) Parentheses
ii) Exponential
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

19. What is the answer to this expression, 22 % 3 is?
a) 7
b) 1
c) 0
d) 5

20. Mathematical operations can be performed on a string. State whether true or false.
a) True
b) False

21. Operators with the same precedence are evaluated in which manner?
a) Left to Right
b) Right to Left
c) Can’t say
d) None of the mentioned

22. What is the output of this expression, 3*1**3?
a) 27
b) 9
c) 3
d) 1

23. Which one of the following has the same precedence level?
a) Addition and Subtraction
b) Multiplication, Division and Addition
c) Multiplication, Division, Addition and Subtraction
d) Addition and Multiplication

24. The expression Int(x) implies that the variable x is converted to integer. State whether true or false.
a) True
b) False

25. Which one of the following has the highest precedence in the expression?
a) Exponential
b) Addition
c) Multiplication
d) Parentheses

26. What is the output of print 0.1 + 0.2 == 0.3?
a) True
b) False
c) Machine dependent
d) Error

27. Which of the following is not a complex number?
a) k = 2 + 3j
b) k = complex(2, 3)
c) k = 2 + 3l
d) k = 2 + 3J

28. What is the type of inf?
a) Boolean
b) Integer
c) Float
d) Complex

29. What does ~4 evaluate to?
a) -5
b) -4
c) -3
d) +3

30. What does ~~~~~~5 evaluate to?
a) +5
b) -11
c) +11
d) -5

31. Which of the following is incorrect?
a) x = 0b101
b) x = 0x4f5
c) x = 19023
d) x = 03964

32. What is the result of cmp(3, 1)?
a) 1
b) 0
c) True
d) False

33. Which of the following is incorrect?
a) float(‘inf’)
b) float(‘nan’)
c) float(’56’+’78’)
d) float(’12+34′)

34. What is the result of round(0.5) – round(-0.5)?
a) 1.0
b) 2.0
c) 0.0
d) None of the mentioned

35. What does 3 ^ 4 evaluate to?
a) 81
b) 12
c) 0.75
d) 7
36. What is the output of the code snippet shown below?
X=”hi”
print(“05d”%X)
a) 00000hi
b) 000hi
c) hi000
d) error

37. Consider the snippet of code shown below and predict the output.
X=”san-foundry”
print(“%56s”,X)
a) 56 blank spaces before san-foundry
b) 56 blank spaces before san and foundry
c) 56 blank spaces after san-foundry
d) no change

38. What is the output of the following expression if x=456?
print("%-06d"%x)
a) 000456
b) 456000
c) 456
d) error

39. What is the output of the following expression if X=345?
print(“%06d”%X)
a) 345000
b) 000345
c) 000000345
d) 345000000

40. Which of the following formatting options can be used in order to add ‘n’ blank spaces after a given string ‘S’?
a) print(“-ns”%S)
b) print(“-ns”%S)
c) print(“%ns”%S)
d) print(“%-ns”%S)

41. What is the output of this expression if X= -122?
print("-%06d"%x)
a) -000122
b) 000122
c) –00122
d) -00122

42. What is the output of the following expression if the value of x is 34?
print(“%f”%x)
a) 34.00
b) 34.0000
c) 34.000000
d) 34.00000000

43. What is the result of the expression shown below if x=56.236?
print("%.2f"%x)
a) 56.00
b) 56.24
c) 56.23
d) 0056.236

44. What is the output of this expression if x=22.19?
print("%5.2f"%x)
a) 22.1900
b) 22.00000
c) 22.19
d) 22.20

45. The expression shown below results in an error. State whether this statement is true or false.
print("-%5d0",989)
a) True
b) False
1. The output of the snippet of code shown below is:
'%d %s %g you' %(1, 'hello', 4.0)
a) Error
b) 1 hello you 4.0
c) 1 hello 4 you
d) 1 4 hello you

46. The output of which of the codes shown below will be: “There are 4 blue birds.”?
a) ‘There are %g %d birds.’ %4 %blue
b) ‘There are %d %s birds.’ %(4, blue)
c) ‘There are %s %d birds.’ %[4, blue]
d) ‘There are %d %s birds.’ 4, blue

47. What is the output of the code shown below if the system date is 18th August, 2016?
x=1234
res='integers:...%d...%-6d...%06d' %(x, x, x)
res
a) ‘integers:…1234…1234 …001234’
b) ‘integers…1234…1234…123400’
c) ‘integers:… 1234…1234…001234’
d) ‘integers:…1234…1234…001234’

48. What is the output of the code shown?
x=3.3456789
'%f | %e | %g' %(x, x, x)
a) Error
b) ‘3.3456789 | 3.3456789+00 | 3.345678’
c) ‘3.345678 | 3.345678e+0 | 3.345678’
d) ‘3.345679 | 3.345679e+00 | 3.34568’

49. What is the output of the code shown below?
x=3.3456789
'%-6.2f | %05.2f | %+06.1f' %(x, x, x)
a) ‘3.35 | 03.35 | +003.3’
b) ‘3.3456789 | 03.3456789 | +03.3456789’
c) Error
d) ‘3.34 | 03.34 | 03.34+’

50. What is the output of the code shown?
x=3.3456789
'%s' %x, str(x)
a) Error
b) (‘3.3456789’, ‘3.3456789’)
c) (3.3456789, 3.3456789)
d) (‘3.3456789’, 3.3456789)

51. What is the output of the code shown?
'%(qty)d more %(food)s' %{'qty':1, 'food': 'spam'}
a) Error
b) No output
c) ‘1 more foods’
d) ‘1 more spam’

52. What is the output of the code shown?
a='hello'
q=10
vars()
a) {‘a’ : ‘hello’, ‘q’ : 10, ……..plus built-in names set by Python….}
b) {……Built in names set by Python……}
c) {‘a’ : ‘hello’, ‘q’ : 10}
d) Error

53. The output of the code shown below is:
s='{0}, {1}, and {2}'
s.format('hello', 'good', 'morning')
a) ‘hello good and morning’
b) ‘hello, good, morning’
c) ‘hello, good, and morning’
d) Error

54. What is the output of the code shown?
s='%s, %s & %s'
s%('mumbai', 'kolkata', 'delhi')
a) mumbai kolkata & delhi
b) Error
c) No output
d) ‘mumbai, kolkata & delhi’

55. What is the output of the code shown below?
t = '%(a)s, %(b)s, %(c)s'
t % dict(a='hello', b='world', c='universe')
a) ‘hello, world, universe’
b) ‘hellos, worlds, universes’
c) Error
d) hellos, world, universe

56. What is the output of the code shown?
'{a}, {0}, {abc}'.format(10, a=2.5, abc=[1, 2])
a) Error
b) ‘2.5, 10, [1, 2]’
c) 2.5, 10, 1, 2
d) ’10, 2.5, [1, 2]’

57. What is the output of the code shown below?
'{0:.2f}'.format(1.234)
a) ‘1’
b) ‘1.234’
c) ‘1.23’
d) ‘1.2’

58. What is the output of the code shown below?
'%x %d' %(255, 255)
a) ‘ff, 255’
b) ‘255, 255’
c) ‘15f, 15f’
d) Error

59. The output of the two codes shown below is the same. State whether true or false.
'{0:.2f}'.format(1/3.0)
'%.2f'%(1/3.0)
a) True
b) False
60. What is the output of the following?
x = ['ab', 'cd']
for i in x:
    i.upper()
print(x)
a) [‘ab’, ‘cd’].
b) [‘AB’, ‘CD’].
c) [None, None].
d) none of the mentioned

61. What is the output of the following?
x = ['ab', 'cd']
for i in x:
    x.append(i.upper())
print(x)
a) [‘AB’, ‘CD’].
b) [‘ab’, ‘cd’, ‘AB’, ‘CD’].
c) [‘ab’, ‘cd’].
d) none of the mentioned

62. What is the output of the following?
i = 1
while True:
    if i%3 == 0:
        break
    print(i)
 
    i + = 1
a) 1 2
b) 1 2 3
c) error
d) none of the mentioned

63. 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 mentioned

64. What is the output of the following?
i = 5
while True:
    if i%0O11 == 0:
        break
    print(i)
    i += 1
a) 5 6 7 8 9 10
b) 5 6 7 8
c) 5 6
d) error

65. What is the output of the following?
i = 5
while True:
    if i%0O9 == 0:
        break
    print(i)
    i += 1
a) 5 6 7 8
b) 5 6 7 8 9
c) 5 6 7 8 9 10 11 12 13 14 15 ….
d) error

66. What is the output of the following?
i = 1
while True:
    if i%2 == 0:
        break
    print(i)
    i += 2
a) 1
b) 1 2
c) 1 2 3 4 5 6 …
d) 1 3 5 7 9 11 …

67. What is the output of the following?
i = 2
while True:
    if i%3 == 0:
        break
    print(i)
    i += 2
a) 2 4 6 8 10 …
b) 2 4
c) 2 3
d) error

68. What is the output of the following?
i = 1
while False:
    if i%2 == 0:
        break
    print(i)
    i += 2
a) 1
b) 1 3 5 7 …
c) 1 2 3 4 …
d) none of the mentioned

69. What is the output of the following?
True = False
while True:
    print(True)
    break
a) True
b) False
c) None
d) none of the mentioned
70. What is the output when following statement is executed ?
1.     >>>"a"+"bc"
a) a
b) bc
c) bca
d) abc

71. What is the output when following statement is executed ?
1.     >>>"abcd"[2:]
a) a
b) ab
c) cd
d) dc

72. 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

73. What is the output when following code is executed ?
1.     >>> str1 = 'hello'
2.     >>> str2 = ','
3.     >>> str3 = 'world'
4.     >>> str1[-1:]
a) olleh
b) hello
c) h
d) o

74. What arithmetic operators cannot be used with strings ?
a) +
b) *
c) –
d) All of the mentioned

75. What is the output when following code is executed ?
1.     >>>print (r"\nhello")
a) a new line and hello
b) \nhello
c) the letter r and then hello
d) error

76. What is the output when following statement is executed ?
1.     >>>print('new' 'line')
a) Error
b) Output equivalent to print ‘new\nline’
c) newline
d) new line

77. What is the output when following statement is executed ?
>>> print(‘x\97\x98’)
a) Error
b) 97
98
c) x\97
d) \x97\x98

78. What is the output when following code is executed ?
1.     >>>str1="helloworld"
2.     >>>str1[::-1]
a) dlrowolleh
b) hello
c) world
d) helloworld

79. print(0xA + 0xB + 0xC) :
a) 0xA0xB0xC
b) Error
c) 0x22
d) 33
80. What is the output of the following?
print("ab\tcd\tef".expandtabs())
a) ab cd ef
b) abcdef
c) ab\tcd\tef
d) ab cd ef

81. What is the output of the following?
print("ab\tcd\tef".expandtabs(4))
a) ab cd ef
b) abcdef
c) ab\tcd\tef
d) ab cd ef

82. What is the output of the following?
print("ab\tcd\tef".expandtabs('+'))
a) ab+cd+ef
b) ab++++++++cd++++++++ef
c) ab cd ef
d) none of the mentioned

83. What is the output of the following?
print("abcdef".find("cd") == "cd" in "abcdef")
a) True
b) False
c) Error
d) None of the mentioned

84. What is the output of the following?
print("abcdef".find("cd"))
a) True
b) 2
c) 3
d) None of the mentioned

85. What is the output of the following?
print("ccdcddcd".find("c"))
a) 4
b) 0
c) Error
d) True

86. What is the output of the following?
print("Hello {0} and {1}".format('foo', 'bin'))
a) Hello foo and bin
b) Hello {0} and {1} foo bin
c) Error
d) Hello 0 and 1

87. What is the output of the following?
print("Hello {1} and {0}".format('bin', 'foo'))
a) Hello foo and bin
b) Hello bin and foo
c) Error
d) None of the mentioned

88. What is the output of the following?
print("Hello {} and {}".format('foo', 'bin'))
a) Hello foo and bin
b) Hello {} and {}
c) Error
d) Hello and

89. What is the output of the following?
print("Hello {name1} and {name2}".format('foo', 'bin'))
a) Hello foo and bin
b) Hello {name1} and {name2}
c) Error
d) Hello and
90. What is the output of the following?
print('for'.isidentifier())
a) True
b) False
c) None
d) Error

91. What is the output of the following?
print('abc'.islower())
a) True
b) False
c) None
d) Error

92. What is the output of the following?
print('a@ 1,'.islower())
a) True
b) False
c) None
d) Error

93. What is the output of the following?
print('11'.isnumeric())
a) True
b) False
c) None
d) Error

94. What is the output of the following?
print('1.1'.isnumeric())
a) True
b) False
c) None
d) Error

95. What is the output of the following?
print('1@ a'.isprintable())
a) True
b) False
c) None
d) Error
96. What is the output of the following?
print(''''''.isspace())
a) True
b) False
c) None
d) Error

97. What is the output of the following?
print('\t'.isspace())
a) True
b) False
c) None
d) Error

98. What is the output of the following?
print('HelloWorld'.istitle())
a) True
b) False
c) None
d) Error

99. What is the output of the following?
print('Hello World'.istitle())
a) True
b) False
c) None
d) Error
100. Which of the following commands will create a list?
a) list1 = list()
b) list1 = [].
c) list1 = list([1, 2, 3])
d) all of the mentioned

101. What is the output when we execute list(“hello”)?
a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’].
b) [‘hello’].
c) [‘llo’].
d) [‘olleh’].

102. Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)?
a) 5
b) 4
c) None
d) Error

103. Suppose list1 is [2445,133,12454,123], what is max(list1) ?
a) 2445
b) 133
c) 12454
d) 123

104. Suppose list1 is [3, 5, 25, 1, 3], what is min(list1) ?
a) 3
b) 5
c) 25
d) 1

105. Suppose list1 is [1, 5, 9], what is sum(list1) ?
a) 1
b) 9
c) 15
d) Error

106. To shuffle the list(say list1) what function do we use ?
a) list1.shuffle()
b) shuffle(list1)
c) random.shuffle(list1)
d) random.shuffleList(list1)

107. 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

108. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1] ?
a) Error
b) None
c) 25
d) 2

109. 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].


110. What is the output of the following code?
a=[1,2,3]
b=a.append(4)
print(a)
print(b)
a) [1,2,3,4].
[1,2,3,4].
b) [1, 2, 3, 4].
None
c) Syntax error
d) [1,2,3].
[1,2,3,4].

111. What will be the output when executed in python shell?
>>> a=[14,52,7]
>>>> b=a.copy()
>>> b is a
a) True
b) False

112. What is the output of the following code?
a=[13,56,17]
a.append([87])
a.extend([45,67])
print(a)
a) [13, 56, 17, [87], 45, 67].
b) [13, 56, 17, 87, 45, 67].
c) [13, 56, 17, 87,[ 45, 67]].
d) [13, 56, 17, [87], [45, 67]].

113. What is the output of the following piece of code?
a=list((45,)*4)
print((45)*4)
print(a)
a) 180
[(45),(45),(45),(45)].
b) (45,45,45,45).
[45,45,45,45].
c) 180
[45,45,45,45].
d) Syntax error

114. What is the output of the following code?
lst=[[1,2],[3,4]]
print(sum(lst,[]))
a) [[3],[7]].
b) [1,2,3,4].
c) Error
d) [10].

115. What is the output of the following code?
word1="Apple"
word2="Apple"
list1=[1,2,3]
list2=[1,2,3]
print(word1 is word2)
print(list1 is list2)
a) True
True
b) False
True
c) False
False
d) True
False
116. What is the output of the following code?
def unpack(a,b,c,d):
    print(a+d)
x = [1,2,3,4]
unpack(*x)
a) Error
b) [1,4].
c) [5].
d) 5

117. What is the output of the following code?
places = ['Bangalore', 'Mumbai', 'Delhi']
<br class="blank" />places1 = places
places2 = places[:]
<br class="blank" />places1[1]="Pune"
places2[2]="Hyderabad"
print(places)
a) [‘Bangalore’, ‘Pune’, ‘Hyderabad’].
b) [‘Bangalore’, ‘Pune’, ‘Delhi’].
c) [‘Bangalore’, ‘Mumbai’, ‘Delhi’].
d) [‘Bangalore’, ‘Mumbai’, ‘Hyderabad’].

118. What is the output of the following piece of code?
x=[[1],[2]]
print(" ".join(list(map(str,x))))
a) [1] [2].
b) [49] [50].
c) Syntax error
d) [[1]] [[2]].

109. What is the output of the following code?
a=165
b=sum(list(map(int,str(a))))
print(b)
a) 561
b) 5
c) 12
d) Syntax error

120. What is the output of the following code?
a= [1, 2, 3, 4, 5]
for i in range(1, 5):
    a[i-1] = a[i]
for i in range(0, 5): 
    print(a[i],end = " ")
a) 5 5 1 2 3
b) 5 1 2 3 4
c) 2 3 4 5 1
d) 2 3 4 5 5

121. What is the output of the following code?
def change(var, lst):
    var = 1
    lst[0] = 44
k = 3
a = [1, 2, 3]
change(k, a)
print(k)
print(a)
a) 3
[44, 2, 3].
b) 1
[1,2,3].
c) 3
[1,2,3].
d) 1
[44,2,3].

122. What is the output of the following code?
a = [1, 5, 7, 9, 9, 1]
<br class="blank" />b=a[0]
<br class="blank" />x= 0
for x in range(1, len(a)):
    if a[x] > b:
        b = a[x]
        b= x
print(b)
a) 5
b) 3
c) 4
d) 0

123. What is the output of the following code?
a=["Apple","Ball","Cobra"]
<br class="blank" />a.sort(key=len)
print(a)
a) [‘Apple’, ‘Ball’, ‘Cobra’].
b) [‘Ball’, ‘Apple’, ‘Cobra’].
c) [‘Cobra’, ‘Apple’, ‘Ball’].
d) Invalid syntax for sort().

124. What is the output of the following code?
num = ['One', 'Two', 'Three']
for i, x in enumerate(num):
    print('{}: {}'.format(i, x),end=" ")
a) 1: 2: 3:
b) Exception is thrown
c) One Two Three
d) 0: One 1: Two 2: Three

125. Which of the following is a Python tuple?
a) [1, 2, 3].
b) (1, 2, 3)
c) {1, 2, 3}
d) {}

126. Suppose t = (1, 2, 4, 3), which of the following is incorrect?
a) print(t[3])
b) t[3] = 45
c) print(max(t))
d) print(len(t))

127. What will be the output?
1.     >>>t=(1,2,4,3)
2.     >>>t[1:3]
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)

128. What will be the output?
1.     >>>t=(1,2,4,3)
2.     >>>t[1:-1]
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)

129. What will be the output?
1.     >>>t = (1, 2, 4, 3, 8, 9)
2.     >>>[t[i] for i in range(0, len(t), 2)]
a) [2, 3, 9].
b) [1, 2, 4, 3, 8, 9].
c) [1, 4, 8].
d) (1, 4, 8)

130. What will be the output?
1.     d = {"john":40, "peter":45}
2.     d["john"]
a) 40
b) 45
c) “john”
d) “peter”

131. What will be the output?
1.     >>>t = (1, 2)
2.     >>>2 * t
a) (1, 2, 1, 2)
b) [1, 2, 1, 2].
c) (1, 1, 2, 2)
d) [1, 1, 2, 2].
132. What will be the output?
1.     >>>t1 = (1, 2, 4, 3)
2.     >>>t2 = (1, 2, 3, 4)
3.     >>>t1 < t2
a) True
b) False
c) Error
d) None

133. What will be the output?
1.     >>>my_tuple = (1, 2, 3, 4)
2.     >>>my_tuple.append( (5, 6, 7) )
3.     >>>print len(my_tuple)
a) 1
b) 2
c) 5
d) Error

134. What will be the output?
1.     numberGames = {}
2.     numberGames[(1,2,4)] = 8
3.     numberGames[(4,2,1)] = 10
4.     numberGames[(1,2)] = 12
5.     sum = 0
6.     for k in numberGames:
7.         sum += numberGames[k]
8.     print len(numberGames) + sum
a) 30
b) 24
c) 33
d) 12
135. What is the data type of (1)?
a) Tuple
b) Integer
c) List
d) Both tuple and integer

136. If a=(1,2,3,4), a[1:-1] is
a) Error, tuple slicing doesn’t exist
b) [2,3].
c) (2,3,4)
d) (2,3)

137. What is the output of the following code?
>>> a=(1,2,(4,5))
>>> b=(1,2,(3,4))
>>> a<b
a) False
b) True
c) Error, < operator is not valid for tuples
d) Error, < operator is valid for tuples but not if there are sub-tuples

138. 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

139. What is the output of the following code?
>>> a=(1,2,3,4)
>>> del(a[2])
a) Now, a=(1,2,4)
b) Now, a=(1,3,4)
c) Now a=(3,4)
d) Error as tuple is immutable

140. What is the output of the following code?
>>> a=(2,3,4)
>>> sum(a,3)
a) Too many arguments for sum() method
b) The method sum() doesn’t exist for tuples
c) 12
d) 9

141. Is the following piece of code valid?
>>> a=(1,2,3,4)
>>> del a
a) No because tuple is immutable
b) Yes, first element in the tuple is deleted
c) Yes, the entire tuple is deleted
d) No, invalid syntax for del method
142. What type of data is: a=[(1,1),(2,4),(3,9)]?
a) Array of tuples
b) List of tuples
c) Tuples of lists
d) Invalid type

143. What is the output of the following piece of code?
>>> a=(0,1,2,3,4)
>>> b=slice(0,2)
>>> a[b]
a) Invalid syntax for slicing
b) [0,2].
c) (0,1)
d) (0,2)

144. Is the following piece of code valid?
>>> a=(1,2,3)
>>> b=('A','B','C')
>>> c=zip(a,b)
a) Yes, c will be ((1,2,3),(‘A’,’B’,’C’))
b) Yes, c will be ((1,2,3),(‘A’,’B’,’C’))
c) No because tuples are immutable
d) No because the syntax for zip function isn’t valid

145. Which of these about a set is not true?
a) Mutable data type
b) Allows duplicate values
c) Data type with unordered values
d) Immutable data type

146. 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])
c) set((1,2,3,4))
d) {1,2,3,4}

147. What is the output of the following code?
nums = set([1,1,2,3,3,3,4,4])
print(len(nums))
a) 7
b) Error, invalid syntax for formation of set
c) 4
d) 8

148. What is the output of the following piece of code?
a = [5,5,6,7,7,7]
b = set(a)
def test(lst):
    if lst in b:
        return 1
    else:
        return 0
for i in  filter(test, a):
    print(i,end=" ")
a) 5 5 6
b) 5 6 7
c) 5 5 6 7 7 7
d) 5 6 7 7 7

149. Which of the following statements is used to create an empty set?
a) { }
b) set()
c) [ ].
d) ( )

150. What is the output of the following piece of code when executed in the python shell?
>>> a={5,4}
>>> b={1,2,4,5}
>>> a<b
a) {1,2}
b) True
c) False
d) Invalid operation

151. If a={5,6,7,8}, which of the following statements is false?
a) print(len(a))
b) print(min(a))
c) a.remove(5)
d) a[2]=45

152. If a={5,6,7}, what happens when a.add(5) is executed?
a) a={5,5,6,7}
b) a={5,6,7}
c) Error as there is no add function for set data type
d) Error as 5 already exists in the set

153. What is the output of the following code?
>>> a={4,5,6}
>>> b={2,8,6}
>>> a+b
a) {4,5,6,2,8}
b) {4,5,6,2,8,6}
c) Error as unsupported operand type for sets
d) Error as the duplicate item 6 is present in both sets
154. What is the output of the following code?
>>> a={4,5,6}
>>> b={2,8,6}
>>> a-b
a) {4,5}
b) {6}
c) Error as unsupported operand type for set data type
d) Error as the duplicate item 6 is present in both sets

155. What is the output of the following piece of code?
>>> a={5,6,7,8}
>>> b={7,8,10,11}
>>> a^b
a) {5,6,7,8,10,11}
b) {7,8}
c) Error as unsupported operand type of set data type
d) {5,6,10,11}

156. What is the output of the following code?
>>> s={5,6}
>>> s*3
a) Error as unsupported operand type for set data type
b) {5,6,5,6,5,6}
c) {5,6}
d) Error as multiplication creates duplicate elements which isn’t allowed

157. What is the output of the following piece of code?
>>> a={5,6,7,8}
>>> b={7,5,6,8}
>>> a==b
a) True
b) False

158. What is the output of the following piece of code?
>>> a={3,4,5}
>>> b={5,6,7}
>>> a|b
a) Invalid operation
b) {3, 4, 5, 6, 7}
c) {5}
d) {3,4,6,7}

159. Is the following piece of code valid?
a={3,4,{7,5}}
print(a[2][0])
a) Yes, 7 is printed
b) Error, elements of a set can’t be printed
c) Error, subsets aren’t allowed
d) Yes, {7,5} is printed
160. What is the output of the code shown?
s=set()
type(s)
a) <’set’>
b) <class ‘set’>
c) set
d) class set

161. The line of code shown below results in an error. State whether this statement is true or false.
s={2, 3, 4, [5, 6]}
a) True
b) False

162. Set makes use of __________
Dictionary makes use of ____________
a) keys, keys
b) key values, keys
c) keys, key values
d) key values, key values

163. Which of the following lines of code will result in an error?
a) s={abs}
b) s={4, ‘abc’, (1,2)}
c) s={2, 2.2, 3, ‘xyz’}
d) s={san}

164. What is the output of the code shown below?
s={2, 5, 6, 6, 7}
s
a) {2, 5, 7}
b) {2, 5, 6, 7}
c) {2, 5, 6, 6, 7}
d) Error

165. Input order is preserved in sets. State whether this statement is true or false.
a) True
b) False
166. 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]

167. What is the output of the code shown below?
s={1, 2, 3}
s.update(4)
s
a) {1, 2, 3, 4}
b) {1, 2, 4, 3}
c) {4, 1, 2, 3}
d) Error

168. Which of the following functions cannot be used on heterogeneous sets?
a) pop
b) remove
c) update
d) sum

169. What is the output of the code shown below?
s={4>3, 0, 3-3}
all(s)
any(s)
a) True
False
b) False
True
c) True
True
d) False
False
170. Which of the following statements create a dictionary?
a) d = {}
b) d = {“john”:40, “peter”:45}
c) d = {40:”john”, 45:”peter”}
d) All of the mentioned

171. Read the code shown below carefully and pick out the keys?
1.     d = {"john":40, "peter":45}
a) “john”, 40, 45, and “peter”
b) “john” and “peter”
c) 40 and 45
d) d = (40:”john”, 45:”peter”)

172. What will be the output?
1.     d = {"john":40, "peter":45}
2.     "john" in d
a) True
b) False
c) None
d) Error

173. What will be the output?
1.     d1 = {"john":40, "peter":45}
2.     d2 = {"john":466, "peter":45}
3.     d1 == d2
a) True
b) False
c) None
d) Error

174. What will be the output?
1.     d1 = {"john":40, "peter":45}
2.     d2 = {"john":466, "peter":45}
3.     d1 > d2
a) True
b) False
c) Error
d) None
175. What is the output?
1.     d = {"john":40, "peter":45}
2.     d["john"]
a) 40
b) 45
c) “john”
d) “peter”

176. 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)

177. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we use?
a) d.size()
b) len(d)
c) size(d)
d) d.len()

178. What will be the output?
1.     d = {"john":40, "peter":45}
2.     print(list(d.keys()))
a) [“john”, “peter”].
b) [“john”:40, “peter”:45].
c) (“john”, “peter”)
d) (“john”:40, “peter”:45)

179. 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
180. The following piece of code is invalid. True or False?
class demo(dict):
  def __test__(self,key):
    return []
a = demo()
a['test'] = 7
print(a)
a) True
b) False

181. What is the output of the following code?
count={}
count[(1,2,4)] = 5
count[(4,2,1)] = 7
count[(1,2)] = 6
count[(4,2,1)] = 2
tot = 0
for i in count:
    tot=tot+count[i]
print(len(count)+tot)
a) 25
b) 17
c) 16
d) Tuples can’t be made keys of a dictionary

182. What is the output of the following code?
a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])
a) [2,3,4].
b) 3
c) 2
d) An exception is thrown

183. What is the output of the following piece of code?
>>> a={'B':5,'A':9,'C':7}
>>> sorted(a)
a) [‘A’,’B’,’C’].
b) [‘B’,’C’,’A’].
c) [5,7,9].
d) [9,5,7].

184. What is the output of the following snippet of code?
>>> a={i: i*i for i in range(6)}
>>> a
a) Dictionary comprehension doesn’t exist
b) {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6:36}
c) {0: 0, 1: 1, 4: 4, 9: 9, 16: 16, 25: 25}
d) {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

185. What is the output of the following piece of code?
>>> a={}
>>> a.fromkeys([1,2,3],"check")
a) Syntax error
b) {1:”check”,2:”check”,3:”check”}
c) “check”
d) {1:None,2:None,3:None}
186. What is the output of the following snippet of code?
>>> b={}
>>> all(b)
a) { }
b) False
c) True
d) An exception is thrown

187. If b is a dictionary, what does any(b) do?
a) Returns True if any key of the dictionary is true
b) Returns False if dictionary is empty
c) Returns True if all keys of the dictionary are true
d) Method any() doesn’t exist for dictionary

188. What is the output of the following code?
>>> a={"a":1,"b":2,"c":3}
>>> b=dict(zip(a.values(),a.keys()))
>>> b
a) {‘a’: 1, ‘b’: 2, ‘c’: 3}
b) An exception is thrown
c) {‘a’: ‘b’: ‘c’: }
d) {1: ‘a’, 2: ‘b’, 3: ‘c’}

189. What is the output of the following piece of code when executed in Python shell?
>>> a={i: 'A' + str(i) for i in range(5)}
>>> a
a) An exception is thrown
b) {0: ‘A0’, 1: ‘A1’, 2: ‘A2’, 3: ‘A3’, 4: ‘A4’}
c) {0: ‘A’, 1: ‘A’, 2: ‘A’, 3: ‘A’, 4: ‘A’}
d) {0: ‘0’, 1: ‘1’, 2: ‘2’, 3: ‘3’, 4: ‘4’}

190. What is the output of the following piece of code when executed in Python shell?
>>> a=dict()
>>> a[1]
a) An exception is thrown since the dictionary is empty
b) ‘ ‘
c) 1
d) 0

191. Which of the following functions is a built-in function in python?
a) seed()
b) sqrt()
c) factorial()
d) print()

192. What is the output of the expression:
round(4.576)
a) 4.5
b) 5
c) 4
d) 4.6

193. The function pow(x,y,z) is evaluated as:
a) (x**y)**z
b) (x**y) / z
c) (x**y) % z
d) (x**y)*z

194. What is the output of the function shown below?
all([2,4,0,6])
a) Error
b) True
c) False
c) 0

195. What is the output of the expression?
round(4.5676,2)?
a) 4.5
b) 4.6
c) 4.57
d) 4.56

196. What is the output of the following function?
any([2>8, 4>2, 1>2])
a) Error
b) True
c) False
d) 4>2

197. What is the output of the function shown below?
import math
abs(math.sqrt(25))
a) Error
b) -5
c) 5
d) 5.0
198. What are the outcomes of the functions shown below?
sum(2,4,6)
sum([1,2,3])
a) Error, 6
b) 12, Error
c) 12, 6
d) Error, Error

199. What is the output of the function:
all(3,0,4.2)
a) True
b) False
c) Error
d) 0

200. What is the output of the functions shown below?
min(max(False,-3,-4), 2,7)
a) 2
b) False
c) -3
d) -4
201. Which of the following functions accepts only integers as arguments?
a) ord()
b) min()
c) chr()
d) any()

202. Suppose there is a list such that: l=[2,3,4].
If we want to print this list in reverse order, which of the following methods should be used?
a) reverse(l)
b) list(reverse[(l)])
c) reversed(l)
d) list(reversed(l))

203. The output of the function:
float('   -12345\n')
(Note that the number of blank spaces before the number is 5)
a) -12345.0 (5 blank spaces before the number)
b) -12345.0
c) Error
d) -12345.000000000…. (infinite decimal places)

204. What is the output of the functions shown below?
ord(65)
ord(‘A’)
a) A
65
b) Error
65
c) A
Error
c) Error
Error

205. What is the output of the functions shown below?
float(‘-infinity’)
float(‘inf’)
a) –inf
inf
b) –infinity
inf
c) Error
Error
d) Error
Junk value

206. 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()
7. What is the output of the function shown below?
hex(15)
a) f
b) 0xF
c) 0Xf
d) 0xf

8. Which of the following functions does not throw an error?
a) ord()
b) ord(‘ ‘)
c) ord(”)
d) ord(“”)

9. What is the output of the function:
len(["hello",2, 4, 6])
a) 4
b) 3
c) Error
d) 6

10. What is the output of the function shown below?
oct(7)
oct(7)
a) Error
07
b) 0o7
Error
c) 0o7
Error
d) 07
0o7
1. Which of the following functions can help us to find the version of python that we are currently working on?
a) sys.version
b) sys.version()
c) sys.version(0)
d) sys.version(1)

2. Which of the following functions is not defined under the sys module?
a) sys.platform
b) sys.path
c) sys.readline
d) sys.argv

3. The output of the functions len(“abc”) and sys.getsizeof(“abc”) will be the same. State whether true or false.
a) True
b) False

4. What is the output of the code shown below, if the code is run on Windows operating system?
import sys
if sys.platform[:2]== 'wi':
            print("Hello")
a) Error
b) Hello
c) No output
d) Junk value

5. What is the output of the following line of code, if the sys module has already been imported?
sys.stdout.write("hello world")
a) helloworld
b) hello world10
c) hello world11
d) error

6. What is the output of the code shown below?
import sys
sys.stdin.readline()
Sanfoundry
a) ‘Sanfoundry\n’
b) ‘Sanfoundry’
c) ‘Sanfoundry10’
d) Error

7. What is the output of this code?
import sys
eval(sys.stdin.readline())
"India"
a) India5
b) India
c) ‘India\n’
d) ‘India’

8. What is the output of the code shown below?
import sys
eval(sys.stdin.readline())
Computer
a) Error
b) ‘Computer\n’
c) Computer8
d) Computer
9. What is the output of the code shown below?
import sys
sys.argv[0]
a) Junk value
b) ‘ ‘
c) No output
d) Error

10. What is the output of the code shown below is:
import sys
sys.stderr.write(“hello”)
a) ‘hello’
b) ‘hello\n’
c) hello
d) hello5

11. What is the output of the code shown below?
import sys
sys.argv
a) ‘ ‘
b) [ ]
c) [‘ ‘]
d) Error

12. To obtain a list of all the functions defined under sys module, which of the following functions can be used?
a) print(sys)
b) print(dir.sys)
c) print(dir[sys])
d) print(dir(sys))

13. The output of the function len(sys.argv) is ____________
a) Error
b) 1
c) 0
d) Junk value
1. The process of pickling in Python includes:
a) conversion of a list into a datatable
b) conversion of a byte stream into Python object hierarchy
c) conversion of a Python object hierarchy into byte stream
d) conversion of a datatable into a list

2. To sterilize an object hierarchy, the _____________ function must be called.
To desterilize a data stream, the ______________ function must be called.
a) dumps(), undumps()
b) loads(), unloads()
c) loads(), dumps()
d) dumps(), loads()

3. Pick the correct statement regarding pickle and marshal modules.
a) The pickle module supports primarily .pyc files whereas marshal module is used to sterilize Python objects
b) The pickle module keeps track of the objects that have already been sterilized whereas the marshal module does not do this
c) The pickle module cannot be used to sterilize user defined classes and their instances whereas marshal module can be used to perform this task
d) The format of sterilization of the pickle module is not guaranteed to be supported across all versions of Python. The marshal module sterilization is compatible across all the versions of Python

4. What is the output of the line of code shown below?
pickle.HIGHEST_PROTOCOL
a) 4
b) 5
c) 3
d) 6

5. Which of the codes shown below will result in an error?
Given that:
object = ‘a’
a) >>> pickle.dumps(object)
b) >>> pickle.dumps(object, 3)
c) >>> pickle.dumps(object, 3, True)
d) >>> pickle.dumps(‘a’, 2)

6. Which of the following functions can be used to find the protocol version of the pickle module currently being used?
a) pickle.DEFAULT
b) pickle.CURRENT
c) pickle.CURRENT_PROTOCOL
d) pickle.DEFAULT_PROTOCOL

7. The output of the two codes shown below is exactly the same. State whether true or false.
object
'a'
CODE 1
>>> pickle.dumps('a', 3)
CODE 2
>>> pickle.dumps(object, 3)
a) True
b) False

8. Which of the following functions can accept more than one positional argument?
a) pickle.dumps
b) pickle.loads
c) pickle.dump
d) pickle.load
9. Which of the following functions raises an error when an unpicklable object is encountered by Pickler?
a) pickle.PickleError
b) pickle.PicklingError
c) pickle.UnpickleError
d) pickle.UnpicklingError

10. The pickle module defines ______ exceptions and exports _______ classes.
a) 2, 3
b) 3, 4
c) 3, 2
d) 4, 3

11. Which of the following cannot be pickled?
a) Functions which are defined at the top level of a module with lambda
b) Functions which are defined at the top level of a module with def
c) Built-in functions which are defined at the top level of a module
d) Classes which are defined at the top level of a module

12. If __getstate__() returns _______________ the __setstate__() module will not be called on pickling.
a) True value
b) False value
c) ValueError
d) OverflowError

13. Lambda functions cannot be pickled because:
a) Lambda functions only deal with binary values, that is, 0 and 1
b) Lambda functions cannot be called directly
c) Lambda functions cannot be identified by the functions of the pickle module
d) All lambda functions have the same name, that is, <lambda>

14. The module _______________ is a comparatively faster implementation of the pickle module.
a) cPickle
b) nPickle
c) gPickle
d) tPickle

15. The copy module uses the ___________________ protocol for shallow and deep copy.
a) pickle
b) marshal
c) shelve
d) copyreg
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”)

2. To open a file c:\scores.txt for writing, we use
a) outfile = open(“c:\scores.txt”, “w”)
b) outfile = open(“c:\\scores.txt”, “w”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “w”)

3. To open a file c:\scores.txt for appending data, we use
a) outfile = open(“c:\\scores.txt”, “a”)
b) outfile = open(“c:\\scores.txt”, “rw”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “w”)

4. Which of the following statements are true?
a) When you open a file for reading, if the file does not exist, an error occurs
b) When you open a file for writing, if the file does not exist, a new file is created
c) When you open a file for writing, if the file exists, the existing file is overwritten with the new file
d) All of the mentioned

5. To read two characters from a file object infile, we use
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

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()

7. What is the output?
1.     f = None
2.     for i in range (5):
3.         with open("data.txt", "w") as f:
4.             if i > 2:
5.                 break
6.     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)
b) infile.read()
C) infile.readline()
d) infile.readlines()

10. The readlines() method returns
a) str
b) a list of lines
c) a list of single characters
d) a list of integers
1. 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

2. What is the output of this program?
1.     str = raw_input("Enter your input: ");
2.     print "Received input is : ", str
a) Enter your input: Hello Python
Received input is : Hello Python
b) Enter your input: Hello Python
Received input is : Hello
c) Enter your input: Hello Python
Received input is : Python
d) None of the mentioned

3. What is the output of this program?
1.     str = input("Enter your input: ");
2.     print "Received input is : ", str
a) Enter your input: [x*5 for x in range(2,10,2)].
Received input is : [x*5 for x in range(2,10,2)].
b) Enter your input: [x*5 for x in range(2,10,2)].
Received input is : [10, 30, 20, 40].
c) Enter your input: [x*5 for x in range(2,10,2)].
Received input is : [10, 10, 30, 40].
d) None of the mentioned

4. Which one of the following is not attributes of file
a) closed
b) softspace
c) rename
d) mode

5. What is the use of tell() method in python?
a) tells you the current position within the file
b) tells you the end position within the file
c) tells you the file is opened or not
d) none of the mentioned

6. What is the current syntax of rename() a file?
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

7. What is the current syntax of remove() a file?
a) remove(file_name)
b) remove(new_file_name, current_file_name,)
c) remove(() , file_name))
d) none of the mentioned

8. What is the output of this program?
1.     fo = open("foo.txt", "rw+")
2.     print "Name of the file: ", fo.name
3.      
4.     # Assuming file has following 5 lines
5.     # This is 1st line
6.     # This is 2nd line
7.     # This is 3rd line
8.     # This is 4th line
9.     # This is 5th line
10.                          
11.                         for index in range(5):
12.                            line = fo.next()
13.                            print "Line No %d - %s" % (index, line)
14.                          
15.                         # Close opened file
16.                         fo.close()
a) Compilation Error
b) Syntax Error
c) Displays Output
d) None of the mentioned

9. What is the use of seek() method in files?
a) sets the file’s current position at the offset
b) sets the file’s previous position at the offset
c) sets the file’s current position within the file
d) none of the mentioned

10. What is the use of truncate() method in file?
a) truncates the file size
b) deletes the content of the file
c) deletes the file size
d) none of the mentioned
1. Which is/are the basic I/O connections in file?
a) Standard Input
b) Standard Output
c) Standard Errors
d) All of the mentioned

2. What is the output of this program?
1.     import sys
2.     print 'Enter your name: ',
3.     name = ''
4.     while True:
5.        c = sys.stdin.read(1)
6.        if c == '\n':
7.           break
8.        name = name + c
9.      
10.                         print 'Your name is:', name
If entered name is
sanfoundry
a) sanfoundry
b) sanfoundry, sanfoundry
c) San
d) None of the mentioned

3. What is the output of this program?
1.     import sys
2.     sys.stdout.write(' Hello\n')
3.     sys.stdout.write('Python\n')
a) Compilation Error
b) Runtime Error
c) Hello Python
d) Hello
Python

4. Which of the following mode will refer to binary data?
a) r
b) w
c) +
d) b

5. What is the pickling?
a) It is used for object serialization
b) It is used for object deserialization
c) None of the mentioned
d) All of the mentioned

6. What is unpickling?
a) It is used for object serialization
b) It is used for object deserialization
c) None of the mentioned
d) All of the mentioned

7. What is the correct syntax of open() function?
a) file = open(file_name [, access_mode][, buffering])
b) file object = open(file_name [, access_mode][, buffering])
c) file object = open(file_name)
d) none of the mentioned

8. What is the output of this program?
1.     fo = open("foo.txt", "wb")
2.     print "Name of the file: ", fo.name
3.     fo.flush()
4.     fo.close()
a) Compilation Error
b) Runtime Error
c) No Output
d) Flushes the file when closing them

9. Correct syntax of file.writelines() is?
a) file.writelines(sequence)
b) fileObject.writelines()
c) fileObject.writelines(sequence)
d) none of the mentioned

10. Correct syntax of file.readlines() is?
a) fileObject.readlines( sizehint );
b) fileObject.readlines();
c) fileObject.readlines(sequence)
d) none of the mentioned
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

2. Which keyword is use for function?
a) Fun
b) Define
c) Def
d) Function

3. What is the output of the below program?
1.     def sayHello():
2.         print('Hello World!') 
3.     sayHello() 
4.     sayHello()
a) Hello World!
Hello World!
b) ‘Hello World!’
‘Hello World!’
c) Hello
Hello
d) None of the mentioned

4. What is the output of the below program?
1.     def printMax(a, b):
2.         if a > b:
3.             print(a, 'is maximum')
4.         elif a == b:
5.             print(a, 'is equal to', b)
6.         else:
7.             print(b, 'is maximum')
8.     printMax(3, 4)
a) 3
b) 4
c) 4 is maximum
d) None of the mentioned

5. What is the output of the below program ?
1.     x = 50
2.     def func(x):
3.         print('x is', x)
4.         x = 2
5.         print('Changed local x to', x)
6.     func(x)
7.     print('x is now', x)
a) x is now 50
b) x is now 2
c) x is now 100
d) None of the mentioned

6. What is the output of the below program?
1.     x = 50
2.     def func():
3.         global x
4.         print('x is', x)
5.         x = 2
6.         print('Changed global x to', x)
7.     func()
8.     print('Value of x is', x)
a) x is 50
Changed global x to 2
Value of x is 50
b) x is 50
Changed global x to 2
Value of x is 2
c) x is 50
Changed global x to 50
Value of x is 50
d) None of the mentioned

7. What is the output of below program?
1.     def say(message, times = 1):
2.         print(message * times)
3.     say('Hello')
4.     say('World', 5)
a) Hello
WorldWorldWorldWorldWorld
b) Hello
World 5
c) Hello
World,World,World,World,World
d) Hello
HelloHelloHelloHelloHello
8. What is the output of the below program?
1.     def func(a, b=5, c=10):
2.         print('a is', a, 'and b is', b, 'and c is', c)
3.      
4.     func(3, 7)
5.     func(25, c = 24)
6.     func(c = 50, a = 100)
a) a is 7 and b is 3 and c is 10
a is 25 and b is 5 and c is 24
a is 5 and b is 100 and c is 50
b) a is 3 and b is 7 and c is 10
a is 5 and b is 25 and c is 24
a is 50 and b is 100 and c is 5
c) a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
d) None of the mentioned

9. What is the output of below program?
1.     def maximum(x, y):
2.         if x > y:
3.             return x
4.         elif x == y:
5.             return 'The numbers are equal'
6.         else:
7.             return y
8.      
9.     print(maximum(2, 3))
a) 2
b) 3
c) The numbers are equal
d) None of the mentioned

10. Which of the following is a features of DocString?
a) Provide a convenient way of associating documentation with Python modules, functions, classes, and methods
b) All functions should have a docstring
c) Docstrings can be accessed by the __doc__ attribute on objects
d) All of the mentioned
1. What is a variable defined outside a function referred to as?
a) A static variable
b) A global variable
c) A local variable
d) An automatic variable

2. What is a variable defined inside a function referred to as?
a) A global variable
b) A volatile variable
c) A local variable
d) An automatic variable

3. What is the output of the following code?
i=0
def change(i):
   i=i+1
   return i
change(1)
print(i)
a) 1
b) Nothing is displayed
c) 0
d) An exception is thrown

4. What is the output of the following piece of code?
def a(b):
    b = b + [5]
 
c = [1, 2, 3, 4]
a(c)
print(len(c))
a) 4
b) 5
c) 1
d) An exception is thrown

5. What is the output of the following code?
a=10
b=20
def change():
    global b
    a=45
    b=56
change()
print(a)
print(b)
a)10
56
b)45
56
c)10
20
d)Syntax Error

6. What is the output of the following code?
def change(i = 1, j = 2):
    i = i + j
    j = j + 1
    print(i, j)
change(j = 1, i = 2)
a) An exception is thrown because of conflicting values
b) 1 2
c) 3 3
d) 3 2

207. What is the output of the following code?
def change(one, *two):
   print(type(two))
change(1,2,3,4)
a) Integer
b) Tuple
c) Dictionary
d) An exception is thrown
208. If a function doesn’t have a return statement, which of the following does the function return?
a) int
b) null
c) None
d) An exception is thrown without the return statement

209. What is the output of the following code?
def display(b, n):
    while n > 0:
        print(b,end="")
        n=n-1
display('z',3)
a) zzz
b) zz
c) An exception is executed
d) Infinite loop
210. What is the output of the following piece of code?
def find(a, **b):
   print(type(b))
find('letters',A='1',B='2')
a) String
b) Tuple
c) Dictionary
d) An exception is thrown

2 comments: