2.3.1 - Introduction to Operators.
1. Which of the following is an example of a unary operator in C?
- A) `+`
- B) `x++`
- C) `x + y`
- D) `x - y`
Correct Answer: B) x++
2. What is the result of the expression `int x = ++y;` when `y` is initially 5?
- A) `x = 5, y = 5`
- B) `x = 6, y = 5`
- C) `x = 5, y = 6`
- D) `x = 6, y = 6`
Correct Answer: D) x = 6, y = 6
3. In a pre-increment operation (`++x`), when does the value of `x` increase by 1?
- A) After it is used in an expression
- B) Before it is used in an expression
- C) Only if `x` is even
- D) Only if `x` is odd
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;
}
- A) 10 20 30
- B) 11 21 32
- C) 10 20 32
- D) 11 21 30
Correct Answer: B) 11 21 32
5. What does the pre-decrement operator (`--x`) do in C?
- A) Decreases the value of `x` after it is used in the expression
- B) Increases the value of `x` after it is used in the expression
- C) Decreases the value of `x` before it is used in the expression
- D) Increases the value of `x` before it is used in the expression
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?
- A) After it is used in the expression
- B) Before it is used in the expression
- C) It does not increment the value
- D) Immediately when the program runs
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--`)?
- A) Decreases `x` before it is used in the expression
- B) Increases `x` after it is used in the expression
- C) Decreases `x` after it is used in the expression
- D) Increases `x` before it is used in the expression
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;
}
- A) ++a = 10, --b = 100, ++c = 10.5, --d = 100.5
- B) ++a = 11, --b = 99, ++c = 11.5, --d = 99.5
- C) ++a = 11, --b = 99, ++c = 10.5, --d = 100.5
- D) ++a = 10, --b = 99, ++c = 11.5, --d = 99.5
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?
- A) Unary operators work on two operands, while binary operators work on one
- B) Unary operators work on one operand, while binary operators work on two
- C) Unary operators are faster than binary operators
- D) Unary operators work only with integers
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?
- A) A symbol that stores a value
- B) A symbol that manipulates a value or variable
- C) A function that calculates a value
- D) A keyword that declares a variable
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?
- A) The variable is used first, then incremented
- B) The variable is incremented first, then used
- C) The variable is incremented, but not used in the expression
- D) The variable is used, but not incremented
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?
- A) The variable is incremented by 1
- B) The variable is used first, then decremented by 1
- C) The variable is decremented first, then used
- D) The variable remains unchanged
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);
- A) 10
- B) -9
- C) -10
- D) -11
Correct Answer: C) -10
7. In a post-increment operation, which of the following is correct?
- A) The variable is used first, then incremented
- B) The variable is incremented first, then used
- C) The variable is decremented, then used
- D) The variable is decremented, but not used
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?
- A) Before its value is used in the statement
- B) After its value is used in the statement
- C) It is not decremented
- D) During the execution of the statement
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);
- A) 10
- B) -9
- C) -10
- D) -11
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);
- A) 11, 99, 11.5, 99.5
- B) 10, 100, 10.5, 100.5
- C) 11, 101, 11.5, 101.5
- D) 10, 100, 10.5, 100.5
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?
- A) If the left operand is greater than or equal to the right operand
- B) If the left operand is less than or equal to the right operand
- C) If the left operand is greater than the right operand
- D) If the left operand is less than the right operand
Correct Answer: A) If the left operand is greater than or equal to the right operand
23. What does the <= operator check?
- A) If the left operand is greater than or equal to the right operand
- B) If the left operand is less than or equal to the right operand
- C) If the left operand is greater than the right operand
- D) If the left operand is less than the right operand
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?
- A) 5 == 5 is 1
- B) 5 == 5 is 0
- C) 5 == 5 is true
- D) 5 == 5 is false
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?
- A) Sets bits that are 1 in either operand
- B) Clears bits that are 0 in both operands
- C) Inverts all bits
- D) Shifts bits to the right
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;
}
- A) 212, 106, 53
- B) 0, 1, 2
- C) 1, 2, 4
- D) 106, 53, 26
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;
}
- A) 212, 424, 848
- B) 0, 1, 2
- C) 848, 424, 212
- D) 53, 106, 212
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;
}
- A) -36, 11
- B) 35, -12
- C) 36, 12
- D) -35, 12
Correct Answer: A) -36, 11
50. What is the ternary operator (also called the conditional operator) used for in C?
- A) Comparing three numbers
- B) Repeating a block of code
- C) Making decisions between two values based on a condition
- D) Swapping values between two variables
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;
}
- A) You can vote
- B) You cannot vote
- C) Error
- D) Depends on the input
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?
- A) Returns the size of the variable
- B) Returns the value stored in the variable
- C) Returns the address of the variable
- D) Inverts all the bits of the 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);
}
- A) 5
- B) Address of i
- C) 0
- D) Undefined
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?
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.