Python Summary Test 2 (Chapter 4-5)
Assuming that the open() invocation has gone successfully, the following snippet will: for x in open('file','rt'): print(x)
read the file line by line
A method able to read data from file into a byte array object, is name:
readinto()
If you want to fill a byte array with data read in from a stream, you use the:
readinto() method
Entering the try: block implies that:
some of the instructions from this block may not be executed
A data structure described as LIFO is actually a:
stack
An assertion can be used to:
stop the program when some data have improper values
The byte array class can create objects which are designed to:
store amorphic data organized in bytes
The top‑most Python exception is named:
BaseException
The following code print('Mike' > "Mikey") prints:
False
An alternative name for a data structure called a stack is:
LIFO
What will be the output of the following code? class A: A = 1 print(hasattr(A,'A'))
True
A predefined Python variable, storing the current module name, is called:
__name__
UTF‑8 is:
a form of encoding Unicode code points
isalnum ( ) checks if a string contains only letters and digits, and this is:
a method
A code point is:
a number which makes up a character
When a file is opened in read mode, it:
it must exist (an exception will be raised otherwise)
What will be the result of executing the following code? class A: def __init__(self): pass a = A(1) print(hasattr(a,'A'))
it will raise an exception
The sys.stdin stream is normally associated with a:
keyboard
A subclass is usually:
more specialized than its superclass
The unnamed except: block:
must be the last one
An object is characterized by the following three:
name, properties, activities
Which of the following is true
packages can contain modules
ASCII is:
short for American Standard Code for Information Interchange
Which of the approachable EXPECT branches is taken into consideration when the exception occurs
the first matching
The strerror function comes from the OS module, and it's designed to:
translate an error number into an error description
The following statement: assert var == 0
will stop the program when var == 0
The following code print(chr(ord('z') - 2)) prints:
x
The following code x = '\'' print(len(x)) prints:
1
The two basic, mutually exclusive, file open modes are named:
binary and text
The pyc file contains:
compiled Python code
The += operator, when applied to string, performs:
concatenation
A function returning a list of all entities available in a module is called:
dir()
The following statement: from a.b import c causes the import of:
entity c from module b from package a
The following code print(3 * 'abc' + 'xyz') prints:
abcabcabcxyz
How does the readline() method react when the end‑of‑file occurs?
it returns an empty string
If the class's constructor is declared as below, which one of the assignments is valid? class Class: def __init__(self): pass
object = Class()
When an exception occurs, we say that it has been:
raised
A user defines exception:
may be derived from the Exception class
Knowing that a function named fun() resides in a module named mod, and it has been imported using the following line: import mod choose the way it can be invoked in your code:
mod.fun()
The following code: print(ord('c') - ord('a')) prints:
2
The readlines() method returns a:
list
The expression: 'mike' > 'Mike'
is True
UNICODE is a standard:
like ASCII, but much more expansive
What will be the output of the following code? class A: def __init__(self,v = 1): self.v = v def set(self,v): self.v = v return v a = A() print(a.set(a.v + 1))
2
What will be the output of the following code? class A: X = 0 def __init__(self,v = 0): self.Y = v A.X += v a = A() b = A(1) c = A(2) print(c.X)
3
If there is a superclass named A and a subclass named B, which one of the presented invocations should you put instead of a comment? class A: def __init__(self): self.a = 1 class B: def __init__(self): # put selected line here self.a = 2
A.__init__(self)
A namespace is:
a space in which names exists
The function named super() may be used to:
access super class's attributes and/or methods
A variable existing as a separate being in separate object is called:
an instance variable
When a module is imported, its contents:
are executed once (implicitly)
If you want to open a text file in append mode, you would use the following mode string:
at
If you want to import pi from math, you'd use:
from math import pi
Knowing that a function named fun() resides in a module named mod, choose the proper way to import it:
from mod import fun
A function able to check if an object is equipped with a given property is name:
hasattr()
What will be the result of executing the following code? def o(p): def q(): return '*' * p return q r = o(1) s = o(2) print(r() + s())
it will print ****
What will be the result of executing the following code? def I(n): s = '+' for i in range(n): s += s yield s for x in I(2): print(x,end='')
it will print ++++++
What will be the result of executing the following code? class A: v = 2 class B(A): v = 1 class C(B): pass o = C() print(o.v)
it will print 1
What will be the result of executing the following code? try: raise Exception(1,2,3) except Exception as e: print(len(e.args))
it will print 3
What will be the result of executing the following code? class A: pass class B(A): pass class C(B): pass print(issubclass(C,A))
it will print True
What will be the 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='')
it will print abc
What will be the result of executing the following code? def I(): s = 'abcdef' for c in s[::2]: yield c for x in I(): print(x,end='')
it will print ace
What will be the result of executing the following code? class A: def __str__(self): return 'a' class B(A): def __str__(self): return 'b' class C(B): pass o = C() print(o)
it will print b
What will be the 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 c(self): self.a() o = C() o.c()
it will print b
What will be the result of executing the following code? def f(x): try: x = x / x except: print("a",end='') else: print("b",end='') finally: print("c",end='') f(1) f(0)
it will print bcac
What will be the result of executing the following code? class Ex(Exception): def __init__(self,msg): Exception.__init__(self,msg + msg) self.args = (msg,) try: raise Ex('ex') except Ex as e: print(e) except Exception as e: print(e)
it will print ex
What will be the effect of running the following code? class A: def __init__(self,v): self.__a = v + 1 a = A(0) print(a.__a)
it will raise an AttributeError exception
The following code: print(float("1,3"))
raises a ValueError exception
If s is a stream opened in read mode, the following line q = s.read(1) will:
read 1 character from the stream
Is there a way to check if a class is a subclass of another class?
yes, there is a function able to do that