Showing posts with label ISRO. Show all posts
Showing posts with label ISRO. Show all posts
Saturday, March 31, 2018
ISRO Questions - C/C++ Programming
INDIAN SPACE RESEARCH ORGANISATION
Recruitment Entrance Test for Scientist/Engineer
Recruitment Entrance Test for Scientist/Engineer
Previously asked questions on C/C++ Programming
ISRO May 2017
1. What is the output of this 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;
}
(a) 910 (b) 920 (c) 870 (d) 900
Ans: option (c)
Explanation:
Starting from main function. number variable has been initialized to 30. We pass address of number as parameters in the square function. As per the concept of function overloading in C++ the second square function will be executed.
--(*y) will be executed first since decrement operator has the higher precedence than multiplication.
Therefore,
*x = (*x) * --(*y);
*x = 30 * 29;
*x = 870
x is a pointer variable and it holds the address of the variable number. Therefore the value of 870 is now stored to number after the execution of *x = (*x) * --(*y); expression. Hence 870 will be printed.
Other points:
Other points:
- iostream is a header file. Header files are included in C/C++ because they have some inbuilt predefined functions.
- Purpose of using namespace std; - When you make a call to using namespace <some_namespace>; all symbols in that namespace will become visible without adding the namespace prefix. If you do not add that statement you need to call cout << number; like this std::cout << number; else you get the error 'cout' was not declared in this scope.
- Function overloading is a feature in C++ where two or more functions can have the same name but different parameters. Difference in parameters can be in terms of number of parameters as we saw in the case of square function or difference can be in terms of data type also. For example refer: https://www.geeksforgeeks.org/function-overloading-c/
Subscribe to:
Posts (Atom)