144k views
3 votes
Given the following function definitions and program fragments, what is the output?

A) 3 4
B) 4 3
C) 4 4
D) 3 3

"Given the following function definitions and program fragments, what is the output?

void f1 (int* z, int *q)
{
int temp
temp = *q;
*q = *z;
*z = temp;
}
void f2( int* a, int* b)
{
if(*a < *b)
f1(a,b);
else
*a = *b;
}

int x = 3, y = 4
f2(&y, &x);
print(""%d %d\\"", x, y);

User Shaydawg
by
7.6k points

1 Answer

1 vote

Final answer:

The output of the given C program is "3 3" after the function 'f2' is called, which sets the value of 'y' to that of 'x' because 'y' was greater than 'x'.

Step-by-step explanation:

The question asks about the output of a C language program consisting of two functions, f1 and f2, and a main program that initializes two integers x and y, calls function f2, and then prints the values of x and y. The function f1 swaps the values pointed to by its pointer arguments, while f2 either calls f1 if the first argument is less than the second, or sets the value pointed to by the first argument to be equal to the value of the second argument.

With x initialized to 3 and y initialized to 4, the call f2(&y, &x) will execute the else statement because y is greater than x, resulting in setting y to the value of x. Finally, the program prints the values of x which is 3, and y which is now also 3, so the output will be "3 3", which corresponds to option D).

User Sendmarsh
by
7.9k points