30 October 2012

C Technical Questions Part 3


C Technical Questions

1.) main()
{
            float me = 1.1;
            double you = 1.1;
            if(me==you)
              printf("Hello");
            else
              printf("Welcome");
}

Ans: 

welcome

Explanation: 

Will print "Welcome " because due to the difference in storage of  float and double. Double will give more 
percision for the numbers stored, so value in variable "me" will be slightly greater than that of variable "you". 

2.) main()
{
            extern int i;
            i=20;
            printf("%d",i);
}

Ans: 

linker error.

Explanation: 

extern will not define a variable, it just says that variable int has been declared else where in the program.

3.) main()
{
            int i=3;
            switch(i)
             {
                default:printf("zero");
                case 1: printf("one");
                           break;
                case 2:printf("two");
                          break;
                case 3: printf("three");
                          break;
              } 
}

Ans:

Three

Explanation: 

Will print "Three", switch will work normally. The order of case labels and default is not a matter, default block will be executed if the value of expression will not match to other case labels.

4.) main()
{
            char string[]="Hello World";
            display(string);
}
void display(char *string)
{
            printf("%s",string);
}                   

Ans: 

Hello World
         
Explanation: 

Nothing special in this program. Its just the passing of one dimensional array to function.

5.) #define int char
main()
{
            int i=65;
            printf("sizeof(i)=%d",sizeof(i));
}

Ans: 

1

Explanation:

Here int is a macro. So when referred inside will not treated as data type. The macro int is equivalent to char.  

6.) main()
{
int i=10;
i=!i>14;
printf ("i=%d",i);
}

Ans: 

i=0

Explanation:

the operator ! will compliment value of the associated variable, if value is zero then value 1 is returned else if non zero the value one is returned. Here  value of !i is 0, hence the operation 0>14 will return 0 which will be assigned to variable i. Hence the result.

7.) #define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf("%d",i);
}

Ans: 

64

Explanation:

When macros are used only blind substitution of the macro definition will take place. Hence the statement i = 64/square(4); will be expanded to 64/4*4. Hence the result.

8.) #include <stdio.h>
#define a 10
main()
{
#define a 50
printf("%d",a);
}

Ans: 

50

Explanation:

Macros once defined can be redefined in a block.

9.) #define clrscr() 100
main()
{
clrscr();
printf("%d\n",clrscr());
}

Ans:

100

Explanation:

Here clrscr() is treated as macro not as function.

10.) main()
{
printf("%p",main);
}

Ans : 
address of main

Explanation:
function name point to the address of the storage location where the function has been stored.

11. main()
{
clrscr();
}
clrscr();
Ans:error type mismatch
12.) main()
{
 int i=400,j=300;
 printf("%d..%d");
}
Ans:300 400
13.) main()
{
    int i=1;
    while (i<=5)
    {
       printf("%d",i);
       if (i>2)
              goto here;
       i++;
    }
}
fun()
{
   here:
     printf("PP");
}
Ans: 

error 

Explanation:
goto here sud be defined and called in same fuction.

14.) #include<stdio.h>
main()
{
int i=1,j=2;
switch(i)
 {
 case 1:  printf("GOOD");
                break;
 case j:  printf("BAD");
               break;
 }
}
Ans: 

Error

Explanation:

case should have a constant value or expression.
15.) main()
{
int i;
printf("%d",scanf("%d",&i));  // value 10 is given as input here
}
Ans: 1

Explanation:

Scanf function will return the number of values successfully read.

16.) main()
     {
       int i=0;
       for(;i++;printf("%d",i)) ;
       printf("%d",i);
      }
Ans: 


Explanation:

Here i++ will be incremented only once. Since it is a post fix expression, for loop will not be executed since test condition value is 0.

17 .)
 main()
{
 extern int i;
 i=20;
 printf("%d",sizeof(i));
}
Ans: 

Linker error

Explanation:

extern will not define a variable, it just says that variable int has been declared else where in the program.

18.) main()
{
 extern out;
 printf("%d", out);
}
 int out=100;
Ans: 

100

Explanation:

extern says that variable int has been declared else where in the program.
19.) main()
{
 show();
}
void show()
{
 printf("I'm the greatest");
}

Ans:

I m the greatest
Explanation:

Nothing special other than a function call

20.) main()
            {
            int i=-1;
            +i;
            printf("i = %d, +i = %d \n",i,+i);
            }
Ans:

 -1 -1
Explanation:

Unary plus have no effect on a variable.


No comments:

Post a Comment

Followers