Java Expressions, Input & Debugging

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Catherine Halcomb
Catherine Halcomb
Community Contributor
Quizzes Created: 4496 | Total Attempts: 7,035,505
| Questions: 30 | Updated: Oct 28, 2026
Please wait...
Question 1 / 31
🏆 Rank #-- ▾
0 %
0/100
Score 0/100

1. What is the output of the trace: int a = 4; int b = 2; a = a + b; b = a - b; System.out.println(b);?

Explanation

In the given code, the initial values are `a = 4` and `b = 2`. The expression `a = a + b` updates `a` to 6 (`4 + 2`). Then, `b = a - b` calculates `b` as `6 - 2`, which results in `b` being 4. Finally, `System.out.println(b)` outputs the current value of `b`, which is 4.

Submit
Please wait...
About This Quiz
Computer Programming Quizzes & Trivia

This assessment focuses on Java expressions, input handling, and debugging techniques. It evaluates your understanding of operator precedence, type conversions, and the use of the Scanner class. Mastering these concepts is essential for effective Java programming and will enhance your coding skills.

2.

What first name or nickname would you like us to use?

You may optionally provide this to label your report, leaderboard, or certificate.

2. In Java, combining an int and a double in an arithmetic expression typically produces a double result.

Submit

3. In Java, assigning an int result of division to a double variable restores the decimal part.

Submit

4. Match each error type with its correct example.

Submit

5. Match each Scanner method with what it reads.

Submit

6. Match each Java expression with its correct output.

Submit

7. The ____ operator in Java is used to get the remainder of a division.

Submit

8. The expression double b = 5 / 2.0; evaluates to ____.

Submit

9. To read a decimal number using Scanner, you use the method ____.

Submit

10. A String in Java is a ____ type, not a primitive type.

Submit

11. In Java, integer division discards the fractional part without ____.

Submit

12. What import statement is required to use Scanner in Java?

Explanation

In Java, the Scanner class is part of the java.util package, which contains utility classes for data manipulation. To use Scanner for reading input, you must import it from this package. The correct import statement, `import java.util.Scanner;`, allows the program to access the Scanner class, enabling functionalities like reading user input from various sources such as keyboard input. The other options are incorrect because they reference the wrong package or are incomplete.

Submit

13. What is the correct operator precedence order in Java (highest to lowest)?

Explanation

In Java, operator precedence determines the order in which operators are evaluated in expressions. Parentheses `( )` have the highest precedence, allowing for explicit control over evaluation order. Following parentheses, multiplication `*`, division `/`, and modulus `%` share the next highest precedence level, evaluated from left to right. Lastly, addition `+` and subtraction `-` have the lowest precedence. This hierarchy ensures that calculations are performed correctly and consistently, adhering to mathematical conventions.

Submit

14. Which type of error occurs when the program runs but produces an incorrect answer?

Explanation

A logic error occurs when the program executes without crashing but produces incorrect results due to flaws in the algorithm or logic used. Unlike syntax or runtime errors, which prevent the program from running or cause it to terminate unexpectedly, logic errors can be subtle and may only become apparent when specific inputs are processed. Debugging logic errors often requires careful review of the code and understanding of the intended functionality to identify where the logic deviates from the expected outcome.

Submit

15. Which type of error occurs when a semicolon is missing in Java?

Explanation

A missing semicolon in Java results in a syntax or compile-time error because it violates the language's grammatical rules. The Java compiler expects a semicolon at the end of statements to indicate their termination. When it encounters a statement without a semicolon, it cannot correctly interpret the code, leading to compilation failure. This type of error is identified before the program runs, preventing it from executing until the issue is resolved.

Submit

16. What is the result of the expression: 2 + 3 * 4?

Explanation

In the expression 2 + 3 * 4, the order of operations, known as PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction), dictates that multiplication is performed before addition. First, calculate 3 * 4, which equals 12. Then, add 2 to this result: 2 + 12 equals 14. Thus, the final result of the expression is 14.

Submit

17. What is the output of: int count = 3; count++; count--; System.out.println(count);?

Explanation

In the given code, an integer variable `count` is initialized to 3. The `count++` operation increments the value by 1, making it 4, but this value is not stored since the next line, `count--`, decrements it back to 3. Therefore, when `System.out.println(count);` is executed, it outputs the current value of `count`, which remains 3 after the increment and decrement operations cancel each other out.

Submit

18. What is the output of: int count = 3; count++; System.out.println(count);?

Explanation

In the code provided, the variable `count` is initialized to 3. The `count++` statement increments the value of `count` by 1, changing it from 3 to 4. When `System.out.println(count);` is executed, it prints the current value of `count`, which is now 4. Thus, the output of the program is 4.

Submit

19. How do you read a single character using Scanner in Java?

Explanation

To read a single character using Scanner in Java, the method `input.next()` is used to read the next token as a string. Since this returns a string, you can then call `charAt(0)` on that string to retrieve the first character. This approach effectively combines reading a string and accessing its first character, making it a straightforward way to obtain a single character input. Other options either do not exist or do not correctly retrieve a single character from user input.

Submit

20. Which Scanner method is used to read a full line of text including spaces?

Explanation

The `nextLine()` method in the Scanner class is specifically designed to read an entire line of input from the user, including any spaces. Unlike other methods like `next()`, which only captures the next token (up to the first space), `nextLine()` retrieves everything until the newline character, making it ideal for reading complete sentences or paragraphs. This functionality is essential when the input may contain multiple words or phrases separated by spaces, ensuring that the full context is captured in a single string.

Submit

21. What is the output of: System.out.println("sum: " + (2 + 3));?

Explanation

The output of the code `System.out.println("sum: " + (2 + 3));` is generated by first evaluating the expression inside the parentheses, which adds 2 and 3 together to get 5. Then, this result is concatenated with the string "sum: ", resulting in "sum: 5". The final output printed to the console is "sum: 5". The other options do not reflect the correct evaluation of the arithmetic operation or the string concatenation process.

Submit

22. What is the output of: System.out.println("sum: " + 2 + 3);?

Explanation

In Java, the `+` operator is used for both addition and string concatenation. When the expression `System.out.println("sum: " + 2 + 3)` is evaluated, the first part `"sum: "` is a string. The `+ 2` concatenates the string with the integer `2`, resulting in `"sum: 2"`. Then, the `+ 3` concatenates `"sum: 2"` with the integer `3`, leading to the final output of `"sum: 23"`. Thus, instead of performing arithmetic addition, the operation results in string concatenation.

Submit

23. What is the result of: int units = 3; double unitPrice = 12.50; double total = units * unitPrice;?

Explanation

In the given code, the variable `units` is set to 3, and `unitPrice` is set to 12.50. When calculating `total`, the multiplication of `units` and `unitPrice` is performed: 3 * 12.50 equals 37.5. Since `unitPrice` is a double, the result is also a double, retaining the decimal value. Therefore, the final value of `total` is 37.5.

Submit

24. What is the output of: double price = 9.99; int roundedDown = (int) price; System.out.println(roundedDown);?

Explanation

In the given code, the variable `price` is a double with a value of 9.99. When casting this double to an int, the decimal portion is truncated, resulting in the integer value of 9. The `System.out.println` statement then prints this integer value, which is why the output is 9. The cast effectively rounds down the value, discarding anything after the decimal point.

Submit

25. Which of the following correctly performs a narrowing type conversion in Java?

Explanation

Narrowing type conversion in Java occurs when a larger data type is converted to a smaller one, which may lead to loss of precision. In the given options, "int roundedDown = (int) 9.99;" explicitly casts the double value 9.99 to an int, effectively truncating the decimal part and resulting in the integer value 9. This operation demonstrates how Java allows narrowing conversions through casting, making it the only correct choice among the options provided.

Submit

26. What is the output of: double a = 5 / 2; System.out.println(a);?

Explanation

In Java, when performing division with two integers, the result is also an integer, which means any fractional part is discarded. In the expression `5 / 2`, both operands are integers, resulting in `2` rather than `2.5`. This integer result is then assigned to the double variable `a`, which converts it to `2.0`. Thus, when `System.out.println(a);` is executed, it outputs `2.0`.

Submit

27. What is the output of: double speed = 7.0 / 2.0; System.out.println(speed);?

Explanation

In the given code, the expression `7.0 / 2.0` performs a division operation between two double values. The result of this division is `3.5`, which is then assigned to the variable `speed`. When `System.out.println(speed);` is executed, it outputs the value of `speed`, which is `3.5`. The use of double literals ensures that the division is done in floating-point arithmetic, yielding a precise result rather than an integer division.

Submit

28. What does the % operator return when evaluating 7 % 2?

Explanation

The % operator, known as the modulus operator, returns the remainder of the division of one number by another. In this case, when 7 is divided by 2, the quotient is 3 and the remainder is 1. Therefore, 7 % 2 evaluates to 1, indicating that after dividing 7 by 2, there is a remainder of 1.

Submit

29. In Java, what is the result of: int each = 7 / 2;?

Explanation

In Java, when performing division between two integers, the result is also an integer, with any fractional part discarded (truncated). In the expression `7 / 2`, both `7` and `2` are integers, so the division yields `3`, as `3.5` is truncated to `3`. Thus, the variable `each` is assigned the value `3`.

Submit

30. What is the result of the expression: (2 + 3) * 4?

Explanation

To solve the expression (2 + 3) * 4, first, calculate the value inside the parentheses: 2 + 3 equals 5. Then, multiply this result by 4: 5 * 4 equals 20. Thus, the final result of the expression is 20.

Submit
×
Saved
Thank you for your feedback!
View My Results
Cancel
  • All
    All (30)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What is the output of the trace: int a = 4; int b = 2; a = a + b; b =...
In Java, combining an int and a double in an arithmetic expression...
In Java, assigning an int result of division to a double variable...
Match each error type with its correct example.
Match each Scanner method with what it reads.
Match each Java expression with its correct output.
The ____ operator in Java is used to get the remainder of a division.
The expression double b = 5 / 2.0; evaluates to ____.
To read a decimal number using Scanner, you use the method ____.
A String in Java is a ____ type, not a primitive type.
In Java, integer division discards the fractional part without ____.
What import statement is required to use Scanner in Java?
What is the correct operator precedence order in Java (highest to...
Which type of error occurs when the program runs but produces an...
Which type of error occurs when a semicolon is missing in Java?
What is the result of the expression: 2 + 3 * 4?
What is the output of: int count = 3; count++; count--;...
What is the output of: int count = 3; count++;...
How do you read a single character using Scanner in Java?
Which Scanner method is used to read a full line of text including...
What is the output of: System.out.println("sum: " + (2 + 3));?
What is the output of: System.out.println("sum: " + 2 + 3);?
What is the result of: int units = 3; double unitPrice = 12.50; double...
What is the output of: double price = 9.99; int roundedDown = (int)...
Which of the following correctly performs a narrowing type conversion...
What is the output of: double a = 5 / 2; System.out.println(a);?
What is the output of: double speed = 7.0 / 2.0;...
What does the % operator return when evaluating 7 % 2?
In Java, what is the result of: int each = 7 / 2;?
What is the result of the expression: (2 + 3) * 4?
play-Mute sad happy unanswered_answer up-hover down-hover success oval cancel Check box square blue
Alert!