100% found this document useful (1 vote)
1K views14 pages

PythonMCQ ANSWERS

The document contains sample code snippets and questions related to Python programming. It tests knowledge of Python concepts like lists, dictionaries, classes, functions, loops, exceptions, files and more. For each question, there are multiple choice answers to select from and the expected or correct answer is provided. Some key questions covered include list comprehensions, reading/writing files, custom exceptions, inheritance, static methods, file dialogs, iterator methods, lambda functions, string formatting and more. The document serves as a Python quiz or assessment to evaluate understanding of core Python programming concepts and syntax.

Uploaded by

Sunny Tiwary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views14 pages

PythonMCQ ANSWERS

The document contains sample code snippets and questions related to Python programming. It tests knowledge of Python concepts like lists, dictionaries, classes, functions, loops, exceptions, files and more. For each question, there are multiple choice answers to select from and the expected or correct answer is provided. Some key questions covered include list comprehensions, reading/writing files, custom exceptions, inheritance, static methods, file dialogs, iterator methods, lambda functions, string formatting and more. The document serves as a Python quiz or assessment to evaluate understanding of core Python programming concepts and syntax.

Uploaded by

Sunny Tiwary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1.select the list comprehension code equivalent to the below script.

sum=[] for x in (range(1,3)

for y in range(3,6)

sum append(x+y)

1.sum=[x + y doe x in range(1,3) for y in range(3,6)]

2.sum=for x in range(1,3) for y in range(3,6) print x+y]

3.sum=[ return x+y for y in range(1,3) for x in range(3,6)]

4.sum=[ for x in range(1,3) for y in range(3,6) return 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
===================================================================

3. which function is used to write the data in the binary format?

a.dump

b.write

c.output

d. all of the above


ans:a

===================================================================
4.which collection is used to store unique values?

list /dict/tuple/set
ans:set
===================================================================

5.identify the output print((2,4)+(6,8))

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

print("i got called")

obj=Name()

Name.first(obj)

******
a. unbound method call

b. bounded method call

c.incorrect object declaration

d.all the above

ans:a
===================================================================
5.which is appropriate to use the static method in a class?

a.to access the attribute of the instance

b. to instantiate the class based on the given parameter


c.to perform the action relevant to the class without instantiating the class
d. to access the static members of the class
Ans:C

===================================================================

6. which function displaya a file dialog for opening an existing file?

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?

1.on sepecific condition

2.on exceptions

3.on errors

4.always
ans:D
===================================================================

8. select the correct option

a. none of the listed options

b.a user defined exception can be created by deriving a class from error class.

c.custom exceptions is unavailable in python

d. a user definedexception can be created by deriving a class from exception 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

b.try: 1/except ax e : print(e)

c. try: 1/ except ZeroDivisionError: print("number cannot be divided with zero")

d.try: 1/ except ValueError: print (("number cannot be divided with zero")

ans:C
===================================================================

10.identify the output-


for i in [1,0]: print(i+1)

a.1

b. 2 1

c.1 1

Ans:B

===================================================================

11.identify the output

y=["test",'Done]

for i in y:i.upper() print(y)

a.['test','done']

b.['TEST','DONE']

c. none of the listed options

d.[None,None]

Ans:a

===================================================================

12.select the correct match

1.__iter__2 next

a.method that is called a initialization of an iterator

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]

newnumbers= tuple(map(lamda x:x*x,numbers))

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

===================================================================

14.what is the o/p

def string_reverse(str1): str2=" index=len(str1)

while index>0;

str2 +=str1[index-1]

index=index-1

return str2

print(string_reverse('5678ABCD'))

a.5678ABCD

b.8765DCBA

c.DCBA8765

D.none of the above

ans:C
===================================================================
14.which of the following code uses inheritance?

a.class Hello: Pass class world(Hello):Pass

b.class Hello: Pass

c. class Hello: Pass class Test: Pass class World(Hello,Test):Pass


d. none of the above

ans:a,c

===================================================================
5.code snippet

class Company:

def__init__(self,name,empnum);

self.name=name

self.empnum=empnum

def myfunc(self);

print("Welcome to"+self.name+' '+self.empnum)

p1=Company("Cognizant","12345")

a.Welcome to Cognizant

b.Syntax error, this program will not run

c. Welcome to Cognizant 12345

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

output of the below statement is "Python"

str_variable="Hello Python"

print(str_variable[6:11])

select one

a. true

b.false

Ans:b
===================================================================

19.which keyword is used at the begining of the class definition

a.pass

b.class

c.__init__

d.def

Ans:b
===================================================================

20.o/p of the code

import OrderedDict

my_string1="Welcome 1"

print(my_string1)

my_string2= "Welcome 2"


print(my_string2)

my_string3="Welcome 4"

print(my_string3)

my_string4="""Welcome All"""

a. Welcome 1 Welcome 2 Welcome 4 Welcome All

b. Error

c. Welcome 1 Welcome 2 Welcome 4

d.Welcome 1 Welcome 2

ans:a
===================================================================

21.choose the correct option

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

output: Number 1=2, Number2=4

Select one

a. none of the listed

b.while i,num in enumerate(numbers);

c. if i, num in enumerate(numbers);

d. for 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
===================================================================

22.identify the output

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
===================================================================

24.select the applicable to with statement( check box)

a.with statement is used to loop over a generator

b.with statement encapsulates try, except , finally patern for code reuse

c.with stement is used to alias an object

d.if an exception occurs in the block __exit__() method of the context manager is
called to clean up resources

ans:a,b,d
===================================================================

25.select the correct option that closes file if exception occurs

a. none of the listed options

b. with open("demo.txt", encoding='utf-8')as f # perform file operations


c. f=open("demo.txt",'f',encoding='utf-8') f.read()

d.try #code that can raise error pass


ans:b,d
===================================================================
26. identify the output

def demo():

try:

return 1

finally:

return 3

test=demo()

print(test)

a.error more than one return statement

b.3

c.1

Ans:b
===================================================================

27. identify the ouptut

file= None for i in range(5):

with open("hello.txt","w") as file

if i>2 break

print the closed

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

def nameFruits("fruit):print("i love",fruit)


nameFruits('Apple','Mango','orange')

select one option

a.('I love',( 'Apple','Mango','orange'))

b.Syntax error nameFruits() can take only one argument

c. i love Apple i love Mango i love Orange

d. i love Apple
Ans:a

===================================================================
32. select the correct statement(check box)

a. a reference variable is an object

b. an object may contain other objects

c. an object can contain reference to other objects

d.a referance vaiable refers to an object.

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
===================================================================

35. select the correct statement

choose one or two

a.tuple is immutable and list is mutable

b.set is an unordered collection of unique items

c.Dictinoary is an ordered collection of key-value pairs

d.tuple is mutable and list is immutable

ans:a,b
===================================================================

36.choose the correct optiona to print pi constant

as declared in math module

a. from math import pi print(math pi)

b. from math import pi print(pi)

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

b. returns a iterator for the given iteratable object

c.converts the given list into generator

d. cycles through the given list


ans:
===================================================================
38. what would be the function return if no return statement is used inside it

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?

a. new file gets created

b.open an alternative file

c.none of the listed options

d.raises an exception

ans:d

===================================================================

40. select the correct option

a.custom exception is unavailable in python

b. a user-defined exception can be created by deriving a class from exception class

c.none of the listed options

d.a user-defined exception can be deriving a class from error class

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.

b.generator are used only to loop over list

c.generator is memory efficient

d. generator yield stament returns one valut at time to the calling loop.
Ans:All

===================================================================

42. identify the output

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}

d.none of the listed options

ans:c
===================================================================

You might also like