Modern Java · interview question
Given the following Java text block for a SQL query, what will be the result of the indentation?
Multiple choiceMediumchapter-12-curatedpremiummodern-javatext-blocks
String query = """
SELECT * FROM products
WHERE price > 100
ORDER BY name;
""";Answer and explanation
Correct answer
B. The indentation will be trimmed to match the least-indented line.
Why
The Java compiler trims leading whitespace up to the level of the least-indented line in a text block. This helps maintain alignment and readability without altering the intended structure.
Test yourself on Modern Java
A free test with instant, explained results. No account needed.
More Modern Java questions
- Given a Java text block, how does the compiler handle incidental whitespace?
- How do you write a multiline string in Java using text blocks?
- In which scenario is a static factory method preferred over a constructor?
- In Java 17, how would you refactor a switch statement to a switch expression for better readability and safety in a…
- Why does overriding hashCode in a value object change runtime behavior?
- How does using Java text blocks simplify embedding JSON structures in code?