d = {'a': 3, 'b': 2, 'c': 1}
for k in sorted(list(d.keys())):
print(k, d[k], sep='', end='')
What is the output of the following code?
Answer:
a3b2c1
1c2b3a
a1b2c3
None of the above - it outputs something else
None of the above - there is an error in this code
a3b2c1
Write the function name (not including the brackets/parameters) for the function that Python calls when you print an object or convert it to a str.
__str__
What is the result of the following expression?
9 % 2
1
What is the purpose/benefit of the with statement when dealing with files?
Answers:
Automatically closes the file
Allows you to open multiple files at once
Allows you to open a file for both reading and writing simultaneously
Automatically performs error handling
Automatically closes the file
Which one of the following clauses in a try-except suite specifies a block to run only when no exceptions have occurred?
Answers:
finally
pass
noexcept
else
do_or_do_not_there_is_no_try
else
d = {'a': 123, 'b': 234}
Which one of the following correctly increments by 1 the value referred to by the key ‘a’?
Answers:
d.values('a') += 1
d['a'] += 1
d.keys('a') += 1
d.get('a') += 1
d['a'] += 1
class Thing:
def __init__(self, a, b):
self.a = a
self.b = b
def change(self, b):
self.a += b
What is the output of the following code:
Answers:
7 6
7 2
5 6
11 6
7 6
What is the result of the following expression?
(3 >= 3) or (5 < 7) and (9 != 5)
True
Given the line
from random import randint
“random” is a:
Answers:
class
function
module
package
module
Which one of the following data structures is the most appropriate choice for storing a collection of 2D points?
Answers:
A class
A dictionary
Two lists
A list of tuples
A list of tuples
d = {'a': 1, 'b': 2, 'c': 3}
print(d.get('a'))
Write the output of the following code:
1
All built-in types in Python (e.g. int, str, list...) are classes
True
Which method does a programmer need to define in a class in order to specify the behaviour for when the == operator is used with objects of that class?
__eq__
Which one of the following is the statement used by a programmer to create an exception?
Answers:
except
Exception
raise
error
raise
What is the result of the following expression?
sum([len(x) for x in ['one', 'two', 'three']])
11
d = {'a': 1, 'b': 2, 'c': 3}
print(d.get(2))
Write the output of the following code:
None
Which one of the following terms can best be described as:
hiding design details to make the program clearer and more easily modified later... binding together data and functions that manipulate the data
Encapsulation
Which one of the following is the correct class header for a Student class that extends (inherits from) the Person class?
Answers:
class Student(Person):
class Student extends Person:
class (Student, Person):
class Student(Person):
Which ones (could be 0 or more) of the following are mutable data types in Python?
Answers:
integer (int)
list
dictionary (dict)
tuple
string (str)
list
dict
Which one of the following Git commands is used to update a local repository from a remote repository?
pull
Write the function name (not including the brackets/parameters) for the standard Python class constructor
__init__
A function defined inside a class is called a...
method
Which one of the following lines of code is equivalent to:
my_instance.my_method(value)
where my_instance is an instance of the class MyClass
Answers:
MyClass.my_method(my_instance, value)
MyClass.my_method(self.my_instance, value)
MyClass(my_method(my_instance, value))
my_method(self, my_instance, value)
MyClass.my_method(my_instance, value)
Which one of the following is the common alternative to the "Look Before You Leap" philosophy of exception handling?
Answers:
Innocent Until Proven Guilty
Fix it in Production
Leap Before You Look
Easier to Ask Forgiveness than Permission
Easier to Ask Forgiveness than Permission
values = (2, 4, 6)
for x, y in enumerate(values):
print("{0}:{1:.0f}".format(x, y), end=" ")
0:2 1:4 2:6
0:2.0 1:4.0 2:6.0
2:0 4:1 6:2
1:2.0 2:4.0 3:6.0
0:2 1:4 2:6