Arduino Questions and Answers – The sizeof() Function

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

Answer: a
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

Answer: a
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?

advertisement
advertisement
void setup() {
    Serial.begin(9600);
    Serial.print(sizeof(int));
}

a) 3
b) 1
c) 2
d) 8
View Answer

Answer: c
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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

Answer: a
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

Answer: a
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.
advertisement

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

Answer: c
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.
advertisement

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

Answer: c
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

Answer: c
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

Answer:a
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

Answer:a
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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.