Input/Output Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Input/Output.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

The mechanisms by which a program receives data from the outside world (input) and sends results back (output). Input can come from keyboards, files, sensors, or network connections; output can go to screens, files, printers, or other devices.

What goes in and what comes out. Keyboard → program → screen.

Read the full concept explanation →

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Programs are useless without I/O—they transform input into output.

Common stuck point: Input needs validation—users may enter unexpected or invalid values that crash the program.

Sense of Study hint: When handling input, always validate and sanitize what you receive before using it in your program. Check for the correct type, reasonable range, and proper format. When producing output, format it clearly so the user or receiving system can understand and use it.

Worked Examples

Example 1

easy
Identify the inputs and outputs in this program: name = INPUT('What is your name?'). age = INPUT('How old are you?'). OUTPUT 'Hello ' + name + ', you are ' + age + '.'

Answer

Inputs: name and age (from user). Output: a personalised greeting message.

First step

1
Step 1: Inputs are data the program receives from the user. Here there are two inputs: name and age.

Full solution

  1. 2
    Step 2: The OUTPUT statement displays a message to the user — this is the program's output.
  2. 3
    Step 3: The program takes two inputs (name and age) and produces one output (a greeting message).
Input is data entering a program (keyboard, file, sensor). Output is data leaving a program (screen, file, speaker). Every useful program has at least one input or output.

Example 2

medium
A temperature converter takes a Celsius value as input and outputs the Fahrenheit equivalent. Write pseudocode and identify the input, process, and output.

Example 3

medium
A program reads three integers and prints their sum. Trace with inputs 4, 7, 9.

Example 4

medium
Trace: input='12', x=int(input); input='5', y=int(input); print(x*y).

Example 5

medium
Trace: prompt 'Enter age:'; user types '15'; convert to int; if age 13\ge 13 print 'teen'. What is printed?

Example 6

hard
A program reads two floats and prints their average. Trace with inputs '2.5' and '7.5'.

Example 7

hard
Trace: a program prints 5 lines, but stdout is piped to 'head -n 3'. How many lines does the user see?

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

medium
A program reads a list of 5 test scores and outputs the average. Identify the inputs, process, and output. What type of validation might you add to the input?

Example 2

hard
Describe three different input devices and three different output devices a computer program might use. For each, give an example of a program that uses it.

Example 3

easy
A program reads from the keyboard and shows results on the screen. Which is input and which is output?

Example 4

easy
A user types `42` at a keyboard prompt. By default, what data type does the program receive?

Example 5

easy
To add two numbers a user types, what must you do to each input first?

Example 6

easy
Besides the keyboard, name one source a program can read input from.

Example 7

easy
Why validate user input before using it?

Example 8

easy
A program writes results to a file instead of the screen. Is the file input or output here?

Example 9

easy
A program transforms input into output. With no I/O at all, why is it useless?

Example 10

easy
What is the typical I/O order for `Enter your name:` then `Hello, Sam`?

Example 11

medium
User types `5` then `3` as two inputs (strings). Program does `a + b`. What is printed and why?

Example 12

medium
A program expects a number but the user types `abc`. What should robust I/O code do?

Example 13

medium
A sensor sends temperature readings continuously. Is this input, and what model fits a never-ending data source?

Example 14

medium
`print("A"); x = read(); print("B")`. The user sees `A`, the program waits, then after typing shows `B`. Why the pause?

Example 15

medium
To print numbers 1 to 3 each on its own line vs all on one line, what part of output controls this?

Example 16

medium
Why does mixing up bits and bytes matter for I/O, e.g. a `100 Mb/s` link vs a `100 MB` file?

Example 17

medium
A buffered output is not appearing on screen even though `print` ran. What concept explains this?

Example 18

challenge
A program reads 1000 lines one-at-a-time vs reading the whole file then processing. Why might line-at-a-time be preferred for a 50 GB file?

Example 19

challenge
ASCII encodes 'A' as 65. A program reads the character 'A'. How many bits minimally store one standard ASCII character, and how many distinct characters fit?

Example 20

challenge
Input validation: a login form passes the username straight into a database query. What category of vulnerability does missing validation create?

Example 21

medium
A program prints a prompt but the cursor stays on the next line before input. What output detail controls staying on the same line?

Example 22

medium
User input "3.5" must be used in math. Converting it with integer conversion fails or truncates. What conversion is correct?

Example 23

easy
Classify each device as input or output: webcam, projector, microphone, speakers.

Example 24

easy
A program reads a temperature from a sensor and turns on a fan. Identify input, process, and output.

Example 25

easy
What is standard output (often called stdout) used for?

Example 26

easy
A program reads characters from stdin and counts them. After typing 'hello' and pressing Enter, how many characters were counted (assume newline excluded)?

Example 27

medium
A user is asked for a positive number. They enter 3-3. Name two ways robust I/O code should respond.

Example 28

medium
A program outputs 1,2,31,2,3 separated by tabs. Which character separates them?

Example 29

medium
A CSV file has 4 rows of 3 columns. How many fields total does the program read?

Example 30

medium
A program writes to stderr instead of stdout. Why might this be useful?

Example 31

medium
A web form submits user data to a server. From the server program's perspective, is the form data input or output?

Example 32

medium
A user enters their name as 'Sam ' (with trailing spaces). Why is trimming the input usually a good idea?

Example 33

hard
A keyboard logger captures every key. From an ethics view, why is hidden input capture problematic?

Example 34

hard
A program produces output to a slow printer. The CPU is much faster than the printer. What I/O technique decouples the speeds?

Example 35

hard
A log file grows unbounded as a program runs. What output strategy prevents disk exhaustion?

Example 36

hard
A program reads JSON from an HTTP response. What two parsing steps happen?

Example 37

hard
In an interactive program, why is echoing the user's typed characters back to the screen useful?

Example 38

challenge
A program processes a 10 GB file but only has 1 GB of RAM. Explain why streaming I/O is necessary and how it works.

Example 39

challenge
A program supports both interactive input and being run with input piped from a file. Why should it not assume the input is a human typing?