Python Final Exam Review
If the file "not_file.txt" does NOT exist, what will print when this code is run? try: myFile = open("not_file.txt", "r") except OSError: print("Error opening file for reading") finally: print("Finally!")
"Error opening file for reading" "Finally!"
What will be the output of the following Python code? class test: def __init__(self,a="Hello World"): self.a=a def display(self): print(self.a) obj=test() obj.display()
"Hello World" is displayed
Which of the following is a feature of DocString?
-Provide a convenient way of associating documentation with Python modules, functions, classes, and methods -All functions should have a docstring -Docstrings can be accessed by the __doc__ attribute on objects -All of the above (correct answer) -None of the above
What will be the output of the following Python code? lamb = lambda x: x ** 3 print(lamb(5))
125
What is JSON?
A way of formatting data as text
What is a Web API?
An interface that allows remote computers to request data from a web server
What is the main advantage of a Virtual Environment when writing Python applications?
Applications can be written against specific versions of modules/packages
What is "Instantiation" in terms of Object Oriented Programming (OOP) terminology?
Creating an instance of class
In a Python class, the __init__( ) function is used as the constructor and it must be defined in your code. True/False
False
If this code is executed when the file output.txt already exists, what will happen? out_file = open("output.txt", "w") out_file.write("Hello") out_file.close()
The file output.txt will be overwritten with the word "Hello"
Python allows you to give a module an alias by renaming it like this. import fibby as f
True
The way to get user input from the command line using Python is input("prompt...") The return value from this function is always a string. True/False?
True
What will be the output of the following Python code? def func(a, b=5, c=10):print(f'a is {a} and b is {b} and c is {c}')func(3, 7)func(25, c = 24)func(c = 50, a = 100)
a is 3 and b is 7 and c is 10 a is 25 and b is 5 and c is 24 a is 100 and b is 5 and c is 50
Which of the following Python code creates an empty class?
class A: pass
The line of Python code that correctly opens a file for reading is:
myFile = open("test_file.txt", "r")
The command typically used to download and install Python packages is:
python -m pip install --user <package_name>
Suppose ElectricCar is a subclass of Car (i.e. it inherits the class Car). How can you call the __init__( ) function of Car from the __init__( ) function of ElectricCar?
super.__init__( )
What is the difference between these two import statements? 1. import fibby2. from fibby import *
#1 requires function calls to be preceded by "fibby.", #2 does not
The correct way to pass an exception on to the function caller is to use this key word.
raise
What will be the output of the following Python code? def printMax(a, b): if a > b: print(f'{a} is maximum') elif a == b: print(f'{a} is equal to {b}') else: print(f'{b} is maximum') printMax(3, 4)
4 is maximum