7.8k views
0 votes
CHALLENGE ACTIVITY 1.4.1: Basic syntax errors. Type the statements. Then, correct the one syntax error in each statement. Hints: Statements end in semicolons, and string literals use double quotes. System.out.println("Predictions are hard."); System.out.print("Especially '); System.out.println("about the future."). System.out.println("Num is: " - userName);

1 Answer

3 votes

Answer:

Lets check each statement for the errors.

Step-by-step explanation:

System.out.println("Predictions are hard.");

This statement has no syntax errors. When this statement is executed the following line will be displayed:

Predictions are hard.

System.out.print("Especially ');

This statement is missing the closing quotation mark. A single quotation mark is placed instead of double quotation mark in the statement.

The following error message will be displayed when this program statement will be compiled:

Main.java:15: error: unclosed string literal

String literals use double quotes. So to correct this syntax error, the statement should be changed as follows:

System.out.print("Especially");

The output of this corrected line is as following:

Especially

System.out.println("about the future.").

In this line a period . is placed at the end of the statement instead of a semicolon ; but according to the syntax rules statements should end in semicolons.

The error message displayed when this line is compiled is as following:

Main.java:15: error: ; expected

Main.java:15: error: not a statement

So in order to correct this syntax error the statement should be changed as following:

System.out.println("about the future.");

The output of this corrected line is as following:

about the future

System.out.println("Num is: " - userName);

There is a syntax error in this statement because of - symbol used instead of +

+ symbol is used to join together a variable and a value a variable and another variable in a single print statement.

The error message displayed when this line is compiled is as following:

Main.java:13: error: bad operand types for binary operator '-'

So in order to correct this syntax error the statement should be changed as following:

System.out.println("Num is: " + userName);

This line will print two things one is the string Num is and the other is the value stored in userName variable.

So let userName= 5 then the output of this corrected line is as following:

Num is: 5

User Jarek Bielicki
by
5.2k points