Python Concepts/Quizzes/Basic data types

From Wikiversity
Jump to navigation Jump to search
Attention
Note: This quiz seems to have several mistakes. You may get some questions marked as wrong, although they could right!

1 With the following Python code

>>> "Hello" + 'World' + """!"""

the following applies:

True False
Single, double and 3 quotation marks all define a string
3 strings are concatenated
3 double quotation marks enable you to write a string spanning multiple lines.
There is an error

2 The following Python code

>>> [1, 2, 'stock', 4]

constructs a

Set
List
An error as there cannot be different data types in such a data structure
Dictionary

3 The following Python code

>>> colors = ('red', 'yellow', 'blue', 'black')

is a

Set
Dictionary
List
Tuple

4 The following Python code

>>> {'stock': 'NVO', 'wikipedia': 'Novo_Nordisk'}

is a

Set
Array
Named array
Dictionary

5 The following Python code

>>> a = [[4, 5], [6, 7], [8, 9]]

may result in

a[1]   type(a)   a[2][2]   a(2,2)
<type 'list'>
4
[6, 7]
[4, 5]
Error
Other result

6 The following Python code

>>> c = {'stock': 'NVO', 'wikipedia': 'Novo_Nordisk'}

may result in

c.keys() c.items() c.values()
['wikipedia', 'stock']
['Novo_Nordisk', 'NVO']
[('wikipedia', 'Novo_Nordisk'), ('stock', 'NVO')]
{'stock', 'wikipedia'}

7 The following Python code

>>> a = range(5)
>>> b = a
>>> a[3] = 77

results in

b = [0, 1, 2, 3, 4]
b = [1, 2, 3, 4, 5]
b = [0, 1, 2, 3, 4, 5]
b = [0, 1, 2, 77, 4]
b = [1, 2, 3, 77, 5]
b = [0, 1, 2, 77, 4, 5]
b = [0, 1, 77, 3, 4]
b = [1, 2, 77, 4, 5]
b = [0, 1, 77, 3, 4, 5]

8 The following Python code

>>> 'Result: ' + 23

results in

A string where the arguments are concatenated: 'Result: 23'
31
An error as you cannot append an integer on a string
A list: ['Results: ', 23]

9 To append a string and an integer

Ok Errors
'Result: ' + 23
'Result: ' + str(23)
'Result: %d' % 23
sprintf('Result: %d', 23)
'Result: {result}'.format(**{'result': 23})

10 Consider the following data structure definition:

>>> names = {1: 'Anders', 2: "Bente", 3: "Charles"}

True False
You can only have strings as keys in Python dictionaries
You can get the "Bente" value with names[2]
You can get the "Bente" value with names{2}
If you do "for name in names: print(name)" the names will be printed
If you do "for key, value in names: print(value)" the names will be printed
If you do "for key, value in names.items(): print(value)" the names will be printed