C and C++ questions for the interview

  1. What is the output of printf(”%d”)
  2. What will happen if I say delete this
  3. Difference between “C structure” and “C++ structure”.
  4. Diffrence between a “assignment operator” and a “copy constructor”
  5. What is the difference between “overloading” and “overridding”?
  6. Explain the need for “Virtual Destructor”.
  7. Can we have “Virtual Constructors”?
  8. What are the different types of polymorphism?
  9. What are Virtual Functions? How to implement virtual functions in “C”
  10. What are the different types of Storage classes?
  11. What is Namespace?
  12. What are the types of STL containers?.
  13. Difference between “vector” and “array”?
  14. How to write a program such that it will delete itself after exectution?
  15. Can we generate a C++ source code from the binary file?
  16. What are inline functions?
  17. What is “strstream” ?
  18. Explain “passing by value”, “passing by pointer” and “passing by reference”
  19. Have you heard of “mutable” keyword?
  20. What is a “RTTI”?
  21. Is there something that I can do in C and not in C++?
  22. What is the difference between “calloc” and “malloc”?
  23. What will happen if I allocate memory using “new” and free it using “free” or allocate sing “calloc” and free it using “delete”?
  24. Difference between “printf” and “sprintf”.
  25. What is “map” in STL?
  26. When shall I use Multiple Inheritance?
  27. Explain working of printf.
  28. Talk sometiming about profiling?
  29. How many lines of code you have written for a single program?
  30. How to write Multithreaded applications using C++?
  31. Write any small program that will compile in “C” but not in “C++”
  32. What is Memory Alignment?
  33. Why preincrement operator is faster than postincrement?
  34. What are the techniques you use for debugging?
  35. How to reduce a final size of executable?
  36. Give 2 examples of a code optimization.
This entry was posted in C++. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

122 Comments on C and C++ questions for the interview

  1. Ashish Valecha
    Posted 5/9/2008 at 9:44 am | Permalink

    output of printf(”%d”); will be garbage value of integer type.

  2. viral
    Posted 5/22/2008 at 3:46 am | Permalink

    20. RTTI - Run time type identification. helps to know abt the types of variables or objects.

  3. viral rami
    Posted 5/22/2008 at 7:38 am | Permalink

    Difference between “vector” and “array”?

    The size of array can is fixed.

    A vector is also like an array sequentially accessed but we can increase/decrease size of a vector in a simple way than it is done for arrays, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

  4. varun
    Posted 7/1/2008 at 5:20 am | Permalink

    well printf(”%d”)
    will give an output 0
    because %d returns the integer value of the variable or the number of characters in othe cases
    thus the answer is 0

  5. Posted 7/7/2008 at 4:38 am | Permalink

    What is the output of printf(”%d”)

    remember that these are Questions, and they are expecting you to look at what they are asking. Anyone that answered this question with a number would have got it wrong. Anyone answering this question with -
    ~Unknown value, as the method was missing a parameter, would have got it right.

    Dependant on the compile and environment, this function can cause the application to crash, or cause unexpected errors.

    printf(”%d”)

    however if they had written the question:

    printf(”%d”,1);

  6. suresh kumar
    Posted 7/11/2008 at 5:14 am | Permalink

    what wil be d output of this programme…?

    main()
    {
    int i=5;
    printf(”%d%d%d%d%d”,i++,++i,i–,–i,i);
    }
    if you have good knowledge in c you can find

    contact me:suresh.jayakumar@yahoo.co.in

  7. Posted 8/8/2008 at 3:34 am | Permalink

    What is the output of printf(”%d”)?
    “%d” is used to read the value of given variable.
    But,inthis progm,It will show some garbage values
    #include
    #include
    main()
    {
    printf(”%d”);
    }

  8. manik
    Posted 8/8/2008 at 6:34 am | Permalink

    printf(”%d”) will print the value present to the function stack.
    try this:

    main()
    {
    int a=10;
    printf(”%d”);
    }
    it gives 10 as o/p since the stack contains 10…

  9. manik
    Posted 8/8/2008 at 6:36 am | Permalink

    3:
    member functions can be defined in c++ structure but not for c structure.

  10. manik
    Posted 8/8/2008 at 6:40 am | Permalink

    4: = oper is used in assignment
    but copy constr is used when we pass or return objects…

  11. Pulkit Dave
    Posted 9/15/2008 at 3:33 am | Permalink

    BOTTOMLINE:
    printf(”%d”); = 0 always

    printf(”%d”) = printf(”%01d”);

    so it will start with ONE(1) ZERO(0);
    printf(”%02d”); = 00
    start with TWO(2) ZERO(0);

    hope u get it

    regards,
    pulkit dave

  12. Posted 9/22/2008 at 12:34 am | Permalink

    1.What is the output of printf(”%d”)?
    Ans: This statement invokes undefined behaviour, because the C langauge standard says “If there are insufficient arguments for the format, the behavior is undefined”. In some compiler implementations, this might end up printing the values of the previously declared integer, which is stored in the stack, but this is not a standard behaviour, and should not be relied upon. This might print garbage value on some implementations, and on some others this can even crash the program.

    There is a known as format string vulnerability which was often used to crash programs a few years back. A string of printf(”%s”) would attempt to read data from the stack (on implementations where a stack is supported) till it attempts to read from an area which it is not supposed to access and hence would ultimately crash the program.

  13. Jagrati Tripathi
    Posted 9/30/2008 at 2:02 am | Permalink

    The diffrence Between Structure in C and c++ is that …in C all the members of structure by default public and in C++ theu by default private.

  14. nick
    Posted 12/29/2008 at 2:22 pm | Permalink

    Q19. Correct answer is “The [b]mutable[/b] keyword overrides any enclosing const statement. A mutable member of a const object can be modified.”

  15. srinivas
    Posted 1/6/2009 at 8:54 am | Permalink

    8. there are two types of polymorphism ,one is compile timeand another is run time.

  16. srinivas
    Posted 1/6/2009 at 8:57 am | Permalink

    21.yes we can do something in c but not c++,example is in c we can follow the structure ,it is necessary,but not in c++.because it is oops oriented

  17. Simar Bahia
    Posted 1/24/2009 at 10:04 am | Permalink

    What are the different types of polymorphism?
    There are number of polymorphisms, some of them are Function Overloading, Function Overriding and so on.

  18. Simar Bahia
    Posted 1/24/2009 at 10:06 am | Permalink

    When shall I use Multiple Inheritance?
    When we need to inherit/acquire properties from two or more superclasses in one subclass, then we can use Multiple Inheritance.

  19. Simar Bahi
    Posted 1/24/2009 at 10:09 am | Permalink

    Explain working of printf.
    printf is the in-built function stored in library of C. It is used to print the text written in printf() function in the output.

  20. Simar Bahi
    Posted 1/24/2009 at 10:12 am | Permalink

    What are the different types of Storage classes?
    auto :- used for local variables.
    extern :- used for global variables.
    static :- initial value is 0.
    register :- uses different types of registers.

  21. shashank
    Posted 1/25/2009 at 4:25 pm | Permalink

    printf(”%d”) prints 0;
    and
    printf(”%s”) prints null;
    why?
    can ne one give me the reason..!!!

  22. Vinod
    Posted 2/7/2009 at 11:12 am | Permalink

    *********************************************
    //snippet1
    int main(){
    printf(”%d”);
    return 0;
    }
    //result = 1314976(garbage)
    *********************************************
    //snippet2
    int one(){
    printf(”%d”);
    return 0;
    }
    int main(){
    one();
    }
    // result = 16384
    *********************************************
    //snippet3
    int one(){
    printf(”%d”);
    return 0;
    }
    int main(){
    printf(”%d”);
    one();
    }
    // result = -1
    *********************************************
    //snippet 4
    int two(){
    int x=10;
    printf(”%d”);
    return 0;
    }
    int main(){
    two();
    return 0;
    }
    // result = 10
    *******************************************
    I feel, the result is unpredictable as in the above
    snippets, different situations different output
    but same code( printf(”%d”); )
    The code deals with stack, that we all know.. but wat
    does the stack contain???

Post a Comment

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

*
*