Java Quiz may actually have two correct answers

I’m learning Java and I just finished a Java quiz and on this test, there are actually two answers that would be correct , answer 1 and 2.

(Just in case the copy and paste doesn’t work, here’s the actual text)

========================================================

Which of the following lines would throw a compilation error?

  1. char grade_on_test = ‘F’;
  2. double isRaining = false;
  3. String gradeOnTest = “A”;
  4. int balance = -30;

1 and 2 will produce errors as both are incorrect Java, #2 is obvious as true or false is boolean, #1 because you can’t use underscores on variables at all in Java.

Both would produce errors to that effect, so either one would be a correct answer on this quiz to me. Any thoughts on why 2 is the preferred response?

If you find an issue with the content, it’s best to contact CS about it. No one from CC is on this forum any longer.

As the explanation in your screenshot mentions, the underscores violate best practices and naming conventions for Java, but they won’t cause a compilation error.

Have you tried experimenting with this? Do you get an error if you use underscores? Write a short program with a variable containing underscore(s). Does it compile or not?

1 Like