Python Quiz Questions
Two conditions that must both be true can be combined with ____ in Python.
"and"
What is the output of the following struct.pack('Q', 1)
'\x01\x00\x00\x00\x00\x00\x00\x00'
What are the results of the following? struct.unpack('HHHH', '\x01\x00\x00\x00\x00\x00\x00\x00')
(1, 0, 0, 0)
If a = (1, 2, 3) then b = 2 * a print (b) produces
(1, 2, 3, 1, 2, 3)
If a = (1, 2, 3) b = (4, 5, 6) then c = a + b print(c) produces
(1, 2, 3, 4, 5, 6)
What is printed by the following script? a = 2 b = 3 print (a/b)
0
The code: for i in range(0, 4): print(i) produces
0 1 2 3
The code: count = 0 while count < 5: print(count) count += 1 produces
0 1 2 3 4
What does print("%08.2f" % 10.50) produce?
00010.50
The format specifier "B" specifies how many bytes of data?
1
The code: for x in range(0, 10): if x % 2 == 0: continue print(x) produces
1 3 5 7 9
What does the following code produce? def myfunc(a, b, c='c'): print(a, b, c) myfunc(1, 'of')
1 of c
What does print("%.2f" % 10.50) produce?
10.50
print( "2" * 3) produces
222
If a = 'Bob' b = 'Smith' then print (a+b) produces
BobSmith
Additional libraries must be installed to read and write files.
False
Dictionaries can store complex types, but every item must be of the same type.
False
If x = 2 then what does print(x >3) produce
False
Python cannot be used to read binary files.
False
Python does not allow you to create your own exceptions.
False
Python does not support inheritance.
False
Python is a strongly typed language.
False
Python supports private class instance variables.
False
Regular expressions are a feature unique to Python (not present in other scripting languages).
False
There is a pack/unpack format specifier for the 48-bit (6-byte) numbers used in NTFS.
False
If a = 'Bob' b = 'Smith' then print("Hello %s %s!" % (a, b)) produces
Hello Bob Smith!
What does print("Hello %s, I see you are %s years old" % ('Bob', 9)) produce?
Hello Bob, I see you are 9 years old
What does print("%5s" % 'Smith and Wesson') produce?
Smith and Wesson
The code: x = ['Bob', 'Doug'] print("The Mackenzie brothers are: ") for i in x: print(i) produces
The Mackenzie brothers are: Bob Doug
If x = 'There are %d types of people in the world.' % 10 then print(x) produces
There are 10 types of people in the world.
Exceptions are objects in Python.
True
Files opened for writing are automatically truncated if they exist.
True
Python 3 is intentionally incompatible with Python 2. (T or F)
True
Python except blocks can handle more than one type of exception.
True
Python supports class variables that are shared with all instances of a particular class.
True
Python supports multiple inheritance.
True
The following code will create a second reference to an instance of a class: a = MyClass() b = a
True
The regular expression '([0-9]{1,3}\.){3}[0-9]{1,3}' will match IP addresses.
True
If a = "Your change is %d" % 10.50 then print(a) produces
Your change is 10
If a = (1, 2) b = [ ] then b[0:] = a[0:] b.append(3) print(b) produces
[1, 2, 3]
If a = { 'First' : 'Bob' , 'Last' : 'Smith' } b = { 'First' : 'Sue' , 'Last' : 'Storm' } then c = [a, b] print(c) produces
[{'Last': 'Smith', 'First': 'Bob'}, {'Last': 'Storm', 'First': 'Sue'}]
The method used to create a new object of a particular class type is ____.
__init__
What file must be added to a directory in order for its contents to be treated as a package by Python?
__init__.py
What does "h" specify in pack and unpack?
a 2-byte signed integer
The "Q" format specifier corresponds to what?
a 8-byte unsigned integer
Python is named after
a British comedy group
What does the following regular expression match? \.
a period
What is the difference between a list and a tuple?
a tuple is immutable and a list is not
What is a regular expression?
a way of specifying matching patterns
To install Python on Windows
a) download and run an installer from python.org b) download the Python source code from python.org, then download and install a compiler, then build and install Python Answer) either a or b
Function parameters may be passed
a) in a specified order b) using names given in the declaration answer) either of the two previous methods can be used
What is the meaning of the following regular expression? abc{3}
abccc
What matches the following regular expression? a*
absolutely anything (including nothing at all)
How much of a file is read by the file read function?
all of it
If a = 'Bob' b = 2 then print(a*b) produces
an error
If a = 'Bob' b = 2 then print(a+b) produces
an error
If a = { 'First' : 'Bob', 'Last' : 'Smith' } then print(a['Middle']) produces
an error
The following code f = open('myfile.txt', 'w') f.write('foo') f.write('bar') print(f.read()) produces
an error
What does the following code produce? def myfunc(a='a', b, c=4): print(a, b, c) myfunc('yes', 'you')
an error
if a = (1, 2, 3) b = (4, 5, 6) then c = a * b print(c) produces
an error
What does "I" mean in an unpack or pack format specifier?
an unsigned 4-byte integer
In a regular expression a period matches
any character except a newline by default
What matches the following regular expression? [0-9a-fA-F]
any single hexadecimal digit
Given the following code in my script from foo import bar how would I call the gamma function in the bar module?
bar.gamma
Python can be used as
both a scripting and programming language
Which of the following is a way to tell if a file is open?
check its opened attribute
What keyword is used to define a class?
class
Which of the following is the correct way to extend a class?
class derived(base):
Function declarations in Python begin with what keyword?
def
What is the difference between a list and a dictionary?
dictionaries may have non-integer keys
What function is used to list a module's functions?
dir
What does print('%x' % 255) produce?
ff
Given the following code appears in my script import foo.bar how would the gamma function in the bar module be called in my script?
foo.bar.gamma
What is the purpose of a finally block?
for cleanup code that should always run whether or not there is an exception
The file seek function is used to
go to another position in a file
What command is used to load a Python module?
import
To install Python on Linux
is likely unnecessary if you want a 2.x version since most versions of Linux pre-install Python
The exists function
is part of the sys module
What is the meaning of a function argument preceded by a "*"?
it represents a variable number of parameters that are passed in as a tuple
What is the meaning of "<" for pack and unpack?
it specifies little endian byte order
Given the following code, how would you create a new dog object? class Dog: def __init__(self, name='Fido'): self.name=name def bark(): print('Woof')
myDog = Dog('Betsy')
What does the following code do? f = open('myfile.txt')
opens a file for reading only
Two conditions either of which must be true can be combined with ____ in Python.
or
print('Bob' 'Smith') produces the same output as
print('Bob'+'Smith')
What does the file tell function do?
reports the position in a file where the next file operation will occur
The ___ keyword is used by a class to refer to a particular instance of that class.
self
Code that might cause an exception to be raised
should be contained in a try block
What is the "in" operator used for in Python?
to determine if an item is in a list, tuple, or other iterable container
What is the purpose of a "break" statement in Python?
to exit the innermost loop
What is the purpose of struct.pack?
to pack binary data into a string
What is the difference between upper and lower case format specifiers for pack and unpack?
upper case specifiers are unsigned and lower case specifiers are signed
If x = 25 then what does if x>=21: print("yes, you may have that beer") else: print("sorry buddy, you are not old enough") produce?
yes, you may have that beer
In a regular expression the asterisks (*) specifies
zero or more repetitions of the preceding RE
If a = { } then a['First'] = 'Bob' a['Last'] = 'Smith' print(a) produces
{'Last': 'Smith', 'First': 'Bob'}