0
0
C Sharp (C#)programming~20 mins

Logical patterns (and, or, not) in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logical Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of combined logical operators
What is the output of this C# code snippet?
C Sharp (C#)
bool a = true;
bool b = false;
bool c = true;

if (a && b || c)
{
    Console.WriteLine("Yes");
}
else
{
    Console.WriteLine("No");
}
ANo
BRuntime exception
CCompilation error
DYes
Attempts:
2 left
💡 Hint
Remember that && has higher precedence than || in C#.
Predict Output
intermediate
2:00remaining
Result of negation with logical operators
What will be printed by this C# code?
C Sharp (C#)
bool x = false;
bool y = true;

if (!(x || y) && y)
{
    Console.WriteLine("True branch");
}
else
{
    Console.WriteLine("False branch");
}
ACompilation error
BNo output
CFalse branch
DTrue branch
Attempts:
2 left
💡 Hint
Evaluate the expression inside the negation first.
🔧 Debug
advanced
3:00remaining
Identify the logical error in condition
This code is intended to print "Access granted" only if the user is an admin and is active. What is wrong with the condition?
C Sharp (C#)
bool isAdmin = true;
bool isActive = false;

if (isAdmin || isActive)
{
    Console.WriteLine("Access granted");
}
else
{
    Console.WriteLine("Access denied");
}
AThe condition uses || instead of &&, so it grants access if either is true.
BThe condition should use !isAdmin && !isActive.
CThe else block is missing a return statement.
DThe variables are not initialized properly.
Attempts:
2 left
💡 Hint
Think about when access should be granted.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in logical expression
Which option contains a syntax error in the logical expression?
Aif (a && (b || !c)) { }
Bif (a && b || !c { }
Cif (a && b || !c) { }
Dif (!(a || b) && c) { }
Attempts:
2 left
💡 Hint
Check for missing parentheses.
🚀 Application
expert
3:00remaining
Determine the number of true conditions
Given the following code, how many times will "Condition met" be printed?
C Sharp (C#)
bool[] flags = { true, false, true, false, true };
int count = 0;

foreach (bool flag in flags)
{
    if (flag && !(!flag || false))
    {
        Console.WriteLine("Condition met");
        count++;
    }
}

Console.WriteLine(count);
A3
B2
C5
D0
Attempts:
2 left
💡 Hint
Simplify the condition inside the if statement.