Courseiva
Exceptions and File I/OhardMultiple ChoiceObjective-mapped

PCAP Exceptions and File I/O Practice Question

Exhibit

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)

What is the output of the Python code after reading the config.txt file?

⚠ Common exam trap

It's easy for candidates to confuse the internal data type (string vs integer) with the printed output, assuming that because the source is a string, the output must also be a string or quoted, when in fact int() converts it to an integer and print() displays it without quotes.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

8080

The code reads the config.txt file and splits its content by newlines. The first line contains 'port=8080', and after splitting by '=', the second element is '8080'. The int() function converts this string to the integer 8080, which is then printed. Option C is correct because the output is the integer 8080, not a string or quoted form.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • 8080 (as string)

    Why it's wrong here

    The parenthetical '(as string)' is incorrect because the conversion to integer removes the string type. While the configuration file stores the value as a text string '8080', the code explicitly calls int() or getint(), which parses that text into an integer object. Therefore the printed output is an integer, not a string, and any notion of it being a string is a misconception.

  • An exception is raised.

    Why it's wrong here

    The code does not throw an exception because the configuration file is read successfully and the value retrieved is a well-formed numeric string. Exceptions such as FileNotFoundError or configparser.Error would only occur if the file were missing or the key/value malformed. Here, the getint() (or int()) operation succeeds, so the program completes normally and prints the converted integer.

  • 8080

    Why this is correct

    This is correct because the code reads the configuration value and converts it to an integer using int() (or a ConfigParser getint() call). The integer 8080 is then passed to print(), which displays the number without quotes. Since the conversion succeeds, the output is exactly the integer 8080.

  • '8080'

    Why it's wrong here

    This option is wrong because print() renders the final value without quotation marks. The value has already been converted from the raw string '8080' to an integer, and integers are displayed as bare numeric literals. Quotation marks would only appear if the output were a string object, but here the type is int, so the output is 8080, not '8080'.

About these practice questions

This PCAP question is part of Courseiva's 169-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.