Interview QA 2

Interview QA 2



Ques 1. what is the output for the below code?

#include <stdio.h>
#include <stdlib.h>
#define DEBUG(x, %d) printf("DEBUG : %d", x) 
int main()
{
    int x=44;
    DEBUG(x,%d);
    return 0;
}

Explanation:
Here in DEBUG macro %d is not a variable, it gives compiler error.

Rules for naming identifiers
A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores.
The first letter of an identifier should be either a letter or an underscore.
You cannot use keywords as identifiers.
There is no rule on how long an identifier can be. 
However, you may run into problems in some compilers if the identifier is longer than 31 characters.



Ques 2. what is the ouput?
#include <stdio.h>
#include <stdlib.h>
#define cube(x) x*x*x
int main()
{
    int x=2, y=4;
     y=y/cube(x) ;
     printf("%d",y);
    return 0;
}

Explanation:
after preprocessing  the statement  y=y/cube(x)
become   y=y/x*x*x;
         it evaluates as
         y= (y/x)*x*x = (4/2)*2*2= 2*2*2= 8



Ques 3. what is dirty bit?

Explanation:
 In computer dirty bit or modified bit indicates the block of memory is modified or not .
if the dirty bit is set then the block of memory is modified otherwise id not modified.
the operating system checks this bit before writing the block of memory to secondary storage
if the bit is set then the block of memory is written to secondary storage.


Ques 4. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int x=-2;
    printf("%x",x>>3);
    return 0;
}

Explanation:
-2 = fffffffe = 11111111 11111111 11111111 11111110
after  >> 3
 = ffffffff

if define the x as unsigned int
after right shift it becomes 1fffffff



Ques 5. which is efficient in switch or if else ?

Explanation:
    In if else each the condition has to be evaluated at run time,
in switch only evaluating the variable value is done at run time, 
remaining cases is fixed and it is evaluated at compile time.


Ques 6. what is the output for the below code?

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int ch =1, y=0;

    if(ch==1, y>ch )
    {
     printf("\n if is evaluated.");
    }
    else
    {
        printf("\n else is evaluated.");
    }

    return 0;
}

Explanation:
Here the if condition is correct in syntax,
however it will evaluate the the first condition, and it considers 
the last condition for control flow.


Ques 7. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int ch =1, y=0;

     printf("\n %d", scanf("%d %d", &ch, &ch));


    return 0;
}

Explanation:
Here the scanf funtion returns how many arguments it is reading.
The return value of scanf funtion is printed in printf funtion.


Ques 8. what are near, huge, far memory ?

Explanation:
near means the variable is stored in the current segment of 64 kb of memory
far means the variable is stored or can access within in the 128kb of memory,
here the segment register is fixed
huge means the variable is stored or can access more than 128kb of memory, 
here the segment register can change.


Ques 9. write a program for swap the bytes in the variable var1=0x4567 ?

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int var1 =0x3255;
    var1 = (var1 >> 8) | ((var1&0xff)<<8);
    printf("0x%x",var1);
    return 0;
}




Ques 10. difference between <> and "" in #include statement?

Explanation:
< > means the file is included from the library or predesignated by the compiler.
the compiler will search the included file in that directory. 
Normally this is used to include the standard header files.

" " means the the compiler searches the included header file in the current working directory.
normally it used to include the user defined header files.