Monday, March 24, 2008

C++ interview questions @ Tech Interviews.com

C++ interview questions @ Tech Interviews.com

This set of C++ interview questions was sent to TechInterviews from Australia:

  1. What is the difference between an ARRAY and a LIST?
  2. What is faster : access the element in an ARRAY or in a LIST?
  3. Define a constructor - what it is and how it might be called (2 methods).
  4. Describe PRIVATE, PROTECTED and PUBLIC – the differences and give examples.
  5. What is a COPY CONSTRUCTOR and when is it called (this is a frequent question !)?
  6. Explain term POLIMORPHISM and give an example using eg. SHAPE object: If I have a base class SHAPE, how would I define DRAW methods for two objects CIRCLE and SQUARE.
  7. What is the word you will use when defining a function in base class to allow this function to be a polimorphic function?
  8. What are 2 ways of exporting a function from a DLL?
  9. You have two pairs: new() and delete() and another pair : alloc() and free(). Explain differences between eg. new() and malloc()
  10. What is a callback function. Explain in C and C++ and WIN API environment.
  11. (From WINDOWS API area): what is LPARAM and WPARAM?

11 Responses to “C++ interview questions”

  1. bhaskar Krishna Says:

    What is the difference between an ARRAY and a LIST?

    Array is collection of homogeneous elements.
    List is collection of heterogeneous elements.

    For Array memory allocated is static and continuous.
    For List memory allocated is dynamic and Random.

    Array: User need not have to keep in track of next memory allocation.
    List: User has to keep in Track of next location where memory is allocated.

  2. bhaskar Krishna Says:

    What is faster : access the element in an ARRAY or in a LIST?

    It’s array the reason is it continuous.

  3. bhaskar Krishna Says:

    Define a constructor - what it is and how it might be called (2 methods).

    constructor is a member function of the class, with the name of the function being the same as the class name. It also specifies how the object should be initialized.

    Ways of calling constructor:

    1) Implicitly: automatically by complier when an object is created.

    2) Calling the constructors explicitly is possible, but it makes the code unverifiable.

  4. vaibhav chauhan Says:

    You have two pairs: new() and delete() and another pair : alloc() and free(). Explain differences between eg. new() and malloc()

    1.) “new and delete” are preprocessors while “malloc() and free()” are functions. [we dont use brackets will calling new or delete].

    2.) no need of allocate the memory while using “new” but in “malloc()” we have to use “sizeof()”.

    3.) “new” will initlize the new memory to 0 but “malloc()” gives random value in the new alloted memory location [better to use calloc()].

    vaibhav chauhan

  5. A question Says:

    I have a problem !.T don’t know How many bytes does “++” advance the pointer LPWORD lpWord?. Can you help me please ! Thanks .

  6. Bumba Says:

    Explain term POLIMORPHISM and give an example using eg. SHAPE object: If I have a base class SHAPE, how would I define DRAW methods for two objects CIRCLE and SQUARE

    POLYMORPHISM : A phenomenon which enables an object to react differently to the same function call.
    in C++ it is attained by using a keyword virtual
    example
    public class SHAPE
    {
    public virtual void SHAPE::DRAW()=0;
    }
    Note here the function DRAW() is pure virtual which means the sub classes must implement the DRAW() method and SHAPE cannot be instatiated
    public class CIRCLE::public SHAPE
    {
    public void CIRCLE::DRAW()
    {
    // TODO drawing circle
    }
    }
    public class SQUARE::public SHAPE
    {
    public void SQUARE::DRAW()
    {
    // TODO drawing square
    }
    }
    now from the user class the calls would be like

    globally
    SHAPE *newShape;

    when user action is to draw
    public void MENU::OnClickDrawCircle(){
    newShape = new CIRCLE();
    }

    public void MENU::OnClickDrawCircle(){
    newShape = new SQUARE();
    }

    the when user actually draws
    public void CANVAS::OnMouseOperations(){
    newShape->DRAW();
    }

    What is the word you will use when defining a function in base class to allow this function to be a polimorphic function?
    virtual

  7. Michael Mountrakis Says:

    Q1.What is the difference between an ARRAY and a LIST?
    A1. Array uses direct access of stored members, list uses sequencial access for members.

    //With Array you have direct access to memory position 5
    Object x = a[5]; // x takes directly a reference to 5th element of array

    //With the list you have to cross all previous nodes in order to get the 5th node:
    list mylist;
    list::iterator it;

    for( it = list.begin() ; it != list.end() ; it++ )
    {
    if( i==5)
    {
    x = *it;
    break;
    }
    i++;
    }

    Q2. What is faster : access the element in an ARRAY or in a LIST?
    A2. See previous example. With the list implementation of our data structure we need n-1 iteration steps before we reach n-nth item.
    With the array implementation we need a single ste to do so.

    Q3. Define a constructor - what it is and how it might be called (2 methods).
    A3.

    class Point2D{
    int x; int y;
    public Point2D() : x(0) , y(0) {} //default (no argument) constructor
    };

    main(){

    Point2D MyPoint; // Implicit Constructor call. In order to allocate memory on stack, the default constructor is implicitly called.

    Point2D * pPoint = new Point2D(); // Explicit Constructor call. In order to allocate memory on HEAP we call the default constructor.

    Q4. Describe PRIVATE, PROTECTED and PUBLIC – the differences and give examples.
    A4.

    class Point2D{
    int x; int y;

    public int color;
    protected bool pinned;
    public Point2D() : x(0) , y(0) {} //default (no argument) constructor
    };

    Point2D MyPoint;

    You cannot directly access private data members when they are declared (implicitly) private:

    MyPoint.x = 5; // Compiler will issue a compile ERROR
    //Nor yoy can see them:
    int x_dim = MyPoint.x; // Compiler will issue a compile ERROR

    On the other hand, you can assign and read the public data members:

    MyPoint.color = 255; // no problem
    int col = MyPoint.color; // no problem

    With protected data members you can read them but not write them:
    MyPoint.pinned = true; // Compiler will issue a compile ERROR

    bool isPinned = MyPoint.pinned; // no problem

    Q5.What is a COPY CONSTRUCTOR and when is it called (this is a frequent question !)?
    A5 A copy constructor is a method that accepts an object of the same class and copies it’s data members to the object on the left part of assignement:

    class Point2D{
    int x; int y;

    public int color;
    protected bool pinned;
    public Point2D() : x(0) , y(0) {} //default (no argument) constructor
    public Point2D( const Point2D & ) ;
    };

    Point2D::Point2D( const Point2D & p )
    {
    this->x = p.x;
    this->y = p.y;
    this->color = p.color;
    this->pinned = p.pinned;
    }

    main(){

    Point2D MyPoint;

    MyPoint.color = 345;

    Point2D AnotherPoint = Point2D( MyPoint ); // now AnotherPoint has color = 345

    Q6 Explain term POLIMORPHISM and give an example using eg. SHAPE object: If I have a base class SHAPE, how would I define DRAW methods for two objects CIRCLE and SQUARE.
    A6
    class SHAPE{
    public virtual Draw() = 0; //abstract class with a pure virtual method
    };

    class CIRCLE{
    public int r;
    public virtual Draw() { this->drawCircle(0,0,r); }
    };

    class SQURE
    public int a;
    public virtual Draw() { this->drawRectangular(0,0,a,a); }
    };

    Each object is driven down from SHAPE implementing Draw() function in its own way.

    Q7 What is the word you will use when defining a function in base class to allow this function to be a polimorphic function?
    A7
    virtual

    Q8 What are 2 ways of exporting a function from a DLL?
    A7
    1.Taking a reference to the function from the DLL instance.
    2. Using the DLL ’s Type Library

    Q9 You have two pairs: new() and delete() and another pair : alloc() and free(). Explain differences between eg. new() and malloc()
    new() allocates continous space for the object instace
    malloc() allocates distributed space.
    new() is castless, meaning that allocates memory for this specific type,
    malloc(), calloc() allocate space for void * that is cated to the specific class type pointer.

    Mike Mountrakis
    Illumine Consulting

  8. MP Says:

    There are many errors in above comments, so I feel a need to post some references.

    3. public methods/attributes are accessible to all classes.
    protected are accessible to class and derived classes.
    private are only for class (not for derived classes)
    http://www.cppreference.com/keywords/protected.html

    8. Windows only! For *nix it is a bit different ..
    write a .def file or mark functions as extern “C” __declspec(dllexport) fn…

    9. new is a C++ operator, and a) allocates memory on heap b) calls class constructor. On the other hand, malloc() is a C function that only allocates memory on heap (does not call constructor). You must then call constructor yourself (or use placement new operator - new(ptr) ). Note that memory is NOT zeroed either with new or malloc (but might be depending on compiler).

  9. Pravin Wagh Says:

    MP - regarding memory being zeroed or not, it’s not necessarily the compiler but the build settings. When you build in a DEBUG configuration, the runtime uses memory manager code that will often take care of zeroing out memory for you.

    In Visual C++, there is also the convention of initializing the memory to predefined values such as 0xcccc or 0xcdcd (which ends up being a great way to discover whether somebody has erroneously set a member variable or failed to set it in the first place - because nobody would intentionally choose a number like 0xcccc or 0xcdcd would they?)

  10. ajay kant singh Says:

    The diffrence between array and the list is that:

    array shows the contigus memory location and list represents the dynamic memory allocation.

    array size should be specified but the list size can be determied at run time also.

    list refers to use of pointers.

  11. ajay kant singh Says:

    The Difference Between The private Public And Protected member are:

    public: The data of the class can be inherited into another class.

    private: The data of the class type private cannot be inherited into another class.

    protected : These data of the class can be inherited but they are same as private .

No comments:

如何发掘出更多退休的钱?

如何发掘出更多退休的钱? http://bbs.wenxuecity.com/bbs/tzlc/1328415.html 按照常规的说法,退休的收入必须得有退休前的80%,或者是4% withdrawal rule,而且每年还得要加2-3%对付通胀,这是一个很大...