• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

CPlusPlus Interview Questions Answers

Question - 1 : - How many ways are there to initialize an int with a constant?

Answer - 1 : - Two. There are two formats for initializers in C++ as shown in the example that follows. The first format uses the traditional C notation. The second format uses constructor notation. int foo = 123; int bar (123);

Question - 2 : - What is an incomplete type?

Answer - 2 : - Incomplete types refers to pointers in which there is non availability of the implementation of the referenced location or it points to some location whose value is not available for modification. int *i=0x400 // i points to address 400 *i=0; //set the value of memory location pointed by i. Incomplete types are otherwise called uninitialized pointers.

Question - 3 : - When is a template a better solution than a base class?

Answer - 3 : - When you are designing a generic class to contain or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or management, and particularly when those other types are unknown (thus, the generosity) to the designer of the container or manager class.

Question - 4 : - What is the difference between class and structure?

Answer - 4 : - Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public. Class: Class is a successor of Structure. By default all the members inside the class are private.

Question - 5 : - What is a template?

Answer - 5 : - Templates allow to create generic functions that admit any data type as parameters and return value without having to overload the function with all the possible data types. Until certain point they fulfill the functionality of a macro. Its prototype is any of the two following ones: template <class indetifier> function_declaration; template <typename indetifier> function_declaration; The only difference between both prototypes is the use of keyword class or typename, its use is indistinct since both expressions have exactly the same meaning and behave exactly the same way.

Question - 6 : - What can I safely assume about the initial values of variables which are not explicitly initialized?

Answer - 6 : - It depends on complier which may assign any garbage value to a variable if it is not initialized.

Question - 7 : - Assignment Operator - What is the diffrence between a "assignment operator" and a "copy constructor"?

Answer - 7 : - 1.   In assignment operator, you are assigning a value to an existing object. But in copy constructor, you are creating a new object and then assigning a value to that object. For example: complex c1,c2; c1=c2; //this is assignment complex c3=c2; //copy constructor 2.   A copy constructor is used to initialize a newly declared variable from an existing variable. This makes a deep copy like assignment, but it is somewhat simpler: There is no need to test to see if it is being initialized from itself. There is no need to clean up (eg, delete) an existing value (there is none). A reference to itself is not returned.

Question - 8 : - What is virtual class and friend class?

Answer - 8 : - Friend classes are used when two or more classes are designed to work together and need access to each other's implementation in ways that the rest of the world shouldn't be allowed to have. In other words, they help keep private things private. For instance, it may be desirable for class DatabaseCursor to have more privilege to the internals of class Database than main() has.

Question - 9 : - How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

Answer - 9 : - 1. If you want the code to be even slightly readable, you will use typedefs. typedef char* (*functiontype_one)(void); typedef functiontype_one (*functiontype_two)(void); functiontype_two myarray[N]; //assuming N is a const integral 2. char* (* (*a[N])())() Here a is that array. And according to question no function will not take any parameter value.

Question - 10 : - What is a dangling pointer?

Answer - 10 : - A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed. The following code snippet shows this: class Sample { public: int *ptr; Sample(int i) { ptr = new int(i); } ~Sample() { delete ptr; } void PrintVal() { cout << "The value is " << *ptr; } }; void SomeFunc(Sample x) { cout << "Say i am in someFunc " << endl; } int main() { Sample s1 = 10; SomeFunc(s1); s1.PrintVal(); } In the above example when PrintVal() function is called it is called by the pointer that has been freed by the destructor in SomeFunc.

Computer Contributors

krishan
Rajeev Katiyar
Yes Baroda

Share your email for latest updates

Name:
Email:

Our partners