Chapter 5: Getting Started with Java

Getting Started with Java · Study Questions
JDK, JRE, JVMjavac and javaFirst program and mainprint and printlnEscape sequencesString concatenationCommentsKinds of errors
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · How Java Runs

Question 1: Where Is javac?

Which of the following contains the Java compiler javac?

JDK+ development toolsJRE+ class libraryJVMruns bytecode? javacjar, javadoc

C: JDK = JRE + development tools (javac, jar, javadoc…). JRE = JVM + class library, and it is enough only to run programs. A developer who writes and compiles programs installs the JDK.

Multiple Choice · How Java Runs

Question 2: Compile and Run Commands

To compile and run the file Hello.java, which is the correct sequence of commands?

Hello.javasourcejavaccompilerHello.classbytecodejava (JVM)runsHellooutput

B: javac takes the file name with its extension and produces Hello.class. The java command takes the class name, and the .class extension is not written. In D the JVM looks for a class named "Hello.class" and cannot find it.

Step-by-Step Trace · How Java Runs

Question 3: A Console Session

The folder contains only Hello.java (a program that prints Hello). When the commands are run in order, what appears on the console?

There is no Hello.class yet: the JVM cannot find a class to run.

Compilation succeeded: javac prints nothing; it only creates the .class file.

java expects a class name; it looks for a class named "Hello.class" and cannot find it. Do not write the extension!

The correct command: the program runs.

The source changed but was not recompiled: the JVM runs the old .class and prints Hello again.

After recompiling, the change shows: Hi. (In Java 11+ a "Caused by" line is also added below the error messages.)

> java Hello
> javac Hello.java
> java Hello.class
> java Hello
// In the source, "Hello" was changed to "Hi"
> java Hello
> javac Hello.java
> java Hello

Multiple Choice · First Program

Question 4: The Program's Entry Point

For the JVM to start a program with the command java App, which method must the class contain? (Classic rule; Java 24 and earlier.)

C: public (accessible from anywhere), static (called without creating an object), void (returns no value), the name is main in lower case, and the parameter is String[] args. A, B and D compile, but the JVM cannot find the entry point and does not start the program; Java is case-sensitive (Main ≠ main). (Since Java 25 a non-static void main can also be an entry point; B and D are still invalid.)

Multiple Choice · First Program

Question 5: Class Name and File Name

The code below is saved in a file named Hello.java and compiled with javac Hello.java. What happens?

public class Hi {
  public static void main(String[] args) {
    System.out.println("x");
  }
}

A: The name of a public class must match the file name exactly (including case). The compiler reports the error on line 1 and produces no .class. Fix: save the file as Hi.java or name the class Hello.

Output Question · First Program

Question 6: Line Endings

What is the output of the code below? How many lines does it produce?

System.out.print("A");
System.out.println("B");
System.out.print("C");
System.out.print("D\n");
System.out.println();
System.out.print("E");
Show answer
AB
CD

E
  • print("A") and println("B") write on the same line: AB, then a line break.
  • The \n inside "D\n" ends the line: CD.
  • println() produces an empty line.
  • E is written on line 4; there is no line break after it.

Output Question · First Program

Question 7: print and println

What is the output of the code below?

System.out.print("Hello ");
System.out.println("World");
System.out.print("Java");
Show answer
Hello World
Java
  • print does not end the line: "Hello " and "World" are written on the same line.
  • println moves to the next line after printing.
  • "Java" is written on the second line; there is no line break after it.

Output Question · Escape Sequences

Question 8: Four Escape Sequences

What is the output of the code? (Tab stops are 8 characters wide.)

System.out.println("Name\tGrade");
System.out.println("C:\\docs\\java");
System.out.println("\"Hello\"");
System.out.print("1\n2\n");
Show answer
Name    Grade
C:\docs\java
"Hello"
1
2
  • \t moves the cursor to the next tab stop (column 9): 4 spaces appear after "Name".
  • \\ prints a single backslash.
  • \" prints a double quote without ending the text.
  • A single print produced two lines: \n is the line-break character.

Multiple Choice · Escape Sequences

Question 9: \n Inside a String

What is the output of the code below?

System.out.println("Ali\nVeli");

C: \n is an escape sequence and produces a line break; the characters \ and n are not printed. The output is two lines: Ali, then Veli on the next line.

Multiple Choice · Escape Sequences

Question 10: Double Backslash and n

System.out.println("A\\nB"); statement?

C: In the source, \\ is an escape sequence and produces a single \ character; the n that follows is an ordinary letter. The screen shows A\nB on one line. For a new line, "A\nB" should have been written.

Output Question · Escape Sequences

Question 11: Printing a Windows Path

The program below is compiled and run. What happens?

public class Path {
  public static void main(String[] args) {
    System.out.println("File: C:\data");
  }
}
Show answer

Compile error; the program does not run at all:

Path.java:3: error: illegal escape character
    System.out.println("File: C:\data");
                                 ^
  • Inside a string, \ starts an escape sequence; \d is not a defined escape sequence.
  • Fix: "File: C:\\data" → the output is File: C:\data.
  • "C:\new" would compile, but \n would become a new line!

Output Question · String Concatenation

Question 12: Number or String?

What is the output of the code?

System.out.println(5 + 3);
System.out.println("5 + 3");
System.out.println("5" + 3);
System.out.println(5 + 3 + "5");
System.out.println("5" + 5 + 3);
Show answer
8
5 + 3
53
85
553
  • Two numbers: arithmetic addition → 8. Everything in quotes is a string → 5 + 3 exactly as written.
  • If one side is a string, + does concatenation: "5" + 3 → "53".
  • Evaluation goes left to right: 5 + 3 = 8 first, then 8 + "5" → "85".
  • "5" + 5 → "55", then "55" + 3 → "553".

Multiple Choice · String Concatenation

Question 13: Text and Two Numbers

What is the output of the code below?

System.out.println("Total: " + 2 + 3);

B: + is evaluated from left to right. "Total: " + 2 gives a String: "Total: 2". Then + 3 is also concatenation: Total: 23. To add the numbers, write "Total: " + (2 + 3).

Step-by-Step Trace · String Concatenation

Question 14: Left-to-Right Concatenation

How is the expression in each println evaluated from left to right?

Two ints: arithmetic addition.

One side is a String: now it is concatenation.

Every number added to a string is appended as text: "334", "3345".

The leftmost operand is already a string.

2 and 3 are not added; they are written side by side.

The parentheses are evaluated first: 5, then it is appended to the text.

* has higher precedence: both multiplications are done first.

Then left-to-right concatenation: 6=6.

public class Concat {
  public static void main(String[] args) {
    System.out.println(1 + 2 + "3" + 4 + 5);
    System.out.println("Total: " + 2 + 3);
    System.out.println("Total: " + (2 + 3));
    System.out.println(2 * 3 + "=" + 2 * 3);
  }
}

Multiple Choice · Language Structure

Question 15: Upper and Lower Case

A student writes system.out.println("x"); and compiles it. What happens?

A: Java is case-sensitive; the System class is written with a capital S. The compiler takes system for a package name and cannot find it. For the same reason number and Number are different names.

Multiple Choice · Language Structure

Question 16: Expression or Statement?

Which of the following is an expression that produces a value on its own (not a statement ending in a semicolon, and not a block)?

C: An expression produces a value (120 * 3 → 360). A statement completes an action and usually ends with ; (A is a declaration, B is a call statement). D is a block grouped with curly braces. A statement can contain one or more expressions.

Output Question · Comments

Question 17: Which Lines Run?

What is the output of this code inside main?

// System.out.println("1");
System.out.println("2"); // System.out.println("3");
/* System.out.println("4");
System.out.println("5"); */
System.out.println("6 // not a comment");
System.out.println("7 /* not a comment */");
Show answer
2
6 // not a comment
7 /* not a comment */
  • Line 1 is a comment from start to end; on line 2, everything after // (the 3) is a comment.
  • /* ... */ comments out two lines (4 and 5) together.
  • // and /* */ inside quotes are not comments; they are part of the text and are printed as written.

Output Question · Comments

Question 18: Nested Multi-Line Comment

The program is compiled and run. What happens?

public class Nested {
  public static void main(String[] args) {
    /* outer /* inner */ rest */
    System.out.println("Hello");
  }
}
Show answer

Compile error; "Hello" is never printed.

  • /* */ comments cannot be nested: the comment ends at the first */.
  • What remains is rest */, which the compiler takes for code: javac reports an error such as "illegal start of expression" and produces no .class.
  • To comment out a block that already contains /* */, it is safer to comment the lines out with //.

Multiple Choice · Kinds of Errors

Question 19: Matching the Kind of Error

Which of the following pairings is wrong?

D is wrong: a + b / 2 compiles and runs, but the result is wrong because / is done first → a logic error. Syntax and type errors are caught at compile time (no .class is produced); integer division by zero throws an ArithmeticException while the program runs.

Multiple Choice · Kinds of Errors

Question 20: cannot find symbol

javac gives this message. What is the most likely cause?

S2.java:4: error: cannot find symbol
    System.out.printn("y");
              ^
  symbol:   method printn(String)
  location: variable out of type PrintStream

D: cannot find symbol says that the compiler does not recognize a name (variable, method, class); the line symbol: method printn(String) shows which name was not found. The ^ mark points to the location of the error.

Output Question · Kinds of Errors

Question 21: A Missing Semicolon

The program is compiled with javac Test.java and run with java Test. What appears on the screen? Is "One" printed?

public class Test {
  public static void main(String[] args) {
    System.out.println("One");
    System.out.println("Two")
    System.out.println("Three");
  }
}
Show answer
Test.java:4: error: ';' expected
    System.out.println("Two")
                             ^
1 error
  • The error is reported at the end of line 4; ^ shows where the missing ; belongs.
  • "One" is not printed either: because compilation failed, Test.class is not produced; the program does not run at all.
  • If there is no previously compiled .class, java Test says "Could not find or load main class".

Step-by-Step Trace · Kinds of Errors

Question 22: A Runtime Error

The program is compiled. When it is run, which lines execute and what appears on the console?

javac checks the code: syntax and types are correct → Crash.class is produced. Division by zero is not a compile error.

The JVM runs main: "Start" is printed.

x10y0

x and y are declared.

An integer cannot be divided by zero: the JVM throws an ArithmeticException; the line number (5) appears in the message.

The program stopped: line 6 never runs, "Done" is not printed.

public class Crash {
  public static void main(String[] args) {
    System.out.println("Start");
    int x = 10, y = 0;
    System.out.println(x / y);
    System.out.println("Done");
  }
}

Output Question · Design

Question 23: Finding a Bug with a Test Table

A test table was prepared before coding. What does this program print for the three inputs in the table? For which test does the expected output not match?

int n1 = 75, n2 = 80;   // inputs
int avg = (n1 + n2) / 2;
System.out.println(avg);
InputExpected
70, 9080
0, 00
75, 8077.5
Show answer
InputOutputResult
70, 9080passed
0, 00passed
75, 8077failed
  • 155 / 2: two ints are divided and the fraction is discarded → 77.
  • The ordinary case and the lower-bound test did not reveal the bug; the input whose sum is odd (155) did. That is why choosing different kinds of sample inputs matters.
  • Fix: double avg = (n1 + n2) / 2.0; → 77.5.

The End

Getting Started with Java: Study Questions
Lecture slides: Chapter 5: Getting Started with Java · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.