Skip to main content

C advanced interview questions and answers

   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.

Comments

Popular posts from this blog

[Fix] SSL Error or Invalid Security Certificate Problem While Opening Facebook or Other Websites

Today we are going to address a very strange and annoying issue which occurs when you try to open a website using  HTTPS  (Hypertext Transfer Protocol Secure) protocol such as  Facebook, Twitter, Google, etc . The problem occurs in all web browsers whether its  Internet Explorer ,  Google Chrome ,  Mozilla Firefox  or  Opera  but the error messages and problem symptom might differ in different web browsers. Problem Symptom: When you open a HTTPS website such as Facebook, Twitter, etc in your favorite web browser, following things happen: In Google Chrome web browser: The website doesn't open and you see a  red cross on padlock icon  and a  red line through the https://  text in the addressbar with following error message: SSL Error Cannot connect to the real www.facebook.com Something is currently interfering with your secure connection to www.facebook.com. Try to reload this page in a few minutes or after switching to a new network. If you have rece

FAQ's Salesforce Lightning Platform Query Optimizer

This article comprises the FAQs related to salesforce internal platform Query Optimizer.and provides valuable information that will help understand how to improve the performance of SOQL queries. For more information, you may also check this Dreamforce Session . 1. Query Optimizer Q: Does Salesforce have its own SQL optimization logic instead of Oracle's built-in? A: Force.com is a multi-tenant platform. To consider multitenant statistics, the platform has its own SOQL query optimizer that determines optimal SQL to satisfy a specific tenant's request. Q: How frequently generated are the statistics used by the query optimizer? A: Statistics are gathered weekly. Where statistics are not calculated, the optimizer also performs dynamic pre-queries, whose results are cached for 1 hour Q: Is there any way to flush the cache when doing your performance testing so your results are not cached biased? A: Unfortunately not. Queries with selective filters will perform mor

Google Tricks

Google isn't just for hypochondriacs looking up their symptoms or for trying to find a cool new restaurant. By just entering a few simple search terms, you can use Google to help plan and organize your life. It is  amazing . 1.) You can use Google as a timer, just set the time in the search bar as shown here. 2.) Google will also help you calculate your tips. 3.) You can find out what date any holiday falls on. 4.) Google will also find movie release dates for you. 5.) You can find full schedules for your favorite television shows. 6.) Google will also find the songs of your favorite bands. 7.) You can use the search engine to find what books your favorite authors wrote. 8.) It'll look up flight information for you. 9.) Do you know what time the sun rises? It'll tell you. 10.) It'll also give you information on your