Sunday 4 August 2013

C PROGRAMMING QUESTIONS AND ANSWER contin..

(6) What will be output if you will compile and execute

the following c code?

#include<stdio.h>

int main(){

int i;

double a=5.2;

char *ptr;

ptr=(char *)&a;

for(i=0;i<=7;i++)

return 0;

printf("%d ",*ptr++);

}

(a) -51 -52 -52 -52 -52 -52 20 64

(b) 51 52 52 52 52 52 20 64

(c) Eight garbage values.

(d) Compiler error

(e) None of these

Answer: (a)

Explanation:
In c double data type is eight byte data type while  char pointer ptr can point one byte of memory at a time.

ptr pointer will point first eighth byte then seventh

byte then sixth byte then fifth byte then fourth byte

then third byte then second byte then first byte as

shown in above figure.

Content of eighth byte:

Binary value=11001101

Decimal value= -128+64+8+4+1=-51

Content of seventh byte:
Binary value=11001100

Decimal value= -128+64+8+4=-52

Content of sixth byte:

Binary value=11001100

Decimal value= -128+64+8+4=-52

Content of fifth byte:

Binary value=11001100

Decimal value= -128+64+8+4=-52

Content of fourth byte:

Binary value=11001100

Decimal value= -128+64+8+4=-52

Content of third byte:

Binary value=11001100

Decimal value= -128+64+8+4=-52

Content of second byte:

Binary value=000010100

Decimal value=16+4=20

Content of first byte:

Binary value=01000000

Decimal value=64
Note: Character pointer treats MSB bit of each byte

i.e. left most bit of above figure as sign bit.

(7) What will be output if you will compile and execute

the following c code?

#include<stdio.h>

int main(){
printf("%s","c" "question" "bank");

return 0;

}

(a) c question bank

(b) c

(c) bank

(d) cquestionbank

(e) Compiler error

Answer: (d)

Explanation:

In c string constant “xy” is same as “x” “y”

(8) What will be output if you will compile and execute

the following c code?

#include<stdio.h>

int main(){

char *str="c-pointer";

printf("%*.*s",10,7,str);

return 0;

}
(a) c-pointer

(b) c-pointer

(c) c-point

(d) cpointer null null

(e) c-point

Answer: (e)

Explanation:
Meaning of %*.*s in the printf function:

First * indicates the width  i.e. how many spaces will

take to print the string and second * indicates how

many characters will print of any string.

Following figure illustrates output of above code:

(9) What will be output if you will compile and

execute the following c code?

#include<stdio.h>

int main(){

int a=-12;

a=a>>3;

printf("%d",a);

return 0;

}

(a) -4

(b) -3

(c) -2

(d) -96

(e) Compiler error

Answer :( c)

Explanation:

Binary value of 12 is: 00000000 00001100

Binary value of -12 wills 2’s complement of 12 i.e.
Right shifting rule:

Rule 1: If number is positive the fill vacant spaces in

the left side by 0.

Rule 2: If number is negative the fill vacant spaces in

the left side by 1.

In this case number is negative. So right shift all the

binary digits by three space and fill vacant space by 1

as shown following figure:
Since it is negative number so output will also a  negative number but its 2’s complement.
And its decimal value is: 2

Hence output will be:-2

No comments:

Post a Comment