PE1 & PE2 : Python Essentials — Final Test
-
What is the expected output of the following snippet?
a = True b = False a = a or b b = a and b a = a or b print(a, b)
False False
True False
False True
True False
-
What is the expected output of the following snippet?
print(len([i for i in range(0, -2)]))
3
1
2
0
-
How many stars
*
will the following snippet send to the console?i = 4 while i > 0 : i -= 2 print("*") if i == 2: break else: print("*")
two
one
zero
- The snippet will enter an infinite loop, constantly printing one
*
per line
-
What is the
sys.stdout
stream normally associated with?- The printer
- The keyboard
- A
null
device - The screen
-
What is the excepted output of the following snippet?
class X: pass class Y(X): pass class Z(Y): pass x = X() z = Z() print(isinstance(x, z), isinstance(z, X))
True True
True False
False True
False False
-
What is the excepted output of the following snippet?
class A: def __init__(self,name): self.name = name a = A("class") print(a)
class
name
- A number
- A string ending with a long hexadecimal number
-
What is the excepted result of executing the following code?
class A: pass class B: pass class C(A, B): pass print(issubclass(C, A) and issubclass(C, B))
- The code will print True
- The code will raise an exception
- The code will print an empty line
- The code will print
False
-
What is the excepted output of the following code?
from datetime import datetime datetime = datatime(2019, 11, 27, 11, 27, 22)] print(datetime.strftime('%Y/%m/%d %H:%M:%S'))
19/11/27 11:27:22
2019/Nov/27 11:27:22
2019/11/27 11:27:22
2019/November/27 11:27:22
-
What is the excepted output of the following code?
my_string_1 = 'Bond' my_string_2 = 'James Bond' print(my_string_1.isalpha(), my_string_2.isalpha())
- False True
- True True
- False False
- True False
-
The meaning of a Keyword argument is determined by its:
- both name and value assigned to it
- position within the argument list
- connection with existing variables
- value only
-
Knowing that the function named
m
, and the code contains the followingimport
statement:from f import m
Choose the right way to invoke the function:
- mod.f()
- The function cannot be invoked because the
import
statement is invalid - f()
- mod:f()
-
The
Exception
class contains a property namedargs
-what is it?- A dictionary
- A list
- A string
- A tuple
-
What is PEP 8?
- A document that provides coding conventions and style guide for the C code computing the C implementation of Python
- A document that describes the development and release schedule for Python versions
- A document that describes an extension to Python’s import mechanism which improves sharing of Python source code files
- A document that provides coding conventions and style guide for Python code
-
Which is the expected behavior of the following snippet?
def fun(x): return 1 if x % 2 != else 2 print(fun(fun(1)))
- The program will output
None
- The code will cause a runtime error
- The program will output
2
- The program will output
1
- The program will output
-
Which operator would you use to check whether two values are equal?
===
is
==
=
-
What is the name of the directory/folder created by Python used to store
pyc
files?__pycfiles
__pyc__
__pycache__
__cache__
-
What can you do if you want tell your module users that a particular variable should not be accessed directly?
- Start its name with
__or__
- Start its name with a capital letter
- Use its number instead of its name
- Build its name with lowercase letters only
- Start its name with
-
What is the expected output of the following snippet?
d = ('one': 1, 'three': 3, 'two':2) for k in sorted(d.values()): print(k, end=' ')
1 2 3
3 1 2
3 2 1
2 3 1
-
Select the true statements.(Select two answers)
- The first parameter of a class method dose not have to be named self
- The first parameter of a class method must be named self
- If a class contains the
__init__
method, it can return a value - If a class contains the
__init__
method, it cannot return any value
-
Select the true statements. (Select two answers)
- PyPI is short for Python Package Index
- PyPI is the only existing Python repository
- PyPI is one of many existing Python repository
- PyPI is short for Python Package Installer
-
What is the expected output of the following snippet?
t = (1, 2, 3, 4) t = t[-2:-1] t = t[-1] print(t)
(3)
(3,)
3
33
-
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 raise an
AttributeError
exception - The code will output
1
- The code will output
2
- The code will output
0
- The code will raise an
-
Which of the following functions provide by the
os
module are available in both Windows and Unix? (Select two answers)chdir()
getgid()
getgroups()
mkdir()
-
What is the expected output of the following piece of code?
v = 1 + 1 // 2 + 1 / 2 + 2
4.0
3
3.5
4
-
If
s
is a stream opened in read mode, the following line:q = s.readlines()
will assign
q
as :dictionary
tuple
list
string
-
Which of the following sentences is true about the snippet below?
str_1 = 'string' str_2 = str_1[:]
str_1
is longer thanstr_2
str_1
andstr_2
are different (but equal) stringsstr_1
andstr_2
are different names of the same stringstr_2
is longer thanstr_1
-
What is the expected result of executed the following code?
class A: def __init__(self): pass def f(self): return 1 def g(): return self.f() a = A() print(a.g())
- The code will output
True
- The code will output
0
- The code will output
1
- The code will raise an exception
- The code will output
-
What is the expected output of the following code, located in the file
module.py
?print(__name__)
main
modle.py
__main__
__module.py__
-
What is the excepted output of the following code?
def a(x): def b(): return x + x return b x = a('x) y = a('') print(x() + y())
xx
xxxx
xxxxxx
x
-
What is the excepted behavior of the following piece of code?
x = 16 while x > 0: print('*', end='') x //= 2
The code will output *****
- The code will output
***
- The code will output
*
- The code will error an infinite loop
-
A package directory/folder may contain a file intended to initialize the package. What is its name?
init.py
__init.py__
__init__.
__init__.py
-
If there is a
finally:
branch inside thetry:
block, we can say that:- the
finally:
branch will always be executed - the
finally:
branch won’t be executed if any of theexcept:
branch is executed - the
finally:
branch won’t be executed if no exception is raised - the
finally:
branch will be executed when there is noelse:
branch
- the
-
What value will be assigned to the
x
variable?z = 2 y = 1 x = y < z or z > y and y > z or z < y
True
False
0
1
-
If you want to write a byte array’s content to a stream, which method can you use?
writeto()
writefrom()
write()
writebytearray()
-
What is the expected behavior of the following snippet?
try: raise Exception except: print("c") except BaseException: print("a") except Exception: print("b")
- The code will cause an error
- The code will output
b
- The code will output
c
- The code will output
a
-
What is true about the following code snippet?
def fun(par2, par1): return par2 + par1 print(fun(par2 = 1, 2))
- The code is erroneous
- The code will output
3
- The code will output
2
- The code will output
1
-
What is the expected output of the following piece of code?
x, y, z = 3, 2, 1 z, y, x = x, y, z print(x, y, z)
2 1 3
1 2 2
3 2 1
1 2 3
-
What is the expected output of the following snippet?
d = {} d['2'] = [1, 2] d['1'] = [3, 4] for x in d.keys(): print(d[x][1], end="")
24
31
13
42
-
What is the expected output of the following snippet?
try: raise Exception except BaseException: print("a", end='') else: print("b", end='') finnaly: print("c")
bc
a
ab
ac
-
If the class constructor is declare as below:
class Class: def __init__(self): pass
which one of the assignment is valid?
object = Class()
object = Class(1,2)
object = Class(None)
object = Class(1)
-
What is the expected output of the following code?
from datetime import timedelta delta = timedelta(weeks = 1, days = 7, hours = 11) print(delta)
7 days, 11:00:00
2 weeks, 11:00:00
1 week, 7 days, 11 hours
14 days, 11:00:00
-
What is the expected output of the following piece of code if the user enters two lines containing
1
and2
respectively?y = input() x = input() print(x + y)
2
21
12
3
-
What is the expected behavior of the following snippet?
my_string = 'abcdef' def fun(s): del s[2] return s print(fun(my_string))
- The program will cause an error
- The program will output
abdef
- The program will output
acdef
- The program will output
abcef
-
What is true about the following snippet?
def fun(d, k, v): d[k] = v my_dictionary = {} print(fun(my_dictionary, '1', 'v'))
- The code is erroneous
- The code will output
None
- The code will output
v
- The code will output
1
-
What is the expected output of the following code?
class A: A = 1 def __init__(self): self.a = 0 print(hasattr(A, 'A'))
False
1
0
True
-
What is the expected behavior of the following code?
x = """ """ print(len(x))
- The code will output
2
- The code will output
3
- The code will cause an error
- The code will output
1
- The code will output
-
What is the expected output of the following code?
import calendar c = calendar.Calendar(calendar.SUNDAY) for weekday in c.iterweekdays(): print(weekday, end=" ")
7 1 2 3 4 5 6
6 0 1 2 3 4 5
Su Mo Tu We Th Fr Sa
Su
-
What is the expected output of the following code?
def fun(n): s = ' ' for i in range(n): s += '*' yield s for x in fun(3): print(x, end='')
****
******
2***
*
-
What is the expected output of the following code?
t = (1, ) t = t[0] + t[0] print(t)
2
(1, )
(1, 1)
1
-
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(A, B): def c(self): self.a() o = C() o.c()
- The code will print
c
- The code will print
a
- The code will raise an exception
- The code will print
b
- The code will print
-
What is the expected behavior of the following code snippet?
my_list = [1, 2, 3, 4] my_list = list(map(lambda x: 2*x, my_list)) print(my_list)
- The code will cause a runtime error
- The code will output
1 2 3 4
- the code will output
10
- The code will output
2 4 6 8
-
What is the expected behavior of the following piece of code?
d = {1: 0, 2: 1, 3: 2, 0: 1} x = 0 for y in range(len(d)): x = d[x] print(x)
- The code will output
0
- The code will output
1
- The code will cause a runtime error
- The code will output
2
- The code will output
-
What pip operation would you use to check what Python package have been installed so far?
show
list
help
dir
-
What is true about the following line of code?
print(len((1, )))
- The code will output
0
- The code is erroneous
- The code will output
1
- The code will output
2
- The code will output
-
Which line properly invokes the function defined as below?
def fun(a, b, c=0): # function body
fun(b=0, b=0)
fun(a=1, b=0, c=0)
fun(1, c=2)
fun(0)
-
What is the expected behavior of the following code?
import os os.makedirs('pictures/thumbnails') os.rmdir('pictures')
- The code will delete both the
pictures
andthumbnails
directories - The code will delete the
pictures
directory only - The code will raise an error
- the code will delete the
thumbnails
directory only
- The code will delete both the
-
What is true about the following piece of code?
print("a", "b", "c", sep=" ' ")
- The code is erroneous
- The code will output
abc
- The code will output
a'b'c
- The code will output
a b c
-
What is the expected behavior of the following code?
x = "\" print(len(x))
- The code will output
3
- The code will cause an error
- The code will output
a2
- The code will output
1
- The code will output
-
What is the expected output of the following code?
class A: A = 1 def __init__(self, v=2): self.v = v +A.A A.A += 1 def set(self, v): self.v +=v A.A += 1 return a = A() a.set(2) print(a.v)
7
1
3
5
-
How many empty lines will the following snippet send to the console?
my_list = [[c for c in range(r)] for r in range(3)] for element in my_list: if len(element) < 2: print()
two
three
zero
one
Subscribe
0 Comments
Newest