Please answer the questions and explain your answer utilizing C++ logic Use the
ID: 3626419 • Letter: P
Question
Please answer the questions and explain your answer utilizing C++ logicUse the following code for the next 2 questions
void f2( int &x )
{
x = 100;
}
void f1( int a, int &b )
{
a = a * b;
b = 12;
if (b < a)
f2(a);
else
f2(b);
}
int main()
{
int num1 = 2;
int num2 = 4;
f1(num1, num2);
cout << num1 << " " << num2 << endl; //OUTPUT 1
num1 = 4;
num2 = 5;
f1(num1, num2);
cout << num1 << " " << num2 << endl; //OUTPUT 2
}
1. W hat will the line of code labeled OUTPUT1 print?
a. 2 4 c. 8 12 e. 8 4
b. 2 100 d. 8 100 f. 100 12
2. W hat will the line of code labeled OUTPUT2 print?
a. 4 12 c. 4 5 e. 100 12
b. 20 12 d. 100 5 f. 4 100
3. Assuming an variable of type int named n, a variable of type double named x, and a
variable of type string named s have been declared, which of the following is not a
correct call to the function f declared below?
void f( int first, double &second, string third );
a. f(1, x, s); c. f(n, 2.0, "it");
b. f(n, x, s); d. f(n, x, "10");
4. Given max is a function that passes in 2 doubles and returns the largest of the 2
values, which of the following is a legal C++ expression equivalent to the largest
value stored within the 4 variables, s, t, u, and v?
a. max(s, t, u, v) c. max(s, t), max(u, v)
b. max(s, max(t, max(u, v))) d. max(s, t) max(u, v)
Explanation / Answer
1. b. 2 100
Call trace:
f1(2, 4) //a remains 2, b becomes 12
f2(b) // b becomes 100
print (2, 100)
2. f1(4,5) //a remains 2, b becomes 12
f2 (4) //the 4 passed is a temporary variable from f1, so it just disappears after we exit f1
print (4, 12)
3. c f(n, 2.0, "it") is the issue. The other three pass variables, which are references, but this one passes a constant double, which is not a reference and does not meet the specs.
4. b max(s, max(t, max(u,v))) is fine. In fact, that's how some ripple adders work, although in a neater form.