RSS Subscribe

This is default featured post 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

Showing posts with label C Sample Technical Questions. Show all posts
Showing posts with label C Sample Technical Questions. Show all posts

Sunday 26 June 2011

C Sample Technical Questions

Here are some sample C sums for a successful job interviewing.

 
 
 1.main() 
{ 
static int var = 6;
printf("%d ",var--); 
if(var) main();
}

Answer:6 5 4 3 2 1
Explanation:
When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.
  
 2.main()
{ 
float me = 2.1; 
double you = 2.1; 
if(me==you) 
printf(" I am a boy"); 
else printfI ("I am a girl);
}Answer: I am a girl
Explanation: For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.
 
  
 3.main()
{
char stmt[]="Hello World";
display(stmt);
}
void display(char *stmt)
{
printf("%s",stmt);
}
Answer: 
Compiler Error : Type mismatch in redeclaration of function display

Explanation : 
In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs
  
 4.

main()
{ 
int i=-1,j=-1,k=0,l=2,m; 
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}

Answer: 0 0 1 3 1 

Explanation :
Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression 'i++ && j++ && k++' is executed first. The result of this expression is 0 (-1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for '0 || 0' combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.
  
 6.#include‹stdio.h› 
#define a 20 main() 
{
#define a 10 printf("%d",a); 
} 

Answer: 10 

Explanation: 
The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken.
  
 7.#include‹stdio.h›
main()
{
char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%d",++*p + ++*str1-32);
}

Answer: 77

Explanation:
p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10, which is then incremented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. 
Now performing (11 + 98 - 32), we get 77("M"); 
So we get the output 77 :: "M" (Ascii is 77). 
  
 8.#define clrscr() 100
main()
{
clrscr();
printf("%d\n",clrscr());
}

Answer: 100

Explanation: 
Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs.The input program to compiler looks like this : 
main()
{
100;
printf("%d\n",100);
}

Note: 
100; is an executable statement but with no action. So it doesn't give any problem
  
9.main()
{
char *p;
p="Welcome";
printf("%c\n",*&*p);
}
Answer: 
W 
Explanation: 
* is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Welcome". *p dereferences it and so its value is W. Again & references it to an address and * dereferences it to the value W.
  
10.void main()
{
int i=5;
printf("%d",i+++++i);
}
Answer: Compiler Error


Explanation: 
The expression i+++++i is parsed as i ++ ++ + i which is an illegal combination of operators.
  
11.#include‹stdio.h›
main()
{
struct xx
{
int x;
struct yy
{
char s;
struct xx *p;
};
struct yy *q;
};
}
Answer:Compiler Error


Explanation: 
in the end of nested structure yy a member have to be declared
  
12.main()
{
int i=_l_abc(10);
printf("%d\n",--i);
}
int _l_abc(int i)
{
return(i++);
}

Answer: 9


Explanation: 
return(i++) it will first return i and then increments. i.e. 10 will be returned.
  
13.main()
{
int i=3;
printf("%d",i=++i ==4);
}
Answer: 
1
Explanation: 
The expression can be treated as i = (++i==4), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 4 yielding true(1). Hence the result.
  
14.void main()
{
int i;
char a[]="\0";
if(printf("%s\n",a))
printf("Good Evening \n");
else
printf("Good Morning\n");
}

Answer: Good Evening

Explanation: 
Printf will return how many characters does it print. Hence printing a null character returns 1 which makes the if statement true, thus "Good Evening" is printed.
  
15.main()
{
char *p;
int *q;
long *r;
p=q=r=0;
p++;
q++;
r++;
printf("%p...%p...%p",p,q,r);
}

Answer: 0001...0002...0004

Explanation: 
++ operator when applied to pointers increments address according to their corresponding data-types.

Twitter Delicious Facebook Digg Stumbleupon Favorites More