C Interview Questions and Answers _ TechInterviews

Embed Size (px)

Citation preview

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    1/13

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    2/13

    Powered byWordPress. Built on the Thematic Theme Framework.

    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

    5.

    What will be printed as the result of the operation below:

    main()

    {

    char s1[]=Cisco;

    char s2[]= systems;

    printf(%s,s1);

    }

    Answer: Cisco

    6.

    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

    7.

    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.

    8.

    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.

    9.

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

    }

    voidmain()

    {

    int x=10;x++;

    changevalue(x);

    x++;

    modifyvalue();

    printf("First output:%dn",x);

    10.

    Ruby on Rails tutorials

    Salary guide for IT jobs

    Self-employment

    TechInterviews guides in PDF

    Understanding pointers

    XML Tutorials

    XUL tutorials

    RSS Feeds

    All posts

    All comments

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

    13 30-09-2012 17:19

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    3/13

    Hardware and software design questions .NET interview questions - Windows Forms

    Ads by Google

    Programming in C

    Self-Paced

    Learning:PC,Android,iPad ByJ.B. Dixit. Just Rs.314

    Attano.com/InstantDelivery

    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

    11.

    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.

    12.

    This entry was posted in C++. Bookmark thepermalink.Post a commentor leave a trackback: Trackback

    URL.

    98 COMMENTS ON C INTERVIEW QUESTIONS AND ANSWERS

    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

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

    13 30-09-2012 17:19

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    4/13

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

    Ankur

    Posted 7/4/2006 at 9:12 a m | 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 charit 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?????????

    srikPosted 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

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

    13 30-09-2012 17:19

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    5/13

    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 fu nction

    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 a m | 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

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

    13 30-09-2012 17:19

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    6/13

    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 a m | Permalink

    hi

    ++y

    andy=++y;

    both r one and the same.using the second one that is y=++y is meaningless becau se

    ++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=55and 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=5a>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)

    {

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

    13 30-09-2012 17:19

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    7/13

    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

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

    13 30-09-2012 17:19

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    8/13

    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 a m | 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)

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

    13 30-09-2012 17:19

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    9/13

    for more info:

    http://en.wikipedia.org/wiki/Sequence_point

    Me

    Posted 6/3/2007 at 2:45 a m | Permalink

    16 is incorrect (answer is not 11 16, but 10 16):

    $ g++ test2.cpp ;./a10 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] areequal 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 a m | 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.

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

    13 30-09-2012 17:19

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    10/13

    sapkota

    Posted 7/30/2007 at 11:55 a m | 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

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

    f 13 30-09-2012 17:19

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    11/13

    Biswajit Dash

    Posted 9/6/2007 at 1 1: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 ch ange 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

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

    f 13 30-09-2012 17:19

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    12/13

    confusion among site users.

    DIGPAL

    Posted 3/7/2008 at 7:31 a m | Permalink

    hi friends,

    somebody can tell me what will be the out put of following code and why?

    for (i= printf("a");i

  • 7/31/2019 C Interview Questions and Answers _ TechInterviews

    13/13

    Name * Email *

    Previous 1 2

    POST A COMMENT

    Your email isnever published nor shared. Required fields are marked *

    Website

    Comment

    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 ?

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