CPA-21-02 practice question 15 of 35
What happens when you attempt to compile and run the following code? #include using namespace std; int op(int x, int y); int main() { float *pf; float f=0.9; pf=&f; cout
Full set: 257 questionsThe variable f is initialized to 0.9, and the pointer pf points to f. When op(1, pf) is called, the dereferenced float value 0.9 is passed as the second argument. The function op expects an int for both parameters. C++ performs an implicit standard conversion (floating-integral conversion) from float to int. This conversion truncates the fractional part, converting 0.9 to 0. The function then returns 1 0, which evaluates to 0, and this value is printed to the standard output.
B: The function expects an int, so the float 0.9 is truncated to 0, not kept as a float. C: The float 0.9 is truncated towards zero to 0, not rounded up to 1. D: There is no logic or conversion in the code that would produce a negative result.
- ISO/IEC 14882:2017 (C++17 Standard), Section 7.10 [conv.fpint], Paragraph 1.
- Stroustrup, B. (2013). The C++ Programming Language (4th Ed.). Addison-Wesley. Chapter 10.5.
Keep practicing
Take the free CPA-21-02 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other CPA-21-02 practice questions
- Question 1What happens when you attempt to compile and run the following code? #include using names…
- Question 2What happens when you attempt to compile and run the following code? #include #include us…
- Question 3Which of the following statements are correct about an array? int tab[10];
- Question 4What happens when you attempt to compile and run the following code? #include #include us…
- Question 5What is the output of the program if characters 'h', 'e', 'l', 'l' , 'o' and enter are su…
- Question 6What happens when you attempt to compile and run the following code? #include using names…
