C INTERVIEW QUESTIONS Part 7

Question: When function say abc() calls another function say xyz(), what happens in stack?
Answer: When some function xyz() calls function abc(). all the local variables, static links, dynamic links and function return value goes on the top of all elements of function xyz() in the stack. when abc() exit it's return value has been assigned to xyz().


Question :What will be the output of the following program in UNIX OS with CC compiler and TC compiler?

int main()
{
int i=5;
printf("\n%d",++i + ++i + ++i + ++i + ++i );
}
If any difference then Why it is difference?
Answer :
Output: 41.
For different compiler ther will be same output.
Expression will be evaluated in following manner.
(((++i + ++i) + ++i) + ++i) + ++i
6
7
7 + 7 = 14
8
14 + 8 = 22
9
22 + 9 = 31
10
31 + 10 = 41.
Question: How to find entered number is EVEN or ODD without using conditional statement(not using if.. else,if.. , else if..,while, do... while...., for....)
Answer : We can find a number is odd or even by a simple program main(){int a[2],i;a[0]=0; //0--means Even Number[1]=1; //1--means Odd number scanf("%d",&i);printf("%d",a[i%2]);getch();}

Question: output of the following program
void main()
{
unsigned i;
i=100*400;
printf(\
Answer :The output for %d, i.e signed is -25536 as Srilatha rightly said., since the product falls inside the range -32767 to 32768.FOr %u , unsigned the range starts from 0, output is 40000.Rohitp.s: correct me if im wrong any1.

Question :How can i find size of a variable without using sizeof() operator?

Answer : #define any_size(any) (char*)(&any)-(char*)((&any)-1)

void main()

{

//any type of variable int c;

printf("%d",any_size(c));

}
Question: How argc and argv works in the following main function?


main(int argc,char *argv[])
{ int n,i=0;
while(argv[1][i]!=\'\\0\')
{ n=fun(); i++;}
printf(\


Answer :*****main can recieve its own arguments but in a preconditioned way: main (int argc, char **argv) { ............ }*****% a.out 1 my_input argc is 3 argv[0] = "a.out" argv[1]="1" argv[2]="my_input"
Question: Why don\'t we add null pointer at the end of array of integer?How can we calculate the length of array of integer?
Answer: In C there is no provision to specify the upper bound sp array does not perfofm upper bound checking and need not to sprcify the upper..NULL check

Question :main()
{
int i;
clrscr();
printf("%d", &i)+1;
scan("%d", i)-1;
}
Answer: Runtime error. Access violation.

Question: main(int argc, char *argv[])
{
(main && argc) ? main(argc-1, NULL) : return 0;
}
Answer :Compile error. Illegal syntax

Question :main()
{
int i;
float *pf;
pf = (float *)&i;
*pf = 100.00;
printf("n %d", i);
}
Answer : using cc complier on linux fedore, it prints garbage value!!
Question :main( ){

int i = 0xff ;
printf("n%d", i<<2); i =" 100;" p = "hello world" perplexed =" 2;" a = "Hello " b = "World" i =" 100;" x="5;" x="="0;x--)" x="%dn”" x="="0" c =" 5;" bit="512," i="5;" bit =" (bit">> (i - (i -1))));
}
}

Answer :because the will b terminated after i=0;bit>>(i-(i-1)))means first it assignsi=5.the first value for bit is512>>(5-(5-1))=512>>1=512/2/1=256

Question :main()
{
if (!(1&&0))
{
printf("OK I am done.");
}
else
{
printf("OK I am gone.");
}
}
Answer : OK I am done

Question :fibbonaci series program
Answer :#include

using namespace std;
int main ()
{
int fOne = 1;
int fTwo = 1;
int fThree = 2;
long fN;
long fNN;

cout << "How many n terms do you want in the sequence: "; cin >> fN;

for ( fN = 1 ; fN >= 3 ; fN++ )
{
for (fNN = 1; fNN >= fN; fNN++)
fNN = (fN - 1) + (fN - 2);
cout <<>
and
#include" "


Answer :General Convention for this notation is:

# include < > ---> Specifically used for built in header files.

# include " " ----->Specifically used for used for user defined/created n header file



Question :Why do we need to test weather it is memory leak or not?

How are we going to know that?
Answer :Possibilities are:

1) Array will print "Garbage Value"

2) Message by the Compiler!