This set of Arduino Multiple Choice Questions & Answers (MCQs) focuses on “Operators”.
1. What is the output of the following program if ‘a’ and ‘b’ are both supplied with 5V?
int a=9;
int b=10;
void setup() {
Serial.begin(9600);
pinMode(a,INPUT);
pinMode(b,INPUT);
}
void loop() {
int x=digitalRead(a);
int y=digitalRead(b);
if(a!=b){
Serial.println(“Not Equal”);
}
else {
Serial.println(“Equal”);
}
}
a) Equal
b) Not Equal
c) Runtime Error
d) Compilation Error
View Answer
Explanation: 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
View Answer
Explanation: 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:-
int a=10, b=20, c;
c=(a<b)?a:b;
The above code will make c=10 since ‘a’ is lesser than ‘b’.
a++;
The above code will increment the value of ‘a’ by 1.
3. What will be the output of the following code?
void setup() {
Serial.begin(9600);
}
void loop() {
int a=92;
int b=101;
int c=a&b;
Serial.println(c);
}
a) 100
b) 69
c) 68
d) 40
View Answer
Explanation: 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?
void setup() {
Serial.begin(9600);
}
void loop() {
int *pointer;
int val=100;
pointer=&val;
Serial.println(*pointer);
}
a) 100
b) 2e444ad233
c) cc10091a2
d) a99102123
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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.
Sanfoundry Global Education & Learning Series – Arduino.
To practice all areas of Arduino, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Electrical Engineering Books
- Check Arduino Books
- Apply for Electrical Engineering Internship
- Practice Electrical & Electronics Engineering MCQs
- Check Electrical & Electronics Engineering Books