WorksheetsPython Quiz
Total questions: 121
Worksheet time: 3570secs
Which of the following are true of Python dictionaries:
Dictionaries can be nested to any depth.
Dictionaries are accessed by key.
All the keys in a dictionary must be of the same type.
Dictionaries are mutable.
A dictionary can contain any object type except another dictionary.
Which of the following is not a valid way to define this dictionary in Python:
d = dict([
('foo', 100),
('bar', 200),
('baz', 300)
])
d = {}
d['foo'] = 100
d['bar'] = 200
d['baz'] = 300
d = dict(foo=100, bar=200, baz=300)
d = {'foo': 100, 'bar': 200, 'baz': 300}
d = { ('foo', 100), ('bar', 200), ('baz', 300) }
Consider this dictionary:
d = {'foo': 100, 'bar': 200, 'baz': 300}
What is the result of this statement:
d['bar':'baz']
It raises an exception
(200, 300)
200 300
[200, 300]
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?
x[2]["bar"]["z"]
x[2]["bar"]
x["bar"]["z"]
x[2]["bar"]["z"][3]
Which of the following could be a valid dictionary key:
(3+2j)
('foo', 'bar')
dict(foo=1, bar=2)
'foo'
['foo', 'bar']
You have the following dictionary definition:
d = {'foo': 100, 'bar': 200, 'baz': 300}
What method call will delete the entry whose value is 200?
delete d('bar')
d.pop("bar")
d.remove("bar")
None of the above
Suppose you have a dictionary d1. Which of the following effectively creates a variable d2 which contains a copy of d1:
d2 = dict(d1.items())
d2 = dict(d1.keys())
d2 = {}
d2.update(d1)
d2 = dict(d1.values())
d2 = dict(d1)
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)
'blue'
'apple'
'dog'
color
fruit
pet
blue
apple
dog
'color'
'fruit'
'pet'
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)
('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')
dict_items([('color'), ('fruit'), ('pet')])
[('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')]
dict_items([('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')])
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?
False
True
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"]
nameList.append(Felipe)
append(nameList,"Felipe")
nameList.append["Felipe",7]
nameList.append("Felipe")
What is the function of the reverse method for lists?
removes the first occurrence of the information inside the parenthesis
adds the information inside the parenthesis to the end of the list
reverses the order of the elements inside a list
sorts the list (lowest to highest for numbers and alphabetical order for strings)
The program above
prints all the odd numbers between 1 and 20
prints all even numbers between 1 and 20
print all the odd numbers between 1 and 21
prints all the even numbers between 1 and 21
The above program prints
Numbers 1 to 10 in reverse order
Numbers 1 to 11 in reverse order
Numbers 1 to 10
Numbers 1 to 11
The program above prints each number between 1 and 10
as many times as the number itself
10 times
11 times
How many times will 1 be printed
3
4
5
6
What is the value of the variable sum after the code above executes
54
44
64
34
74
What does this program print
2 + 2 = 4
4 + 4 = 8
8 + 8 = 16
16 + 16 = 32
32 + 32 = 64
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 4 = 6
2 + 6 = 8
2 + 8 = 10
2 + 10 = 12
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
2 + 4 = 6
2 + 6 = 8
2 + 8 = 10
Which of the following are true of Python dictionaries:
Dictionaries can be nested to any depth.
Dictionaries are accessed by key.
All the keys in a dictionary must be of the same type.
Dictionaries are mutable.
A dictionary can contain any object type except another dictionary.
Which of the following could be a valid dictionary key:
(3+2j)
('foo', 'bar')
dict(foo=1, bar=2)
'foo'
['foo', 'bar']
Which of the following are true for objects of Python’s set type:
The order of elements in a set is significant.
A given element can’t appear in a set more than once.
Sets are mutable.
A set may contain elements that are mutable.
What will be the output of the following Python code? *
x={1,3,5}
y={2,4,6}
x+y
{1,3,5,2,4}
{1,3,5,2,4,6}
Error as the duplicate item 6 is present in both sets
Error as unsupported operand type(s) for +: 'set' and 'set'
Which of the following functions will return the symmetric difference between two sets, x and y? *
x | y
x ^ y
x & y
x – y
What will be the output of the following Python code?
x = {4,7,8,8,9}
print(x)
{4, 7, 9}
{8, 9, 4, 7}
{4, 7, 8, 8, 9}
Error
Which of the following statements is used to create an empty set?
{ }
set()
[ ]
( )
What will be the output of the following Python code?
x = {6,5}
y = {2,3,5,6}
x<y
{1,2}
True
False
Invalid operation
The _________ function removes the first element of a set and the last element of a list.
remove
pop
discard
dispose
Which of these about a set is not true?
Mutable data type
Allows duplicate values
Data type with unordered values
Immutable data type
If x={5,6,7,8}, which of the following statements is false?
print(len(x))
print(min(x))
x.remove(5)
x[2]=45
Which of the following is not the correct syntax for creating a set?
set([[1,2],[3,4]])
set([1,2,2,3,4])
set((1,2,3,4))
{1,2,3,4}
Find the output of the code:
# abc.py
def func(n):
return n + 10
func('Hello')
(a)
Find the output of the code:
nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv']
print nameList[1][-1]
(a)
Find the output of the code:
for i in range(2):
print i
for i in range(4,6):
print i
0,1,4,5
0,1,2,4,5,6
0,1,2,3,4,5,6
2,4,6
Find the output of the code:
geekCodes = [1, 2, 3, 4]
geekCodes.append([5,6,7,8])
print len(geekCodes)
4
5
6
8
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
10
11
12
13
What is the output of the code:
d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
print d1 > d2
True
False
Not applicable
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])]]])
2
3
'2'
Key error
Find the output of the code:
D = dict()
for i in range (3):
for j in range(2):
D[i] = j
print(D)
{0: 0, 1: 0, 2: 0}
{0: 1, 1: 1, 2: 1}
{0: 0, 1: 0, 2: 0, 0: 1, 1: 1, 2: 1}
TypeError: Immutable object
Find the output of the code:
List = [True, 50, 10]
List.insert(2, 5)
print(List, "Sum is: ", sum(List))
[True, 50, 10, 5] Sum is: 66
[True, 50, 5, 10] Sum is: 65
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
[True, 50, 5, 10] Sum is: 66
Find the output of the code:
L = list('123456')
L[0] = L[5] = 0
L[3] = L[-2]
print(L)
[0, ‘2’, ‘3’, ‘4’, ‘5’, 0]
[‘6’, ‘2’, ‘3’, ‘5’, ‘5’, ‘6’]
[‘0’, ‘2’, ‘3’, ‘5’, ‘5’, ‘0’]
[0, ‘2’, ‘3’, ‘5’, ‘5’, 0]
The result of this program:
Friday = False
if Friday:
print "Jeans day!"
else:
print "Uniform day"
Jeans day
Today is Friday
Uniform day
Not Friday
What is the output?
condition
True
Program has a syntax error.
3 > 2
What is the output?
True
False
condition1
condition2
What is the output?
more than 7
more than 23
spam
7
What is the output?
250.0
200.0
300.0
350.0
What is the output?
next
stop
next
stop
syntax error - program doesn't work
What is the output?
3
3
7
3
5
7
7
What is the output?
True
NIL
True
NIL
True
NIL
NIL
What is the output?
level 3
level 3
level 1
level 3
level 1
NIL
level 3
level 2
level 1
NIL
What part of an if statement should be indented?
All of it
The statements within it
the first line
the lines within the brackets
Given the nested if-else below, what will be the value x when the code is executed successfully.
0
4
2
3
Given the nested if-else structure below, what will be the value of x after code execution completes.
2
0
3
4
What is the output of the following if statement?
False
True
What is the result of the code:
team = Cowboys
if team = Cowboys:
print(Dallas is #1)
Error
Cowboys
Team
Dallas is #1
Find the output of the code:
# abc.py
def func(n):
return n + 10
func('Hello')
(a)
Find the output of the code:
nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv']
print nameList[1][-1]
(a)
Find the output of the code:
for i in range(2):
print i
for i in range(4,6):
print i
0,1,4,5
0,1,2,4,5,6
0,1,2,3,4,5,6
2,4,6
Find the output of the code:
geekCodes = [1, 2, 3, 4]
geekCodes.append([5,6,7,8])
print len(geekCodes)
4
5
6
8
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
10
11
12
13
What is the output of the code:
d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
print d1 > d2
True
False
Not applicable
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])]]])
2
3
'2'
Key error
Find the output of the code:
D = dict()
for i in range (3):
for j in range(2):
D[i] = j
print(D)
{0: 0, 1: 0, 2: 0}
{0: 1, 1: 1, 2: 1}
{0: 0, 1: 0, 2: 0, 0: 1, 1: 1, 2: 1}
TypeError: Immutable object
Find the output of the code:
List = [True, 50, 10]
List.insert(2, 5)
print(List, "Sum is: ", sum(List))
[True, 50, 10, 5] Sum is: 66
[True, 50, 5, 10] Sum is: 65
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
[True, 50, 5, 10] Sum is: 66
Find the output of the code:
L = list('123456')
L[0] = L[5] = 0
L[3] = L[-2]
print(L)
[0, ‘2’, ‘3’, ‘4’, ‘5’, 0]
[‘6’, ‘2’, ‘3’, ‘5’, ‘5’, ‘6’]
[‘0’, ‘2’, ‘3’, ‘5’, ‘5’, ‘0’]
[0, ‘2’, ‘3’, ‘5’, ‘5’, 0]
Who is the founder of Python Programming Language?
Rasmus Ganilo
Dennis Ritchie
Guido van Rossum
James Gosling
File extension of Python program is ______
.py
.p
.pyx
.pyy
List index begins with _________.
0
1
-1
-2
Which method adds element at the end of the List?
add()
append()
new()
insert()
_________________is an anonymous function.
User Defined
Default
Lambda
Reserved
____________ is a collection which is unordered, changable and indexed.
Dictionary
List
Tuple
Set
Python 3.7 was released on
June 2016
June 2017
June 2018
June 2019
IN and NOT IN are membership operators.
TRUE
FALSE
IS is an Identity Operator.
True
False
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop()) What willbe the answer?
2.3
2.5
1.054
4.445
What will be the anser for this code?
120
650
225
400
In Python_____________ are written with round brackets
tuples
dictionary
list
set
a=(1,2,3,2,4,5,2)
a.count(2)
What will be the answer?
2
1
3
4
Keys and Values are related to ________
tuple
dictionary
list
set
The def keyword, along with the function name is used to define the __________
file
block
function
variable
_______is used as a dummy place holder whenever a syntactical requirement of a certain programming element is to be fulfilled without assigning any operation
Pass
Break
While
Goto
What will be the Output?
1
2
3
4
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.count(1))
What will be the output?
1
2
3
4
x = lambda a, b : a * b
print(x(5, 8))
What will be the output?
35
13
40
45
____________does not hold duplicate values and is unordered.
Set
Tuple
Dictionary
List
Which of the following is used to define a block of code in Python language?
Key
Indentation
Brackets
All of the mentioned
What will be the value of the following Python expression?4 + 3 % 5
2
7
1
4
Is Python code compiled or interpreted?
Python code is both compiled and interpreted
Python code is neither compiled nor interpreted
Python code is only interpreted
Python code is only compiled
Which keyword is used for function in Python language?
Function
Fun
Define
def
Which of the following is the correct extension of the Python file?
.pl
.py
.pl
p
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)
list
tuple
string
int
What is the output of the following code snippet ?
2 2
error
2 3
None of the above
Which one of the following is the correct output of the following code snippet?
[1] [2] [3]
[1, 2, 3]
[1] [1, 2] [1, 2, 3]
None of the above
What is the output of the following code snippet ?
1
2
error
None of the above
Which of these is the definition for packages in Python
A set of programs making use of Python modules
A set of main modules
A folder of python modules
A set of programs making use of Python modules
Which one is not a legal variable name?
my-var
Myvar
my_var
_myvar
How do you create a variable with the floating number 2.8?
X=2.8
X=float(2.8)
Both are correct
Only A is correct
What is the correct way to create a function in Python?
function myfunction()
def myfunction()
create myFunction()
both a and b are correct
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)) ?
I ca
I can
I 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?
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)) ?
I can't
I can
I can't write one song that's not about you
Error
Given a text file opened as file_in, which of the following will print the entire file? (Select ALL that apply.)
print(file_in.read())
print(file_in)
lines = file_in.readlines()
for line in lines:
print(line)
lines = file_in.readlines()
for i in range(len(lines)):
print(lines)
for line in file_in:
print(line)
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]) ?
Can't drink without thinkin' about you
I can't write one song that's not about you
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?
I
What is the correct way to open the file file_in.txt for reading?
open('file_in.txt', 'r')
open('file_in.txt','r+')
open('file_in.txt','w')
open('file_in.txt','a')
What is the best way to read an entire file into a single string?
.read()
.readline()
.readlines()
.read(all)
What is the best way to read an entire file into a list of strings?
.read()
.readline()
.readlines()
.read(all)
What is the best way to read the first line of a file?
.read()
.readline()
.readlines()
.read(all)
When reading text files, what are the hidden characters typically at the end of each line?
\n
#
@
»
This command will open MyFile = open('MyText.txt', 'r')
To read the file
To append to the file
To write to the file
If I want to add to the end of a text file, I should:
MyCipher = open("MyCipher.txt", "w")
MyCipher = open("MyCipher.txt", "a")
MyCipher = open("MyCipher.txt", "w+")
MyCipher = open("MyCipher.txt", "s")
Which of these will generate an error?
MyCipher.write("writing")
MyCipher..write(writing)
MyCipher.write("writing",writing)
MyCipher.readline()
How do you close the file?
MyFile = close()
MyFile.close
close = MyFile()
MyFile.close()
Which is the best type of loop to write a file in?
While loop
for loop
repeat loop
What command can you use to separate parts of a line?
separate()
split()
divide()
cut()
Choose the correct statement to close a file stream named myfile:
myfile.terminate()
myfile.end()
myfile.close()
myfile.remove()
How do you read a single line of text from a file?
f.readline()
f.getline()
f.openline()
f.pullline()
What command should you call when you have finished using a text file?
f.close()
f.done()
f.unopen()
f.finished()
