PythonMCQ ANSWERS
PythonMCQ ANSWERS
for y in range(3,6)
sum append(x+y)
ans:a
===================================================================
2. which function is used to read two charecters from a file object in file?
a. infile.readline(2)
b.infile.read(2)
c.infile.read()
d.infile.readlines(2)
ans:b
===================================================================
a.dump
b.write
c.output
===================================================================
4.which collection is used to store unique values?
list /dict/tuple/set
ans:set
===================================================================
a.(2,4,6,8)
b.error
c.{4,6}
d.{(2,4),(6,8)}
ans:a
===================================================================
4. class Name(object)
def first(
print("Iam callled")
def second()
obj=Name()
Name.first(obj)
******
a. unbound method call
ans:a
===================================================================
5.which is appropriate to use the static method in a class?
===================================================================
a. tmpfile=askopenfilename()
b.tmpfile=openfilename()
c.tmpfile=saveasfilename()
d.tmpfile=asksaveasfilename()
ans:A
===================================================================
7. def demo():
try;
print(1)
finally;
print(2)
demo()
a.2
b.1 2
c.1
Ans:b
===================================================================
7.on what condition finally block gets executed?
2.on exceptions
3.on errors
4.always
ans:D
===================================================================
b.a user defined exception can be created by deriving a class from error class.
ans:D
===================================================================
9.select the code snippet that divide by zero error with error message " number
cannot be divided with zero"
a. try: 1/except:raise
ans:C
===================================================================
a.1
b. 2 1
c.1 1
Ans:B
===================================================================
y=["test",'Done]
a.['test','done']
b.['TEST','DONE']
d.[None,None]
Ans:a
===================================================================
1.__iter__2 next
b. the iterator next method return the next value for the iterable.
options
--------
a. 1 -a 2 -b
b.***
b.****
d.***
ans:a
===================================================================
13.numbers=[2,4,6,8,1]
print(newnumbers)
a.(4,16,36,64,1)
b.(2,4,6,8,1)
c.[2,4,6,8,1]
d.[4,16,36,64,1]
ans:a
===================================================================
while index>0;
str2 +=str1[index-1]
index=index-1
return str2
print(string_reverse('5678ABCD'))
a.5678ABCD
b.8765DCBA
c.DCBA8765
ans:C
===================================================================
14.which of the following code uses inheritance?
ans:a,c
===================================================================
5.code snippet
class Company:
def__init__(self,name,empnum);
self.name=name
self.empnum=empnum
def myfunc(self);
p1=Company("Cognizant","12345")
a.Welcome to Cognizant
d. Cognizant 12345
ans:C
===================================================================
16. code snippet
def coverFunction();
global a
a=4
def innerFunction();
global a
a=5
print('a=',a)
a=6
coverFunction()
print('a=',a)
a.a=5 a=4
b.a=5
c.a=6
d.a=4
ans:d
===================================================================
17.List the return type of format function on strings
a.boolean
b.string
c.error
d.integer
ans:b
===================================================================
18.true or false
str_variable="Hello Python"
print(str_variable[6:11])
select one
a. true
b.false
Ans:b
===================================================================
a.pass
b.class
c.__init__
d.def
Ans:b
===================================================================
import OrderedDict
my_string1="Welcome 1"
print(my_string1)
my_string3="Welcome 4"
print(my_string3)
my_string4="""Welcome All"""
b. Error
d.Welcome 1 Welcome 2
ans:a
===================================================================
class py_Arraysum
def twoSum(self,numbers,total);
lookup={}
if total-num in lookup;
return(lookup[total-num]+1,i+1)
lookup[num]=i
print("Number1=%d"%py_ArraySum().twoSum((3,1,4,5,2,7),6))
Select one
c. if i, num in enumerate(numbers);
ANS:d
===================================================================
22. Let a tuplevalue contains 5 elements , how to set third element of the tuple to
'india'
a.def test
b.test[3]='india'
c.elements of the tuple remains unchanged
d.test[2]='india'
ans:c
===================================================================
integer=[1,3,4,5,6,7,8,9]
print(integer[::3])
a.[1,3,5,7,9]
b.[1,3,5]
c.[1,3,5,7]
d.[1,4,7]
ans:[1,5,8]
===================================================================
23. sample_dict('n':3,'e':7,'o':9)
sorted(sample_dict)
a.[3,7,9]
b. {'n':3,'e':7,'o':9}
c.{'e':7.'n':3,'o':9}
d.[e,n,o]
Ans:d
===================================================================
b.with statement encapsulates try, except , finally patern for code reuse
d.if an exception occurs in the block __exit__() method of the context manager is
called to clean up resources
ans:a,b,d
===================================================================
def demo():
try:
return 1
finally:
return 3
test=demo()
print(test)
b.3
c.1
Ans:b
===================================================================
if i>2 break
a. true
b.false
ANS:a
===================================================================
30. state the below statements are true or not
python module should contain only definitions like functions,classes and variables
and not statements like print
a. true
b.false
ans:b
===================================================================
31.what is the output of the following
d. i love Apple
Ans:a
===================================================================
32. select the correct statement(check box)
Ans:c,d
===================================================================
33.output for the below code
class Hello:
__init__(self,name)
self.name=name sayHello(self):
print(self.name)
p1=Hello("Tom")
p2=Hello("Sam")
p2.sayHello()
a.Hello Tom
b.Sam
c.Tom
d.Hello Sam
ans:b
===================================================================
34. identify the output
class MapPoint:
def __init__(self,x=0,y=0):
self.x=x+2
self.y=y+3
s1=MapPoint()
print(s1.x,s1.y)
select one
a.error
b.2,3
c.x,y
d.,0
ans:b
===================================================================
ans:a,b
===================================================================
c.print(math pi)
d.print(pi)
ans:b
===================================================================
37. what does the "cycle " function drom "iterators" module do?
a. return a iterator wich cycles through the given list over and over again
a. none object
b.an arbitary object
c.Error function must have a return statement.
ans:a
===================================================================
39. what happens if a non-existant file is opened?
d.raises an exception
ans:d
===================================================================
ans:a
===================================================================
41.select that all applicable to generators(check box)
a.generator help program to perform faster since the code do not wait for the while
data set to be generated.
d. generator yield stament returns one valut at time to the calling loop.
Ans:All
===================================================================
cubes={1,1,28,3:27,4:64,5:125}
print(cubes pop(4))
print(cubes)
a.125{1:1,2:8,3:27,5:125}
b.4{1:1,2:8,3:27,5:125}
c.64{1:1,2:8,3:27,5:125}
ans:c
===================================================================