Apply for Zend Framework Certification Training

c

< Order of Precedence in C Types of Errors in C >




C Operator Precedence Quiz
Instructions: Choose the correct output for each question. Answers and explanations are provided below.
Questions
1. Arithmetic Precedence
What is the output?
#include<stdio.h>
int main(){
    int result = 10 + 5 * 2;
    printf("%d", result);
    return 0;
}
A) 30
B) 20
C) 25
D) 15
2. Division and Modulus
What is the output?
#include
<stdio.h>
int main(){
    int result = 20 / 3 + 5 % 2;
    printf("%d", result);
    return 0;
}
A) 7
B) 6
C) 8
D) 5
3. Parentheses
What is the output?
#include
<stdio.h>
int main(){
    int result = (10 + 5) * 2 - 4;
    printf("%d", result);
    return 0;
}
A) 26
B) 30
C) 22
D) 18
4. Relational and Logical Operators
What is the output?
#include
<stdio.h>
int main(){
    int result = 5 + 3 > 6 && 2 < 4;
    printf("%d", result);
    return 0;
}
A) 0
B) 1
C) 8
D) 4
5. Logical AND and OR
What is the output?
#include
<stdio.h>
int main(){
    int result = 0 || 1 && 0 || 1;
    printf("%d", result);
    return 0;
}
A) 0
B) 1
C) 2
D) Compilation error
6. Assignment Associativity
What is the output?
#include
<stdio.h>
int main(){
    int a, b, c;
    a = b = c = 5 + 2;
    printf("%d %d %d", a, b, c);
    return 0;
}
A) 5 5 5
B) 7 7 7
C) 7 5 2
D) 2 2 2
7. Increment and Arithmetic
What is the output?
#include
<stdio.h>
int main(){
    int a = 5;
    int result = ++a * 2 + 3;
    printf("%d %d", a, result);
    return 0;
}
A) 5 13
B) 6 15
C) 6 13
D) 7 17
8. Bitwise and Equality Operators
What is the output?
#include
<stdio.h>
int main(){
    int result = 4 & 2 == 0;
    printf("%d", result);
    return 0;
}
A) 0
B) 1
C) 2
D) 4
9. Conditional Operator
What is the output?
#include
<stdio.h>
int main(){
    int a = 8, b = 12;
    int result = a > b ? a + 2 : b + 2;
    printf("%d", result);
    return 0;
}
A) 10
B) 12
C) 14
D) 22
10. Mixed Operators
What is the output?
#include
<stdio.h>
int main(){
    int result = 20 - 6 / 2 + 3 * 4;
    printf("%d", result);
    return 0;
}
A) 23
B) 26
C) 29
D) 17

Answers with Explanations
1. Answer: B) 20
10 + 5 * 2
= 10 + 10
= 20
Explanation: Multiplication has higher precedence than addition.
2. Answer: A) 7
20 / 3 + 5 % 2
= 6 + 1
= 7
Explanation: Division and modulus have higher precedence than addition. Since the operands are integers, 20 / 3 gives 6.
3. Answer: A) 26
(10 + 5) * 2 - 4
= 15 * 2 - 4
= 30 - 4
= 26
Explanation: Parentheses are evaluated first.
4. Answer: B) 1
5 + 3 > 6 && 2 < 4
= 8 > 6 && 2 < 4
= 1 && 1
= 1
Explanation: Arithmetic operators are evaluated before relational operators, and relational operators are evaluated before logical AND.
5. Answer: B) 1
0 || 1 && 0 || 1
= 0 || 0 || 1
= 1
Explanation: Logical AND (&&) has higher precedence than logical OR (||). The OR operations are then evaluated from left to right.
6. Answer: B) 7 7 7
a = b = c = 5 + 2;
First, addition is evaluated:
a = b = c = 7;
Assignment operators have right-to-left associativity:
a = (b = (c = 7));
Therefore:
a = 7, b = 7, c = 7
7. Answer: B) 6 15
int a = 5;
int result = ++a * 2 + 3;
First, prefix increment increases a:
++a → 6
Then:
6 * 2 + 3
= 12 + 3
= 15
Therefore, a = 6 and result = 15.
8. Answer: B) 1
int result = 4 & 2 == 0;
Important precedence rule: Equality (==) has higher precedence than bitwise AND (&).
Therefore, the expression is evaluated as:
4 & (2 == 0)
= 4 & 0
= 0
Wait—the correct result is actually A) 0, not B.
Final Answer: A) 0
Explanation: 2 == 0 is false, producing 0. Then 4 & 0 is 0.
Exam Tip: Equality operators have higher precedence than bitwise AND in C.
9. Answer: C) 14
a > b ? a + 2 : b + 2
Since:
8 > 12 → false
The false expression is selected:
b + 2
= 12 + 2
= 14
Explanation: The conditional operator evaluates the condition first and selects one of the two expressions.
10. Answer: A) 23
20 - 6 / 2 + 3 * 4
First, division and multiplication:
20 - 3 + 12
Then addition and subtraction from left to right:
17 + 12
= 29
Correction: The correct answer is C) 29, not A.
Quick Revision
Rule Example
Parentheses first (2 + 3) * 4
Multiplication before addition 2 + 3 * 4
Same precedence → associativity 20 / 5 * 2
Relational before logical 5 > 3 && 2 < 4
&& before `
Assignment is right to left a = b = 10
Equality before bitwise AND 4 & 2 == 0

< Order of Precedence in C Types of Errors in C >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top