InterviewQAs

Python Input and Output

Download as PDF
All questions in this page are included
Preparing…
Download PDF
PIA
Python Input and Output

Python input and output operations form the backbone of interactive and data-driven programs. Mastering these techniques ensures your programs can effectively communicate with users and external systems.

User input is commonly handled using the built-in input() function, which reads data from the console. Understanding how to validate and transform this input into the desired data types is crucial for building robust applications.

Output in Python is typically managed with the print() function for console display or file handling methods for persistent storage. Knowing how to format output, manage file pointers, and handle exceptions ensures your programs remain reliable.

Advanced input/output concepts include reading and writing large files efficiently, using context managers to avoid resource leaks, and leveraging modules like csv, json, or pathlib for structured data handling.

Real-world applications often require combining these techniques—for example, gathering user configuration, logging system activity, or processing large datasets for analysis—making practical understanding essential for professional Python development.

Question 01

Explain how Python's input() function works and its typical use cases in real-world applications.

EASY

The input() function in Python allows programs to read a line of text entered by the user via the console. When called, it pauses program execution and waits for the user to type a response, returning it as a string.

In real-world scenarios, input() is used to gather configuration data, user preferences, or interactive commands. For example, a command-line tool may ask the user for a file path or login credentials using input().

A key consideration is that input() always returns a string, so conversion is often necessary. For instance, numerical input should be wrapped with int() or float() functions. This ensures correct data handling and prevents runtime errors.

Question 02

Discuss the advantages of using context managers for file I/O in Python.

MEDIUM

Context managers, used with the 'with' statement, automatically handle opening and closing of files. This eliminates the need for explicit close() calls and reduces the risk of resource leaks, especially in programs that handle multiple files or encounter exceptions.

They also improve code readability and maintainability. A block of code using 'with' clearly delineates the scope of file access, making it easier to understand where and how the file is used.

In practice, context managers are vital for applications like data pipelines or log processing, where files must be reliably closed after reading or writing, ensuring data integrity and preventing file locks.

Question 03

Explain the differences between text and binary file modes in Python and when to use each.

HARD

Python allows files to be opened in text ('t') or binary ('b') modes. Text mode interprets the file content as strings, applying universal newline conversions and character encoding decoding, typically UTF-8.

Binary mode treats the content as raw bytes without any translation. It is essential for images, videos, compressed files, or any data where precise byte-level representation is required.

Choosing the correct mode is critical. For example, reading an image file in text mode can corrupt the data, while writing JSON in binary mode would require explicit encoding. Understanding these differences ensures that I/O operations behave predictably in real-world applications.

Question 04

Which of the following are valid ways to read an entire file into memory in Python?

MEDIUM
  • A file.read()
  • B file.readlines()
  • C file.readline()
  • D file.read().splitlines()

file.read() reads the entire file as a single string, while file.readlines() reads it into a list of lines including newline characters. Using file.read().splitlines() removes the newline characters, producing a clean list of lines.

file.readline() reads only one line at a time, which is useful for iterative processing but does not read the entire file in a single call.

Question 05

When writing to a file in Python, which statements are correct regarding modes and file handling?

HARD
  • A 'w' mode overwrites the file if it exists
  • B 'a' mode appends to the file if it exists
  • C 'x' mode opens the file for reading only
  • D 'w+' mode allows reading and writing, truncating the file first

'w' creates a new file or truncates an existing one, while 'a' appends data without truncating. 'x' creates a new file and raises an error if it exists, it is not for reading. 'w+' allows both reading and writing but starts with a truncated file, which is useful for updating content from scratch.

Question 06

Which of the following are valid ways to format output in Python?

EASY
  • A Using f-strings: f"Value is {value}"
  • B Using %-formatting: "Value is %d" % value
  • C Using str.format(): "Value is {}".format(value)
  • D Using print.format(): print.format(value)

Python offers multiple ways to format strings: f-strings (introduced in 3.6) allow inline expressions, %-formatting uses traditional C-style syntax, and str.format() provides flexible template-based formatting. print.format() does not exist as a valid method.

Question 07

Write a Python program to read a user's name and age and print a greeting message.

EASY

This code uses input() to capture user data, converting the age string to an integer. The f-string is then used to format the output in a readable, dynamic way.

In real-world applications, similar constructs are used for command-line utilities, interactive scripts, or initial configuration prompts.

// Python
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello, {name}! You are {age} years old.")
Question 08

Write a Python program to read a file and print all lines that contain the word 'error'.

MEDIUM

Using a context manager ensures the file is properly closed after processing. Iterating line by line is memory-efficient, especially for large log files.

Converting the line to lowercase allows case-insensitive matching, which is common when filtering log messages in real-world monitoring scripts.

// Python
with open("logfile.txt", "r") as file:
    for line in file:
        if "error" in line.lower():
            print(line.strip())
Question 09

Write a Python program to copy a binary file (e.g., image) from one location to another.

HARD

The program opens the source file in binary read mode and the destination file in binary write mode. Reading in chunks prevents excessive memory usage for large files.

This technique is widely used in real-world applications for copying images, videos, or other large binary data safely and efficiently.

// Python
source_path = "source_image.jpg"
dest_path = "copy_image.jpg"

with open(source_path, "rb") as src, open(dest_path, "wb") as dst:
    while chunk := src.read(4096):
        dst.write(chunk)
Question 10

Write a Python program to write a list of dictionaries to a JSON file.

MEDIUM

The json module provides dump() to serialize Python objects into JSON. Using indent makes the output human-readable.

This pattern is frequently used in real-world scenarios for exporting structured data for APIs, configuration files, or data exchange between systems.

// Python
import json
data = [
    {"name": "Alice", "age": 30},
    {"name": "Bob", "age": 25}
]

with open("output.json", "w") as file:
    json.dump(data, file, indent=4)
Question 11

Why is input validation important in Python applications, and what are some practical strategies for implementing it?

MEDIUM

Input validation protects applications from unexpected data, crashes, and security issues. In production systems, user input is often unpredictable. A script expecting a number may instead receive empty strings, symbols, or malformed data. Without validation, even simple programs can fail with runtime exceptions.

A practical strategy is to validate data as early as possible. For example, when reading numeric input, developers commonly wrap conversions in try-except blocks to catch ValueError exceptions. Range checks, regular expressions, and type checks are also widely used depending on the use case.

In enterprise applications, validation is especially important when processing uploaded files, API payloads, or configuration values. Strong validation improves reliability, prevents corrupt data from entering systems, and makes troubleshooting easier for support teams.

Question 12

Explain the performance implications of reading large files entirely into memory versus processing them line by line.

HARD

Reading an entire file using read() can be convenient for small datasets, but it becomes inefficient and risky when handling large files. A multi-gigabyte file loaded fully into memory can significantly increase RAM usage and may even crash the application on constrained systems.

Processing files line by line is generally more memory-efficient because only one portion of the file is held in memory at a time. This streaming approach is commonly used in log analysis, ETL pipelines, and data ingestion systems where files can be extremely large.

In real-world environments, the choice depends on workload characteristics. If the application needs random access or complex transformations, loading the content may be justified for smaller files. For scalable systems, chunked or streamed processing is typically the safer and more performant design.

Question 13

Which statements about Python file pointers and seek operations are correct?

MEDIUM
  • A file.tell() returns the current file pointer position
  • B file.seek(0) moves the pointer to the beginning of the file
  • C file.seek() works only in binary mode
  • D After reading a file completely, the pointer is positioned at the end

The tell() method helps track the current cursor location inside a file, while seek() repositions the pointer. seek(0) is commonly used when a file must be re-read without reopening it.

seek() works in both text and binary modes, although behavior is more predictable in binary mode because byte offsets are exact. Understanding pointer movement becomes important when processing partial file content or implementing resumable operations.

Question 14

Which of the following print() features are valid in Python?

EASY
  • A Using the sep parameter to change separators
  • B Using the end parameter to avoid newline characters
  • C Printing directly to files using the file parameter
  • D Using print.append() to concatenate output

The print() function provides several configurable parameters. sep controls how multiple values are separated, end changes what is printed after the output, and file allows redirection to file streams.

print.append() is not a valid method. In practice, redirecting print output to files is useful for lightweight logging, report generation, or debugging during development.

Question 15

Which scenarios are strong candidates for using binary mode instead of text mode?

HARD
  • A Copying image files
  • B Reading CSV files
  • C Streaming video data
  • D Handling compressed ZIP archives

Binary mode is required whenever exact byte preservation matters. Images, videos, and compressed archives contain non-textual binary data that should not be altered by encoding or newline transformations.

CSV files are generally text-based and are normally handled using text mode with appropriate encoding settings. Choosing the wrong mode can silently corrupt binary data, which is why experienced developers pay close attention to file modes.

Question 16

Write a Python program that continuously accepts user input until the user types 'exit'.

MEDIUM

This program uses an infinite loop to repeatedly collect user input until a termination condition is met. Converting the input to lowercase ensures case-insensitive matching for the exit command.

Interactive loops like this are commonly used in command-line tools, admin consoles, and chatbot-style utilities where users perform multiple actions during a single session.

// Python
while True:
    user_input = input("Enter command: ")

    if user_input.lower() == "exit":
        print("Program terminated.")
        break

    print(f"You entered: {user_input}")
Question 17

Write a Python program to safely append application logs to a file with timestamps.

HARD

The program appends log entries instead of overwriting existing content, which is essential for maintaining historical records. Timestamps provide traceability during debugging or incident investigations.

This pattern mirrors lightweight logging systems often used in automation scripts, ETL jobs, and monitoring utilities where maintaining execution history is operationally important.

// Python
from datetime import datetime

log_message = input("Enter log message: ")

timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

with open("application.log", "a") as log_file:
    log_file.write(f"[{timestamp}] {log_message}\n")

print("Log written successfully.")
Question 18

Write a Python program that reads a CSV file and prints only rows where the salary is greater than 50000.

MEDIUM

The csv.DictReader class converts each row into a dictionary, making column-based access more readable and maintainable compared to index-based approaches.

Filtering records during file reading is common in reporting systems, payroll analysis, and ETL transformations where only specific subsets of data are required.

// Python
import csv

with open("employees.csv", "r", newline="") as file:
    reader = csv.DictReader(file)

    for row in reader:
        if int(row["salary"]) > 50000:
            print(row)
Question 19

Write a Python program that reads a large text file in chunks instead of loading the entire file into memory.

HARD

Reading files in chunks prevents excessive memory usage and improves scalability when processing very large files. This approach is especially useful for data migration utilities or streaming applications.

Chunked processing is widely adopted in enterprise systems handling large logs, exports, or archive files where loading everything into memory would negatively impact performance.

// Python
chunk_size = 1024

with open("largefile.txt", "r") as file:
    while True:
        chunk = file.read(chunk_size)

        if not chunk:
            break

        print(chunk)
Question 20

What are common mistakes developers make when working with file input and output in Python?

MEDIUM

One common mistake is forgetting to close files properly. This can lead to resource leaks, locked files, or incomplete writes. Using context managers with the 'with' statement eliminates most of these issues automatically.

Another frequent problem is assuming all files use UTF-8 encoding. In real-world environments, files may originate from legacy systems using different encodings. Failing to specify encoding can cause UnicodeDecodeError exceptions during processing.

Developers also sometimes load large files entirely into memory without considering scalability. While this may work during development with small datasets, production workloads often require streaming or chunked processing for stability and performance.

Question 21

How does buffering impact file input and output performance in Python applications?

HARD

Buffering is a performance optimization technique where data is temporarily stored in memory before being written to disk or after being read from disk. Without buffering, every small read or write operation would directly interact with the storage device, significantly slowing down execution.

Python automatically applies buffering in most file operations, which improves efficiency for sequential reads and writes. For example, writing thousands of log entries individually without buffering would generate excessive disk operations, while buffered writes batch them together efficiently.

In production systems, developers may adjust buffering behavior depending on the use case. Real-time monitoring tools may require immediate flushing for critical logs, while batch processing systems prioritize throughput using larger buffers.

Question 22

Explain why encoding is important during text file operations in Python.

MEDIUM

Encoding determines how characters are converted between bytes and human-readable text. When Python reads a text file, it decodes raw bytes into strings using a specific encoding such as UTF-8.

If the wrong encoding is used, applications may encounter UnicodeDecodeError exceptions or display corrupted characters. This is especially common when processing files generated by older systems or applications running in different regions.

Professional applications often explicitly define encodings instead of relying on operating system defaults. This improves consistency across environments and prevents unpredictable behavior during deployments or data migrations.

Question 23

Which statements about Python's input() function are correct?

MEDIUM
  • A input() always returns a string
  • B input() automatically converts numeric values into integers
  • C input() can display a prompt message
  • D input() removes leading and trailing whitespace automatically

The input() function always returns user input as a string, regardless of what the user types. Numeric conversions must be handled explicitly using functions like int() or float().

The optional prompt argument improves usability by guiding users during interaction. input() does not automatically trim whitespace unless methods like strip() are used separately.

Question 24

Which practices help improve reliability during file write operations?

HARD
  • A Using context managers
  • B Handling exceptions during writes
  • C Ignoring file permissions because Python handles them automatically
  • D Flushing buffered data when immediate persistence is required

Reliable file writing requires both proper resource management and error handling. Context managers guarantee cleanup, while exception handling helps detect storage issues such as permission errors or unavailable disks.

Flushing ensures buffered data is physically written when durability matters, such as financial transactions or audit logging. Python does not automatically bypass operating system permissions, so developers must account for access restrictions.

Question 25

Which file modes allow writing data to a file in Python?

EASY
  • A "w"
  • B "a"
  • C "r"
  • D "w+"

"w" creates or overwrites files, "a" appends content, and "w+" supports both reading and writing while truncating existing content.

"r" is strictly for reading and raises an exception if the file does not exist. Choosing the correct mode is important because accidental truncation can permanently erase file content.

Question 26

Write a Python program that accepts comma-separated values from the user and stores them in a list.

MEDIUM

The split() method separates the string using commas, while strip() removes unnecessary whitespace around each value. This produces cleaner and more predictable data.

This pattern is commonly used in command-line tools where users provide multiple parameters in a compact format, such as tags, filenames, or configuration values.

// Python
user_input = input("Enter comma-separated values: ")

items = [item.strip() for item in user_input.split(",")]

print(items)
Question 27

Write a Python program that safely reads a file and handles the case where the file does not exist.

HARD

The program uses exception handling to prevent crashes when files are missing or inaccessible. Catching FileNotFoundError separately provides more meaningful feedback to users.

Defensive I/O handling is critical in enterprise applications because external resources such as files, network drives, or mounted storage systems may not always be available.

// Python
file_name = "data.txt"

try:
    with open(file_name, "r") as file:
        content = file.read()
        print(content)
except FileNotFoundError:
    print(f"File '{file_name}' does not exist.")
except Exception as error:
    print(f"Unexpected error: {error}")
Question 28

Write a Python program that writes user-entered notes into a text file until the user types 'save'.

MEDIUM

The program continuously collects user input and appends it to a file until a termination keyword is entered. Appending preserves previously stored notes instead of overwriting them.

This type of logic is often used in lightweight journaling tools, interactive logging systems, or temporary note-taking utilities.

// Python
with open("notes.txt", "a") as file:
    while True:
        note = input("Enter note (type 'save' to finish): ")

        if note.lower() == "save":
            break

        file.write(note + "\n")

print("Notes saved successfully.")
Question 29

Write a Python program that counts the number of lines, words, and characters in a text file.

HARD

The program processes the file incrementally, which is more scalable than loading the entire file into memory. Each line contributes to cumulative counts for lines, words, and characters.

Similar processing logic appears in analytics tools, content auditing systems, and preprocessing stages of data engineering pipelines.

// Python
line_count = 0
word_count = 0
char_count = 0

with open("sample.txt", "r") as file:
    for line in file:
        line_count += 1
        word_count += len(line.split())
        char_count += len(line)

print(f"Lines: {line_count}")
print(f"Words: {word_count}")
print(f"Characters: {char_count}")
Question 30

What are the advantages of using JSON for input and output operations in Python applications?

MEDIUM

JSON is lightweight, human-readable, and widely supported across programming languages, making it one of the most practical formats for data exchange. Python's built-in json module simplifies serialization and deserialization significantly.

Compared to plain text files, JSON preserves structured relationships between values. Nested dictionaries and arrays can represent complex configurations, API responses, or application state in a clean and maintainable way.

In enterprise environments, JSON is heavily used in REST APIs, cloud integrations, and microservices communication. Its compatibility and readability make debugging and interoperability far easier than many legacy formats.