Saturday, March 31, 2018

ISRO Questions - C/C++ Programming

INDIAN SPACE RESEARCH ORGANISATION
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:
  1. iostream is a header file. Header files are included in C/C++ because they have some inbuilt predefined functions. 
  2. 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.
  3. 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/


ISRO May 2017
2. Which of the following operator(s) cannot be overloaded?
(a) . (Member Access or Dot operator)
(b) ?: (Ternary or Conditional Operator )
(c) :: (Scope Resolution Operator)
(d) All of the above

Ans: option (d)
Explanation:
Using Operator Overloading in C++ we can make operators to work for user defined classes.
The operators that cannot be overloaded in C++ are:
1) Scope Resolution Operator  (::) 
2) Pointer-to-member Operator (.*) 
3) Member Access or Dot operator  (.) 
4) Ternary or Conditional Operator (?:)
5) Object size Operator   (sizeof)
6) Object type Operator   (typeid)

ISRO May 2017
3. What is the output of the following program?
main( )
{
    int a = 10;
    if ((fork ( ) == 0))
        a++;
    printf ("%d\n", a );
}
(a) 10 and 11     (b) 10    (c) 11     (d) 11 and 11

Ans: option (a)
Explanation:
fork() will create a new child process identical to the parent. So everything you run in the code after that will be run by both processes. fork() returns 0 on the child and the pid of the child on the parent. Hence in the given program only child process will execute the if part. Therefore parent prints 10 and child prints 11.

ISRO May 2017
4. Consider the following C function
void swap ( int x, int y )
{
    int tmp;
    tmp = x;
    x= y;
    y = tmp;
}

In order to exchange the values of two variables a and b:
(a) Call swap (a, b)
(b) Call swap (&a, &b)
(c) swap(a, b) cannot be used as it does not return any value
(d) swap(a, b) cannot be used as the parameters passed by value

Ans: option (d)
Explanation:
This question was also asked in GATE 2004.
Call swap (a, b) - will swap only the formal parameters (i.e. x and y). The value actual parameters (i.e. a and b) will remain the same, because the value are passed by  CALL BY VALUE.
call swap (&a, &b) - will not work because, swap function accepts VALUES as parameters, but we are passing addresses of a and b.

ISRO May 2017
5. What does the following C-statement declare?
int (*f) (int*);

(a) A function that takes an integer pointer as argument and returns an integer
(b) A function that takes an integer as argument and returns an integer pointer
(c) A pointer to a function that takes an integer pointer as argument and returns an
integer
(d) A function that takes an integer pointer as argument and returns a function
pointer

Ans: option (c)
Explanation:
This question was also asked in GATE 2005.
Syntax to declare pointer to a function
=> datatype (*pointer_variable)(list of arguments)

To make a pointer to a function => pointer_variable = function_name 
Note: don't use parenthesis of the function.
To call (invoke) the function => pointer_variable(list of arguments)

ISRO May 2017
6. What will be the output of the following C code?
#include <stdio.h>
main( )
{
    int i;
    for ( i=0; i<5; i++ )
    {
        int i = 10;
        printf ( " %d", i );
        i++;
    }
    return 0;
}

(a) 10 11 12 13 14
(b) 10 10 10 10 10
(c)  0 1 2 3 4
(d) Compilation error

Ans: option (b)
Explanation:
If an inner block (here for loop block) declares a variable with the same name as the variable declared by the outer block (main block), then the visibility of the outer block variable ends at the point of declaration by inner block. Even if i is incremented at the end of for loop, i is re-initialized to 10 on each iteration and hence 10 is printed 5 times.

ISRO May 2017
7. What does the following program do when the input is unsigned 16-bit integer?
#include <stdio.h>
main( )
{
    unsigned int num;
    int i;
    scanf ("%u", &num);
    for ( i = 0; i<16; i++)
    {
        printf ("%d", (num << i & 1 << 15 ) ? 1:0);
    }
}

(a) It prints all even bits from num 
(b) It prints all odd bits from num
(c) It prints binary equivalent of num 
(d) None of the above

Ans: option (c)
Explanation:
<< , left shift operator, takes two operands, left shifts the bits of the first operand and the second operand decides the number of places to shift.

1<<4 = 1 is left shifted 4 times =10000
1<<15 = 1 is left shifted 15 times =1000000000000000

first iteration:
7 << 0 & 1 << 15 = 0000000000000111 & 1000000000000000 = 000000000000000 = 0
7 << 1 & 1 << 15 = 0000000000001110 & 1000000000000000 = 000000000000000 = 0
7 << 2 & 1 << 15 = 0000000000011100 & 1000000000000000 = 000000000000000 = 0
...
7 << 12 & 1 << 15 = 0111000000000000 & 1000000000000000 = 000000000000000 = 0
7 << 13 & 1 << 15 = 1110000000000000 & 1000000000000000 = 000000000000000 = 1
7 << 14 & 1 << 15 = 1100000000000000 & 1000000000000000 = 000000000000000 = 1
7 << 15 & 1 << 15 = 1000000000000000 & 1000000000000000 = 000000000000000 = 1

ISRO May 2017
8. What is the output of the following program?
#include<stdio.h>
int tmp=20;
main()
{
  printf("%d", tmp);
  func();
  printf("%d", tmp);
}

func()
{
 static int tmp=10;
 printf("%d", tmp);
}

(a) 20 10 10            (b) 20 10 20
(c) 20 20 20            (d) 10 10 10

Ans: option (b)
Explanation:
The program starts its execution from the main function. tmp is printed first. Since main function has no local variable tmp defined it checks whether global variable is present or not. Since tmp is defined globally and since it has been initialized to 20 it prints 20.
2nd line in the main function gets executed which invokes func(). Since func() has a local variable defined and since it is initialized to 10 it prints 10. Then execution returns back to the main function.
Now last line of the main function is executed, again since main function has no local variable tmp, it prints 20 since tmp variable is globally present.

ISRO May 2017
9. We use malloc and calloc for
(a) Dynamic memory allocation
(b) Static memory allocation
(c) Both dynamic and static memory allocation
(d) None of the above

Ans: option(a)

ISRO Dec 2017
10. A process executes the following code
for (i = 0; i < n; i ++) fork( );

The total number of child processes created is
(a) n2                (b) 2n+1 -1
(c) 2n                (d) 2n-1

Ans: option(d)
Explanation:
Loop executes for n times. Therefore, fork() is executed for n times.
For n fork statements,  2n – 1 child processes are created.

ISRO Dec 2017
11. Consider the code segment
int i, j, x, y, m, n;
n=20;
for (i = 0, i < n; i++)
{
    for (j = 0; j < n; j++)
    {
        if (i % 2)
        {
            x + = ((4*j) + 5*i);
            y += (7 + 4*j);
        }
    }
}
m = x + y;

Which one of the following is false?
(a) The code contains loop invariant computation
(b) There is scope of common sub-expression elimination in this code
(c) There is scope of strength reduction in this code
(d) There is scope of dead code elimination in this code

Ans:
Explanation:
Loop-invariant code consists of statements or expressions which can be moved outside the body of a loop without affecting the semantics of the program. In our code i%2 and 5*i calculations can be moved outside of the second for loop. Hence code contains loop invariant computation.
Common subexpression elimination is a compiler optimization that searches for instances of identical expressions, and analyzes whether it is worthwhile replacing them with a single variable holding the computed value. In our code 4*j can be seen in two places hence there is scope of common sub-expression elimination in this code.
Strength reduction is a compiler optimization where expensive operations are replaced with equivalent but less expensive operations. The classic example of strength reduction converts "strong" multiplications inside a loop into "weaker" additions. We can calculate 4*j and store it in a temp variable and add that temp variable replacing 4*j expression. Also 5*i can be calculated before the second for loop and store in a temp2 variable and then we can replace temp2 with 5*i.
Dead code elimination (also known as DCE, dead code removal, dead code stripping, or dead code strip) is a compiler optimization to remove code which does not affect the program results. Here we could not see any such code. Hence answer is option (d)
Ref: Wikipedia

ISRO Dec 2017
12. Consider the function
int fun(x: integer)
{
    If x > 100 then fun = x – 10;
    else
        fun = fun(fun(x + 11));
}

For the input x = 95, the function will return
(a) 89         (b) 90        (c) 91         (d) 92

Ans: option (c)
Explanation:
fun(95)  = fun ( fun(95+11) ) = fun( fun(106) ) = fun(96)
fun(96)  = fun ( fun(96+11) ) = fun( fun(107) ) = fun(97)
fun(97)  = fun ( fun(97+11) ) = fun( fun(108) ) = fun(98)
fun(98)  = fun ( fun(98+11) ) = fun( fun(109) ) = fun(99)
fun(99)  = fun ( fun(99+11) ) = fun( fun(110) ) = fun(100)
fun(100)  = fun ( fun(100+11) ) = fun( fun(111) ) = fun(101) = 91

ISRO Dec 2017
13. Consider the function
int func(int num) {
    int count = 0;
    while(num) {
        count++;
        num >>= 1;
    }
    return(count) ;
}

For func(435) the value returned is
(a) 9       (b) 8        (c) 0        (d) 10

Ans: option(a)
Explanation:
num >>= 1; is equivalent to num = num >> 1;
Binary of 435 = 110110010
while loop iterates until num = 0.
In first iteration num >> 1 = 11011001 = 217
In second iteration num = 108 .. so on..
The function mainly gets us position of the most significant bit of in the binary representation of num. Hence func(435) returns 9.

ISRO Dec 2017
14. Consider the following C function
#include <stdio.h>
int main(void)
{
    char c[ ] = "ICRBCSIT17";
    char *p=c;
    printf("%s", c+2[p] – 6[p] – 1);
    return 0;
}

The output of the program is
(a) SI         (b) IT         (c) T1          (d) 17

Ans: option(d)
Explanation:
2[p] = p[2] = R
6[p] = p[6] = I
R - I = AsciiValue(R) - AsciiValue(I) = 18 -9 = 9
c + 9 - 1 = c+8
Hence 17 is printed

ISRO July 2016
15. What is the output of this C  code?
#include<stdio.h>
void main()
{
    int k=5;
    int *p=&k;
    int **m=&p;
    printf("%d %d %d",k,*p,**m);
}

(a) 5 5 5
(b) 5 5 junk
(c) 5 junk junk
(d) compile time error

Ans: option(a)
Explanation:
p is a pointer variable and it holds the address of k. *p means we are accessing contents of the memory that p points to. Since p points to k and k contains the value 5, *p prints 5 .
m is a pointer to another pointer. Hence we declare m using two stars. i.e. int **m = &p.
m holds the address of pointer variable p.
Address of k = 1000. Data stored in k = 5.
Address of p = 2000. Data stored in p = 1000 (which is the address of k).
Address of m = 3000. Data stored in m = 2000 (which is the address of p).
printf("%d",p); => Outputs 1000
printf("%d",*p); => Outputs 5

printf("%d",m); => Outputs 2000
printf("%d",*m); => Outputs 1000
printf("%d",**m); => Outputs 5


No comments:

Post a Comment