MCQ
Q.
Which of the following C++ statements correctly declares and initializes an integer variable named
count with the value 10 and a single-precision floating-point variable named price with the value 99.99?
Correct Answer: A
The correct answer is A.
🔑 Key Points
- In C++, an integer variable is declared using the
intkeyword, e.g.,int count = 10;. - A single-precision floating-point variable is declared using the
floatkeyword. - When initializing a
floatvariable with a literal value, it's crucial to append theforFsuffix (e.g.,99.99f). Without the suffix, a decimal literal like99.99is treated as adoubleby default. - Option A correctly uses
intfor the integer andfloatwith thefsuffix for the floating-point literal, ensuring proper type matching and avoiding potential narrowing conversion warnings.
📄 Additional Information
- C++ provides several fundamental data types, including
intfor integers,floatfor single-precision floating-point numbers,doublefor double-precision floating-point numbers, andcharfor characters. - Implicit conversion from
doubletofloat(as in option C:float price = 99.99;) is a narrowing conversion, which might lead to loss of precision and can generate a compiler warning in some cases. Explicitly using thefsuffix makes the code more robust and clear about the intended type of the literal. - Variables must be declared before they are used, specifying their type and name.