Back to Student Worksheet
CS Grade 9-12 Answer Key

CS: Java: Classes, Methods, and Public Static Void Main

Understanding Java program structure, methods, and the main method

Answer Key
Name:
Date:
Score: / 12

CS: Java: Classes, Methods, and Public Static Void Main

Understanding Java program structure, methods, and the main method

CS - Grade 9-12

Instructions: Read each Java problem carefully. Write clear answers and include code when asked. Use correct capitalization, punctuation, and braces.
  1. 1

    In your own words, explain what a class is in Java. Then explain why the file name often matches the public class name.

    Think about a class as the main container for a Java program.

    A class in Java is a blueprint or container that defines code such as variables and methods. When a class is public, the Java file name must match the public class name so the compiler can find and organize the program correctly.
  2. 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.

    The class name is HelloWorld. The method name is main. The statement that prints text is System.out.println("Hello, Java!");.
  3. 3

    Write the standard header for the Java main method exactly as it is usually written.

    Include the access modifier, the keyword static, the return type, the method name, and the parameter list.

    The standard Java main method header is public static void main(String[] args).
  4. 4

    Explain what the word public means in public static void main(String[] args).

    The word public means that the method can be accessed from outside the class. This is needed so the Java Virtual Machine can call the main method to start the program.
  5. 5

    Explain what the word static means in public static void main(String[] args).

    Static methods can be called using the class rather than an object.

    The word static means the method belongs to the class itself instead of to a specific object. This lets Java run the main method without first creating an object of the class.
  6. 6

    Explain what the word void means in public static void main(String[] args).

    The word void means the method does not return a value. The main method can run statements, but it does not send a result back to the code that called it.
  7. 7

    What does String[] args represent in the main method header? Give a simple example of when it might be used.

    The brackets mean it can hold more than one String value.

    String[] args is an array of text values that can be passed to the program when it starts. For example, a user could run a program with a name as a command-line argument, and the program could read that name from args.
  8. 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) { } }

    One correct program is: public class AboutMe { public static void main(String[] args) { System.out.println("My name is Alex."); System.out.println("My favorite subject is computer science."); } }. The exact name and subject may be different, but the program should use two valid System.out.println statements inside the main method.
  9. 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.

    A class needs braces around its body, and a method also needs braces around its body.

    The corrected code is: public class Test { public static void main(String[] args) { System.out.println("Hi"); } }. The class body needs an opening brace after Test, and the braces must properly enclose the main method.
  10. 10

    Write a Java class named CalculatorDemo with a main method. Inside main, print the result of 8 + 5 using System.out.println.

    A correct solution is: public class CalculatorDemo { public static void main(String[] args) { System.out.println(8 + 5); } }. This program prints 13 because Java evaluates the addition before printing the result.
  11. 11

    Create a method named greet that prints "Welcome!". Then call greet from the main method. Write the full Java class named GreetingProgram.

    Because main is static, make greet static too for this beginner program.

    A correct solution is: public class GreetingProgram { public static void main(String[] args) { greet(); } public static void greet() { System.out.println("Welcome!"); } }. The greet method is static, so it can be called directly from the static main method.
  12. 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?

    Follow the statements in main from top to bottom, and jump to the method when it is called.

    The program prints A, then B, then C on separate lines. Java starts in the main method, prints A, calls showLetter to print B, then returns to main and prints C.
LivePhysics™.com CS - Grade 9-12 - Answer Key