Worksheetspython_22
Total questions: 71
Worksheet time: 34mins
Which of the following is used in Python to calculate ten squared?
print(Hello world!)
print(3+4)
print("3+4")
print("Hello" + "world!")
Which has integer data type?
Phone number
Zip code
HN
Number of students in the class
Look at the following code, what will it generate
1,2,3,4,5,6,7,8,9,10,11,12
0,1,2,3,4,5,6,7,8,9,10,11,12
1,2,3,4,5,6,7,8,9,10,11,12,13
0,1,2,3,4,5,6,7,8,9,10,11,12,13
What does the following program display?
1,2,3,4
1,2,3,4,5
counter,counter,counter,counter,counter
0,1,2,3,4,5
1,2,3,4
When do you know when you come to the end of the loop?
Its states End loop
The command End is given
The indentation stops
what is the name of the variable used in the following code?
counter
for
#
Which of the following is an acceptable variable name?
no-text
8text
no_text
no_text$
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
One of the following functions is not used with Python String
Split
Remove
Replace
Choose the correct syntax of the split function:
x.split[]
split(x)
x.split(",", 3)
X.SPLIT(",", 3)
Choose the correct syntax of the replace function:
x.Replace(x)
x.replace(y)
x.replace(3,x)
x.replace("j","3")
Choose the correct code to print line of 10 stars using repetition operation:
print("**********")
print("*"*10)
print("*"^10)
print("*"+10)
The correct syntax of the len function for string x:
x.len()
len[x]
LEN[]
len(x)
The correct syntax for converting the string x to uppercase is :
X.upper()
upper(x)
x.UPPER[ ]
x.upper()
The correct syntax for converting the string myname to lowercase is :
myNAME.lower[ ]
myname.lower( )
myname.upper( )
myname_lower(12 )
To remove the spaces present at start and end of the string, you can use
split()
len()
strip()
remove()
Let x= "Python", the output of print(x.upper()) is
python
PYTHON
_PYTHON
"PYTHON"
Let x= "java", the output of Print(x.replace("j","L") ) is
LAVA
Error
lava
Lava
Let x= "python is a high level language", the output of print(x[5].replace("n","*")) is
pytho* is a high level language
python is a high level language
*
Error
Let x= "python is a high level language", the output of print(len(x[-1])) is
1
31
Error
25
Let x= "Python is a high level language", the output of print(x.replace(x[-3],"3")) is
Error
Python is 3 high level l3ngu3ge
Python is a high l3v3l languag3
Python is a high level language
Let x= "Python is a high level language", the output of print(x[7].upper()) is
Python is a high level language
PYTHON IS A HIGH LEVEL LANGUAGE
I
S
Let x= "Python is a high level language", the output of print(x[2].upper()+x[3].lower()+x[-1]) is
THE
The
ThE
the
Let x= "Python is a high level language", the output of print(x[-4].upper()+x[8]+x[-1]) is
Use
Age
USE
Error
Choose the correct python program that has the output 6
print(len("Python"))
Print(len("Python"))
x= "Python"
print(x.replace("Python","6"))
Both a and c
Let x= "Python is a high level language", the output of
print(x.split("i", 2))
['Pyth', 'n is a high level language']
['Python is ', ' high level l', 'nguage']
['Python ', 's a h', 'gh level language']
Error
Let x= "Python", the output of
print(x[0])
print(x[1])
print(x[2])
PYT
P
y
t
T
h
o
n
oh
Let x= "Python", the output of
print(x[0])
print(x[-1])
print(x[-2])
P
N
O
p
O
n
t
h
y
P
n
o
What is a list in python?
A collection of variables
A collection of items assigned to a single variable
A statement that cant be changed
A Christmas list
Which of these codes is correct for creating a list of numbers?
num = ('10','29','37','109')
num = ['10','29','37','109']
num == ('10','29','37','109')
num = ['10 29 37 109']
What is the index number for 'Spain'?
['England','Brazil','Spain','France']
0
1
2
3
How do i remove something from a list?
variable.append()
variable.delete()
variable.remove()
variable=(new variable)
Which code would i use to add an item to a list?
variable.add()
variable.append()
append.variable()
add.variable()
What is used to separate elements in a list?
Bracket
Comma
Full Stop
Space
Why is using lists beneficial?
Shorter Code
More Accurate
It takes less time for the program to run
Its not beneficial
variable.sort() will do what?
Sort the list into alphabetical order
Sort the list into reverse order
Create a new list
Error
What brackets are used to create a list?
()
[]
What sort of loop is this?
Count Controlled
Condition Controlled
What does the '#' allow you to do?
Repeat code
Comment on code
Print code
End code
A condition controlled loop is...
A while loop
A for loop
Which of these operators means EQUAL TO?
'='
'=>'
'<='
'=='
What will this code do?
Print Numbers 1-11
Print Numbers 1-10
Print Numbers 2-10
Print Error
