recategorized by
7,083 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

0 votes
0 votes
Option C 870
0 votes
0 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.

Answer:

Related questions

5 votes
5 votes
4 answers
5
sh!va asked May 7, 2017
4,572 views
Which of the following is associated with objects?StateBehaviorIdentityAll of the above
7 votes
7 votes
3 answers
7
sh!va asked May 7, 2017
4,701 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
8
Arjun asked May 9, 2017
13,237 views
Which of the following data structure is useful in traversing a given graph by breadth first search?StackQueueListNone of the above