Skip to content

ISRO Scientist/Engineer CSE 2017Object-Oriented Programming and Java › Not yet assigned to a topic

ISRO Scientist/Engineer CSE 2017 PYQ: What is the output of the C++ program? #include <iostream> using namespace std; void square(int *x){ *x =…

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. A
    910
  2. B
    920
  3. C
    870
    Correct answer
  4. D
    900

Answer

(C) 870

Approach

The program defines two overloaded functions named square. The call square(&number, &number) has two arguments, so it resolves to the two-parameter overload. That function computes *x = (x) --(*y). Since both pointers point to the same variable number (initially 30), the pre-decrement on y changes number to 29, and then the multiplication uses the updated value for both operands, yielding 30 29 = 870. Because the arguments are pointers, the change is reflected in main, so the output is 870.

Reviewed by ISRO IQ Editorial Team.