C advanced interview questions and answers
(1) What will be output if you will compile and execute the following c code?
struct marks{
int p:3;
int c:3;
int m:2;
};
void main(){
struct marks s={2,-6,5};
printf("%d %d %d",s.p,s.c,s.m);
}
(a) 2 -6 5
(b) 2 -6 1
(c) 2 2 1
(d) Compiler error
(e) None of these
Answer: (c)
Explanation:
Binary value of 2: 00000010 (Select three two bit)
Binary value of 6: 00000110
Binary value of -6: 11111001+1=11111010
(Select last three bit)
Binary value of 5: 00000101 (Select last two bit)
Complete memory representation:
(2) What will be output if you will compile and execute the following c code?
void main(){
int huge*p=(int huge*)0XC0563331;
int huge*q=(int huge*)0xC2551341;
*p=200;
printf("%d",*q);
}
(a)0
(b)Garbage value
(c)null
(d) 200
(e)Compiler error
Answer: (d)
Explanation:
Physical address of huge pointer p
Huge address: 0XC0563331
Offset address: 0x3331
Segment address: 0XC056
Physical address= Segment address * 0X10 + Offset address
=0XC056 * 0X10 +0X3331
=0XC0560 + 0X3331
=0XC3891
Physical address of huge pointer q
Huge address: 0XC2551341
Offset address: 0x1341
Segment address: 0XC255
Physical address= Segment address * 0X10 + Offset address
=0XC255 * 0X10 +0X1341
=0XC2550 + 0X1341
=0XC3891
Since both huge pointers p and q are pointing same physical address so content of q will also same as content of q.
(3) Write c program which display mouse pointer and position of pointer.(In x coordinate, y coordinate)?
Answer:
#include”dos.h”
#include”stdio.h”
void main()
{
union REGS i,o;
int x,y,k;
//show mouse pointer
i.x.ax=1;
int86(0x33,&i,&o);
while(!kbhit()) //its value will false when we hit key in the key board
{
i.x.ax=3; //get mouse position
x=o.x.cx;
y=o.x.dx;
clrscr();
printf("(%d , %d)",x,y);
delay(250);
int86(0x33,&i,&o);
}
getch();
}
(4) Write a c program to create dos command: dir.
Answer:
Step 1: Write following code.
#include “stdio.h”
#include “dos.h”
void main(int count,char *argv[]){
struct find_t q ;
int a;
if(count==1)
argv[1]="*.*";
a = _dos_findfirst(argv[1],1,&q);
if(a==0){
while (!a){
printf(" %s\n", q.name);
a = _dos_findnext(&q);
}
}
else{
printf("File not found");
}
}
Step 2: Save the as list.c (You can give any name)
Step 3: Compile and execute the file.
Step 4: Write click on My computer of Window XP operating system and select properties.
Step 5: Select Advanced -> Environment Variables
Step 6: You will find following window:
Click on new button (Button inside the red box)
Step 7: Write following:
Variable name: path
Variable value: c:\tc\bin\list.c (Path where you have saved)
Step 8: Open command prompt and write list and press enter.
Command line argument tutorial.
(6) What will be output if you will compile and execute the following c code?
void main(){
int i=10;
static int x=i;
if(x==i)
printf("Equal");
else if(x>i)
printf("Greater than");
else
printf("Less than");
}
(a) Equal
(b) Greater than
(c) Less than
(d) Compiler error
(e) None of above
Answer: (d)
Explanation:
static
variables are load time entity while auto variables are run time
entity. We can not initialize any load time variable by the run time
variable.
In this example i is run time variable while x is load time variable.
(7) What will be output if you will compile and execute the following c code?
void main(){
int i;
float a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=3;i++)
printf("%d ",*ptr++);
}
(a)0 0 0 0
(b)Garbage Garbage Garbage Garbage
(c)102 56 -80 32
(d)102 102 -90 64
(e)Compiler error
Answer: (d)
Explanation:
In c float data type is four byte data type while char pointer ptr can point one byte of memory at a time.
Memory representation of float a=5.2
ptr pointer will point first fourth byte then third byte then second byte then first byte.
Content of fourth byte:
Binary value=01100110
Decimal value= 64+32+4+2=102
Content of third byte:
Binary value=01100110
Decimal value=64+32+4+2=102
Content of second byte:
Binary value=10100110
Decimal value=-128+32+4+2=-90
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.
(8) What will be output if you will compile and execute the following c code?
void main(){
int i;
double a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=7;i++)
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.
Memory representation of double a=5.2
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.
(9) What will be output if you will compile and execute the following c code?
void main(){
printf("%s","c" "question" "bank");
}
(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”
(10) What will be output if you will compile and execute the following c code?
void main(){
printf("%s",__DATE__);
}
(a) Current system date
(b) Current system date with time
(c) null
(d) Compiler error
(e) None of these
Answer: (a)
Explanation:
__DATE__ is global identifier which returns current system date.
Step 7: Write following:
(7) What will be output if you will compile and execute the following c code?
ptr pointer will point first fourth byte then third byte then second byte then first byte.
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.
(10) What will be output if you will compile and execute the following c code?
Comments
Post a Comment