Practice reading, writing, and explaining Java classes, methods, and the public static void main method.
Read each Java problem carefully. Write clear answers and include code when asked. Use correct capitalization, punctuation, and braces.
Understanding Java program structure, methods, and the main method
CS - Grade 9-12
- 1
In your own words, explain what a class is in Java. Then explain why the file name often matches the public class name.
- 2
Look at this Java code: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, Java!"); } } Identify the class name, the method name, and the statement that prints text.
- 3
Write the standard header for the Java main method exactly as it is usually written.
- 4
Explain what the word public means in public static void main(String[] args).
- 5
Explain what the word static means in public static void main(String[] args).
- 6
Explain what the word void means in public static void main(String[] args).
- 7
What does String[] args represent in the main method header? Give a simple example of when it might be used.
- 8
Complete this Java program so it prints your name on one line and your favorite subject on the next line: public class AboutMe { public static void main(String[] args) { } }
- 9
The following code has errors: public class Test public static void main(String[] args) { System.out.println("Hi"); } } Rewrite it with the braces in the correct places.
- 10
Write a Java class named CalculatorDemo with a main method. Inside main, print the result of 8 + 5 using System.out.println.
- 11
Create a method named greet that prints "Welcome!". Then call greet from the main method. Write the full Java class named GreetingProgram.
- 12
Trace the order of execution in this program: public class OrderDemo { public static void main(String[] args) { System.out.println("A"); showLetter(); System.out.println("C"); } public static void showLetter() { System.out.println("B"); } } What is printed, and why?