2.3 - Operators.


Back Button Home

2.3.1 - Introduction to Operators.


1. Which of the following is an example of a unary operator in C?
Correct Answer: B) x++
2. What is the result of the expression `int x = ++y;` when `y` is initially 5?
Correct Answer: D) x = 6, y = 6
3. In a pre-increment operation (`++x`), when does the value of `x` increase by 1?
Correct Answer: B) Before it is used in an expression
4. What is the output of the following program?
                #include <stdio.h>
                int main() {
                    int x = 10, y = 20;
                    int p = ++x + ++y;
                    printf("%d\n", x);
                    printf("%d\n", y);
                    printf("%d\n", p);
                    return 0;
                }
                
Correct Answer: B) 11 21 32
5. What does the pre-decrement operator (`--x`) do in C?
Correct Answer: C) Decreases the value of x before it is used in the expression
6. In post-increment (`x++`), when is the value of the variable incremented?
Correct Answer: A) After it is used in the expression
7. What will be the value of `p` in the following program?
                #include <stdio.h>
                int main() {
                    int x = 10, y = 20;
                    int p = x++ + y++;
                    printf("%d\n", p);
                    return 0;
                }
                
Correct Answer: A) 30
8. Which of the following statements correctly describes a post-decrement operation (`x--`)?
Correct Answer: C) Decreases x after it is used in the expression
9. What is the output of the following program?
                #include <stdio.h>
                int main() {
                    int a = 10, b = 100;
                    float c = 10.5, d = 100.5;
                    printf("++a = %d \n", ++a);
                    printf("--b = %d \n", --b);
                    printf("++c = %f \n", ++c);
                    printf("--d = %f \n", --d);
                    return 0;
                }
                
Correct Answer: B) ++a = 11, --b = 99, ++c = 11.5, --d = 99.5
10. What is the main difference between unary and binary operators in C?
Correct Answer: B) Unary operators work on one operand, while binary operators work on two

2.3.1 - Introduction to Operators.


1. What is an operator in C?
Correct Answer: B) A symbol that manipulates a value or variable
2. Which of the following is a unary operator?
Correct Answer: B) ++
3. In a pre-increment operation, which of the following is correct?
Correct Answer: B) The variable is incremented first, then used
4. What will be the output of the following code?
int x = 10, y = 20;
            int p = ++x + ++y;
            printf("%d\n", p);
Correct Answer: C) 32
5. In a pre-decrement operation, what happens to the variable?
Correct Answer: C) The variable is decremented first, then used
6. What will be the output of the following code?
int x = 10, y = 20;
            int p = --x - --y;
            printf("%d\n", p);
Correct Answer: C) -10
7. In a post-increment operation, which of the following is correct?
Correct Answer: A) The variable is used first, then incremented
8. What will be the output of the following code?
int x = 10, y = 20;
            int p = x++ + y++;
            printf("%d\n", p);
Correct Answer: A) 30
9. In a postfix decrement operation, when is the variable decremented?
Correct Answer: B) After its value is used in the statement
10. What will be the output of the following code?
int x = 10, y = 20;
            int p = x-- - y--;
            printf("%d\n", p);
Correct Answer: C) -10
11. Which of the following operators is used for binary operations?
Correct Answer: C) +
12. What will be the output of the following code?
int a = 10, b = 100;
            float c = 10.5, d = 100.5;
            printf("++a = %d \n", ++a);
            printf("--b = %d \n", --b);
            printf("++c = %f \n", ++c);
            printf("--d = %f \n", --d);
Correct Answer: A) 11, 99, 11.5, 99.5

2.3.2 - Arithmatic, Relatiional, Logical, Assignment and Operators.


13. Which operator is used for addition in C programming?
Correct Answer: B) +
14. What will be the result of the operation 9 % 4 in C programming?
Correct Answer: C) 1
15. Which operator is used for multiplication in C programming?
Correct Answer: B) *
16. What is the result of a / b when a = 9 and b = 4 if both are integers?
Correct Answer: B) 2
17. Which operator is used to find the remainder after division?
Correct Answer: A) %
18. Which operator checks if two values are equal?
Correct Answer: B) ==
19. Which operator checks if two values are not equal?
Correct Answer: B) !=
20. Which relational operator checks if the left operand is greater than the right operand?
Correct Answer: B) >
21. Which operator checks if the left operand is less than the right operand?
Correct Answer: C) <
22. What does the >= operator check?
Correct Answer: A) If the left operand is greater than or equal to the right operand
23. What does the <= operator check?
Correct Answer: B) If the left operand is less than or equal to the right operand
24. What is the output of the following code:
                printf("%d == %d is %d \n", a, b, a == b); 
                
if a = 5 and b = 5?
Correct Answer: A) 5 == 5 is 1
25. Which logical operator represents AND operation in C?
Correct Answer: A) &&
26. Which logical operator represents OR operation in C?
Correct Answer: B) ||
27. Which logical operator represents NOT operation in C?
Correct Answer: C) !
28. What is the result of the expression (a == b) && (c > b) if a = 5, b = 5, and c = 10?
Correct Answer: B) 1
29. What does the expression !(a == b) return when a = 5 and b = 5?
Correct Answer: A) 0
30. What is the output of (a != b) || (c < b) if a = 5, b = 5, and c = 10?
Correct Answer: A) 0
31. Which assignment operator adds the right operand to the left operand and assigns the result to the left operand?
Correct Answer: B) +=
32. Which assignment operator is used to subtract and assign the result?
Correct Answer: A) -=
33. Which assignment operator multiplies the left operand with the right operand and assigns the result to the left operand?
Correct Answer: C) *=
34. What is the output of the following code:
                int a = 5, c; 
                c = a; 
                c += a; 
                printf("%d", c);
                
Correct Answer: B) 10
35. Which assignment operator divides the left operand by the right operand and assigns the result to the left operand?
Correct Answer: A) /=
36. Which assignment operator is used to take modulus and assign the result?
Correct Answer: A) %=
37. What is the output of the following code:
                int a = 5, c; 
                c = a; 
                c %= a; 
                printf("%d", c);
                
Correct Answer: B) 0

2.3.3 - Bitwise, Conditional and Miscellanous Operators.


38. Which operator in C is used to perform a bitwise AND operation on two numbers?
Correct Answer: C) &
39. What is the output of the following C code:
     #include 

     int main() {
         int a = 12, b = 25;
         printf("Output = %d", a & b);
         return 0;
     }
                
Correct Answer: B) 8
40. What does the bitwise OR operator ( | ) do in C?
Correct Answer: A) Sets bits that are 1 in either operand
41. What is the output of the following C code:
                #include 
            
                int main() {
                    int a = 12, b = 25;
                    printf("Output = %d", a | b);
                    return 0;
                }
                
Correct Answer: B) 37
42. Which operator performs a bitwise XOR operation in C?
Correct Answer: C) ^
43. What is the result of a bitwise XOR operation between 12 and 25 in C?
        #include 
    
        int main() {
            int a = 12, b = 25;
            printf("Output = %d", a ^ b);
            return 0;
        }
                
Correct Answer: C) 21
44. Which operator in C shifts the bits of a number to the left?
Correct Answer: B) <<
45. Which operator in C shifts the bits of a number to the right?
Correct Answer: A) >>
46. What is the output of the following code snippet for right shift operation in C?
#include 
int main() {
    int num = 212, i;
    for (i = 0; i <= 2; ++i) {
        printf("Right shift by %d: %d\n", i, num >> i);
    }
    return 0;
}
                
Correct Answer: A) 212, 106, 53
47. What is the output of the following code snippet for left shift operation in C?
#include 
int main() {
    int num = 212, i;
    for (i = 0; i <= 2; ++i) {
        printf("Left shift by %d: %d\n", i, num << i);
    }
    return 0;
}
                
Correct Answer: A) 212, 424, 848
48. Which operator in C inverts all bits of a number?
Correct Answer: D) ~
49. What is the output of the following code using the bitwise NOT operator?
                #include 
            
                int main() {
                    printf("Output = %d\n", ~35);
                    printf("Output = %d\n", ~-12);
                    return 0;
                }
                
Correct Answer: A) -36, 11
50. What is the ternary operator (also called the conditional operator) used for in C?
Correct Answer: C) Making decisions between two values based on a condition
51. What is the output of the following C code using the conditional (ternary) operator?
#include 
int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    (age >= 18) ? printf("You can vote") :
     printf("You cannot vote");
    return 0;
}
                
Correct Answer: D) Depends on the input
52. Which operator returns the size of a variable in C?
Correct Answer: C) sizeof
53. What is the output of the following C code using the sizeof operator?
 #include 
 int main() {
     int a = 5;
     printf("Size of a = %lu\n", sizeof(a));
     return 0;
 }
                
Correct Answer: B) 4
54. What does the & operator do in C when applied to a variable?
Correct Answer: C) Returns the address of the variable
55. Which operator in C is used as a pointer dereference operator to access the value at a given address?
Correct Answer: B) *
56. What is the output of the following C code using pointer dereferencing?
 #include 
 void main() {
     int i = 5;
     int *ptr;
     ptr = &i;
     printf("Value of *ptr is %d.\n", *ptr);
 }
                
Correct Answer: A) 5

2.3.4 - Operators Precedence and Associativity.


57. Which of the following operators has the highest precedence?
  • (A) + (addition)
  • (B) * (multiplication)
  • (C) = (assignment)
  • (D) || (logical OR)
Correct Answer: (B) * (multiplication).
58. How will the expression 2 + 3 * 4 be evaluated based on operator precedence?
  • (A) (2 + 3) * 4
  • (B) 2 + (3 * 4)
  • (C) (2 * 3) + 4
  • (D) None of the above
Correct Answer: (B) 2 + (3 * 4).
59. Which operator has the lowest precedence among the following?
  • (A) +
  • (B) *
  • (C) ||
  • (D) =
Correct Answer: (D) =.
60. What is the result of the expression (2 + 3) * 4 when parentheses are used to change precedence?
  • (A) 14
  • (B) 20
  • (C) 12
  • (D) 24
Correct Answer: (B) 20.
61. What is the associativity of the assignment operator (=) in the expression x = y = z?
  • (A) Left-to-right
  • (B) Right-to-left
  • (C) No associativity
  • (D) Top-to-bottom
Correct Answer: (B) Right-to-left.
62. In the expression 2 + 3 + 4, which operation will be performed first due to left-to-right associativity?
  • (A) (2 + 3) first
  • (B) (3 + 4) first
  • (C) (2 + 4) first
  • (D) The order does not matter
Correct Answer: (A) (2 + 3) first.
63. Which of the following statements is true regarding operator precedence?
  • (A) Parentheses can override operator precedence.
  • (B) Addition has higher precedence than multiplication.
  • (C) Logical AND (&&) has higher precedence than multiplication.
  • (D) Assignment operators always have the highest precedence.
Correct Answer: (A) Parentheses can override operator precedence.
64. Which operator is left-associative?
  • (A) * (multiplication)
  • (B) = (assignment)
  • (C) || (logical OR)
  • (D) && (logical AND)
Correct Answer: (A) * (multiplication).
65. In the expression a = b + c * d, which operation is performed first?
  • (A) b + c
  • (B) a = b
  • (C) c * d
  • (D) None of the above
Correct Answer: (C) c * d.
66. What is the associativity of the logical OR (||) operator?
  • (A) Left-to-right
  • (B) Right-to-left
  • (C) No associativity
  • (D) Top-to-bottom
Correct Answer: (A) Left-to-right.