This page is written in AsciiDoc, which enables some nice features like automatic section numbering, admonitions (which is what this section is) and collapsible sections for solutions. Using passthrough blocks, we can also include interactive exercises like Parsons problems.

1. Code Reading

What is the output of the following program?

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, " + "World!");
    }
}
Solution
Hello, World!

2. Comments

Which of the following lines of code are valid and, if so, what do they print?

  1. System.out.println(/*"Hi"*/);

  2. System.out.println("println()");

  3. // System.out.println("Hi");

  4. /**/ System.out.println("//Hi");

  5. /* System.out.println("Hi */");

  6. System.out.println("println("Hi")");

Solution
  1. Valid, prints empty line (argument is commented out)

  2. Valid, prints println()

  3. Valid, prints nothing (whole line is commented out)

  4. Valid, prints //Hi

  5. Invalid, characters after comment (");) are not valid code

  6. Invalid, nested quotes are not allowed

3. Code Writing

Compose a program that outputs the following text:

Once upon a time...
The end.

using some of the following lines of code. Beware of the distractor lines, which are not needed. You also have to get the indentation right.

4. Math

Write a program that calculates the sum of two numbers, \(a\) and \(b\), and outputs the result:

\[r = a + b\]

5. Code Writing from Class Diagram

Convert the following class diagram to Java code:

Personname: StringPerson(name: String)getName(): StringsetName(name: String): voidStudentstudentId: intStudent(name: String, studentId: int)getStudentId(): intsetStudentId(studentId: int): void
The diagram above was generated automatically from PlantUML code.