Showing posts with label C aptitude. Show all posts
Showing posts with label C aptitude. Show all posts

Mar 13, 2011

35. Find the ouput.

#include<stdio.h>
int i;
int fun();

int main()
{
    while(i)
    {
        fun();
        main();
    }
    printf("Hello\n");
    return 0;
}
int fun()
{
    printf("Hi");
}
Answer: Hello
Explanation:
Step 1: int i; The variable i is declared as an integer type.
Step 1: int fun(); This prototype tells the compiler that the function fun() does not accept any arguments and it returns an integer value.
Step 1: while(i) The value of i is not initialized so this while condition is failed. So, it does not execute the while block.
Step 1: printf("Hello\n"); It prints "Hello".
Hence the output of the program is "Hello".

Mar 2, 2011

28. Find the output


Program:
main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
}



Ans : 3,2,15

Explanation :

Step 1int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to
a[0] = 5a[1] = 1a[2] = 15a[3] = 20a[4] = 25 .
Step 2int i, j, m; The variable i,j,m are declared as an integer type.
Step 3i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
Step 6printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15

Feb 24, 2011

24. Find the output.

void main()
{
int i=3, *j, k;
j = &i;
printf("%d\n", i**j*i+*j);
}


Answer: 
30

Nov 25, 2010

16. Find the output.

int main()
{
int ***r, **q, *p, i=8;
p = &i;
q = &p;
r = &q;
printf("%d, %d, %d\n", *p, **q, ***r);
return 0;
}

Answer:
8,8,8

Nov 13, 2010

6. Predict the output

main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}


Answer:
5 4 3 2 1
Explanation:
When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.

Nov 6, 2010

Find the output.

void main()
{
static int i=5;
if(--i)
{
main();
printf("%d ",i);
}
}


Output: 
0 0 0 0


Explanation:
The variable 'i' is declared as static, hence memory for 'i' will be allocated
for only once, as it encounters the statement. The function main() will be called
recursively unless 'i' becomes equal to 0, and since main() is recursively called, so
the value of static 'i' ie., 0 will be printed every time the control is returned.

Oct 29, 2010

Find The Output

main()
{
i=5;
printf("%d",i++ + ++i);
printf("\n%d",i++);
printf("\n%d",++i);
}

Answer:
12
7
9

Oct 28, 2010

Find the output.

main()
{
int i=0;
while(+(+i--)!=0)
i-=i++;
printf("%d",i);
}



Answer:
-1


Explanation:
Unary + is the only dummy operator in C. So it has no effect on the expression and now the while loop is, while(i--!=0) which is false and so breaks out of while loop. The value –1 is printed due to the post-decrement operator.

Oct 27, 2010

Find the output.

Try this..
main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}
What is the output of above program??

Answer:
hai

Explanation:
The first printf statement prints 'ab'
The second printf statement prints 'asi'(bcoz \b backspace cmd prints next to 'a' and overwrites 'b')
The third printf statement prints 'hai' (bcoz \r return cmd overwrites on 'asi')

Oct 26, 2010

Find the output.

main()
{
int i=-1;
+i;
printf("i = %d, +i = %d \n",i,+i);
}
what's the output??


Answer: 
i = -1, +i = -1

Explanation: 
   Unary + is the only dummy operator in C. Where-ever it comes you can
just ignore it just because it has no effect in the expressions (hence the
name dummy operator).