Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Quiz

Total questions: 121

Worksheet time: 3570secs

Name
Class
Date
1.

Which of the following are true of Python dictionaries:

a)

Dictionaries can be nested to any depth.

b)

Dictionaries are accessed by key.

c)

All the keys in a dictionary must be of the same type.

d)

Dictionaries are mutable.

e)

A dictionary can contain any object type except another dictionary.

2.

Which of the following is not a valid way to define this dictionary in Python:

a)

d = dict([

('foo', 100),

('bar', 200),

('baz', 300)

])

b)

d = {}

d['foo'] = 100

d['bar'] = 200

d['baz'] = 300

c)

d = dict(foo=100, bar=200, baz=300)

d)

d = {'foo': 100, 'bar': 200, 'baz': 300}

e)

d = { ('foo', 100), ('bar', 200), ('baz', 300) }

3.

Consider this dictionary:


d = {'foo': 100, 'bar': 200, 'baz': 300}


What is the result of this statement:


d['bar':'baz']

a)

It raises an exception

b)

(200, 300)

c)

200 300

d)

[200, 300]

4.

Suppose x is defined as follows:

x = [

'a',

'b',

{

'foo': 1,

'bar':

{

'x' : 10,

'y' : 20,

'z' : 30

},

'baz': 3

},

'c',

'd'

]

What is the expression involving x that accesses the value 30?

a)

x[2]["bar"]["z"]

b)

x[2]["bar"]

c)

x["bar"]["z"]

d)

x[2]["bar"]["z"][3]

5.

Which of the following could be a valid dictionary key:

a)

(3+2j)

b)

('foo', 'bar')

c)

dict(foo=1, bar=2)

d)

'foo'

e)

['foo', 'bar']

6.

You have the following dictionary definition:


d = {'foo': 100, 'bar': 200, 'baz': 300}


What method call will delete the entry whose value is 200?

a)

delete d('bar')

b)

d.pop("bar")

c)

d.remove("bar")

d)

None of the above

7.

Suppose you have a dictionary d1. Which of the following effectively creates a variable d2 which contains a copy of d1:

a)

d2 = dict(d1.items())

b)

d2 = dict(d1.keys())

c)

d2 = {}

d2.update(d1)

d)

d2 = dict(d1.values())

e)

d2 = dict(d1)

8.

What would be the output of the following code snippet?


a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}


for key in a_dict:

print(key)

a)

'blue'

'apple'

'dog'

b)

color

fruit

pet

c)

blue

apple

dog

d)

'color'

'fruit'

'pet'

9.

What would be the output of the following code snippet?


a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}

d_items = a_dict.items()

print(d_items)

a)

('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')

b)

dict_items([('color'), ('fruit'), ('pet')])

c)

[('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')]

d)

dict_items([('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')])

10.

Consider the following statement:


If you just need to work with the keys of a dictionary, then you can use .keys(), which is a method that returns a new view object containing the dictionary’s keys.


Is this statement True or False?

a)

False

b)

True

11.

The list needs one more name added to the end - "Felipe". Which piece of code below would do this?

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

a)

nameList.append(Felipe)

b)

append(nameList,"Felipe")

c)

nameList.append["Felipe",7]

d)

nameList.append("Felipe")

12.

What is the function of the reverse method for lists?

a)

removes the first occurrence of the information inside the parenthesis

b)

adds the information inside the parenthesis to the end of the list

c)

reverses the order of the elements inside a list

d)

sorts the list (lowest to highest for numbers and alphabetical order for strings)

13.

The program above

a)

prints all the odd numbers between 1 and 20

b)

prints all even numbers between 1 and 20

c)

print all the odd numbers between 1 and 21

d)

prints all the even numbers between 1 and 21

14.

The above program prints

a)

Numbers 1 to 10 in reverse order

b)

Numbers 1 to 11 in reverse order

c)

Numbers 1 to 10

d)

Numbers 1 to 11

15.

The program above prints each number between 1 and 10

a)

as many times as the number itself

b)

10 times

c)

11 times

16.

How many times will 1 be printed

a)

3

b)

4

c)

5

d)

6

17.

What is the value of the variable sum after the code above executes

a)

54

b)

44

c)

64

d)

34

e)

74

18.

What does this program print

a)

2 + 2 = 4

4 + 4 = 8

8 + 8 = 16

16 + 16 = 32

32 + 32 = 64

b)

2 + 2 = 4

2 + 2 = 4

2 + 2 = 4

2 + 2 = 4

2 + 2 = 4

c)

2 + 2 = 4

2 + 4 = 6

2 + 6 = 8

2 + 8 = 10

2 + 10 = 12

d)

2 + 2 = 4

2 + 2 = 4

2 + 2 = 4

2 + 2 = 4

e)

2 + 2 = 4

2 + 4 = 6

2 + 6 = 8

2 + 8 = 10

19.

Which of the following are true of Python dictionaries:

a)

Dictionaries can be nested to any depth.

b)

Dictionaries are accessed by key.

c)

All the keys in a dictionary must be of the same type.

d)

Dictionaries are mutable.

e)

A dictionary can contain any object type except another dictionary.

20.

Which of the following could be a valid dictionary key:

a)

(3+2j)

b)

('foo', 'bar')

c)

dict(foo=1, bar=2)

d)

'foo'

e)

['foo', 'bar']

21.

Which of the following are true for objects of Python’s set type:

a)

The order of elements in a set is significant.

b)

A given element can’t appear in a set more than once.

c)

Sets are mutable.

d)

A set may contain elements that are mutable.

22.

What will be the output of the following Python code? *

x={1,3,5}

y={2,4,6}

x+y

a)

{1,3,5,2,4}

b)

{1,3,5,2,4,6}

c)

Error as the duplicate item 6 is present in both sets

d)

Error as unsupported operand type(s) for +: 'set' and 'set'

23.

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

24.

What will be the output of the following Python code?

x = {4,7,8,8,9}

print(x)

a)

{4, 7, 9}

b)

{8, 9, 4, 7}

c)

{4, 7, 8, 8, 9}

d)

Error

25.

Which of the following statements is used to create an empty set?

a)

{ }

b)

set()

c)

[ ]

d)

( )

26.

What will be the output of the following Python code?

x = {6,5}

y = {2,3,5,6}

x<y

a)

{1,2}

b)

True

c)

False

d)

Invalid operation

27.

The _________ function removes the first element of a set and the last element of a list.

a)

remove

b)

pop

c)

discard

d)

dispose

28.

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

29.

If x={5,6,7,8}, which of the following statements is false?

a)

print(len(x))

b)

print(min(x))

c)

x.remove(5)

d)

x[2]=45

30.

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}

31.

Find the output of the code:

# abc.py

def func(n):

return n + 10


func('Hello')

(a)  

32.

Find the output of the code:


nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv']

print nameList[1][-1]

(a)  

33.

Find the output of the code:

for i in range(2):

print i


for i in range(4,6):

print i

a)

0,1,4,5

b)

0,1,2,4,5,6

c)

0,1,2,3,4,5,6

d)

2,4,6

34.

Find the output of the code:

geekCodes = [1, 2, 3, 4]

geekCodes.append([5,6,7,8])

print len(geekCodes)

a)

4

b)

5

c)

6

d)

8

35.

Find the output of the code:

check1 = ['Learn', 'Quiz', 'Practice', 'Contribute']

check2 = check1

check3 = check1[:]

check2[0] = 'Code'

check3[1] = 'Mcq'

count = 0

for c in (check1, check2, check3):

if c[0] == 'Code':

count += 1

if c[1] == 'Mcq':

count += 10

print count

a)

10

b)

11

c)

12

d)

13

36.

What is the output of the code:

d1 = {"john":40, "peter":45}

d2 = {"john":466, "peter":45}

print d1 > d2

a)

True

b)

False

c)

Not applicable

37.

Find the output of the code:

D = {1 : 1, 2 : '2', '1' : 1, '2' : 3}

D['1'] = 2

print(D[D[D[str(D[1])]]])

a)

2

b)

3

c)

'2'

d)

Key error

38.

Find the output of the code:

D = dict()

for i in range (3):

for j in range(2):

D[i] = j

print(D)

a)

{0: 0, 1: 0, 2: 0}

b)

{0: 1, 1: 1, 2: 1}

c)

{0: 0, 1: 0, 2: 0, 0: 1, 1: 1, 2: 1}

d)

TypeError: Immutable object

39.

Find the output of the code:

List = [True, 50, 10]

List.insert(2, 5)

print(List, "Sum is: ", sum(List))

a)

[True, 50, 10, 5] Sum is: 66

b)

[True, 50, 5, 10] Sum is: 65

c)

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

d)

[True, 50, 5, 10] Sum is: 66

40.

Find the output of the code:

L = list('123456')

L[0] = L[5] = 0

L[3] = L[-2]

print(L)

a)

[0, ‘2’, ‘3’, ‘4’, ‘5’, 0]

b)

[‘6’, ‘2’, ‘3’, ‘5’, ‘5’, ‘6’]

c)

[‘0’, ‘2’, ‘3’, ‘5’, ‘5’, ‘0’]

d)

[0, ‘2’, ‘3’, ‘5’, ‘5’, 0]

41.

The result of this program:

Friday = False

if Friday:

print "Jeans day!"

else:

print "Uniform day"

a)

Jeans day

b)

Today is Friday

c)

Uniform day

d)

Not Friday

42.

What is the output?

a)

condition

b)

True

c)

Program has a syntax error.

d)

3 > 2

43.

What is the output?

a)

True

b)

False

c)

condition1

d)

condition2

44.

What is the output?

a)

more than 7

b)

more than 23

c)

spam

d)

7

45.

What is the output?

a)

250.0

b)

200.0

c)

300.0

d)

350.0

46.

What is the output?

a)

next

stop

b)

next

c)

stop

d)

syntax error - program doesn't work

47.

What is the output?

a)

3

b)

3

7

c)

3

5

7

d)

7

48.

What is the output?

a)

True

b)

NIL

c)

True

NIL

d)

True

NIL

NIL

49.

What is the output?

a)

level 3

b)

level 3

level 1

c)

level 3

level 1

NIL

d)

level 3

level 2

level 1

NIL

50.

What part of an if statement should be indented?

a)

All of it

b)

The statements within it

c)

the first line

d)

the lines within the brackets

51.

Given the nested if-else below, what will be the value x when the code is executed successfully.

a)

0

b)

4

c)

2

d)

3

52.

Given the nested if-else structure below, what will be the value of x after code execution completes.

a)

2

b)

0

c)

3

d)

4

53.

What is the output of the following if statement?

a)

False

b)

True

54.
Which is the correct syntax?
a)
has_dog = true
b)
has_dog = True
c)
has_dog = Treu
d)
HAS_DOG = tRue
55.
An if statement must evaluate to:
a)
Right or Wrong
b)
True
c)
True or False
d)
False
56.
This operator means that one value is the same as the other value
a)
==
b)
!=
c)
>
d)
<
57.

What is the result of the code:

team = Cowboys

if team = Cowboys:

print(Dallas is #1)

a)

Error

b)

Cowboys

c)

Team

d)

Dallas is #1

58.
What best defines an IF statement in python?
a)
Is an embedded string with a variable
b)
An IF statement checks to see if a statement is true or false and then does one of two things depending on the result.
59.
I am 71 years old! What will the output be?: IF you are 70 or older, say “You are aged to perfection!” ELIF you are exactly 50, say “Wow, you are half a century old!” ELSE say “You are a spring chicken!”
a)
“You are aged to perfection!”
b)
"Wow, you are half a century old!"
c)
"You are a spring chicken!"
60.
I am 18 years old! What will the output be?: IF you are 70 or older, say “You are aged to perfection!” ELIF you are exactly 50, say “Wow, you are half a century old!” ELSE say “You are a spring chicken!”
a)
"You are aged to perfection"
b)
"Wow, you are half a century old!"
c)
"You are spring chicken!"
61.

Find the output of the code:

# abc.py

def func(n):

return n + 10


func('Hello')

(a)  

62.

Find the output of the code:


nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv']

print nameList[1][-1]

(a)  

63.

Find the output of the code:

for i in range(2):

print i


for i in range(4,6):

print i

a)

0,1,4,5

b)

0,1,2,4,5,6

c)

0,1,2,3,4,5,6

d)

2,4,6

64.

Find the output of the code:

geekCodes = [1, 2, 3, 4]

geekCodes.append([5,6,7,8])

print len(geekCodes)

a)

4

b)

5

c)

6

d)

8

65.

Find the output of the code:

check1 = ['Learn', 'Quiz', 'Practice', 'Contribute']

check2 = check1

check3 = check1[:]

check2[0] = 'Code'

check3[1] = 'Mcq'

count = 0

for c in (check1, check2, check3):

if c[0] == 'Code':

count += 1

if c[1] == 'Mcq':

count += 10

print count

a)

10

b)

11

c)

12

d)

13

66.

What is the output of the code:

d1 = {"john":40, "peter":45}

d2 = {"john":466, "peter":45}

print d1 > d2

a)

True

b)

False

c)

Not applicable

67.

Find the output of the code:

D = {1 : 1, 2 : '2', '1' : 1, '2' : 3}

D['1'] = 2

print(D[D[D[str(D[1])]]])

a)

2

b)

3

c)

'2'

d)

Key error

68.

Find the output of the code:

D = dict()

for i in range (3):

for j in range(2):

D[i] = j

print(D)

a)

{0: 0, 1: 0, 2: 0}

b)

{0: 1, 1: 1, 2: 1}

c)

{0: 0, 1: 0, 2: 0, 0: 1, 1: 1, 2: 1}

d)

TypeError: Immutable object

69.

Find the output of the code:

List = [True, 50, 10]

List.insert(2, 5)

print(List, "Sum is: ", sum(List))

a)

[True, 50, 10, 5] Sum is: 66

b)

[True, 50, 5, 10] Sum is: 65

c)

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

d)

[True, 50, 5, 10] Sum is: 66

70.

Find the output of the code:

L = list('123456')

L[0] = L[5] = 0

L[3] = L[-2]

print(L)

a)

[0, ‘2’, ‘3’, ‘4’, ‘5’, 0]

b)

[‘6’, ‘2’, ‘3’, ‘5’, ‘5’, ‘6’]

c)

[‘0’, ‘2’, ‘3’, ‘5’, ‘5’, ‘0’]

d)

[0, ‘2’, ‘3’, ‘5’, ‘5’, 0]

71.

Who is the founder of Python Programming Language?

a)

Rasmus Ganilo

b)

Dennis Ritchie

c)

Guido van Rossum

d)

James Gosling

72.

File extension of Python program is ______

a)

.py

b)

.p

c)

.pyx

d)

.pyy

73.

List index begins with _________.

a)

0

b)

1

c)

-1

d)

-2

74.

Which method adds element at the end of the List?

a)

add()

b)

append()

c)

new()

d)

insert()

75.

_________________is an anonymous function.

a)

User Defined

b)

Default

c)

Lambda

d)

Reserved

76.

____________ is a collection which is unordered, changable and indexed.

a)

Dictionary

b)

List

c)

Tuple

d)

Set

77.

Python 3.7 was released on

a)

June 2016

b)

June 2017

c)

June 2018

d)

June 2019

78.

IN and NOT IN are membership operators.

a)

TRUE

b)

FALSE

79.

IS is an Identity Operator.

a)

True

b)

False

80.

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]

print(List.pop()) What willbe the answer?

a)

2.3

b)

2.5

c)

1.054

d)

4.445

81.

What will be the anser for this code?

a)

120

b)

650

c)

225

d)

400

82.

In Python_____________ are written with round brackets

a)

tuples

b)

dictionary

c)

list

d)

set

83.

a=(1,2,3,2,4,5,2)

a.count(2)

What will be the answer?

a)

2

b)

1

c)

3

d)

4

84.

Keys and Values are related to ________

a)

tuple

b)

dictionary

c)

list

d)

set

85.

The def keyword, along with the function name is used to define the __________

a)

file

b)

block

c)

function

d)

variable

86.

_______is used as a dummy place holder whenever a syntactical requirement of a certain programming element is to be fulfilled without assigning any operation

a)

Pass

b)

Break

c)

While

d)

Goto

87.

What will be the Output?

a)

1

b)

2

c)

3

d)

4

88.

List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]

print(List.count(1))

What will be the output?

a)

1

b)

2

c)

3

d)

4

89.

x = lambda a, b : a * b

print(x(5, 8))

What will be the output?

a)

35

b)

13

c)

40

d)

45

90.

____________does not hold duplicate values and is unordered.

a)

Set

b)

Tuple

c)

Dictionary

d)

List

91.

Which of the following is used to define a block of code in Python language?

a)

Key

b)

Indentation

c)

Brackets

d)

All of the mentioned

92.

What will be the value of the following Python expression?4 + 3 % 5

a)

2

b)

7

c)

1

d)

4

93.

Is Python code compiled or interpreted?

a)

Python code is both compiled and interpreted

b)

Python code is neither compiled nor interpreted

c)

Python code is only interpreted

d)

Python code is only compiled

94.

Which keyword is used for function in Python language?

a)

Function

b)

Fun

c)

Define

d)

def

95.

Which of the following is the correct extension of the Python file?

a)

.pl

b)

.py

c)

.pl

d)

p

96.

What will be the type of the variable sorted_numbers in the below code snippet?

numbers = (4, 7, 19, 2, 89, 45, 72, 22)

sorted_numbers = sorted(numbers)

print(sorted_numbers)

a)

list

b)

tuple

c)

string

d)

int

97.

What is the output of the following code snippet ?

a)

2 2

b)

error

c)

2 3

d)

None of the above

98.

Which one of the following is the correct output of the following code snippet?

a)

[1] [2] [3]

b)

[1, 2, 3]

c)

[1] [1, 2] [1, 2, 3]

d)

None of the above

99.

What is the output of the following code snippet ?

a)

1

b)

2

c)

error

d)

None of the above

100.

Which of these is the definition for packages in Python

a)

A set of programs making use of Python modules

b)

A set of main modules

c)

A folder of python modules

d)

A set of programs making use of Python modules

101.

Which one is not a legal variable name?

a)

my-var

b)

Myvar

c)

my_var

d)

_myvar

102.

How do you create a variable with the floating number 2.8?

a)

X=2.8

b)

X=float(2.8)

c)

Both are correct

d)

Only A is correct

103.

What is the correct way to create a function in Python?

a)

function myfunction()

b)

def myfunction()

c)

create myFunction()

d)

both a and b are correct

104.

Given the following file opened as song_file:


I can't write one song that's not about you

Can't drink without thinkin' about you

Is it too late to tell you that

Everything means nothing if I can't have you?


What is the result of

print(song_file.read(4)) ?

a)

I ca

b)

I can

c)

I c

d)

I can't write one song that's not about you

Can't drink without thinkin' about you

Is it too late to tell you that

Everything means nothing if I can't have you?

105.

Given the following file opened as song_file:


I can't write one song that's not about you

Can't drink without thinkin' about you

Is it too late to tell you that

Everything means nothing if I can't have you?


What is the result of

print(text_file.readline(8)) ?

a)

I can't

b)

I can

c)

I can't write one song that's not about you

d)

Error

106.

Given a text file opened as file_in, which of the following will print the entire file? (Select ALL that apply.)

a)

print(file_in.read())

b)

print(file_in)

c)

lines = file_in.readlines()

for line in lines:

print(line)

d)

lines = file_in.readlines()

for i in range(len(lines)):

print(lines)

e)

for line in file_in:

print(line)

107.

Given the following file opened as song_file:


I can't write one song that's not about you

Can't drink without thinkin' about you

Is it too late to tell you that

Everything means nothing if I can't have you?


What is the result of

print(text_file.readlines()[1]) ?

a)

Can't drink without thinkin' about you

b)

I can't write one song that's not about you

c)

I can't write one song that's not about you

Can't drink without thinkin' about you

Is it too late to tell you that

Everything means nothing if I can't have you?

d)

I

108.

What is the correct way to open the file file_in.txt for reading?

a)

open('file_in.txt', 'r')

b)

open('file_in.txt','r+')

c)

open('file_in.txt','w')

d)

open('file_in.txt','a')

109.

What is the best way to read an entire file into a single string?

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

110.

What is the best way to read an entire file into a list of strings?

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

111.

What is the best way to read the first line of a file?

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

112.

When reading text files, what are the hidden characters typically at the end of each line?

a)

\n

b)

#

c)

@

d)

»

113.

This command will open MyFile = open('MyText.txt', 'r')

a)

To read the file

b)

To append to the file

c)

To write to the file

114.

If I want to add to the end of a text file, I should:

a)

MyCipher = open("MyCipher.txt", "w")

b)

MyCipher = open("MyCipher.txt", "a")

c)

MyCipher = open("MyCipher.txt", "w+")

d)

MyCipher = open("MyCipher.txt", "s")

115.

Which of these will generate an error?

a)

MyCipher.write("writing")

b)

MyCipher..write(writing)

c)

MyCipher.write("writing",writing)

d)

MyCipher.readline()

116.

How do you close the file?

a)

MyFile = close()

b)

MyFile.close

c)

close = MyFile()

d)

MyFile.close()

117.

Which is the best type of loop to write a file in?

a)

While loop

b)

for loop

c)

repeat loop

118.

What command can you use to separate parts of a line?

a)

separate()

b)

split()

c)

divide()

d)

cut()

119.

Choose the correct statement to close a file stream named myfile:

a)

myfile.terminate()

b)

myfile.end()

c)

myfile.close()

d)

myfile.remove()

120.

How do you read a single line of text from a file?

a)

f.readline()

b)

f.getline()

c)

f.openline()

d)

f.pullline()

121.

What command should you call when you have finished using a text file?

a)

f.close()

b)

f.done()

c)

f.unopen()

d)

f.finished()