Python Quiz Answers

1.  What is an iterator in Python?

A)  A stream of data that is manipulated or interacted with as an object.
B)  A function that returns a namespace.
C)  A module of nested objects.
D)  A function that returns packages.

Answer: A. For more information, see the Python.org glossary.

2.  Which module in Python allows you to translate strings to and from binary formats?

A)  Marshal
B)  Shelve
C)  Pickle
D)  DMAC

Answer: A. For more information, see the official Python.org site here.

3.  Which of the following is a Rich Internet Application toolkit?

A) binascii
B) shelve
C) pyjamas
D) alglib

Answer:  C. For more information, see page 362 of Programming Python: Powerful Object-Oriented Programming(4th Edition) by Mark Lutz, published by O'Reilly in 2011.

4.  Which of the following provides an interface to AWS?

A) binascii
B) botocore
C) sndhdr
D) xdrlib

Answer: B.  For more information, see this external link.

5.  How does Python store an error?

A)  As a static variable inside the interpreter
B)  It normally uses an operating system environmental variable.  But if there were too many arguments, it writes to a buffer outside the interpreter.
C)  Inside a pseudo class file (.pyc) in /tmp/
D)  It raises the error to the exception logger outside of the interpreter
E)  In the internal sqlite database

Answer: A. For more information, see the official Python explanation.

6.  What does the yield keyword do in Python?

A)  It is a CPython mechanism to synchronize threads.
B)  A reserved word to control the flow of execution to support conditional logic with Python generators.
C)  A reserved word that pauses a function from parsing named tuples.
D)  It sets null points to evaluate as zeroes in arithmetic operations.

Answer: B.  For more information, see the Python.org glossary.

7.  Which of the following can allow for non-destructive testing of whether an exception has been set?

A)  pyyaml and pypy modules
B)  PyErr_Config()
C)  PyErr_Clear()
D)  PyErr_Occurred()

Answer: D. For more information, see this official Python document for an explanation.

8.  If you are receiving an error with a Python program that attempts to connect to a network resource with SSL, there is a way to avoid an error.  This error is '"SSL: CERTIFICATE_VERIFY_FAILED" Error'
One workaround involves these two lines of Python code:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

Which of the following most nearly addresses the above two-line solution:

A)  The solution makes the Python program more secure.
B)  The solution makes use of the sys library.
C)  The solution is very inadvisable.
D)  The solution would never work.

Answer:  C.  For more information, see this posting.

9.  What is a generator in Python?

A)  Any class that is a factory design pattern.
B)  A reserved word that is a parent class of all iterators in the program.
C)  An object that controls the CPU of the Python Just-In-Time compiler.
D)  A function that has a yield statement and returns an iterator.

Answer:  D.  For more information, see this external site.

10.  Which two of the following add to thread safety in Python (so different threads do not modify data to have unexpected results)?

A)  metaclass
B)  global interpreter lock
C)  trash collection
D)  overwatch
E)  duck-typing
F)  lbyl
G)  lambda
H)  list comprehension
I)  Pythonic sequence
J)  object slice

Answer:  A) and B)  For more information, see this python.org link.

11.  When trying to install pycrypto you run this:

python setup.py build

and you receive an error like this "warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath." what does it mean?

A) You cannot proceed with installing pycrypto; the installation has been aborted.
B) You can proceed with installing pycrypto; the installation may still work.
C) You have a Python Fabric vulnerability
D) A German edition of pycrypto was already installed and you may or may not be able to proceed.

Answer: B.  You may move on to "python setup.py install".  To be on the safe side, you may want to investigate why the error is happening.  Here is a related external link.

12.  How do you find the version of Tornado (python-tornado) on a RedHat server?

A)  python
>>> print python-tornado.version_info

B)  python
>>> import tornado
>>> print tornado.version_info

C)  which python-tornado

D)  python-tornado --version

Answer:  B

13.  What does GIL stand for?

A)  Gears Interpreting Language
B)  Good Invention Language
C)  Global Interpreter Lock
D)  Generate Interprocess Loquitur
E)  Global Instant Lookup

Answer:  C. Source is page 1564 of Programming Python: Powerful Object-Oriented Programming by Mark Lutz.  Published by O'Reilly.

14.  Where does the "kw" come from or mean in the **kwargs you see in Python error messages and/or code?

A)  kilowatt (wildcard kilowatt arguments)
B)  You hear stars on the radio.  Radio stations on the West Coast of the U.S. traditionally have call signs that start with the letter "k," and radio stations on the East Coast have call signs that start with the letter "w."  For a radio button to appear in a GUI written in Python, there needs to be arguments. 
C)  keyword
D)  keep working

Answer: C.  **args is for iterable objects (e.g., a list).  **kwargs is for key-worded pairs (e.g., a dictionary).  For more information see this external link.

15. How many different directory locations does the "import" command look to for a .py file when invoked?

A) 0
B) 1
C) 2
D) Often several but it depends

Answer: D. For more information, see this posting.

16. True or False? A function in Python has to have a return statement.

Answer: False. If you want more information, see this posting.

17. What is a common way (as of 2019) to start a new thread in Python assuming the proper module has been imported? Choose two.

A) nameofthread = Thread(nameoffunction)
B) nameofthread = start_new(nameoffucntion)
C) thread.start_new(nameoffunction)
D) thread.start_new_thread(nameoffunction)
E) nameofthread = newthread()

Answer: A and D.

We found this works:

from threading import Thread

def contint():
  print("Hello!")

if __name__ == "__main__":
    thread = Thread(contint())
    thread.start()
    thread.join()
    print("thread finished...exiting")

We found this works:

import _thread as thread 
def contint():
   print("Hello!")
if name == "main":
     foobar = thread.start_new_thread(contint, () )
     print("thread finished…exiting")

18. What percentage of data types and classes in Python are objects?

A) 0%
B) 25%
C) 50%
D) 75%
E) 100%

Answer: E. The source of this is here. n.b. Python is not a purely object-oriented language because of the way it handles encapsulation. If you want to read more about this, see this Quora answer or this analyticbridge website page.

19. What is a decorator in Python?

A) A function that uses a function as a parameter and returns a function with a special @ syntax.
B) A function that uses a function as a parameter and returns a function with a special ^ syntax.
C) An anonymous function that passes along parameters.
D) A library module that enhances GUI python programming.
E) A library module that obfuscates system functions programming.

Answer: A. See this posting for more information.

20. What is the recommended way of calling subprocesses in Python 3.5 or higher?

A) Using the os.exec function.
B) Using the os.spwan function.
C) Using the subprocess.open function.
D) Using the subprocess.run() function.

Answer: D. Source: https://docs.python.org/3/library/subprocess.html
Here is an example:

python
>>> from subprocess import PIPE
>>> subprocess.run(["ls", "-l", "/dev/null"], stdout=PIPE, stderr=PIPE)

21. Without "import re" using Python 3, can you use the .find() method to match a pattern in a string?

A) Yes
B) No
C) It depends

Answer: A. Try this program as an example:

string1 = "abcdefghijklmnopqrstuvwxyz"
number_pattern_found = string1.find("jkl")
print(number_pattern_found)

22. Which of the following is true about Python class names?

A) It is mandatory that a class name's first letter be uppercase.
B) It is mandatory that a class name's first letter be lowercase.
C) It is recommended that a class name's first letter be uppercase.
D) It is recommended that a class name's first letter be lowercase.

Answer: C. Source: https://www.python.org/dev/peps/pep-0008/#class-names

23. What is a way to implement a hash table with Python?

A) a nested list
B) a list of tuples
C) a dictionary
D) a nested dictionary
E) all of the above
F) none of the above

Answer: E.
For A, the source is https://www.geeksforgeeks.org/implementation-of-hashing-with-chaining-in-python/
For B and C, the source is http://blog.chapagain.com.np/hash-table-implementation-in-python-data-structures-algorithms/
This helps explain B:
https://coderbook.com/@marcus/how-to-create-a-hash-table-from-scratch-in-python/
This explains D: https://www.edureka.co/blog/hash-tables-and-hashmaps-in-python/

24. To use a class in Python, what is necessary?

A) The init() function needs to be present.
B) An encapsulated function with the syntax of two leading two underscores __
C) Have an indented block underneath the class definition.
D) Use the class keyword.

Answer: C.
The init() function is not strictly necessary. We tested it out. You may want to read this: https://www.w3schools.com/python/python_classes.asp

It is not necessary to use encapsulation. To learn how to use it (but it is optional), see this posting (internal for encapsulation). The creator of this quiz tested that you need more than the class keyword. With nothing underneath a class definition, you may see this error when you try to run the Python program: "IndentationError: expected an indented block". The indented block beneath the class definition can be any (or almost any) valid Python statement. You can run a program such as this (and it will not fail):

class Example:
  a = "nothing"

z = Example()

25. Items in a Python set {} have which of the following traits? Choose all that apply.

A) Unindexed
B) Ordered
C) Immutable
D) Potentially a duplicate of another item in a set

Answer: A and C. Source for each one: https://www.w3schools.com/python/python_sets.asp

26. Where are variables stored? Choose all that apply.

A) In the heap if they are local variables
B) In the heap if they are global variables
C) In the stack if they are local variables
D) In the stack if they are global variables

Answer: B and C. Source: https://www.geeksforgeeks.org/how-are-variables-stored-in-python-stack-or-heap/

27. Of the following, when should there be space after an equals sign "="?

A) When there is an equivalence test
B) When there is an initialization of an unannotated function parameter
C) When there is a variable assignment involving a reserved word in Python
D) All of the above
E) None of the above

Answer: A. Source: https://www.python.org/dev/peps/pep-0008/#other-recommendations
B and C are instances when there should be no space after the equals sign.

Leave a comment

Your email address will not be published. Required fields are marked *