You are on page 1of 13

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

Search Tech Interviews

Tech Interviews
Prepare for job interviews with the questions and answers asked by high-tech employers
.NET C++ Database General Hardware Java Networking Puzzles SAP ABAP Testing Unix/Linux VB Web dev Windows

C++ >> C interview questions and answers

C interview questions and answers


By admin | December 23, 2003

1. 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. 2. What will be printed as the result of the operation below:
Ads by Google

Programming in C
Self-Paced Learning:PC,Android,iPad By J.B. Dixit. Just Rs.314
Attano.com/InstantDelivery

main() { int x=20,y=35; x=y++ + x++; y= ++y + ++x; printf(%d%dn,x,y); }


Answer : 5794 3. What will be printed as the result of the operation below:

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

main() { int x=5; printf(%d,%d,%dn,x,x< <2,x>>2); }


Answer: 5,20,1 4. What will be printed as the result of the operation below:

Resources
Technology Question and Answer Website How to dance around the salary-expectation question 10 mistakes managers make during job interviews Stupid interview questions How to Answer These Tricky Interview Questions Seven tips for writing an online profile for LinkedIn, MySpace or Facebook Laptop computers Affordable life insurance Ink cartridges

#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; }

Tutorials
AJAX Tutorials Dealing with your job Getting a job JavaScript tutorials Job interview tips from Yahoo! HotJobs MySQL tutorials Retiring from your job

1 of 13

30-09-2012 17:19

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

Answer: 10, 5 10, 5 5. What will be printed as the result of the operation below:
Powered by WordPress. Built on the Thematic Theme Framework. main()

Ruby on Rails tutorials Salary guide for IT jobs Self-employment TechInterviews guides in PDF Understanding pointers XML Tutorials XUL tutorials

{ char *ptr = Cisco Systems; *ptr++; printf(%sn,ptr); ptr++; printf(%sn,ptr); }


Answer:Cisco Systems isco systems 6. What will be printed as the result of the operation below:

RSS Feeds
All posts All comments

main() { char s1[]=Cisco; char s2[]= systems; printf(%s,s1); }


Answer: Cisco 7. 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 8. 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. 9. 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. 10. 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);

2 of 13

30-09-2012 17:19

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

x++; changevalue(x); printf("Second output:%dn",x); modifyvalue(); printf("Third output:%dn",x); }


Answer: 12 , 13 , 13 11. 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 12. 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.
This entry was posted in C++. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Hardware and software design questions


Ads by Google

.NET interview questions - Windows Forms

Programming in C
Self-Paced Learning:PC,Android,iPad By J.B. Dixit. Just Rs.314
Attano.com/InstantDelivery

98 COMMENTS O N C INTERV IEW Q UESTIONS AND A NS WERS

moimart
Posted 4/7/2006 at 4:16 am | Permalink

#1 Its very very very easy. When printf, p2 points to the 4th position not the beginning. (from 4th position to 20th position is 0)

Joe Yabyam
Posted 6/14/2006 at 4:46 pm | Permalink

#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().

Joe Yabyam
Posted 6/14/2006 at 4:48 pm | Permalink

3 of 13

30-09-2012 17:19

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

re: #5 is correct That is also if you fix the quotes problem in the strings.

Ankur
Posted 7/4/2006 at 9:12 am | Permalink

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?????????

srik
Posted 7/8/2006 at 2:52 pm | Permalink

for #11 it the answer is 10 16. I got scared for a while there.

sreejesh
Posted 7/21/2006 at 5:28 am | Permalink

can we multiply two long integers by using only integer and string

Scott
Posted 7/21/2006 at 4:20 pm | Permalink

Question 8This fails to compile in gcc: void test() { stvar = 3; } static int stvar = 3; Its a small point, but that seems to be what this test is about.

navya
Posted 8/23/2006 at 3:11 am | Permalink

#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

Divya
Posted 9/13/2006 at 2:53 pm | Permalink

4 of 13

30-09-2012 17:19

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

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); }

ben lev
Posted 10/16/2006 at 6:17 pm | Permalink

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

Wei
Posted 10/18/2006 at 2:03 am | Permalink

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), its called by value, the x in function changevalue(x) wont effect the x in main().

bharat biswal
Posted 11/3/2006 at 4:50 am | Permalink

How can you find the number of elements stored in a static array at a time ?

mannu
Posted 11/18/2006 at 4:35 am | Permalink

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

Surya
Posted 11/30/2006 at 2:08 pm | Permalink

helloo manu , as u said ys value shud be 15 is correct but u should note the line as it is y=++y which

5 of 13

30-09-2012 17:19

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

means the value will be incremented and stored into y itself. So when it come to next line it will show as 16 only.

mrinu
Posted 12/18/2006 at 2:11 am | Permalink

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,

ajay kant singh


Posted 1/20/2007 at 8:49 am | Permalink

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..

Anders
Posted 1/22/2007 at 3:00 am | Permalink

Question #3: This is a peculiarity with printf if I am not mistakensince 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 !

malaram
Posted 1/24/2007 at 1:58 am | Permalink

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)

Jessie
Posted 2/16/2007 at 1:04 pm | Permalink

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) {

6 of 13

30-09-2012 17:19

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

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

sangeeta
Posted 2/16/2007 at 10:59 pm | Permalink

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

sangeeta
Posted 2/16/2007 at 11:03 pm | Permalink

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 Jessies doubt

Ratheesh
Posted 2/18/2007 at 11:23 pm | Permalink

hi ajay kant singh , depends on compiler

Ashish
Posted 3/1/2007 at 12:47 pm | Permalink

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

sirisha
Posted 3/9/2007 at 11:12 pm | Permalink

7 of 13

30-09-2012 17:19

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

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.

satya
Posted 3/14/2007 at 12:20 am | Permalink

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

Sathish Kumar
Posted 4/18/2007 at 5:02 am | Permalink

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..

Bibin Thomas
Posted 4/19/2007 at 8:00 am | Permalink

Q:2 x=y++ + x++; y= ++y + ++x; Q:11 x = x++; Lots of comments on this!!! i = i++ is UB(Undefined Behaviour) beacuse youre modifying the same variable more than once without an intervening sequence point. For more info read: .

Bibin Thomas
Posted 4/19/2007 at 8:02 am | Permalink

(UB)

8 of 13

30-09-2012 17:19

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

for more info: http://en.wikipedia.org/wiki/Sequence_point

Me
Posted 6/3/2007 at 2:45 am | Permalink

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.

James
Posted 6/7/2007 at 7:23 am | Permalink

Answer to muralipattas 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.

RameshReddy
Posted 6/15/2007 at 5:44 am | Permalink

# 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.

9 of 13

30-09-2012 17:19

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

sapkota
Posted 7/30/2007 at 11:55 am | Permalink

# kauai said, On #1, while(*p2++ = *p1++); When you do an assignment like this isnt 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

SHYJU KV
Posted 8/13/2007 at 4:00 pm | Permalink

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 )

Karthik ...
Posted 8/17/2007 at 5:35 am | Permalink

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

Karthik ...
Posted 8/17/2007 at 5:40 am | Permalink

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

priya
Posted 8/27/2007 at 12:33 pm | Permalink

= 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

10 of 13

30-09-2012 17:19

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

Biswajit Dash
Posted 9/6/2007 at 11:34 am | Permalink

int main() { int iX = 40000 , iY = 20000; if( iX + iY > 0) printf( I am in if ); else printf( I am in else ); }

Tarak
Posted 11/23/2007 at 4:06 am | Permalink

How can i write a macro for finding squreroot of any number?

Aia
Posted 12/1/2007 at 1:23 pm | Permalink

main() { int x=20,y=35; x=y++ + x++; y= ++y + ++x; printf(%d%d\n,x,y); } Answer : 5794 UNDEFINED BEHAVIOR Thats the answer to it.

Aia
Posted 12/1/2007 at 2:12 pm | Permalink

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

Swapnil
Posted 12/2/2007 at 11:14 am | Permalink

1) what is mean by object? Ans : Object is a instance of a class.

Mihai Gospodaru
Posted 2/29/2008 at 3:06 pm | Permalink

#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

11 of 13

30-09-2012 17:19

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

confusion among site users.

DIGPAL
Posted 3/7/2008 at 7:31 am | Permalink

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");

jyothi
Posted 3/12/2008 at 11:55 pm | Permalink

hi friends what is the difference between exit(0) and exit(1)? and printf(); and xprintf();?

ashish mishra
Posted 8/23/2008 at 4:11 pm | Permalink

#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!!!!!

Chetana
Posted 10/9/2008 at 11:06 am | Permalink

# 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

AJ
Posted 11/7/2008 at 4:44 am | Permalink

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)

12 of 13

30-09-2012 17:19

C interview questions and answers | TechInterviews

http://www.techinterviews.com/c-interview-questions-and-answers-3

Susanta kumar swain


Posted 11/18/2008 at 1:30 am | Permalink

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 ?

Previous 1 2

P OS T A COMMENT

Your email is never published nor shared. Required fields are marked *
Name * Email *

Website

Comment

13 of 13

30-09-2012 17:19

You might also like