This set of Arduino Multiple Choice Questions & Answers (MCQs) focuses on “The sizeof() Function”.
1. What is the meaning of the data returned by the sizeof() function?
a) Length
b) Location
c) Pointer
d) Memory
View Answer
Explanation: When the sizeof() function is used, it returns a number which corresponds to the length of the structure in question. It’s used by programmers for finding the, say number of elements in an array, or the number of characters in a string, etc…
2. When the sizeof() function is invoked on an array. What information does it return?
a) Number of elements in the array
b) The largest element in the array
c) The total sum of the ASCII values of the elements in the array
d) The largest ASCII value of any element in the array
View Answer
Explanation: The primary use of the sizeof() function is to return the length or size of an element that is given to it. So, if this element is an array which is a collection of elements, it will give the number of elements present inside the array.
3. What will the output for the code given below be, if executed on an Arduino UNO?
void setup() { Serial.begin(9600); Serial.print(sizeof(int)); }
a) 3
b) 1
c) 2
d) 8
View Answer
Explanation: The above code uses the sizeof() function to find the capacity of the integer for that particular board. It returns the number of bytes allocated for an int. Normally on 64-bit systems like laptops and desktop computers, this would give a value of 4, however on an Arduino UNO, it will give a value of 2 since the ATMega Board powering the Arduino UNO has a 16-bit architecture.
4. What function will we use to find out the number of elements in an array?
a) sizeof()
b) size()
c) sf()
d) sizeOf()
View Answer
Explanation: The primary use of the sizeof() function is to return the length or size of an element that is given to it. So, if this element is an array which is a collection of elements, it will give the number of elements present inside the array.
5. What notation should we use for denoting the breadth of an array?
a) sizeof(a[0])
b) sizeof(a)
c) sizeof(a[])
d) sizeof(a.0)
View Answer
Explanation: When the first element of an array is invoked in the sizeof() function, if the array is a 2D array then it will give the number of elements present in the breadth of the matrix.
6. What is the output of the following line of code?
printf("%lu\n", sizeof(char));
a) 21
b) %fdf
c) 1
d) null
View Answer
Explanation: When sizeof() is used with the data types such as int, float, char, etc. It returns the amount of memory that is allocated to the respective data types.
7. What will be the output of the following code given below?
int a = 0; double d = 10.21; printf("%lu", sizeof(a + d));
a) 23
b) null
c) 8
d) 10.21
View Answer
Explanation: The sizes of int and double are 4 and 8 respectively, a is an integer variable whereas d is a double variable. The final result will be a double in this case in order to keep the precision. Hence the output of the code is 8 bytes.
8. What does the following code do?
int* ptr = (int*)malloc(100 * sizeof(int));
a) Static Memory Allocation
b) Static Memory Clearance
c) Dynamic Memory Allocation
d) Dynamic Memory Clearance
View Answer
Explanation: The sizeof() function here is being used to allocate a block of memory. This code is dynamic because the size of int is different on different machine architectures. So, we have allocated a block of memory that is enough to hold 100 integers irrespective of the size of the int data type in the machine in which this code is going to be executed.
9. What is the output of the code given below?
int x = 3; printf("%d\n", sizeof(x++)); printf("x = %d", x);
a) 3
b) 4
c) Runtime Error
d) null
View Answer
Explanation: The output of the code will be 3 instead of 4 because inspite of the increment operator put inside the sizeof() function, the function never really processes the variable as a whole and puts into memory, but only processes the value that is held.
10. What will be the output of the following code given below?
int a = 10; double d = 10.21; printf("%lu", sizeof(a + d));
a) 8
b) null
c) 9
d) 20.21
View Answer
Explanation: The sizes of int and double are 4 and 8 respectively, a is an integer variable whereas d is a double variable. The final result will be a double in this case in order to keep the precision. Hence the output of the code is 8 bytes.
Sanfoundry Global Education & Learning Series – Arduino.
To practice all areas of Arduino, here is complete set of 1000+ Multiple Choice Questions and Answers.