250+ TOP MCQs on Operators and Answers

Arduino Multiple Choice Questions on “Operators”.

1. What is the output of the following program if ‘a’ and ‘b’ are both supplied with 5V?

  1. int a=9;
  2. int b=10;
  3. void setup() {
  4.     Serial.begin(9600);
  5.     pinMode(a,INPUT);
  6.     pinMode(b,INPUT);
  7. }
  8. void loop() {
  9.     int x=digitalRead(a);
  10.     int y=digitalRead(b);
  11.     if(a!=b){
  12.         Serial.println(“Not Equal”);
  13.     }
  14.     else {
  15.         Serial.println(“Equal”);
  16.     }
  17. }

a) Equal
b) Not Equal
c) Runtime Error
d) Compilation Error
Answer: a
Calrification: In the program above, we use a simple Boolean operator to demonstrate the “not equal to” operator. Since values of ‘x’ and ‘y’ are exactly 1 as stored by the Arduino, the ‘else’ case of the program runs and prints the output to the Serial Monitor.

2. What is the difference between ternary operators and unary operators?
a) Ternary Operators work with 1 operand while unary operators work with 3 operands
b) Ternary Operators work with 3 operands while unary operators work with 1 operand
c) Ternary Operators work with 2 operands while unary operators work with 3 operands
d) Ternary Operators work with 4 operands while unary operators work with 1 operand
Answer: b
Calrification: The term “ternary” depicts three and the term “unary” depicts the term one. So therefore, ternary operators need 3 operands and unary operators use 1 operand.
Eg:-

  1. int a=10, b=20, c;
  2. c=(a
    

The above code will make c=10 since ‘a’ is lesser than ‘b’.

The above code will increment the value of ‘a’ by 1.

3. What will be the output of the following code?

  1. void setup() {
  2.     Serial.begin(9600);
  3. }
  4. void loop() {
  5.     int a=92;
  6.     int b=101;
  7.     int c=a&b;
  8.     Serial.println(c);
  9. }

a) 100
b) 69
c) 68
d) 40
Answer: c
Calrification: The above code uses the ‘&’ symbol to perform a bitwise AND operation on ‘a’ and ‘b’ which serve as the operands. It performs an AND operation on each bit starting from the most significant bit to the least significant bit. The resulting binary number is then converted to int and then printed on the serial monitor.

4. What is the output of the following code?

  1. void setup() {
  2.     Serial.begin(9600);
  3. }
  4. void loop() {
  5.     int *pointer;
  6.     int val=100;
  7.     pointer=&val;
  8.     Serial.println(*pointer);
  9. }

a) 100
b) 2e444ad233
c) cc10091a2
d) a99102123
Answer: a
Calrification: The above code plays around with a feature called pointers. In line 5 we create a pointer to an integer datatype. Then in the next line we initialize a variable with a value. Then we store the address of that variable to the pointer. And finally, we access the value of the pointer using the “*” operator.

5. What is the name of the | operator?
a) Logical OR
b) Bitwise OR
c) Logical AND
d) Bitwise AND
Answer: b
Calrification: The ‘|’ operator is used to perform a bitwise OR operation on any two numbers which serve as the operands. It performs an OR operation on each bit starting from the most significant bit to the least significant bit.

6. What is the name of the ~ operator?
a) Bitwise NOT
b) Logical NOT
c) Bitwise SHIFT
d) Pointer Address
Answer: a
Calrification: The ~ operator is used to perform a bitwise NOT operation on any two numbers which serve as the operands. It performs a NOT operation on each bit starting from the most significant bit to the least significant bit.

7. What is the name of the ^ operator?
a) Bitwise XNOR
b) Bitwise NAND
c) Bitwise XOR
d) Bitwise AND
Answer: c
Calrification: The ^ operator is used to perform a bitwise XOR operation on any two numbers which serve as the operands. It performs an XOR operation on each bit starting from the most significant bit to the least significant bit.

8. What is the purpose of the << operator?
a) Bitwise AND
b) Bitwise Right Shift
c) Bitwise Left Shift
d) Bitwise OR
Answer: c
Calrification: The << operator takes two numbers as operands. Then it shifts the bits of the first operand by the value present in the second operand from right to left. The second operand however has to be a number less than 32 only. Otherwise the operation will not work.

9. Is there a difference between the = and the == operators?
a) Yes
b) No
Answer: a
Calrification: Yes, there is a difference between those two operators. The = operator signifies any assignment of values to some variable or data storage, while the == operator is a comparison operator which checks the equality of the operands it works with.

10. How many operands does the >= operator require?
a) 1
b) 2
c) 3
d) 4
Answer: b
Calrification: The >= operator is a binary operator and it requires only 2 operands. The operator takes the two operands as input which should be 2 numbers and then it outputs a Boolean True or a False depending upon whether the operand 1 is greater than or equal to operand 2.

Global Education & Learning Series – Arduino.