- What will print out?
main()
{
char *p1=“name”;
char *p2;
p2=(char*)malloc(20);
memset (p2, 0, 20);
while(*p2++ = *p1++);
printf(“%sn”,p2);}
Answer:empty string.
- What will be printed as the result of the operation below:
main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf(“%d%dn”,x,y);}
Answer : 5794
- What will be printed as the result of the operation below:
main()
{
int x=5;
printf(“%d,%d,%dn”,x,x< <2,x>>2);}
Answer: 5,20,1
- What will be printed as the result of the operation below:
#define swap(a,b) a=a+b;b=a-b;a=a-b;
void main()
{
int x=5, y=10;
swap (x,y);
printf(“%d %dn”,x,y);
swap2(x,y);
printf(“%d %dn”,x,y);
}int swap2(int a, int b)
{
int temp;
temp=a;
b=a;
a=temp;
return 0;}
Answer: 10, 5
10, 5 - What will be printed as the result of the operation below:
main()
{
char *ptr = ” Cisco Systems”;
*ptr++; printf(“%sn”,ptr);
ptr++;
printf(“%sn”,ptr);}
Answer:Cisco Systems
isco systems - What will be printed as the result of the operation below:
main()
{
char s1[]=“Cisco”;
char s2[]= “systems”;
printf(“%s”,s1);
}Answer: Cisco
- What will be printed as the result of the operation below:
main()
{
char *p1;
char *p2;p1=(char *)malloc(25);
p2=(char *)malloc(25);strcpy(p1,”Cisco”);
strcpy(p2,“systems”);
strcat(p1,p2);printf(“%s”,p1);
}
Answer: Ciscosystems
- The following variable is available in file1.c, who can access it?:
static int average;
Answer: all the functions in the file1.c can access the variable.
- WHat will be the result of the following code?
#define TRUE 0 // some code
while(TRUE)
{// some code
}
Answer: This will not go into the loop as TRUE is defined as 0.
- What will be printed as the result of the operation below:
int x;
int modifyvalue()
{
return(x+=10);
}int changevalue(int x)
{
return(x+=1);
}void main()
{
int x=10;
x++;
changevalue(x);
x++;
modifyvalue();
printf("First output:%dn",x);x++;
changevalue(x);
printf("Second output:%dn",x);
modifyvalue();
printf("Third output:%dn",x);}
Answer: 12 , 13 , 13
- What will be printed as the result of the operation below:
main()
{
int x=10, y=15;
x = x++;
y = ++y;
printf(“%d %dn”,x,y);}
Answer: 11, 16
- What will be printed as the result of the operation below:
main()
{
int a=0;
if(a==0)
printf(“Cisco Systemsn”);
printf(“Cisco Systemsn”);}
Answer: Two lines with “Cisco Systems” will be printed.
-
Job Interview Question Articles
- C# Interview Questions and Answers
- QTP Interview Questions and Answers
- C++ Interview Questions and Answers
- PHP Interview Questions and Answers
- XML Interview Questions and Answers
- JavaScript Interview Questions and Answers
- Asp.Net Interview Questions and Answers
- J2EE Interview Questions and Answers
- ABAP Interview Questions and Answers
- Perl Interview Questions and Answers
- Java Interview Questions and Answers
-
Resources
- Technology Question and Answer Website
- How to dance around the salary-expectation question
- 10 mistakes managers make during job interviews
- ID Maker
- Stupid interview questions
- How to Answer These Tricky Interview Questions
- Seven tips for writing an online profile for LinkedIn, MySpace or Facebook
- Video surveillance
- Ink cartridges
- Laptop computers
- Affordable life insurance
- Ink cartridges
-
Tutorials
-
RSS Feeds

98 Comments on C interview questions and answers
#1 It’s very very very easy. When printf, p2 points to the 4th position not the beginning. (from 4th position to 20th position is 0)
#5 is correct. Both GCC/linux as well as Windows (both VC6 and Cygwin) print as answered - (Answer:Cisco Systems
isco systems)
This is assuming you “#include ” prior to main().
re: #5 is correct That is also if you fix the quotes problem in the strings….
there is some prob with q 5 and q 10
i use turbo c compiler
*ptr++ or *(ptr++)
if i nw print ptr as a char
it should be printed as C..
but tc displays error… code has no effect..
even with %s it says code has no effect….
with (*ptr)++ it is correct..
! is printed
please ans this query…..
also in q no. 10 if i mention x as static
it should persist bw functn calls
but still 12,13,13 is output
tel me why?????????
for #11 it the answer is 10 16.
I got scared for a while there.
can we multiply two long integers by using only integer and string
Question 8–This fails to compile in gcc:
void test()
{
stvar = 3;
}
static int stvar = 3;
It’s a small point, but that seems to be what this test is about.
#10 explanation
1)variable X is intialized to 10
2)x++ will have the value 11
eventhough the function changevalue() have a return stmt there is no variable to store the value
3) after excecution of the function changevalue() the control comes back to the next stmt x++ where the value of x becomes 12
4)the modifyvalue() function also dont store the value(similar to the above said changevalue())
5)so the value of x ie 12 is printed
rest of the excecution is similar please try it out
hi,
what wil be the output of below function return and whats the importance of volatile here
int sqrt(volatile int *ptr)
{
return (*ptr**ptr);
}
Pretty good questions.
Only one error in your output in question #4
Your output
10, 5
10, 5
should be
10 5
10 5
No comma between the numbers only space.
Thanks
Explanation to #10:
First line x is global variable, the x in main() is local variable. The function modifyvalue() only effects global x, but not the local x in main().
As to changevalue(x), it’s called by value, the x in function changevalue(x) won’t effect the x in main().
How can you find the number of elements stored in a static array at a time ?
hi everybody
main()
{
int x=10, y=15;
x = x++;
y = ++y;
printf(“%d %d\nâ€Â,x,y);
}
ans should be
11,15
for x++ x=x+1 i.e 11
y;but due to preincrement value of y after print is 15
can u clarify the ans
helloo manu ,
as u said y’s value shud be 15 is correct but u should note the line as it is y=++y which means the value will be incremented and stored into y itself.
So when it come to next line it will show as 16 only.
hi
++y
and
y=++y;
both r one and the same.using the second one that is y=++y is meaningless because
++y itself means increment y and store it back in y itself so using y=++y is worth nothing.
and because there r no expresions used ++y and y++ both give the same answer,
In Question 2, at step 1: x=y++ + x++
it 20+35=55
and at second statement , ++y + ++x
and 22+56=78
the answrer is :5578
can you explaini it to me..
Question #3:
This is a peculiarity with printf if I am not mistaken…since a separate shift with print produces the sequence 5 20 5. The printf version is correctly 5 20 1, so if anyone can explain the printf mangling of the 3rd bit, thank you !
Hi Maxmelbin
The answer of Q#3 is 5,20,1
because
a>b=a/(square of b)
That means
for a=5
a>2=5/(2*2)=5/4=1(take only integer value)
Hi!
I dont understand the 10th question.Can any one of u explain me?
What will be printed as the result of the operation below:
int x;
int modifyvalue()
{
return(x+=10);
}
int changevalue(int x)
{
return(x+=1);
}
void main()
{
int x=10;
x++;
changevalue(x);
x++;
modifyvalue();
printf(”First output:%d\n”,x);
x++;
changevalue(x);
printf(”Second output:%d\n”,x);
modifyvalue();
printf(”Third output:%d\n”,x);
}
Answer: 12 , 13 , 13
the variable x been defined in main (which is local to main) can be accessed in main, where as the variable x accessed in the subroutines is the global variable
in other words x in modifyvalue() is the global variable, in changevalue(x) is the x local to changevalue, in main() again as it is defined here its local to main.
hope this clears Jessie’s doubt
hi ajay kant singh ,
depends on compiler…
Hello All,
Can ne one help me in getting the right answer of the question?
q-> Which one of the following is NOT a default promotion?
Choice 1 If either operand of an arithmetic operator is unsigned long, the other operand is promoted to unsigned long.
Choice 2 If either operand of an arithmetic operator is unsigned int, the other operand is promoted to unsigned int.
Choice 3 If either operand of an arithmetic operator is int, the other operand is promoted to int.
Choice 4 If either operand of an arithmetic operator is double, the other operand is promoted to double.
Choice 5 If either operand of an arithmetic operator is short, the other operand is promoted to
Reffered to Poly…
The answer to the Q # 4 is correct.
The first function is declared as global and hence the x and y values are swapped after calling the swap(x,y) function. Now the current values of x and y are 10 and 5.
When the second function swap1(x,y) is called the values x=10 and y=5 are passed to the function. But as this is not call by reference the swapped values are lost once it comes out of the man function. Hence the original values x=10 and y=5 are printed.
priyanka said,
hi ppl ,
#2. x=20, y=35
now x= y++ + x++;
i.e. 35 + 20 ; // after this x= 21 ,y=36
now y= ++x + ++y ;
i.e. 22 + 37 ;
So the answer sd : 5559(i.e 55 and 59 ,,94 is out of question..Plz explain) .
This answer was wrong……..
correct answer is…..
x= y++ + x++;
i.e. 35 + 20 ; // after this x= 76 ,y=36
now y= ++x + ++y ;
i.e. 77 + 37 ; // after this x=77 , y=114;
is it clear………
main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf(“%d%d\nâ€Â,x,y);
}
Answer : 5794
Can anybody explain me this increments in a order..
Q:2
x=y++ + x++;
y= ++y + ++x;
Q:11
x = x++;
Lots of comments on this!!!
i = i++ is UB(Undefined Behaviour) beacuse you’re modifying the same variable more than once without an intervening sequence point.
For more info read: .
(UB)
for more info:
http://en.wikipedia.org/wiki/Sequence_point
16 is incorrect (answer is not “11 16″, but “10 16″):
$ g++ test2.cpp ;./a
10 16
$ g++ –version
g++ (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125)
Copyright (C) 2004 Free Software Foundation, Inc.
Answer to muralipatta’s Qn,
===================================================================
1.if i hav a declatration like this int a[5]={1,3); why the elements a[2],a[3],a[4] are equal to zero.
2.int a[5];
a[0]=4;
a[1]=5;
then why the values a[2],a[3],a[4] are garbage.
===================================================================
In the first case, as explicit array size is specified, and a shorter initiliazation list is specified, the unspecified elements are set to zero by the compiler. So you will get zeroes for the 2,3,4 elements.
In the second case the array has no initialization, so the values are undefined during compile time. So the array will have garbage values for all locations. And you are changing the values of indexes 0 and 1 only at runtime.
#
satya said,
priyanka said,
hi ppl ,
#2. x=20, y=35
now x= y++ + x++;
i.e. 35 + 20 ; // after this x= 21 ,y=36
now y= ++x + ++y ;
i.e. 22 + 37 ;
So the answer sd : 5559(i.e 55 and 59 ,,94 is out of question..Plz explain) .
This answer was wrong……..
correct answer is…..
x= y++ + x++;
i.e. 35 + 20 ; // after this x= 76 ,y=36
now y= ++x + ++y ;
i.e. 77 + 37 ; // after this x=77 , y=114;
is it clear………
My opinion is like, Answer is compiler dependent.
It behaves different for different compilers.
In GCC its ans is 5693
Its Compiler dependent.
#
kauai said,
On #1,
while(*p2++ = *p1++); When you do an assignment like this isn’t always true? Hence an infinite loop (or at least until there is a memory access violation) ??
THIS IS TOTALL WRONG ,
since value which is pointed by p2 (ie “name”) is a string and string always terminat with 0 so there is no chance at all that causes Access Violation.. Hope it clears
clarification for Question#2
step1:
x=y++ + x++; (note “operator precedence” ++ have high then +)
so x=21 + 36=57
step2:
y=++y + ++x; (note “operator precedence” all have same so precedence is right to left)
so y=36 + 58 =94 (++y value assigned only after addition )
ya ,ans 4 2nd 1 is,
5794…
x=20; y=35;
I step: x=y++ + x++;
x=36 + 21 = 57;
II step:y=++y + ++x;
y=36 + 58 = 94; /*here value of x assigns 57 then incremented to 58*/
Nw got it …
So ans s 5794 …
Can any1 say wat s d o/p of printf(”%d”);
Hi 2 all , i think d ans 4 d abve qn s
o/p shows some “Garbage” values ,its my guess,so any other answers / comments …
= y++ + x++;
i.e. 35 + 20 ; // after this x= 76 ,y=36
now y= ++x + ++y ;
i.e. 77 + 37 ; // after this x=77 , y=114;
is it clear………
explain again
int main()
{
int iX = 40000 , iY = 20000;
if( iX + iY > 0)
printf(” I am in if “);
else
printf(” I am in else “);
}
How can i write a macro for finding squreroot of any number?
main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf(“%d%d\nâ€Â,x,y);
}
Answer : 5794
UNDEFINED BEHAVIOR
That’s the answer to it.
The parts of x = x++ and y = ++y invokes what is called in programming Undefined Behavior.
When you change the value of a variable more than once in the same sequence point you are in the land of Undefined Behavior. Meaning you get unpredictable effect. Depending of compiler implementation or situation. Not a good thing to do.
For further reference read in the C Standards:
ISO Sec. 5.1.2.3, Sec. 6.3, Sec. 6.6, Annex C
Sec. 6.3
and in K&R1 Sec. 2.12 p. 50
K&R2 Sec. 2.12 p. 54
1) what is mean by object?
Ans : Object is a instance of a class.
#5 The answer is displayed correctly, because the string begins with a space ” Cisco Systems”.
It might be useful to display all pieces of code with Courier New font to avoid creating confusion among site users.
hi friends,
somebody can tell me what will be the out put of following code and why?
for (i=printf("a");i<printf("%d");printf("b"))
c+=printf("c");
hi friends
what is the difference between exit(0) and exit(1)?
and printf(); and xprintf();?
#explanation for ans10
whenever thre is a function call the value of x gets modifird in that fuc. only..when the fun. returns control to main function the value of x in main persidts .SInce the pritf is called for x in main thus the changed value in main gets printed.
as:int x=10;
x++; //x=11
changevalue(x) //no effect of this change
in value of x in main;
x++ //x=12
…….is it clear!!!!!
# SHYJU KV wrote:
clarification for Question#2
step1:
x=y++ + x++; (note “operator precedence†++ have high then +)
so x=21 + 36=57
step2:
y=++y + ++x; (note “operator precedence†all have same so precedence is right to left)
so y=36 + 58 =94 (++y value assigned only after addition )
In step 2:What do you mean by operator precedence all have same?? I think ++ has more precedence to +?? please clarify
Chetana,
@Question2: I think the answers are: 35+20=55 and 36+56=92.
Itz about where the increment (++) is positioned - before the variable or after the variable.
If the incrementation is performed after the variable, then the incremented variable wont be used in the equation. (unlike pre positioned incrementation)
Class afixi {
Function biswa(){
String s= “MY Name Is Biswa .â€Â;
}
}
i) How to get the value of s out side of the class ?
ii) How to initialize a class in another class ?
iii) How to get the method of a class in another class.
iv) Define the class, object, method ?