C Interview Questions
Questions by Rejinpaul.com
1. void main()
{
int const * p=5;
printf("%d",++(*p));
}
a)Compiler error
b)error
c)5
d)it will compile but not run
The Correct Answer is:
a)Compiler error
Explanation:
Compiler error:Cannot modify a constant value.
p is a pointer to
a "constant integer". But we tried tochange the value of the "constant integer".
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
2. main()
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[
i ],*(s+i),*(i+s),i[s]);
}
a)mmmm aaaa nnnn
b)error
c)nnna aaan mmmm
d)runtime error
The Correct Answer is:
a)mmmm aaaa nnnn
Explanation:
s[i], *(i+s), *(s+i), i[s] are all different ways of
expressing the same idea. Generally array name is
the base address for that array. Here s is the base address. i
is the index number/displacement from the base
address. So, indirecting it with * is same as s[i]. i[s] may
be surprising. But in the case of C it is same as s[i].
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
3. main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}
a)I love u
b)I hate u
c)error
d)a and b
The Correct Answer is:
b)I hate u
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.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
4. main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}
a)Compiled error
b)1 2 3 5 4
c)1 2 3 4 5
d)5 4 3 2 1
The Correct Answer is:
d)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.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
5. main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5 data-blogger-escaped-br="" data-blogger-escaped-j=""> printf(" %d ",*c);
++q; }
for(j=0;j<5 data-blogger-escaped-br="" data-blogger-escaped-j=""> printf(" %d ",*p);
++p;
}
}
a)Compiled error
b)2 2 2 2 2 2 3 4 6 5
c)2 2 2 2 2 2 2 4 6 5
d)2 2 2 2 2 2 3 3 6 5
The Correct Answer is:
b)2 2 2 2 2 2 3 4 6 5
Explanation:
Initially pointer c is assigned to both p and q. In the first
loop, since only q is incremented and not c , the value 2
will be printed 5 times. In second loop p itself is
incremented. So the values 2 3 4 6 5 will be printed.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
6. main()
{
extern int i;
i=20;
printf("%d",i);
}
a) error
b)compiled error
c)Linker Error : Undefined symbol '_i'
d)run time error
The Correct Answer is:
c)Linker Error : Undefined symbol '_i'
Explanation:
extern storage class in the following declaration, extern int i; specifies
to the compiler that the memory for i is allocated in some other program and that address will
be given to the current program at the time of linking.
But linker finds that no other variable of name i is available in any other program with memory space
allocated for it. Hence a linker error has occurred .
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
7. 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);
}
a)0 0 1 3 1
b)compiled error
c)Linker Error : Undefined symbol '_i'
d)0 0 3 1 3
The Correct Answer is:
a)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.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
8. main()
{
char *p;
printf("%d %d ",sizeof(*p),sizeof(p));
}
a)12
b)compiled error
c)14
d)the program will loop continus
The Correct Answer is:
a)12
Explanation:
The sizeof() operator gives the number of bytes taken
by its operand. P is a character pointer, which needs one
byte for storing its value (a character). Hence sizeof(*p)
gives a value of 1. Since it needs two bytes to store the
address of the character pointer sizeof(p) gives 2.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
9. main()
{
int i=3;
switch(i)
{
default:printf("zero");
case 1: printf("one");
break;
case 2:printf("two");
break;
case 3: printf("three");
break;
}
}
a)one
b)three
c)four
d)error
The Correct Answer is:
b)three
Explanation:
The default case can be placed anywhere inside the
loop. It is executed only when all other cases doesn't
match.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
10. main()
{
printf("%x",-1<<4 data-blogger-escaped-br=""> }
a)fff0
b)offo
c)oooo
d)error
The Correct Answer is:
b)fffo
Explanation:
-1 is internally represented as all 1's. When left shifted
four times the least significant 4 bits are filled with
0's.The %x format specifier specifies that the integer
value be printed as a hexadecimal value.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
11. #define int char
main()
{
int i=65;
printf("sizeof(i)=%d",sizeof(i));
}
a)sizeof(i)=1
b)sizeof(i)=0
c)sizeof(i)=i
d)runtime error
The Correct Answer is:
a)sizeof(i)=1
Explanation:
Since the #define replaces the string int by the macro
char
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
12. main()
{
int i=10;
i=!i>14;
Printf ("i=%d",i);
}
a)i=1
b)i=2
c)i=0
d) i=5
The Correct Answer is:
C)i=0
Explanation:
In the expression !i>14 , NOT (!) operator has more
precedence than ‘ >’ symbol. ! is a unary logical
operator. !i (!10) is 0 (not of true is false). 0>14 is false
(zero).
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
13. #include
main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}
a)SomeGarbageValue---1
b)SomeGarbageValue---2
c)error
d) default value
The Correct Answer is:
a)SomeGarbageValue---1
Explanation:
p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the third
2D(which you are not declared) it will print garbage values. *q=***a starting address of
a is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, it
will print first element of 3D array.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
14. main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}
a)link error
b)hia
c)hai
d)not display
The Correct Answer is:
c)hai
Explanation:
\n - newline
\b - backspace
\r - linefeed
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
15. main()
{
int i=5;
printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
}
a)compiled error
b)45544
c)45545
d)eruntime error
The Correct Answer is:
c)45545
Explanation:
The arguments in a function call are pushed into the stack from left to right. The
evaluation is by popping out from the stack. and the evaluation is from right to left,
hence the result.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
16. #include
#define a 10
main()
{
#define a 50
printf("%d",a);
}
a)0
b)50
c)error
d)40
The Correct Answer is:
b)50
Explanation:
The preprocessor directives can be redefined anywhere in the program. So the most
recently assigned value will be taken.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
17. main()
{
printf("%p",main);
}
a)Some address will be printed.
b)input
c)error
d)output
The Correct Answer is:
a)Some address will be printed.
Explanation:
Function names are just addresses (just like array names are addresses).
main() is also a function. So the address of function main will be printed. %p in printf
specifies that the argument is an address. They are printed as hexadecimal numbers.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
18. main()
{
clrscr();
}
clrscr();
a)No output/error
b)run time error
c)compiled error
d)not display
The Correct Answer is:
a)No output/error
Explanation:
The first clrscr() occurs inside a function. So it becomes a function call. In the second
clrscr(); is a function declaration (because it is not inside any function).
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
19. enum colors {BLACK,BLUE,GREEN}
main()
{
printf("%d..%d..%d",BLACK,BLUE,GREEN);
return(1);
}
a)1..1..2
b)0..0..2
c)0..1..2
d)error
The Correct Answer is:
c)0..1..2
Explanation:
enum assigns numbers starting from 0, if not explicitly defined.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
20. void main()
{
char far *farther,*farthest;
printf("%d..%d",sizeof(farther),sizeof(farthest));
}
a)4..2
b)4..4
c)0..0
d)error
The Correct Answer is:
a)4..2
Explanation:
the second pointer is of char type and not a far pointer
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
Pages:
1
2
3
4
Newer Post
Home
Ad Inside Post
Comments system
Disqus Shortname