PCAP Exceptions and File I/O • Complete Question Bank
Complete PCAP Exceptions and File I/O question bank — all 0 questions with answers and detailed explanations.
Consider the following code snippet:
try:
x = int(input()) y = 10 / x
print(y)
except ZeroDivisionError:
print('Division by zero')except ValueError:
print('Invalid integer')If the user enters '0', what is the output?
Refer to the exhibit.
# config.txt
[Settings]
host = localhost
port = 8080
debug = True
# Python code
import configparser
config = configparser.ConfigParser()
config.read('config.txt')
port = config.getint('Settings', 'port')
print(port)Refer to the exhibit.
# error.log
2025-03-15 10:23:45 ERROR: Division by zero in module calc.py line 42
2025-03-15 10:23:46 WARNING: Low disk space on /dev/sda1
2025-03-15 10:23:47 INFO: User login successful
# Python code
try:
with open('error.log', 'r') as f:
for line in f:
if 'ERROR' in line:
raise RuntimeError(line.strip())
except RuntimeError as e:
print('Error found:', e)Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag a concept onto its matching description — or click a concept then click the description.
Mutable
Immutable
Mutable
Immutable
Mutable
Drag a concept onto its matching description — or click a concept then click the description.
Mathematical functions
Generate pseudo-random numbers
Manipulate dates and times
Work with JSON data
Interact with operating system
Consider the following code snippet:
x = [1, 2, 3]
try:
print(x[5])except IndexError:
print('Index')except LookupError:
print('Lookup')Which THREE statements about this code are correct? (Select exactly 3.)
Traceback (most recent call last):
File "app.py", line 9, in <module>
with open("config.json", "r") as f:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'config.json'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "app.py", line 11, in <module>
config = json.load(f)
^^^^^^^^^^^
NameError: name 'json' is not defined{
"logging": {
"level": "DEBUG",
"file": "/var/log/app.log"
},
"database": {
"host": "localhost",
"port": 5432
}
}2025-04-01 10:23:45 ERROR root: Unhandled exception
Traceback (most recent call last):
File "process.py", line 15, in <module>
process_data()
File "process.py", line 7, in process_data
raise ValueError('Invalid value')
ValueError: Invalid valueWhat is the output of the following code?
try:
print(1/0)
except:
print('err')finally:
print('fin')What is the output of the following code?
try:
raise ValueError('a') except ValueError as e:
print(e.args[0])
finally:
print('b')What is the output of the following code?
try:
exec('1/0')
except:
print('error')else:
print('no error')finally:
print('done')try:
with open('config.cfg', 'r') as f:
data = f.read()
except FileNotFoundError:
print('File not found')
except PermissionError:
print('Permission denied')[ERROR] 2025-01-01 12:00:00: Unhandled exception: ValueError: invalid literal for int() with base 10: 'abc'
try:
with open('data.txt') as f:
print(f.read())
except FileNotFoundError:
try:
with open('backup.txt') as f:
print(f.read())
except FileNotFoundError:
print('No file found')A Python script that processes log files uses the following code:
with open('log.txt', 'r') as f:lines = f.readlines()
for line in lines:
# processWhat is a potential inefficiency in this code?
What will be the output of the following code?
try:
print(1/0)
except:
print('error')else:
print('no error')finally:
print('done')Refer to the exhibit.
Exhibit:
def parse_value(data):
try:
value = int(data)
return value
except ValueError:
return None
except TypeError:
return None
except:
return -1
result = parse_value('abc')
print(result)Refer to the exhibit.
Exhibit:
# log_output.txt contains:
# INFO: Process started
# ERROR: Disk full
# WARN: Memory high
# INFO: Process ended
with open('log_output.txt', 'r') as f:
for line in f:
if 'ERROR' in line:
print(line.strip())
# Expected output: ERROR: Disk fullRefer to the exhibit.
Exhibit:
try:
with open('config.json', 'r') as f:
data = json.load(f)
except FileNotFoundError:
print('Missing config file')
except json.JSONDecodeError:
print('Invalid JSON')
except Exception:
print('Unexpected error')Traceback (most recent call last):
File "script.py", line 5, in <module>
f = open('data.txt', 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'data.txt'with open('file.txt', 'r') as f:
data = f.read()
print(f.closed)
# Output: FalseTraceback (most recent call last):
File "app.py", line 10, in <module>
raise ValueError('Invalid value')
ValueError: Invalid value
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "app.py", line 12, in <module>
raise TypeError('Type mismatch') from None
TypeError: Type mismatchfile = open('test.txt', 'w')
file.write('Hello')
file.close()
print(file.closed)def read_file():
try:
f = open('data.txt', 'r')
return f.read()
finally:
f.close()
result = read_file()