recategorized by
6,974 views
7 votes
7 votes

What is the output of the C++ program?
 

#include <iostream>
using namespace std;

void square(int *x){
    *x = (*x)++ * (*x);
}

void square(int *x, int *y){
    *x = (*x) * --(*y);
}

int main()
{
  int number = 30;
  square(&number, &number);
  cout<<number;
  return 0;
}
  1. 910
  2. 920
  3. 870
  4. 900
recategorized by

6 Answers

Best answer
10 votes
10 votes
C. 870 is the Ans.

Due to function overloading concept support in C++, it will call the 2nd square function.

Moreover, it is passed by reference So  30*29=870 will reflect in the main function.
selected by
2 votes
2 votes
From main function, square function is called with two arguments => square(&number, &number);

Hence, the second definition of square will be overloaded. And it has:

*x= (*x) * --(*y);                where *x = 30 and *y = 30

-- (decrement) operator has the higher precedence than * (multiplication).

hence,

*x = 30 * 29

*x = 870

And the call to square function was a call by reference. Address of parameters was passed. So the value of number will be changed. And output will be 870.

 

And ofcourse, the line in first definition of square is typed wrongly. It should be:
*x= (*x)++ * (*x);    // In exam paper, it was correct.
0 votes
0 votes
Option C : square(&number, &number);
this statement will call ,
void square(int *x, int *y){
    *x = (*x) * --(*y);
}
there fore
x = 30 * 29 =   870
Answer:

Related questions

5 votes
5 votes
4 answers
1
sh!va asked May 7, 2017
4,518 views
Which of the following is associated with objects?StateBehaviorIdentityAll of the above
7 votes
7 votes
3 answers
3
sh!va asked May 7, 2017
4,654 views
Which of the following operator(s) cannot be overloaded?.(member Access or Dot operator)?: (ternary or Conditional Operator):: ( Scope Resolution Operator)All of the abov...
10 votes
10 votes
8 answers
4
Arjun asked May 9, 2017
13,152 views
Which of the following data structure is useful in traversing a given graph by breadth first search?StackQueueListNone of the above