← all problems

Find the Line: Errors in Unit 1 Code 🌶️🌶️

Reading an error is a skill of its own. Each program below has exactly one thing wrong with it. The question says how it goes wrong (the compiler refuses it, it stops with an exception, or it runs and prints the wrong thing), and you click the line that is the problem. A wrong pick is explained, but never points at the right line. The compiler reports the line where it noticed the problem, which is not always the line you would change.

Part 1: the compiler says no. The message is the real one from javac. Click the line it points at.

1. This program does not compile. The compiler's message is incompatible types: possible lossy conversion from double to int. Click the line the compiler points at.

Click a line to select it (click it again to unselect), then check.

2. This program does not compile. The compiler's message is cannot find symbol. Click the line the compiler points at.

Click a line to select it (click it again to unselect), then check.

3. This program does not compile. The compiler's message is ';' expected. Click the line the compiler points at.

Click a line to select it (click it again to unselect), then check.

4. This program does not compile. The compiler's message is incompatible types: possible lossy conversion from double to int. Click the line the compiler points at.

Click a line to select it (click it again to unselect), then check.

5. This program does not compile. The compiler's message is incompatible types: String cannot be converted to int. Click the line the compiler points at.

Click a line to select it (click it again to unselect), then check.

Part 2: an exception. The program compiles and starts, then stops with the error shown. Click the line that throws it.

6. This program compiles, but when it runs it stops with this error: java.lang.StringIndexOutOfBoundsException: begin 4, end 6, length 5. Click the line that throws it.

Click a line to select it (click it again to unselect), then check.

7. This program compiles, but when it runs it stops with this error: java.lang.ArithmeticException: / by zero. Click the line that throws it.

Click a line to select it (click it again to unselect), then check.

8. This program compiles, but when it runs it stops with this error: java.lang.NullPointerException: Cannot invoke "String.length()" because "t" is null. Click the line that throws it.

Click a line to select it (click it again to unselect), then check.

Part 3: no error, wrong answer. The program runs to the end, but its output is not the intended one. Click the line that is wrong.

9. This program compiles and runs without an error, but it prints 3.0 where it should print 3.5. Click the line that is wrong.

Click a line to select it (click it again to unselect), then check.

10. This program compiles and runs without an error, but it prints Total: 34 where it should print Total: 7. Click the line that is wrong.

Click a line to select it (click it again to unselect), then check.

11. This program compiles and runs without an error, but it prints false where it should print true. Click the line that is wrong.

Click a line to select it (click it again to unselect), then check.

Your score for your class is recorded when you click Submit all — checking individual questions doesn't save it. You can submit again to improve your score; your best one counts.