Google

C structure and C++ structure interview questions

This is not really a set of interview questions, a reader sent TechInterviews.com what looks like a copy of his notes from C++ class that describes various tricks and code for C++.

  1. What is the output of printf("%d")?
    1. %d helps to read integer data type of a given variable
    2. when we write ("%d", X) compiler will print the value of x assumed in the main
    3. but nothing after ("%d") so the output will be garbage
    4. printf is an overload function doesnt check consistency of the arg list – segmentation fault
  2. What will happen if I say delete this? - destructor executed, but memory will not be freed (other than work done by destructor). If we have class Test and method Destroy { delete this } the destructor for Test will execute, if we have Test *var = new Test()
    1. pointer var will still be valid
    2. object created by new exists until explicitly destroyed by delete
    3. space it occupied can be reused by new
    4. delete may only be applied to a pointer by new or zero, applying delete to zero = no FX
    5. delete = delete objects
    6. delete[] – delete array
    7. delete operator destroys the object created with new by deallocating the memory assoc. with the object
    8. if a destructor has been defined fir a class delete invokes that desructor
  3. Difference between C structure and C++ structure - C++ places greater emphasis on type checking, compiler can diagnose every diff between C and C++
    1. structures are a way of storing many different values in variables of potentially diff types under under the same name
    2. classes and structures make program modular, easier to modify make things compact
    3. useful when a lot of data needs to be grouped together
    4. struct Tag {…}struct example {Int x;}example ex; ex.x = 33; //accessing variable of structure
    5. members of a struct in C are by default public, in C++ private
    6. unions like structs except they share memory – allocates largest data type in memory - like a giant storage: store one small OR one large but never both @ the same time
    7. pointers can point to struct:
    8. C++ can use class instead of struct (almost the same thing) - difference: C++ classes can include functions as members
    9. members can be declared as: private: members of a class are accessible only from other members of their same class; protected: members are accessible from members of their same class and friend classes and also members of their derived classes; public: members are accessible from anywhere the class is visible
    10. structs usually used for data only structures, classes for classes that have procedures and member functions
    11. use private because in large projects important that values not be modified in an unexpected way from the POV of the object
    12. advantage of class declare several diff objects from it, each object of Rect has its own variable x, y AND its own functions
    13. concept of OO programming: data and functions are properties of the object instead of the usual view of objects as function parameters in structured programming
  4. Difference between assignment operator and copy constructor - -assignment operator = assigning a variable to a value - copy constructor
    1. constructor with only one parameter of its same type that assigns to every nonstatic class member variable of the object a copy of the passed object
    2. copy assignment operator must correctly deal with a well constructed object - but copy constructor initializes uninitialized memory
    3. copy constructor takes care of initialization by an object of the same type x
    4. for a class for which the copy assignment and copy constructor not explicitly declared missing operation will be generated by the compiler. Copy operations are not inherited - copy of a class object is a copy of each member
    5. memberwise assignment: each member of the right hand object is assigned to the corresponding member of the left hand object
    6. if a class needs a copy constructor it will also need an assignment operator
    7. copy constructor creates a new object, assignment operator has to deal w/ existing data in the object
    8. assignment is like deconstruction followed by construction
    9. assignment operator assigns a value to a already existing object
    10. copy constructor creates a new object by copying an existing one
    11. copy constructor initializes a freshly created object using data from an existing one. It must allocate memory if necessary then copy the data
    12. the assignment operator makes an already existing object into a copy of an existing one.
    13. copy constructor always creates a new object, assignment never does