Why is input important for a program?
Think about how a program gets information to work with.
Input is how a program gets data to work on. Without input, the program cannot change its behavior based on user needs or external data.
What is the main reason a program produces output?
Think about why you want to see what the program did.
Output is how a program communicates results or information after processing input or performing tasks.
Look at the code below. What will it print?
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int num = scanner.nextInt(); System.out.println("Double is: " + (num * 2)); scanner.close(); } }
Think about what happens when the user inputs 10.
The program reads the number 10, doubles it to 20, and prints "Double is: 20".
What error will this Java code produce?
public class Main { public static void main(String[] args) { int x; System.out.println(x); } }
Think about using a variable before giving it a value.
In Java, local variables must be initialized before use. Here, x is declared but not initialized, causing a compile-time error.
Why must interactive programs have input and output?
Think about how you talk to a program and how it talks back.
Interactive programs rely on input to get user commands or data and output to show results or feedback, making the interaction possible.