A bool type in C# can hold only two values: true or false.
Click to reveal answer
beginner
How does C# treat non-boolean values in conditional statements?
C# does not allow non-boolean values in conditions. For example, integers like 0 or 1 cannot be used directly as conditions; only bool expressions are valid.
Click to reveal answer
beginner
What is the default value of a bool variable in C#?
The default value of a bool variable is false.
Click to reveal answer
intermediate
Explain short-circuit evaluation with && and || operators in C#.
In C#, && (AND) and || (OR) operators use short-circuit evaluation. This means the second operand is evaluated only if needed. For example, in a && b, if a is false, b is not checked.
Click to reveal answer
intermediate
Can you convert a bool to an integer directly in C#?
No direct cast from bool to int. Use Convert.ToInt32(myBool) or conditional expressions, e.g., int x = myBool ? 1 : 0;.
Click to reveal answer
Which of the following is a valid bool value in C#?
A1
Btrue
C"false"
D0
✗ Incorrect
Only true and false are valid bool values in C#.
What happens if you try to use an integer directly in an if condition in C#?
AIt causes a compile-time error.
BIt treats all integers as true.
CIt works like in C, where 0 is false and non-zero is true.
DIt treats all integers as false.
✗ Incorrect
C# requires a bool expression in conditions; integers cannot be used directly.
What is the default value of a bool field in a class if not initialized?
AUndefined
Btrue
Cnull
Dfalse
✗ Incorrect
Boolean fields default to false if not explicitly initialized.
In the expression a && b, when is b evaluated?
AAlways
BNever
COnly if <code>a</code> is true
DOnly if <code>a</code> is false
✗ Incorrect
b is evaluated only if a is true because of short-circuit evaluation.
How can you convert a bool variable flag to an integer 1 or 0?
Aint x = flag ? 1 : 0;
Bint x = (int)flag;
Cint x = Convert.ToInt32(flag);
Dint x = flag + 0;
✗ Incorrect
Both the conditional (ternary) operator and Convert.ToInt32(flag) safely convert bool to int (1 for true, 0 for false). Direct casting does not work.
Describe how C# handles boolean values and their use in conditional statements.
Think about what values are allowed and how conditions work.
You got /3 concepts.
Explain short-circuit evaluation in C# boolean operators and why it matters.
Consider when the second part of a condition runs.
You got /3 concepts.
Practice
(1/5)
1. Which of the following is a valid Boolean value in C#?
easy
A. yes
B. 1
C. 0
D. true
Solution
Step 1: Understand Boolean values in C#
Booleans in C# can only be true or false.
Step 2: Check each option
Only true is a valid Boolean. Numbers or words like yes are not Booleans.
Final Answer:
true -> Option D
Quick Check:
Boolean values = true or false [OK]
Hint: Booleans are only true or false, nothing else [OK]
Common Mistakes:
Thinking 1 or 0 are Boolean values
Using strings like 'yes' or 'no' as Booleans
Confusing integers with Booleans
2. Which of the following is the correct way to declare a Boolean variable in C#?
easy
A. bool isActive = true;
B. boolean isActive = true;
C. Bool isActive = true;
D. bool isActive = "true";
Solution
Step 1: Recall C# Boolean declaration syntax
In C#, the keyword for Boolean type is bool (all lowercase).
Step 2: Check each option
bool isActive = true; uses bool and assigns true correctly. boolean isActive = true; uses boolean which is incorrect in C#. Bool isActive = true; uses Bool with uppercase B, which is invalid. bool isActive = "true"; assigns a string "true" instead of Boolean true.
Final Answer:
bool isActive = true; -> Option A
Quick Check:
Boolean declaration uses 'bool' keyword [OK]
Hint: Use lowercase 'bool' and assign true or false without quotes [OK]
Common Mistakes:
Using 'boolean' instead of 'bool'
Capitalizing 'Bool'
Assigning Boolean as a string
3. What will be the output of the following C# code?
bool a = true; bool b = false; Console.WriteLine(a && b);
medium
A. true
B. 1
C. False
D. 0
Solution
Step 1: Understand the '&&' operator with Booleans
The '&&' operator means AND. It returns true only if both sides are true.
Step 2: Evaluate the expression
Here, a is true and b is false. true AND false is false, which Console.WriteLine outputs as False.
In C#, Boolean literals are lowercase: true and false. True with uppercase T is invalid.
Step 2: Verify other parts of code
Semicolon is present after variable declaration. Console.WriteLine syntax is correct. Variable name 'isReady' is valid.
Final Answer:
Boolean value should be lowercase true -> Option B
Quick Check:
Boolean literals are lowercase true/false [OK]
Hint: Boolean literals are always lowercase true or false [OK]
Common Mistakes:
Using uppercase True or False
Thinking semicolon is missing after if
Assuming variable names are restricted
5. You want to write a C# program that prints "Access granted" only if a user is logged in and has admin rights. Which Boolean expression correctly checks this?
bool isLoggedIn = true; bool isAdmin = false; // Which condition to use in if?
hard
A. if (isLoggedIn && isAdmin)
B. if (isLoggedIn || isAdmin)
C. if (!isLoggedIn && isAdmin)
D. if (!isLoggedIn || !isAdmin)
Solution
Step 1: Understand the requirement
The program should print "Access granted" only if the user is logged in AND has admin rights.
Step 2: Choose the correct Boolean operator
AND operator && ensures both conditions are true. OR || would allow access if either is true, which is incorrect.
Final Answer:
if (isLoggedIn && isAdmin) -> Option A
Quick Check:
Use AND to require both conditions true [OK]
Hint: Use && to require both conditions true simultaneously [OK]
Common Mistakes:
Using OR instead of AND
Negating conditions incorrectly
Confusing ! (not) operator usage
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.