Question 7
Back to Exercise
Access should be granted to admins or editors whose account is active. The admin's account is inactive. What does the program print, and why?
#include <stdbool.h>
#include <stdio.h>
int main(void) {
bool is_admin = true, is_editor = false, is_active = false;
if (is_admin || is_editor && is_active)
puts("access granted");
else
puts("access denied");
return 0;
}
- A access denied
- B access granted – && binds tighter, so the test is is_admin || (is_editor && is_active); brackets are needed: (is_admin || is_editor) && is_active Correct
- C Nothing
- D It does not compile
Correct Answer: Option B
Because && has higher precedence than ||, any admin passes regardless of is_active. Compilers warn about && within ||; bracket the intended grouping.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!