Which declaration initializes a boolean to true?

Prepare for the Computer Programming Test. Study with flashcards and multiple-choice questions, each with detailed hints and explanations. Gear up for success!

Multiple Choice

Which declaration initializes a boolean to true?

Explanation:
Booleans hold either true or false, so to initialize a boolean to true you must use the boolean literal true (without quotes). The declaration that uses true directly sets the value to the actual boolean true, which is the correct way to initialize a boolean variable to that state. Using false would set it to false, not true. Putting "true" in quotes makes it a string, not a boolean value, so that isn’t valid for a boolean assignment. Writing 1 uses a numeric value, and booleans expect a true or false literal rather than a number. So the only correct way to initialize to true is the declaration that uses the true literal.

Booleans hold either true or false, so to initialize a boolean to true you must use the boolean literal true (without quotes). The declaration that uses true directly sets the value to the actual boolean true, which is the correct way to initialize a boolean variable to that state. Using false would set it to false, not true. Putting "true" in quotes makes it a string, not a boolean value, so that isn’t valid for a boolean assignment. Writing 1 uses a numeric value, and booleans expect a true or false literal rather than a number. So the only correct way to initialize to true is the declaration that uses the true literal.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy