C interview questions and answers

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

  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

  6. What will be printed as the result of the operation below:
    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);

        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.

98 Comments on C interview questions and answers

  1. Posted 4/7/2006 at 4:16 am | Permalink

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

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

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

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

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

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

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

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

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

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

    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.

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

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

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

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

  11. 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), it’s called by value, the x in function changevalue(x) won’t effect the x in main().

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

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

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

    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.

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

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

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

    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 !

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

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

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

  21. 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 Jessie’s doubt

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

    hi ajay kant singh ,
    depends on compiler…

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

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

    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.

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

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

  27. 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 you’re modifying the same variable more than once without an intervening sequence point.

    For more info read: .

  28. Bibin Thomas
    Posted 4/19/2007 at 8:02 am | Permalink
  29. 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.

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

    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.

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

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

    #
    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

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

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

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

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

  37. 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 “);
    }

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

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

  39. 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
    That’s the answer to it.

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

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

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

  42. 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 confusion among site users.

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

  44. 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();?

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

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

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

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

Post a Comment

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

*
*