Syntax basics (classes, main method)

Computer Science · Java

Question 1 of 10

0

    How many lines does this Java code print? public class Main { public static void main(String[] args) { System.out.println("kilo"); System.out.println("lemon"); // System.out.println("delta"); } }

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Syntax basics (classes, main method) explained

    A Java program is built from classes, and every program starts running in a method called main. The classic main method has the form public static void main(String[] args), and a public class must have the same name as its file, so Greeting.java defines class Greeting. Java is compiled before it runs: the javac tool turns the source file into bytecode, and the java command runs the result.

    Java is case-sensitive, so System, system and SYSTEM are different names, and String is not the same as string. Each statement ends with a semicolon, and blocks of code are wrapped in curly braces. Indentation is for people to read and does not affect how the program runs. System.out.println prints a value followed by a new line, and System.out.print prints without the new line.

    Comments use // for a single line or /* and */ for several lines.

    public class Greeting {
        public static void main(String[] args) {
            int a = 6;
            int b = 4;
            System.out.println("Sum: " + (a + b));
        }
    }
    

    Worked example

    Compile with javac Greeting.java and run with java Greeting. Trace the main method.

    1. a is set to 6 and b to 4.
    2. The brackets make Java add first: a + b = 6 + 4 = 10.
    3. "Sum: " + 10 joins the text and the number, giving "Sum: 10".
    4. println prints it on one line.

    Without the brackets, "Sum: " + a + b is joined from left to right and prints "Sum: 64". That is why the brackets matter.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      How many lines does this Java code print?

      public class Main { public static void main(String[] args) { // System.out.println("bravo"); System.out.println("jolly"); System.out.println("kilo"); } }

    • Medium

      How many lines does this Java code print?

      public class Main { public static void main(String[] args) { System.out.println("gamma"); boolean done = false; if (done) { return; } System.out.println("jolly"); System.out.println("lemon"); System.out.println("ivory"); } }

    • Hard

      How many characters are on the first line of output? (print() does not end the line; println() does.)

      public class Main { public static void main(String[] args) { System.out.print("fjo"); System.out.println("lemon"); System.out.println("carg"); } }