Python Essentials 2: INTERMEDIATE


Part 2 Summary Test


                        

1 - The following code: x = "\\\\" print(len(x))
a) will print 1
b) will print 2
c) will cause an error
c) will print 3
Solution: b

2 - What information can be read using the uname function provided by the os module? (Select two answers) import os os.mkdir('pictures') os.chdir('pictures') print(os.getcwd())
a) Last login date.
b) Current path
c) Operating system name
d) Hardware identifier
Solution: c, d

3 - The following statement: assert var != 0
a) ia erroneous
b) has no effect
c) will stop the program when var == 0
d) will stop the program when var !=0
Solution: c

4 - What is he except output of the following code? class A: A = 1 def __init__(self): self.a = 0 print(hasattr(A, 'a'))
a) 0
b) True
c) 1
d) False
Solution: d

5 - What is the excepted result of the following code? from datetime import timedelta delta = timedelta(weeks = 1, days = 7, hours = 11) print(delta * 2)
a) 28 days, 22:00:00
b) 2 weeks, 14 days, 22 hours
c) 7 days, 22:00:00
d) The code will raise an exception
Solution: a

6 - What is the excepted output of the following code? class A: def __init__(self, v=2) def set(self, v=1): self.v +=v return self.v a = A() b = a b.set() print(a.v)

a) 1
b) 0
c) 2
d) 3
Solution: d

7 - 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)

a) The code will raise an AttributeError except
b) The code will print 2
c) The code will print 0
d) The code will print 1
Solution: a

8 - 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 to invoke the fun() function:

a) mod:fun()
b) mod::fun()
c) fun()
d) mod.fun()
Solution: c

9 - What output will appear after running the following snippet?
a) The number of all the entities residing in the math module
b) A string containing the fully qualified name of the module
c) An error message
d) A list of all the entities residing in the math module
Solution: d

10 - Look at the code below: import random # # Insert lines of code here. # print(a, b, Which lines of code would you insert so that it is possible for the program to output the following result: 6 82 0
a) a = random.randint(0, 100) b = random.randrange(10, 100, 3) 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.randrange(10, 100, 3) b = random.randint(0, 100) c = random.choice((0, 100, 3))
d) a = random.randint(0, 100) b = random.choice((0, 100, 3)) c = random.randrange(10, 100, 3)
Solution: a

11 - 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)
a) o days
b) 0 days, 11:27:22
c) 11:27:22
d) 11 hours, 27 minutes, 22 seconds
Solution: c

12 - What is the expected result of the following code? import calendar calendar.setfirstweekday(calendar.SUNDAY) print(calendar.weekheader(3))
a) Su Mo Tu We Th We Fr Sa
b) Sun Mon Tue Wed Thu Fri Sat
c) Tu
d) Tue
Solution: b

13 - what is the expected result of executing the following code? class A:
a) The code will print c
b) The code will raise an excepion
c) The code will print b
d) The code will print a
Solution: c

14 - 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? [0, 4, 49, 81, 100]
a) foo = lambda num: num ** 2, numbers
b) foo = lambda num: num * 2, numbers)
c) foo = filter(lambda num: num ** 2, numbers)
d) foo = map(lambda num : num ** 2, numbers)
Solution: d

15 - What is the expected result of executing the following code? class I: def __init__(self): self.s = 'abc' self.i = 0 def __init__(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='')
a) The code will print 210
b) The code will print abc
c) The code will print 012
d) The code will print cba
Solution: b

16 - The complied Python bytecode is stored in files which have their names ending with:
a) py
b) pyb
c) pc
d) pyc
Solution: d

17 - Which pip command would you use to uninstall a previously install package?
a) pip delete packagename
b) pip –uninstall packagename
c) pip –remove packagename
d) pip uninstall packagename
Solution: d

18 - What is the excepted result of executed the following code? try: raise Exception(1, 2, 3) except Exception as e: print(len(e.args))
a) The code will raise an unhandled exception
b) The code will print 2
c) The code will print 3
d) The code will print 1
Solution: c

19 - What is the excepted result of executed the following snippet? try: raise Exception except BaseException: print("a") except Exception: print("b") except: print("c")
a) b
b) a
c) An error message
d) 1
Solution: b

20 - 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)) a) The code will print True
b) The code will print 1
c) The code will print False
d) The code will raise an exception
Solution: c

21 - If you want to fill a byte array with data read in from a stream, which method you can use?
a) The read() method
b) The readbytes() method
c) The readfrom() method
d) The readinto() method
Solution: d

22 - The following code: print(float("1.3"))
a) will print 1.3
b) will print 13
c) will print 1,3
d) will raise a ValueError exception
Solution: a

23 - The following code: print(chr(ord('p) + 2)) will print:
a) p
b) s
c) r
d) t
Solution: c

24 - will print:
a) exactly one except: block will be executed
b) one or more except: blocks will be executed
c) not more than one except: block will be executed
d) none of the except: blocks will be executed
Solution: c

25 -If the class constructor is declared in the following way: class Class: def __init__(self, vla = 0): pass
which one of the assignments is invalid? a) object = Class(1)
b) object = Class(None)
c) object = Class(1, 2)
d) object = Class()
Solution: c

26 - 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
b) 1
c) b
d) a
Solution: a

27 - What is the expected result of 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='')
a) The code will print +
b) The code will print +++
c) The code will print ++
d) The code will print ++++++
Solution: d

28 - 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? [1, 9]
a) foo = list(filter(lambda x: x % 2, numbers))
b) foo = list(filter(lambda x: x / 2, numbers))
c) foo = list(map(lambda x: x % 2, numbers))
d) foo = list(map(lambda x: x // 2, numbers))
Solution: a

29 - 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())
a) The code will print ***
b) The code will print ****
c) The code will print *
d) The code will print **
Solution: a

30 - The following statement: from a.b import c causes the import of:
a) entity a from module b from package c
b) entity b from module a from package c
c) entity c from module a from package b
d) entity c from module b from package a
Solution: d

31 - The sys.stderr stream is normally associaated with:
a) the keyboard
b) the printer
c) a null device
d) the screen
Solution: d

32 - What will be the output of the following code, located in the p.py file? print(__name__)
a) main
b) p.py
c) __main__
d) __p.py__
Solution: c

33 - Assuming that the​ open() invocation has gone successfully, the following snippet: for x in open('file', 'rt')) print(x) will:
a) read the file character by character
b) read the file line by line
c) read the whole file at once
d) cause an exception
Solution: b

34 - If a is a stream opened in read mod, the following line: q = s.read(1) will read:
a) one line from the stream
b) one kilobyte from the stream
c) one buffer from the stream
d) one character from the stream
Solution: d

35 - The following line of code: for line in open('text.txt', 'rt'):
a) in invalid because open returns nothing
b) is invalid because open returns a non-iterable object
c) is invalid because open returns an iterable object
d) may be valid if line is a list
Solution: c

36 - What is the expected result of the following code? import os os.mkdir('pictures') os.chdir('pictures') print(os.getcwd())
a) The code will print the owner of the created directory
b) The code will print the content of the created directory
c) The code will print the name of the created directory
d) The code will print the path to the created directory
Solution: d

37 - Assuming that the following three files a.py ,and c.py reside in the same directory, what will be the output produce 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
a) cab
b) cab
c) abc
d) bac
Solution: a

38 - What is the expected result of executing the followinf code? class A: def __init__(self): pass a = A(1) print(hasattr(a, 'A'))
a) The code will print 1
b) The code will raise an exception
c) The code will print False
d) The code will print True
Solution: b

39 - The following code: x = " \\" print(len(x))
a) will print 1
b) will print 3
c) will print 2
d) will cause an error
Solution: d

Which of the following commands would you use to check pip ‘s version? (Select two answers)Assuming that the following three files a.py ,and c.py reside in the same directory, what will be the output produce after running the c.py file?:
a) pip version
b) pip --version
c) pip–version
d) pip3 --version
Solution: b