Python Essentials 2 (PE2) Course Final Test Exam Answers

Python Essentials 2 (PE2) Course Final Exam Answers

1. Knowing that a function named fun() resides in a module named mod, and was imported using the following statement:

from mod import fun

Choose the right way to invoke the fun() function:

  • mod:fun()
  • mod::fun()
  • fun()
  • mod.fun()
Explanation:

The following import syntax: from module_name import entity allows you to import specific entities into the main namespace. The name of the imported entity is accessible without qualification, in which case the fun() function can be called using regular function call syntax.

2. What output will appear after running the following snippet?

import math
print(dir(math))
  • A list of all the entities residing in the math module
  • The number of all the entities residing in the math module
  • An error message
  • A string containing the fully qualified name of the module
Explanation:

The dir() function is a built-in Python function, which returns the list of names of all the attributes and methods of a given object, in this case, the math module.

3. The compiled Python bytecode is stored in files which have their names ending with:

  • py
  • pyc
  • pc
  • pyb
Explanation:

During the first import of a module, Python translates its source code into a semi-compiled format stored inside the pyc files, and deploys these files into the __pycache__ directory located in the module’s home directory.

4. Assuming that the following three files: a.py, b.py, and c.py reside in the same directory, what will be the output produced after running the c.py file?

# file a.py
print("a", end='')
 
# file b.py
import a
print("b", end='')
 
# file c.py
print("c", end='')
import a
import b
  • cba
  • cab
  • bc
  • bac
Explanation:

Executing the file c.py results in the following:

  • the print(“c”, end=”) function prints c to the screen;
  • the import a statement imports the print(“a”, end=”) function, which prints a to the screen;
  • the import b statement imports the print(“b”, end=”) function, which prints b to the screen;
  • The file b.py also contains the import a statement, but this module has already been imported into the mainspace. When a particular module is imported, its contents are executed once only. Note that the order of imports does not matter here.

5. What will be the output of the following code, located in the p.py file?

print(__name__)
  • main
  • __p.py__
  • p.py
  • __main__
Explanation:

The __name__ variable is a built-in variable, which evaluates to the name of the current module being executed in the form of a string. Now, because the file is run directly, not imported, its __name__ variable is set to __main__.

6. The following statement:

from a.b import c

causes the import of:

  • entity a from module b from package c
  • entity b from module a from package c
  • entity c from module a from package b
  • entity c from module b from package a
Explanation:

When using the following syntax: from a.b import c, c identifies the entity or function being imported, b identifies the module (source file), and a identifies the package (folder).

7. If there is more than one except: branches after the try: clause, we can say that:

  • one or more except: blocks will be executed
  • exactly one except: block will be executed
  • none of the except: blocks will be executed
  • not more than one except: block will be executed
Explanation:

At most, one of the except branches is executed – none of the except branches is performed when the raised exception doesn’t match the specified exceptions, or when no exception is raised at all.

8. What is the expected result of the following snippet?

try:
    raise Exception
except BaseException:
    print("a")
except Exception:
    print("b")
except:
    print("c")
  • a
  • b
  • 1
  • an error message
Explanation:

Because the raise Exception statement in the try branch raises an exception on demand, the exception is handled by one of the except branches.

Even though Exception is a more concrete exception class than BaseException, because the order of branches matters, the exception is handled by the first matching except branch, which is except BaseException. The code in the example is an example of bad practice – you should not put more general exceptions before more concrete ones.

9. The following line of code:

for line in open('text.txt', 'rt'):
  • is invalid because open returns a non-iterable object
  • is invalid because open returns nothing
  • is valid because open returns an iterable object
  • may be valid if line is a list
Explanation:

The open() method returns an iterable object, which can be used to iterate through all the file’s lines inside a for loop.

10. What is the expected result of the following snippet?

try:
    raise Exception
except:
    print("c")
except BaseException:
    print("a")
except Exception:
    print("b")
  • a
  • The code will cause a syntax error
  • 1
  • b
Explanation: Executing the code will result in a SyntaxError exception, because the default except branch must be the last except branch (in this example, it’s the first).

11. The following statement:

assert var != 0
  • has no effect
  • will stop the program when var == 0
  • will stop the program when var != 0
  • is erroneous
Explanation:

The assert keyword tests if a condition is true. If it is not, the program will raise an AssertionError exception.

12. The following code:

x = "\\"
print(len(x))
  • will print 2
  • will cause an error
  • will print 3
  • will print 1
Explanation: The string passed to the x variable contains the following sequence of characters: escape character, backslash, escape character, and backslash. Escape characters don’t count toward the length of a string.

13. The following code:

x = "\\\"
print(len(x))
  • will cause an error
  • will print 3
  • will print 1
  • will print 2
Explanation:

An attempt to execute the code will result in a SyntaxError and the following error message: EOL while scanning string literal. This means that the Python interpreter expects a particular character or set of characters before the end of the line, in this case the quotation marks, which are missing due to the use of the last escape character in the string.

14. The following code:

print(chr(ord('p') + 2))
  • will print r
  • will print s
  • will print t
  • will print q
Explanation:

The chr() function returns the character that represents the specified Unicode, while the ord() function returns the Unicode code from a given character (in this case, ‘p’ → 112).

Because the ord() function returns 112, the chr() function takes 114 as its argument (112 + 2114), and returns r, because the character represents the Unicode code 114.

15. The following code:

print(float("1.3"))
  • will print 1.3
  • will raise a ValueError exception
  • will print 1,3
  • will print 13
Explanation:

The float() function will successfully convert the string object “1.3” to a floating-point number, and print it to the console.

16. If the class constructor is declared in the following way:
Which one of the assignments is invalid?

class Class:
    def __init__(self, val=0):
        pass
  • object = Class(1)
  • object = Class()
  • object = Class(None)
  • object = Class(1, 2)
Explanation:

An attempt to create an object of class Class using the assignment object = Class(1, 2) will raise a TypeError exception, because a maximum of two arguments are accepted by the __init__ declaration, not three arguments (self, 1, and 2).

17. What is the expected output of the following code?

class A:
    def __init__(self, v=2):
        self.v = v
 
    def set(self, v=1):
        self.v += v
        return self.v
 
 
a = A()
b = a
b.set()
print(a.v)
  • 3
  • 1
  • 2
  • 0
Explanation:

Let’s analyze the code:

  • The A class constructor creates an instance variable named v equal to the default value passed to the constructor’s parameter v, which is 2.
  • The set method takes the v variable, and returns the value passed to it incremented by 1 (v = 2 + 1 = 3)
  • The a object is created from class A.
  • The b object is created: it becomes a reference to object a, and it is enriched with the set property – which updates the value held by the instance variable and shared by object a.
  • The updated value assigned to the instance variable v in object a is now printed to the console.

18. What is the expected output of the following code?

class A:
    A = 1
    def __init__(self):
        self.a = 0
 
 
print(hasattr(A, 'a'))
  • 1
  • 0
  • True
  • False
Explanation:

The hasattr() function can also operate on classes to find out if a class has a particular class variable available. In this case, class A has the class variable A, not a, which is why the function will return False.

19. What is the expected result of executing the following code?

class A:
    pass
 
 
class B(A):
    pass
 
 
class C(B):
    pass
 
 
print(issubclass(A, C))
  • The code will print 1
  • The code will print True
  • The code will raise an exception
  • The code will print False
Explanation:

The issubclass(Class1, Class2) function checks if Class1 is a subclass of Class2. Because class C is not a child of class A, the function returns False.

20. The sys.stderr stream is normally associated with:

  • a null device
  • the printer
  • the keyboard
  • the screen
Explanation:

The stderr stream (standard error output) is normally associated with the screen, pre-opened for writing, regarded as the primary place where the running program should send information on the errors encountered during its work.

21. What is the expected effect of running the following code?

class A:
    def __init__(self, v):
        self.__a = v + 1
 
 
a = A(0)
print(a.__a)
  • The code will print 1
  • The code will print 2
  • The code will raise an AttributeError exception
  • The code will print 0
Explanation:

An attempt to execute the code will result in the following error message: AttributeError: ‘A’ object has no attribute ‘__a’. Because __a is a private variable (note the two underscores), it cannot be accessed from outside the class.

22. What is the expected result of executing the following code?

class A:
    def __init__(self):
        pass
 
 
a = A(1)
print(hasattr(a, 'A'))
  • The code will raise an exception
  • The code will print False
  • The code will print 1
  • The code will print True
Explanation:

The code will raise a TypeError exception with the following error message: TypeError: __init__() takes 1 positional argument but 2 were given, because when object a of class A is being created, it calls the __init__ method with two arguments (self, and 1), while it requires only one (self).

23. What is the expected result of executing the following code?

class A:
    def a(self):
        print('a')
 
 
class B:
    def a(self):
        print('b')
 
 
class C(B, A):
    def a(self):
        self.a()
 
 
o = C()
o.c()
  • The code will print a
  • The code will print c
  • The code will print b
  • The code will raise an exception
Explanation:

The code will not raise an exception because it is consistent with the Method Resolution Order (MRO).

The code will output b to the console, because class C inherits from classes B and A respectively, and if any of the subclasses defines a method of the same name as existing in the superclass – in this case, class B defines the method def a(self): – the new name overrides any of the previous instances of the name (in this case, print(‘b’) overrides print(‘a’)). As a result, even though the c method defined in class C makes a reference to the a method defined in class A, the invocation o.c() results in printing b, not a to the screen.

24. What is the expected result of executing the following code?

try:
    raise Exception(1, 2, 3)
except Exception as e:
     print(len(e.args))
  • The code will print 3
  • The code will print 1
  • The code will print 2
  • The code will raise an unhandled exception
Explanation:

The except branch is executed and intercepts the object carrying information about the exception. The args property, which is a tuple, gathers all arguments passed to the Exception class constructor: 1, 2, and 3. The len function computes the length of the tuple (i.e. 3), which is printed to the console.

25. What is the expected result of executing the following code?

def my_fun(n):
    s = '+'
    for i in range(n):
        s += s
        yield s
 
 
for x in my_fun(2):
    print(x, end='')
  • The code will print +
  • The code will print ++
  • The code will print ++++++
  • The code will print +++
Explanation:

Let’s analyze this code snippet:

  • The code begins by executing the for x in fun(2): loop.
  • It uses the x variable to iterate through what fun(2): yields, and outputs it on the same line due to the end=” argument in the print function.
  • Once the fun(2): iterator is invoked, it initializes a string that contains the characters s = ’+’.
  • A for loop uses the range function with 2 as its argument, which was sent to the iterator when it was invoked. The for loop will iterate twice (the values for the i variable are 0 and 1).
  • In the first iteration, the s += s operation is applied to the string, and the new string s = ’++’ is yielded and outputted in the console.
  • In the second iteration, the s += s operation is again applied to the last produced string, and the new string s = ’++++’ is yielded and outputted in the console.
  • The final result shown in the console is ++++++ .

26. What is the expected result of executing the following code?

class I:
    def __init__(self):
        self.s = 'abc'
        self.i = 0
 
    def __iter__(self):
        return self
 
    def __next__(self):
        if self.i == len(self.s):
            raise StopIteration
        v = self.s[self.i]
        self.i += 1
        return v
 
 
for x in I():
    print(x, end='')
  • The code will print 210
  • The code will print cba
  • The code will print abc
  • The code will print 012
Explanation:

Let’s analyze this code snippet:

  • First, a for loop using the x variable iterates through the already defined I() generator.
  • The generator class initializes the self.s = ‘abc’ and self.i = 0 variables within the constructor.
  • The __next__ method checks if the value in i is equal to the length of s.
  • Since it is not, v is assigned the first character of s, which is a, i is incremented by one, v is returned and printed in the console.
  • The end=” argument in the print function will prevent new lines, so all the outputs will be shown on the same line.
  • The iteration continues for the remaining characters in s. The b and c characters are also printed in the console.
  • When the condition self.i == len(self.s) becomes true, a StopIteration is raised. This terminates the execution.

27. What is the expected result of executing the following code?

def o(p):
    def q():
        return '*' * p
    return q
 
 
r = o(1)
s = o(2)
print(r() + s())
  • The code will print **
  • The code will print ***
  • The code will print *
  • The code will print ****
Explanation:

This code snippet implements Closures, which are inner functions enclosed within an outer function. Let’s analyze this code snippet:

  • The code begins by creating a Closure named r by invoking the outer function o(1). The Closure contains the inner function q() that will return the result of the operation ‘*’ * 1, which is * .
  • A second Closure named s is created by invoking the outer function o(2). The Closure contains the inner function q() that will return the result of the operation ‘*’ * 2, which is ** .
  • Finally, both closures are invoked within a print function, concatenating their results. Therefore, the console output is *** .

28. If s is a stream opened in read mode, the following line:

q = s.read(1)

will read:

  • one character from the stream
  • one buffer from the stream
  • one line from the stream
  • one kilobyte from the stream
Explanation:

The read() method reads the number of characters/bytes from the file, and returns them as a string. It has the following syntax: filename.read(size), and is able to read the whole file at once, in which case the size argument defaults to -1.

29. Assuming that the open() invocation has gone successfully, the following snippet:

for x in open('file', 'rt'):
    print(x)

will:

  • read the whole file at once
  • cause an exception
  • read the file character by character
  • read the file line by line
Explanation:

The open() method returns an iterable object which can be used to iterate through all the file’s lines inside a for loop, and the stream closes itself automatically when it reaches the end of the file.

30. If you want to fill a byte array with data read in from a stream, which method can you use?

  • The readfrom() method
  • The readbytes() method
  • The readinto() method
  • The read() method
Explanation:

The readinto(bytearray) reads the bytes from the file and fills the bytearray with them.

31. Which of the following commands would you use to check pip’s version? (Select two answers)

  • pip-version
  • pip –version
  • pip3 –version
  • pip version
Explanation:

To check the pip version, one the following commands can be used: pip –version, or pip3 –version.

32. Which pip command would you use to uninstall a previously installed package?

  • pip delete packagename
  • pip uninstall packagename
  • pip –uninstall packagename
  • pip –remove packagename
Explanation:

The command pip uninstall packagename uninstalls a previously installed package.

33. Look at the following code:

numbers = [0, 2, 7, 9, 10]
# Insert line of code here.
print(list(foo))

Which line would you insert in order for the program to produce the expected output?

Output[0, 2, 7, 9, 10]
  • foo = filter(lambda num: num ** 2, numbers)
  • foo = map(lambda num: num ** 2, numbers)
  • foo = lambda num: num ** 2, numbers
  • foo = lambda num: num * 2, numbers)
Explanation:

In order to obtain the expected output list, the map() function used. The map(function, list) function creates a copy of the list, and applies the function function to all of its elements, returning a generator that provides the new contents element by element.

In this example, the map() function creates a copy of the numbers list, and applies the lambda num: num ** 2 function to all the elements of the list, which results in each element of the list being raised to the power of two. Then, the map() function returns a generator which provides the new contents, and assigns it to the foo object.

34. Look at the following code:

numbers = [i*i for i in range(5)]
# Insert line of code here.
print(foo)

Which line would you insert in order for the program to produce the expected output?

Output[1, 9]
  • foo = list(filter(lambda x: x % 2, numbers))
  • foo = list(map(lambda x: x // 2, numbers))
  • foo = list(filter(lambda x: x / 2, numbers))
  • foo = list(map(lambda x: x % 2, numbers))
Explanation:

In order to obtain the expected output, the filter() function is used. The filter(function, list) function creates a copy of the list elements which cause the function function to return True. The filter() function’s result is a generator providing the new contents element by element.

Let’s analyze the code:

  • The following list is created with the use of a list comprehension mechanism: [0, 1, 4, 9, 16].
  • After inserting the correct line, that is foo = list(filter(lambda x: x % 2, numbers)), the lambda function lambda x: x % 2 and the numbers list are provided as the filter() function arguments. The lambda function performs the operation x: x % 2 on each element of the numbers list, and the filter() function filters out the elements that evaluate to 0, that is, False (0, 4, 16), and makes a copy of the elements that evaluate to 1, which is True (1, 9). The result is converted to a list, and passed to the foo object.

35. Look at the code below:

import random
 
#
# Insert lines of code here.
#
 
print(a, b, c)

Which lines of code would you insert so that it is possible for the program to output the following result:

Output 6 82 0

A:

a = random.randrange(10, 100, 3)
b = random.randint(0, 100)
c = random.choice((0, 100, 3))

B:

a = random.choice((0, 100, 3))
b = random.randrange(10, 100, 3)
c = random.randint(0, 100)

C:

a = random.randint(0, 100)
b = random.randrange(10, 100, 3)
c = random.choice((0, 100, 3))

D:

a = random.randint(0, 100)
b = random.choice((0, 100, 3))
c = random.randrange(10, 100, 3)
  • D
  • C
  • B
  • A
Explanation:

Let’s analyze the randint, randrange, and choice functions:

  • The randint(0, 100) function returns any random integer from 0 to 100 inclusive, so it’s possible that a 6 is returned.
  • The randrange(10, 100, 3) function returns any random integer from 10 to 100 with step three, so it’s possible that 82 is returned.
  • The choice(0, 100, 3) function returns (“chooses”) one element from the sequence of elements 0, 100, 3, so it’s possible that a 0 is returned.

36. What is the expected result of the following code?

import os
 
os.mkdir('pictures')
os.chdir('pictures')
 
print(os.getcwd())
  • The code will print the name of the created directory
  • The code will print the path to the created directory
  • The code will print the owner of the created directory
  • The code will print the content of the created directory
Explanation:

Let’s analyze the code:

  • The first line imports the os module, which provides functions that interact with the Operating System.
  • The third line creates a directory named pictures.
  • The fourth line changes the current working directory to a folder named pictures.
  • The sixth line prints the path to the current working directory (cwd) which is now the pictures folder.

37. What information can be read using the uname function provided by the os module? (Select two answers)

  • Current path
  • Operating system name
  • Last login date
  • Hardware identifier
Explanation:

The uname function returns an object that contains information about the current operating system. The object has the following attributes: systemname (stores the name of the operating system), nodename (stores the machine name on the network), release (stores the operating system release), version (stores the operating system version), and machine (stores the hardware identifier, e.g. x86_64.)

38. What is the expected result of the following code?

from datetime import datetime
 
datetime_1 = datetime(2019, 11, 27, 11, 27, 22)
datetime_2 = datetime(2019, 11, 27, 0, 0, 0)
 
print(datetime_1 - datetime_2)
  • 11:27:22
  • 11 hours, 27 minutes, 22 seconds
  • 0 days
  • 0 days, 11:27:22
Explanation:

Let’s analyze the code:

  • Line 1: the datetime function is imported from the datetime module.
  • Line 3: a datetime_1 object is created and assigned the following value: 2019-11-27 11:27:22 returned as a result of the datetime(2019, 11, 27, 11, 27, 22) function operation.
  • Line 4: a datetime_2 object is created and assigned the following value: 2019-11-27 00:00:00 returned as a result of the datetime(2019, 11, 27, 0, 0, 0) function operation.
  • Line 6: it’s possible to perform arithmetic operations on datetime objects, and the result of the subtraction is returned as a timedelta object that expresses the difference in days between the two dates, which is printed to the console.

39. What is the expected result of the following code?

from datetime import timedelta
 
delta = timedelta(weeks = 1, days = 7, hours = 11)
print(delta * 2)
  • The code will raise an exception
  • 2 weeks, 14 days, 22 hours
  • 28 days, 22:00:00
  • 7 days, 22:00:00
Explanation:

The arguments of timedelta objects are converted to days and hours in the following format: DD days, HH:MM:SS, which means that (weeks = 1, days = 7, hours = 11) is internally stored as 14 days, 11:00:00. This multiplied by two (delta * 2) is evaluated to 28 days, 22:00:00.

40. What is the expected result of the following code?

import calendar
 
calendar.setfirstweekday(calendar.SUNDAY)
print(calendar.weekheader(3))
  • Sun Mon Tue Wed Thu Fri Sat
  • Su Mo Tu We Th Fr Sa
  • Tu
  • Tue
Explanation:

The setfirstweekday() function is used to change the first day of the week to Sunday (from Monday as a default). The weekheader() function returns the weekday names in a shortened form, in this case to three characters.

Leave a Reply

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