What is the output of the given code? [code] void setup() { double x=11.3; int s=(int)x+12; Serial.begin(9600); Serial.print(s);

What is the output of the given code?
Code:
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
 
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.
 

Tìm kiếm

Back
Top