CS Grade 9-12

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

Understanding Java program structure, methods, and the main method

View Answer Key
Name:
Date:
Score: / 12

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.

  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.

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

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

  7. 7
    Diagram showing command-line input tokens flowing into an array and then into a program.

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

  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) { } }

  9. 9
    Nested block diagram showing a method block contained inside an outer program block.

    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. 10

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

  11. 11

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

  12. 12
    Flow diagram showing execution moving downward, branching to a helper method, then returning to continue.

    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?

LivePhysics™.com CS - Grade 9-12

More CS Worksheets

See all CS worksheets

More Grade 9-12 Worksheets

See all Grade 9-12 worksheets