Arduino Questions and Answers – Type Conversions

This set of Arduino Multiple Choice Questions & Answers (MCQs) focuses on “Type Conversions”.

1. In C++ what type of operator is a cast operator?
a) Unary
b) Binary
c) Ternary
d) Quaternary
View Answer

Answer: a
Explanation: The cast operator is a unary operator which is used to convert one data type into another forcefully. One example is:

void setup() {
    float f=34.5;
    int i = static_cast<int>(f);
}

The code above results in the variable ‘i’ having a value of 34 stored in it.

advertisement
advertisement

2. What is Type Casting?
a) Converting a file from one type to another
b) Creating new arrays
c) Deleting the variable from memory
d) Converting a variable from one type to another
View Answer

Answer: d
Explanation: Casting is the process of forcefully converting a variable from one data type to another. There are 4 types of casting; static cast, dynamic cast, const cast, reinterpret cast.

3. Who carries out implicit type casting?
a) The programmer
b) The assembler
c) The compiler
d) The microcontroller
View Answer

Answer: c
Explanation: This is also called type promotion and is carried out automatically by the compiler wherein it automatically converts a variable from one data type to another data type which is higher than that. Usually this is done to avoid any loss of data during calculations of floating type numbers.
Note: Join free Sanfoundry classes at Telegram or Youtube

4. Who initiates explicit type casting?
a) The programmer
b) The compiler
c) The microcontroller
d) The assembler
View Answer

Answer: a
Explanation: Explicit type casting is the process wherein the programmer specifies in the code the source and destination data type of any variable whose type needs to be converted. Unlike Implicit type casting, here the compiler doesn’t initiate the conversion until the programmer says so.

5. What is the output of the given code?

advertisement
void setup() {
    double x=11.3;
    int s=(int)x+12;
    Serial.begin(9600);
    Serial.print(s);
}
void loop() {
    //Do nothing.
}

a) 12.2
b) 23.3
c) null
d) 23
View Answer

Answer: d
Explanation: Here normally since we have the value of the variable ‘x’ as 11.3, if we normally add 12 to it the answer should have been 23.3. However, since we are converting the value of ‘x’ to an integer, the value of ‘x’ gets rounded off to 11 before the operation. Hence, we have the value 23.
advertisement

6. What is the conversion type for reinterpret casting?
a) Pointer to Variable
b) Variable to Variable
c) Pointer to Pointer
d) Variable to Pointer
View Answer

Answer: c
Explanation: Reinterpret casting is used to convert a pointer of any data type to another pointer of any other data type. Additionally, this does not match the datatype of the value and the pointer.

7. What is the output of the following code?

void print(char *str)
{
    Serial.print(x);
}
void setup()
{
    Serial.begin(9600);
    const char *x=”Hello”;
    print(const<char *>(x));
    return 0;
}

a) Hello
b) H
c) Ello
d) null
View Answer

Answer: a
Explanation: The code above uses the const_cast function of C++, which toggles whether a variable is constant or not, i.e. if it is constant then removes the constant tag and vice versa. In the code above we see how to pass a constant variable through a function which accepts only non-constant variables.

8. What is the use of the typeid() function?
a) To find the datatype of the variable
b) To find the “const” status of the variable
c) To find the address of the variable
d) To find the value of the variable
View Answer

Answer: a
Explanation: The typeid() function returns the datatype of the expression or variable that is passed through it. It returns a constant type_info object which can be used as a string in further programming.

9. Given below are 3 lines of code. Select the options given below with respect to the syntactical correctness regarding both C and C++?

  1. short x = 200;
  2. int b = (int) a;
  3. int c = int (a);

a) Lines 1 and 2 are incorrect
b) Lines 2 and 3 are incorrect
c) Only line 1 is incorrect
d) No lines are incorrect
View Answer

Answer: d
Explanation: In the code snippet given above, none of the lines are incorrect. Lines 2 and 3 exhibit the explicit type casting method for both C-Like cast notation and functional notation respectively, while line 1 simply declares and initiates a variable x of the short data type and value of 200.

10. Is there any difference between the usage of the size() and the length() function?
a) Yes
b) No
View Answer

Answer: b
Explanation: The size() and length() functions both return the number of characters that constitute the string given and can be used interchangeably from a utilitarian standpoint. There is absolutely no difference between the two functions.

Sanfoundry Global Education & Learning Series – Arduino.

To practice all areas of Arduino, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.