hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknf...

Upload: subend

Post on 14-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbh 1/57

    what is C++?

    Released in 1985, C++ is an object-oriented programming languagecreated by Bjarne Stroustrup. C++maintains almost all aspects of theC language, while simplifying

    memory management and addingseveral features - including a newdatatype known as a class (you willlearn more about these later) - toallow object-oriented programming.

    C++ maintains the features of Cwhich allowed for low-level memoryaccess but also gives theprogrammer new tools to simplifymemory management.

    C++ used for:

    C++ is a powerful general-purpose

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbh 2/57

    programming language. It can beused to create small programs or

    large applications. It can be used tomake CGI scripts or console-onlyDOS programs. C++ allows you tocreate programs to do almostanything you need to do. The

    creator of C++, Bjarne Stroustrup,has put together a partial list ofapplications written in C++.

    How do you find out if a linked-list has an end? (i.e. the list isnot a cycle)

    You can find out by using 2pointers. One of them goes 2 nodeseach time. The second one goes at 1nodes each time. If there is a cycle,the one that goes 2 nodes each timewill eventually meet the one that

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbh 3/57

    goes slower. If that is the case, thenyou will know the linked-list is a

    cycle.What is the difference betweenrealloc() and free()?

    The free subroutine frees a block ofmemory previously allocated by themalloc subroutine. Undefinedresults occur if the Pointerparameter is not a valid pointer. Ifthe Pointer parameter is a nullvalue, no action will occur. Therealloc subroutine changes the sizeof the block of memory pointed to bythe Pointer parameter to thenumber of bytes specified by theSize parameter and returns a newpointer to the block. The pointerspecified by the Pointer parameter

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbh 4/57

    must have been created with themalloc, calloc, or realloc subroutines

    and not been deallocated with thefree or realloc subroutines.Undefined results occur if thePointer parameter is not a validpointer.

    What is function overloadingand operator overloading?

    Function overloading: C++ enablesseveral functions of the same name

    to be defined, as long as thesefunctions have different sets ofparameters (at least as far as theirtypes are concerned). This capabilityis called function

    overloading33333333333333. Whenan overloaded function is called, theC++ compiler selects the properfunction by examining the number,

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbh 5/57

    types and order of the arguments inthe call. Function overloading is

    commonly used to create severalfunctions of the same name thatperform similar tasks but ondifferent data types.Operator overloading allows existing

    C++ operators to be redefined sothat they work on objects of user-defined classes. Overloadedoperators are syntactic sugar forequivalent function calls. They form

    a pleasant facade that doesn't addanything fundamental to thelanguage (but they can improveunderstandability and reducemaintenance costs).

    What is the difference betweendeclaration and definition?

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbh 6/57

    The declaration tells the compilerthat at some later point we plan to

    present the definition of thisdeclaration.E.g.: void stars () //functiondeclaration

    The definition contains the actual

    implementation.E.g.: void stars () // declarator{for(int j=10; j > =0; j--) //functionbody

    cout

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbh 7/57

    reuse of proven and debugged high-quality software, thus reducing

    problem after a system becomesfunctional.

    How do you write a functionthat can reverse a linked-list?

    void reverselist(void){if(head==0)return;if(head->next==0)return;if(head->next==tail){head->next = 0;tail->next = head;}else{

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbh 8/57

    node* pre = head;node* cur = head->next;

    node* curnext = cur->next;head->next = 0;cur-> next = head;

    for(; curnext!=0; )

    {cur->next = pre;pre = cur;cur = curnext;curnext = curnext->next;

    }

    curnext->next = cur;}}

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbh 9/57

    1. What is a class?Ans: The objects with the same data structure (attributes) and behavior (operations) are called class.

    2. What is an object?Ans: It is an entity which may correspond to real-world entities such as students, employees, bankaccount. It may be concrete such as file system or conceptual such as scheduling policies inmultiprocessor operating system.Every object will have data structures called attributes and behavior called operations.

    3. What is the difference between an object and a class? Ans: All objects possessing similar properties are grouped into class.Example :person is a class, ram, hari are objects of person class. All have similar attributes like name,age, sex and similar operations like speak, walk.

    Class person{

    private:char name[20];int age;char sex;public: speak();walk();};

    4. What is the difference between class and structure?Ans: In class the data members by default are private but in structure they are by default public

    5. Define object based programming language?Ans: Object based programming language support encapsulation and object identity without supporting

    some important features of OOPs language.Object based language=Encapsulation + object Identity

    6. Define object oriented language?Ans: Object-oriented language incorporates all the features of object based programming languagesalong with inheritance and polymorphism.Example: c++, java.

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 10/57

    7. Define OOPs?Ans: OOP is a method of implementation in which programs are organized as co-operative collection ofobjects, each of which represents an instance of some class and whose classes are all member of ahierarchy of classes united through the property of inheritance.

    8. What is public, protected, and private?

    Ans: These are access specifier or a visibility lebels .The class member that has been declared asprivate can be accessed only from within the class. Public members can be accessed from outside theclass also. Within the class or from the object of a class protected access limit is same as that of privatebut it plays a prominent role in case of inheritance

    9. What is a scope resolution operator? Ans: The scope resolution operator permits a program to reference an identifier in the global scope thathas been hidden by another identifier with the same name in the local scope.

    10. What do you mean by inheritance? Ans: The mechanism of deriving a new class (derived) from an old class (base class) is calledinheritance. It allows the extension and reuse of existing code without having to rewrite the code fromscratch.

    Read more:c++ interview questions and answers for freshers |

    FreeJobAlert.comhttp://www.freejobalert.com/c-interview-questions-and-answers-

    part1/6727/#ixzz2cglQiDmv

    11. What is abstraction?Ans: The technique of creating user-defined data types, having the properties of built-in data types and aset of permitted operators that are well suited to the application to be programmed is known as dataabstraction. Class is a construct for abstract data types (ADT).

    12. What is encapsulation?Ans: It is the mechanism that wraps the data and function it manipulates into single unit and keeps it safefrom external interference.

    13. How variable declaration in c++ differs that in c?Ans: C requires all the variables to be declared at the beginning of a scope but in c++ we can declarevariables anywhere in the scope. This makes the programmer easier to understand because the variablesare declared in the context of their use.

    14. What are the c++ tokens?Ans: c++ has the following tokensI. keywords

    http://www.freejobalert.com/c-interview-questions-and-answers-part1/6727/#ixzz2cglQiDmvhttp://www.freejobalert.com/c-interview-questions-and-answers-part1/6727/#ixzz2cglQiDmvhttp://www.freejobalert.com/c-interview-questions-and-answers-part1/6727/#ixzz2cglQiDmvhttp://www.freejobalert.com/c-interview-questions-and-answers-part1/6727/#ixzz2cglQiDmvhttp://www.freejobalert.com/c-interview-questions-and-answers-part1/6727/#ixzz2cglQiDmvhttp://www.freejobalert.com/c-interview-questions-and-answers-part1/6727/#ixzz2cglQiDmvhttp://www.freejobalert.com/c-interview-questions-and-answers-part1/6727/#ixzz2cglQiDmvhttp://www.freejobalert.com/c-interview-questions-and-answers-part1/6727/#ixzz2cglQiDmvhttp://www.freejobalert.com/c-interview-questions-and-answers-part1/6727/#ixzz2cglQiDmvhttp://www.freejobalert.com/c-interview-questions-and-answers-part1/6727/#ixzz2cglQiDmvhttp://www.freejobalert.com/c-interview-questions-and-answers-part1/6727/#ixzz2cglQiDmv
  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 11/57

    II. IdentifiersIII. ConstantsIV. StringsV. operators

    15. What do you mean by reference variable in c++?

    Ans: A reference variable provides an alias to a previously defined variable.Data type & reference-name = variable name

    16. What do you mean by implicit conversion?Ans: Whenever data types are mixed in an expression then c++ performs the conversion automatically.Here smaller type is converted to wider type.Example- in case of integer and float integer is converted into float type.

    17. What is the difference between method overloading and method overriding?Ans: Overloading a method (or function) in C++ is the ability for functions of the same name to be definedas long as these methods have different signatures (different set of parameters). Method overriding is theability of the inherited class rewriting the virtual method of the base class.

    18. What are the defining traits of an object-oriented language?The defining traits of an object-oriented language are:encapsulationinheritancepolymorphismAns:Polymorphism: is a feature of OOPL that at run time depending upon the type of object the appropriatemethod is called.

    Inheritance:is a feature of OOPL that represents the is a relationship between different objects(classes). Say in real life a manager is a employee. So in OOPL manger class is inherited from theemployee class.

    Encapsulation: is a feature of OOPL that is used to hide the information.

    19. What is polymorphism?Ans: Polymorphism is the idea that a base class can be inherited by several classes. A base classpointer can point to its child class and a base class array can store different child class objects.

    20. What do you mean by inline function?Ans: An inline function is a function that is expanded inline when invoked.ie. the compiler replaces thefunction call with the corresponding function code. An inline function is a function that is expanded in linewhen it is invoked. That is the compiler replaces the function call with the corresponding function code(similar to macro).

    Read more:c++ interview questions and answers for freshers |

    FreeJobAlert.comhttp://www.freejobalert.com/c-interview-questions-and-answers-

    part2/6731/#ixzz2cglmutwo

    http://www.freejobalert.com/c-interview-questions-and-answers-part2/6731/#ixzz2cglmutwohttp://www.freejobalert.com/c-interview-questions-and-answers-part2/6731/#ixzz2cglmutwohttp://www.freejobalert.com/c-interview-questions-and-answers-part2/6731/#ixzz2cglmutwohttp://www.freejobalert.com/c-interview-questions-and-answers-part2/6731/#ixzz2cglmutwohttp://www.freejobalert.com/c-interview-questions-and-answers-part2/6731/#ixzz2cglmutwohttp://www.freejobalert.com/c-interview-questions-and-answers-part2/6731/#ixzz2cglmutwohttp://www.freejobalert.com/c-interview-questions-and-answers-part2/6731/#ixzz2cglmutwohttp://www.freejobalert.com/c-interview-questions-and-answers-part2/6731/#ixzz2cglmutwohttp://www.freejobalert.com/c-interview-questions-and-answers-part2/6731/#ixzz2cglmutwohttp://www.freejobalert.com/c-interview-questions-and-answers-part2/6731/#ixzz2cglmutwohttp://www.freejobalert.com/c-interview-questions-and-answers-part2/6731/#ixzz2cglmutwo
  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 12/57

    21 What is the difference between a NULL pointer and a void pointer? Ans: A NULL pointer is a pointer of any type whose value is zero. A void pointer is a pointer to an objectof an unknown type, and is guaranteed to have enough bits to hold a pointer to any object. A void pointeris not guaranteed to have enough bits to point to a function (though in general practice it does).

    22. What is difference between C++ and Java?

    Ans: C++ has pointers Java does not.Java is platform independent C++ is not.Java has garbage collection C++ does not.

    23. What do you mean by multiple inheritance in C++ ? Ans: Multiple inheritance is a feature in C++ by which one class can be of different types. Say classteaching Assistant is inherited from two classes say teacher and Student.

    24. What do you mean by virtual methods? Ans: virtual methods are used to use the polymorphism feature in C++. Say class A is inherited fromclass B. If we declare say function f() as virtual in class B and override the same function in class A thenat runtime appropriate method of the class will be called depending upon the type of the object.

    25. What do you mean by static methods? Ans: By using the static method there is no need creating an object of that class to use that method. Wecan directly call that method on that class. For example, say class A has static function f(), then we cancall f() function as A.f(). There is no need of creating an object of class A.

    26. How many ways are there to initialize an int with a constant?Ans: Two.There are two formats for initializers in C++ as shown in the example that follows. The first format usesthe traditional C notation. The second format uses constructor notation.int foo = 123;int bar (123);

    27. What is a constructor?

    Ans: Constructor is a special member function of a class, which is invoked automatically whenever aninstance of the class is created. It has the same name as its class.

    28. What is destructor?Ans: Destructor is a special member function of a class, which is invoked automatically whenever anobject goes out of the scope. It has the same name as its class with a tilde character prefixed.

    29. What is an explicit constructor?Ans: A conversion constructor declared with the explicit keyword. The compiler does not use an explicitconstructor to implement an implied conversion of types. Its purpose is reserved explicitly forconstruction.

    30 What is the Standard Template Library?Ans: A library of container templates approved by the ANSI committee for inclusion in the standard C++specification. A programmer who then launches into a discussion of the generic programming model,iterators, allocators, algorithms, and such, has a higher than average understanding of the newtechnology that STL brings to C++ programming.

    Read more:c++ interview questions and answers for freshers |

    http://www.freejobalert.com/c-interview-questions-and-answers-part3/6734/#ixzz2cgm7y8Bvhttp://www.freejobalert.com/c-interview-questions-and-answers-part3/6734/#ixzz2cgm7y8Bvhttp://www.freejobalert.com/c-interview-questions-and-answers-part3/6734/#ixzz2cgm7y8Bv
  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 13/57

    FreeJobAlert.comhttp://www.freejobalert.com/c-interview-questions-and-answers-

    part3/6734/#ixzz2cgm7y8Bv

    31. What problem does the namespace feature solve?

    Ans: Multiple providers of libraries might use common global identifiers causing a name collision when anapplication tries to link with two or more such libraries. The namespace feature surrounds a librarysexternal declarations with a unique namespace that eliminates the potential for those collisions. Thissolution assumes that two library vendors dont use the same namespace identifier, of course.

    32. What is the use of using declaration?Ans: A using declaration makes it possible to use a name from a namespace

    33. What is a template?Ans: Templates allow us to create generic functions that admit any data type as parameters and return avalue without having to overload the function with all the possible data types. Until certain point they fulfillthe functionality of a macro. Its prototype is any of the two following ones:template function_declaration;

    template function_declaration;

    34. Differentiate between a template class and class template? Ans:Template class:A generic definition or a parameterized class not instantiated until the client provides the neededinformation. Its jargon for plain templates.Class template:A class template specifies how individual classes can be constructed much like the way a class specifieshow individual objects can be constructed. Its jargon for plain classes.

    35. What is the difference between a copy constructor and an overloaded assignment operator? Ans: A copy constructor constructs a new object by using the content of the argument object. An

    overloaded assignment operator assigns the contents of an existing object to another existing object ofthe same class.

    36. What is a virtual destructor?Ans: The simple answer is that a virtual destructor is one that is declared with the virtual attribute.

    37. What is an incomplete type?Ans: Incomplete type refers to pointers in which there is non availability of the implementation of thereferenced location or it points to some location whose value is not available for modification.Example:int *i=0400 // i points to address 400*i=0; //set the value of memory location pointed by i.Incomplete types are otherwise called uninitialized pointers.

    38. What do you mean by Stack unwinding?Ans: It is a process during exception handling when the destructor is called for all local objects betweenthe place where the exception was thrown and where it is caught.

    39. What is a container class? What are the types of container classes? Ans: A container class is a class that is used to hold objects in memory or external storage. A containerclass acts as a generic holder. A container class has a predefined behavior and a well-known interface. Acontainer class is a supporting class whose purpose is to hide the topology used for maintaining the list of

    http://www.freejobalert.com/c-interview-questions-and-answers-part3/6734/#ixzz2cgm7y8Bvhttp://www.freejobalert.com/c-interview-questions-and-answers-part3/6734/#ixzz2cgm7y8Bvhttp://www.freejobalert.com/c-interview-questions-and-answers-part3/6734/#ixzz2cgm7y8Bvhttp://www.freejobalert.com/c-interview-questions-and-answers-part3/6734/#ixzz2cgm7y8Bvhttp://www.freejobalert.com/c-interview-questions-and-answers-part3/6734/#ixzz2cgm7y8Bvhttp://www.freejobalert.com/c-interview-questions-and-answers-part3/6734/#ixzz2cgm7y8Bvhttp://www.freejobalert.com/c-interview-questions-and-answers-part3/6734/#ixzz2cgm7y8Bvhttp://www.freejobalert.com/c-interview-questions-and-answers-part3/6734/#ixzz2cgm7y8Bv
  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 14/57

    objects in memory. When a container class contains a group of mixed objects, the container is called aheterogeneous container; when the container is holding a group of objects that are all the same, thecontainer is called a homogeneous container

    40. Name some pure object oriented languages? Ans: Smalltalk, Java, Eiffel, Sather.

    Read more:cpp interview questions and answers for freshers |

    FreeJobAlert.comhttp://www.freejobalert.com/c-interview-questions-and-answers-

    part4/6780/#ixzz2cgmKBUkK

    41. Name the operators that cannot be overloaded?

    Ans: sizeof, ., .*, .->, ::, ?:

    42. What is an adaptor class or Wrapper class? Ans: A class that has no functionality of its own. Its member functions hide the use of a third partysoftware component or an object with the non-compatible interface or a non-object-orientedimplementation.

    43. What is a Null object?Ans: It is an object of some class whose purpose is to indicate that a real object of that class does notexist. One common use for a null object is a return value from a member function that is supposed toreturn an object with some specified properties but cannot find such an object.

    44. What is class invariant?

    Ans: A class invariant is a condition that defines all valid states for an object. It is a logical condition toensure the correct working of a class. Class invariants must hold when an object is created, and theymust be preserved under all operations of the class. In particular all class invariants are bothpreconditions and post-conditions for all operations or member functions of the class.

    45. What is a dangling pointer?Ans: A dangling pointer arises when you use the address of an object after its lifetime is over. This mayoccur in situations like returning addresses of the automatic variables from a function or using theaddress of the memory block after it is freed. Example: The following code snippet shows this:class Sample{public:int *ptr;

    Sample(int i){ptr = new int(i);}~Sample(){delete ptr;}void PrintVal(){

    http://www.freejobalert.com/c-interview-questions-and-answers-part4/6780/#ixzz2cgmKBUkKhttp://www.freejobalert.com/c-interview-questions-and-answers-part4/6780/#ixzz2cgmKBUkKhttp://www.freejobalert.com/c-interview-questions-and-answers-part4/6780/#ixzz2cgmKBUkKhttp://www.freejobalert.com/c-interview-questions-and-answers-part4/6780/#ixzz2cgmKBUkKhttp://www.freejobalert.com/c-interview-questions-and-answers-part4/6780/#ixzz2cgmKBUkKhttp://www.freejobalert.com/c-interview-questions-and-answers-part4/6780/#ixzz2cgmKBUkKhttp://www.freejobalert.com/c-interview-questions-and-answers-part4/6780/#ixzz2cgmKBUkKhttp://www.freejobalert.com/c-interview-questions-and-answers-part4/6780/#ixzz2cgmKBUkKhttp://www.freejobalert.com/c-interview-questions-and-answers-part4/6780/#ixzz2cgmKBUkKhttp://www.freejobalert.com/c-interview-questions-and-answers-part4/6780/#ixzz2cgmKBUkKhttp://www.freejobalert.com/c-interview-questions-and-answers-part4/6780/#ixzz2cgmKBUkK
  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 15/57

    cout

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 16/57

    FreeJobAlert.comhttp://www.freejobalert.com/c-interview-questions-and-answers-

    part5/6784/#ixzz2cgmSBVtP

    1. What is C++

    C++ is created by Bjarne Stroustrup of AT&T Bell Labs as an extension of C, C++ is

    an object-oriented computer language used in the development of enterprise and

    commercial applications. Microsofts Visual C++ became the premier language of

    choice among developers and programmers.

    2. What are the basic concepts of object oriented programming?

    It is necessary to understand some of the concepts used extensively in object

    oriented programming.These include

    Objects

    Classes

    Data abstraction and encapsulation

    Inheritance

    Polymorphism

    Dynamic Binding

    Message passing

    3. Define inheritance?

    The mechanism of deriving a new class (derived) from an old class (base class) is

    called inheritance. It allows the extension and reuse of existing code without having

    to rewrite the code from scratch. Inheritance is the process by which objects of one

    class acquire properties of objects of another class.

    4. Define polymorphism?

    Polymorphism means one name, multiple forms. It allows us to have more than one

    function with the same name in a program.It allows us to have overloading of

    operators so that an operation can exhibit different behaviours in different instances.

    5. What is encapsulation?

    The wrapping up of data and functions into a single unit (called class) is known as

    http://www.freejobalert.com/c-interview-questions-and-answers-part5/6784/#ixzz2cgmSBVtPhttp://www.freejobalert.com/c-interview-questions-and-answers-part5/6784/#ixzz2cgmSBVtPhttp://www.freejobalert.com/c-interview-questions-and-answers-part5/6784/#ixzz2cgmSBVtPhttp://www.freejobalert.com/c-interview-questions-and-answers-part5/6784/#ixzz2cgmSBVtPhttp://www.freejobalert.com/c-interview-questions-and-answers-part5/6784/#ixzz2cgmSBVtPhttp://www.freejobalert.com/c-interview-questions-and-answers-part5/6784/#ixzz2cgmSBVtPhttp://www.freejobalert.com/c-interview-questions-and-answers-part5/6784/#ixzz2cgmSBVtPhttp://www.freejobalert.com/c-interview-questions-and-answers-part5/6784/#ixzz2cgmSBVtP
  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 17/57

    encapsulation. Encapsulation containing and hiding information about an object,

    such as internal data structures and code.

    What is

    messag

    e

    passing

    ?

    An object oriented program consists of a set of objects that communicate with

    each other. Message passing involves specifying the name of the object, the

    name of the function and the information to be sent.

    7. What are tokens in C++?

    The smallest individual units of a program is known as tokens. c++ has the following

    tokens :

    Keywords

    Identifiers

    Constants

    Strings

    Operators

    8. What is the use of enumerated data type?

    An enumerated data type is another user defined type which provides a way for

    attaching names to numbers thereby increasing comprehensibility of the code. The

    enum keyword automatically enumerates a list of words by assigning them values0,1,2, and so on.

    9. What is the use of default constructor?

    A constructors that accepts no parameters is called the default constructor.If no

    user-defined constructor exists for a class A and one is needed, the compiler

    implicitly declares a default parameterless constructor A::A(). This constructor is an

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 18/57

    inline public member of its class. The compiler will implicitly define A::A() when the

    compiler uses this constructor to create an object of type A. The constructor will

    have no constructor initializer and a null body.

    10. Define Constructors?

    A constructor is a member function with the same name as its class. The constructor

    is invoked whenever an object of its associated class is created.It is called

    constructor because it constructs the values of data members of the class.

    11. How variable declaration in c++ differs that in c?

    C requires all the variables to be declared at the beginning of a scope but in c++ we

    can declare variables anywhere in the scope. This makes the programmer easier to

    understand because the variables are declared in the context of their use.

    12. Define destuctors?

    A destructor is called for a class object when that object passes out of scope or is

    explicitly deleted.A destructors as the name implies is used to destroy the objects

    that have been created by a constructors.Like a constructor , the destructor is a

    member function whose name is the same as the class name but is precided by a

    tilde.

    13. What is a class?

    A class is a collection of objects.

    14. what is the difference between c &c++?

    c++ ia an object oriented programing but c is a procedure oriented programing.c is

    super set of c++. c can't suport inheritance,function overloading, method

    overloading etc. but c++ can do this.In c-programe the main function could not

    return a value but in the c++ the main function shuld return a value.

    15. What is copy constructor?

    Copy constructor is a constructor function with the same name as the class and used

    to make deep copy of objects.

    16. What is default constructor?

    A default constructor is a constructor that either has no parameters, or if it hasparameters, all the parameters have default values.

    17. What is a scope resolution operator?

    The scope resolution operator permits a program to reference an identifier in the

    global scope that has been hidden by another identifier with the same name in the

    local scope.

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 19/57

    18. What is the difference between Object and Instance?

    An instance of a user-defined type is called an object. We can instantiate many

    objects from one class.

    An object is an instance of a class.

    19. What is the difference between macro and iniine?

    Inline follows strict parameter type checking, macros do not.

    Macros are always expanded by preprocessor, whereas compiler may or may not

    replace the inline definitions.

    20. How variable declaration in c++ differs that in c?

    C requires all the variables to be declared at the beginning of a scope but in c++ we

    can declare variables anywhere in the scope. This makes the programmer easier to

    understand because the variables are declared in the context of their use.

    21. What is multiple inheritance?

    A class can inherit properties from more than one class which is known as multiple

    inheritance.

    22. what is the use of virtual destructor in c++?

    A destructor is automatically called when the object is destroyed. A virtual

    destructor in C++ is used primarily to prevent resource leaks by performing a clean-

    up of the object.

    23. What do you mean by reference variable in c++?

    A reference variable provides an alias to a previously defined variable.

    Data -type & reference-name = variable name

    24. What do you mean by implicit conversion?

    Whenever data types are mixed in an expression then c++ performs the conversion

    automatically.

    Here smaller type is converted to wider type.

    Example : in case of integer and float integer is converted into float type.

    25. What are virtual functions?

    The virtual fuctions must be members of some class.

    They cannot be static members.

    They are accessed by using object pointers.

    A virtual function can be a friend of another class.

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 20/57

    C++ Interview Question and Answers

    26. What is the difference between class and structure?

    By default, the members ot structures are public while that tor class is private.

    structures doesnt provide something like data hiding which is provided by the

    classes.

    structures contains only data while class bind both data and member functions.

    27. What are storage qualifiers in C++ ?

    ConstKeyword indicates that memory once initialized, should not be altered by a

    program.

    Volatile keyword indicates that the value in the memory location can be altered even

    though nothing in the program.

    Mutable keyword indicates that particular member of a structure or class can be

    altered even if a particular structure variable, class, or class member function is

    constant.

    28. What is virtual class and friend class?

    Friend classes are used when two or more classes and virtual base class aids in

    multiple inheritance.

    Virtual class is used for run time polymorphism when object is linked to procedure

    call at run time.

    29. what is an abstract base class?

    An abstract class is a class that is designed to be specifically used as a base class.

    An abstract class contains at least one pure virtual function.

    30. What is dynamic binding?

    Dynamic binding (also known as late binding) means that the code associated with a

    given procedure call is not known until the time of the call at run time.It is

    associated with polymorphism and inheritance.

    31. what is difference between function overloading and operator overloading?

    A function is overloaded when same name is given to different function.

    While overloading a function, the return type of the functions need to be the same.

    32. What are the advantages of inheritance?

    Code reusability

    Saves time in program development.

    33. What is a dynamic constructor?

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 21/57

    The constructor can also be used to allocate memory while creating objects.

    Allocation of memory to objects at the time of their construction is known as

    dynamic construction of objects.The memory is allocated with the help of the new

    operator.

    34. What is the difference between an Array and a List?

    The main difference between an array and a list is how they internally store the

    data. whereas Array is collection of homogeneous elements. List is collection of

    heterogeneous elements.

    35. What is the use of using declaration?

    A using declaration makes it possible to use a name from a namespace.

    36. What is the difference between a template class and class template?

    Template classA generic definition or a parameterized class not instantiated until

    the client provides the needed information. Its jargon for plain templates.

    Class templateA class template specifies how individual classes can be constructed

    much like the way a class specifies how individual objects can be constructed. Its

    argon for plain classes.

    37. What is friend function?

    The function declaration should be preceded by the keyword friend.The function

    definitions does not use either the keyword or the scope operator ::. The functions

    that are declared with the keyword friend as friend function.Thus, a friend function is

    an ordinary function or a member of another class.

    38. What is a scope resolution operator?

    A scope resolution operator (::), can be used to define the member functions of a

    class outside the class.

    39. What do you mean by pure virtual functions?

    A pure virtual member function is a member function that the base class forces

    derived classes to provide. Any class containing any pure virtual function cannot be

    used to create object of its own type.

    40. What is a conversion constructor?

    A converting constructor is a single-parameter constructor that is declared without

    the function specifier explicit. The compiler uses converting constructors to convert

    objects from the type of the first parameter to the type of the converting

    constructors class.

    41. What is a container class? What are the types of container classes?

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 22/57

    A container class is a class that is used to hold objects in memory or external

    storage. A container class acts as a generic holder. A container class has a

    predefined behavior and a wellknown interface. A container class is a supporting

    class whose purpose is to hide the topology used for maintaining the list of objects

    in memory. When a container class contains a group of mixed objects, the containeris called a heterogeneous container; when the container is holding a group of

    objects that are all the same, the container is called a homogeneous container.

    42. What is Associative container?

    Associative containers are designed to support direct access to elements using keys.

    They are not sequential. There are four types of associatives containers :

    Set

    Multiset

    Map

    Multimap

    43. What is an iterator?

    Iterators are like pointers. They are used to access the elements of containers thus

    providing a link between algorithms and containers. Iterators are defined for specific

    containers and used as arguments to algorithms.

    44. What are the defining traits of an object-oriented language?

    The defining traits of an object-oriented langauge are :

    Encapsulation

    Inheritance

    Polymorphism

    45. Name some pure object oriented languages?

    Smalltalk

    Java

    Eiffel

    Sather

    46. What is this pointer?

    It is a pointer that points to the current object. This can be used to access the

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 23/57

    members of the current object with the help of the arrow operator.

    47. What is encapsulation?

    Encapsulation (or information hiding) is the process of combining data and functions

    into a single unit called class.

    48. What is problem with Runtime type identification?

    The run time type identification comes at a cost of performance penalty. Compiler

    maintains the class.

    49. What are the differences between new and malloc?

    New initializes the allocated memory by calling the constructor. Memory allocated

    with new should be released with delete.

    Malloc allocates uninitialized memory.

    The allocated memory has to be released with free.new automatically calls the

    constructor while malloc(dosent)

    50. What is conversion operator?

    You can define a member function of a class, called a conversion function, that

    converts from the type of its class to another specified type.

    51. What is difference between template and macro?

    A template can be used to create a family of classes or function.A template

    describes a set of related classes or set of related functions in which a list of

    parameters in the declaration describe how the members of the set vary.

    Identifiers that represent statements or expressions are called macros.

    52. What is reference?

    Reference is a name that acts as an alias, or alternative name, for a previously

    defined variable or an object.

    53. What are the access specifier in c++?

    There are three types of access specifier in c++ . They are

    Public

    protected

    private

    54. What is difference between C++ and Java?

    C++ has pointers Java does not.

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 24/57

    Java is the platform independent as it works on any type of operating systems.

    ava has no pointers where c ++ has pointers.

    Java has garbage collection C++ does not.

    55. What is namespace?

    The C++ language provides a single global namespace.Namespaces allow to group

    entities like classes, objects and functions under a name.

    56. What is an explicit constructor?

    A conversion constructor declared with the explicit keyword.The compiler does not use an explicit constructor to implement

    an implied conversion of types. Its purpose is reserved

    explicitly for construction.Explicit constructors are simply

    constructors that cannot take part in an implicit conversion.57. What is the use of storage class specifiers?

    A storage class specifier is used to refine the declaration of avariable, a function, and parameters. The following are storage

    class specifiers :

    auto

    register

    static

    extern

    58. what is assignment operator in c++?

    Default assignment operator handles assigning one object toanother of the same class. Member to member copy (shallow

    copy).

    59. Can destructor be private?

    Yes destructors can be private. But according it is not advisable

    to have destructors to be private.

    60. What is strstream?stringstream provides an interface to manipulate strings as ifthey were input/output streams.

    strstream to define several classes that support iostreamsoperations on sequences stored in an allocated array of char

    object.

    61. What are the types of STL containers?

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 25/57

    deque

    hash map

    hashmultimap

    hash_multiset

    hashset

    list

    map

    multimap

    multiset

    set

    vector

    .

    62. What is the difference between method overloading and method

    overriding?

    Overloading a method (or function) in C++ is the ability for functions of the same

    name to be defined as long as these methods have different signatures (different set

    of parameters).

    Method overriding is the ability of the inherited class rewriting the virtual method

    of the base class.

    63. What do you mean by inline function?

    An inline function is a function that is expanded inline when invoked.ie. the compiler

    replaces the function call with the corresponding function code. An inline function is

    a function that is expanded in line when it is invoked. That is the compiler replaces

    the function call with the corresponding function code (similar to macro).

    64. What is a template?

    A template can be used to create a family of classes or function.A template

    describes a set of related classes or set of related functions in which a list of

    parameters in the declaration describe how the members of the set vary.

    65. What is a copy constructor and when is it called?

    A copy constructor is a method that accepts an object of the same class and copies

    it members to the object on the left part of assignement.

    C++ Interview Question and Answers

    66. What is the difference between a copy constructor and

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 26/57

    an overloaded assignment operator?

    A copy constructor constructs a new object by using the

    content of the argument object. An overloaded assignmentoperator assigns the contents of an existing object to another

    existing object of the same class.

    67. What is a virtual destructor?

    The simple answer is that a virtual destructor is one that is

    declared with the virtual attribute.

    68. What do you mean by Stack unwinding?

    It is a process during exception handling when the destructor is

    called for all local objects between the place where the

    exception was thrown and where it is caught.

    69. What is STL? and what are the components of stl?

    A collection of generic classes and functions is called asStandard Template Library (STL).The stl components are

    containers

    Algorithm

    Iterators

    .

    70. What is a modifier?

    A modifier, also called a modifying function is a memberfunction that changes the value of at least one data member.

    In other words, an operation that modifies the state of anobject. Modifiers are also known as mutators.

    71. What is an adaptor class or Wrapper class?

    A class that has no functionality of its own. Its member

    functions hide the use of a third party software component oran object with the non-compatible interface or a non-

    objectoriented implementation.

    72. What is a Null object?

    It is an object of some class whose purpose is to indicate that a

    real object of that class does not exist. One common use for anull object is a return value from a member function that is

    supposed to return an object with some specified propertiesbut cannot find such an object.

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 27/57

    73. What is class invariant?

    A class invariant is a condition that defines all valid states for

    an object. It is a logical condition to ensure the correct workingof a class. Class invariants must hold when an object is

    created, and they must be preserved under all operations ofthe class. In particular all class invariants are both

    preconditions and post-conditions for all operations or memberfunctions of the class.

    74. What is the difference between the message andmethod?

    Message : Objects communicate by sending messages to eachother.A message is sent to invoke a method.

    Method : Provides response to a message and it is animplementation of an operation.

    75. How can we access protected and private members of a

    class?

    In the case of members protected and private, these could not

    be accessed from outside the same class at which they aredeclared. This rule can be transgressed with the use of the

    friend keyword in a class, so we can allow an external functionto gain access to the protected and private members of a class.

    76. What do you mean by late binding?

    Late binding refers to function calls that are not resolved untilrun time. Virtual functions are used to achieve late binding.When access is via a base pointer or reference, the virtualfunction actually called is determined by the type of object

    pointed to by the pointer.

    77. What is virtual function?

    A virtual function is a member function that is declared within abase class and redefined by a derived class .To create a virtual

    function, the function declaration in the base class is preceded

    by the keyword virtual.

    78. What do you mean by early binding?

    Early binding refers to the events that occur at compile time.Early binding occurs when all information needed to call a

    function is known at compile time. Examples of early bindinginclude normal function calls, overloaded function calls, andoverloaded operators. The advantage of early binding is

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 28/57

    efficiency.

    What do you mean by inline function?The idea behind inline functions is to insert the code of a called function at the pointwhere the function is called. If done carefully, this can improve the application's

    performance in exchange for increased compile time and possibly (but not always) anincrease in the size of the generated binary executables.

    Write a program that ask for user input from 5 to 9 then calculate the average#include "iostream.h"int main() {int MAX = 4;int total = 0;int average;int numb;for (int i=0; i numb;while ( numb9) {cout > numb;

    }total = total + numb;

    }average = total/MAX;cout

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 29/57

    Tell how to check whether a linked list is circular.Create two pointers, each set to the start of the list. Update each as follows:

    while (pointer1) {pointer1 = pointer1->next;pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next;

    if (pointer1 == pointer2) {print (\"circular\n\");

    }

    }

    OK, why does this work?If a list is circular, at some point pointer2 will wrap around and be either at the item justbefore pointer1, or the item before that. Either way, its either 1 or 2 jumps until theymeet.

    What is virtual constructors/destructors?Answer1Virtual destructors:If an object (with a non-virtual destructor) is destroyed explicitly by applying the deleteoperator to a base-class pointer to the object, the base-class destructor function (matchingthe pointer type) is called on the object.There is a simple solution to this problem declare a virtual base-class destructor.This makes all derived-class destructors virtual even though they dont have the samename as the base-class destructor. Now, if the object in the hierarchy is destroyedexplicitly by applying the delete operator to a base-class pointer to a derived-class object,the destructor for the appropriate class is called. Virtual constructor: Constructors cannotbe virtual. Declaring a constructor as a virtual function is a syntax error.

    Answer2

    Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly byapplying the delete operator to a base-class pointer to the object, the base-class destructorfunction (matching the pointer type) is called on the object.There is a simple solution to this problemdeclare a virtual base-class destructor. Thismakes all derived-class destructors virtual even though they dont have the same name asthe base-class destructor. Now, if the object in the hierarchy is destroyed explicitly byapplying the delete operator to a base-class pointer to a derived-class object, thedestructor for the appropriate class is called.

    Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a

    virtual function is a syntax error. Does c++ support multilevel and multipleinheritance?

    Yes.

    What are the advantages of inheritance? It permits code reusability. Reusability saves time in program development. It encourages the reuse of proven and debugged high-quality software, thus reducingproblem after a system becomes functional.

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 30/57

    What is the difference between declaration and definition?The declaration tells the compiler that at some later point we plan to present the definitionof this declaration.E.g.: void stars () //function declarationThe definition contains the actual implementation.E.g.: void stars () // declarator

    {for(int j=10; j>=0; j--) //function bodycout

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 31/57

    template function_declaration; template function_declaration;The only difference between both prototypes is the use of keyword class or typename, itsuse is indistinct since both expressions have exactly the same meaning and behave exactlythe same way.

    Define a constructor - What it is and how it might be called(2 methods).Answer1constructor is a member function of the class, with the name of the function being thesame 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.

    Answer2class 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, thedefault constructor is implicitly called.

    Point2D * pPoint = new Point2D(); // Explicit Constructor call. In order to allocate memory

    on HEAP we call the default constructor.

    You have two pairs: new() and delete() and another pair : alloc() and free().Explain differences between eg. new() and malloc()Answer11.) new and delete are preprocessors while malloc() and free() are functions. [we dontuse brackets will calling new or delete].2.) no need of allocate the memory while using new but in malloc() we have to usesizeof().3.) new will initlize the new memory to 0 but malloc() gives random value in the newalloted memory location [better to use calloc()]

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

    What is the difference between class and structure?Structure: Initially (in C) a structure was used to bundle different type of data typestogether to perform a particular functionality. But C++ extended the structure to containfunctions also. The major difference is that all declarations inside a structure are bydefault public.

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 32/57

    Class: Class is a successor of Structure. By default all the members inside the class areprivate.

    What is RTTI?Runtime type identification (RTTI) lets you find the dynamic type of an object when youhave only a pointer or a reference to the base type. RTTI is the official way in standard C++

    to discover the type of an object and to convert the type of a pointer or reference (that is,dynamic typing). The need came from practical experience with C++. RTTI replaces manyInterview Questions - Homegrown versions with a solid, consistent approach.

    What is encapsulation?Packaging an objects variables within its methods is called encapsulation.

    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

    Answer1

    POLYMORPHISM : A phenomenon which enables an object to react differently to the samefunction call.in C++ it is attained by using a keyword virtual

    Examplepublic class SHAPE

    {public virtual void SHAPE::DRAW()=0;

    }Note here the function DRAW() is pure virtual which means the sub classes mustimplement 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 likegloballySHAPE *newShape;

    When user action is to draw

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 33/57

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

    }

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

    }

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

    }

    Answer2class 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 SQUREpublic 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.

    What is an object?Object is a software bundle of variables and related methods. Objects have state andbehavior.

    How can you tell what shell you are running on UNIX system?You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit randomnumbers if you are from the Korn shell. You could also do a ps -l and look for the shellwith the highest PID.

    What do you mean by inheritance?Inheritance is the process of creating new classes, called derived classes, from existingclasses or base classes. The derived class inherits all the capabilities of the base class, butcan add embellishments and refinements of its own.

    Describe PRIVATE, PROTECTED and PUBLIC the differences and give examples.class Point2D{int x; int y;

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 34/57

    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 problemint 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

    What is namespace?Namespaces allow us to group a set of global classes, objects and/or functions under aname. To say it somehow, they serve to split the global scope in sub-scopes known asnamespaces.The form to use namespaces is:namespace identifier { namespace-body }Where identifier is any valid identifier and namespace-body is the set of classes, objects

    and functions that are included within the namespace. For example:namespace general { int a, b; } In this case, a and b are normal variables integrated withinthe general namespace. In order to access to these variables from outside the namespacewe have to use the scope operator ::. For example, to access the previous variables wewould have to put:general::a general::bThe functionality of namespaces is specially useful in case that there is a possibility that aglobal object or function can have the same name than another one, causing a redefinitionerror.

    What is a COPY CONSTRUCTOR and when is it called?A copy constructor is a method that accepts an object of the same class and copies its

    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) constructorpublic Point2D( const Point2D & ) ;

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 35/57

    };

    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

    What is Boyce Codd Normal form?A relation schema R is in BCNF with respect to a set F of functional dependencies if for allfunctional dependencies in F+ of the form a-> , where a and b is a subset of R, at least one

    of the following holds:* a- > b is a trivial functional dependency (b is a subset of a)* a is a superkey for schema R

    What is virtual class and friend class?Friend classes are used when two or more classes are designed to work together and needaccess to each other's implementation in ways that the rest of the world shouldn't beallowed to have. In other words, they help keep private things private. For instance, it maybe desirable for class DatabaseCursor to have more privilege to the internals of classDatabase than main() has.

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

    function to be a polimorphic function?virtual

    What do you mean by binding of data and functions?Encapsulation.

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

    What is the difference between an object and a class?Classes and objects are separate but related concepts. Every object belongs to a class andevery class contains one or more related objects.- A Class is static. All of the attributes of a class are fixed before, during, and after theexecution of a program. The attributes of a class don't change.- The class to which an object belongs is also (usually) static. If a particular object belongsto a certain class at the time that it is created then it almost certainly will still belong tothat class right up until the time that it is destroyed.- An Object on the other hand has a limited lifespan. Objects are created and eventually

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 36/57

    destroyed. Also during that lifetime, the attributes of the object may undergo significantchange.

    Suppose that data is an array of 1000 integers. Write a single function call thatwill sort the 100 elements data [222] through data [321].quicksort ((data + 222), 100);

    What is a class?Class is a user-defined data type in C++. It can be created to solve a particular kind ofproblem. After creation the user need not know the specifics of the working of a class.

    What is friend function?As the name suggests, the function acts as a friend to a class. As a friend of a class, it canaccess its private and protected members. A friend function is not a member of the class.But it must be listed in the class definition.

    Which recursive sorting technique always makes recursive calls to sort subarrays

    that are about half size of the original array?

    Mergesort always makes recursive calls to sort subarrays that are about half size of theoriginal array, resulting in O(n log n) time.

    What is abstraction?Abstraction is of the process of hiding unwanted details from the user.

    What are virtual functions?A virtual function allows derived classes to replace the implementation provided by thebase class. The compiler makes sure the replacement is always called whenever the objectin question is actually of the derived class, even if the object is accessed by a base pointerrather than a derived pointer. This allows algorithms in the base class to be replaced inthe derived class, even if users don't know about the derived class.

    What is the difference between an external iterator and an internal iterator?

    Describe an advantage of an external iterator.An internal iterator is implemented with member functions of the class that has items tostep through. .An external iterator is implemented as a separate class that can be "attach"to the object that has items to step through. .An external iterator has the advantage that

    many difference iterators can be active simultaneously on the same object.

    What is a scope resolution operator?A scope resolution operator (::), can be used to define the member functions of a classoutside the class.

    What do you mean by pure virtual functions?A pure virtual member function is a member function that the base class forces derivedclasses to provide. Normally these member functions have no implementation. Pure virtualfunctions are equated to zero.class Shape { public: virtual void draw() = 0; };

    What is polymorphism? Explain with an example?"Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object(or reference) to assume (be replaced by) or become many different forms of object.

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 37/57

    Example: function overloading, function overriding, virtual functions. Another example canbe a plus + sign, used for adding two integers or for using it to concatenate two strings.

    Whats the output of the following program? Why?#include main()

    {typedef union

    {int a;char b[10];float c;

    }Union;

    Union x,y = {100};x.a = 50;

    strcpy(x.b,\"hello\");x.c = 21.50;

    printf(\"Union x : %d %s %f \n\",x.a,x.b,x.c );printf(\"Union y :%d %s%f \n\",y.a,y.b,y.c);

    }

    Given inputs X, Y, Z and operations | and & (meaning bitwise OR and AND, respectively)What is output equal to inoutput = (X & Y) | (X & Z) | (Y & Z)

    Why are arrays usually processed with for loop?

    The real power of arrays comes from their facility of using an index variable to traverse thearray, accessing each element with the same expression a[i]. All the is needed to make thiswork is a iterated statement in which the variable i serves as a counter, incrementing from0 to a.length -1. That is exactly what a loop does.

    What is an HTML tag?Answer: An HTML tag is a syntactical construct in the HTML language that abbreviatesspecific instructions to be executed when the HTML script is loaded into a Web browser. Itis like a method in Java, a function in C++, a procedure in Pascal, or a subroutine inFORTRAN.

    Explain which of the following declarations will compile and what will be constant

    - a pointer or the value pointed at: * const char *

    * char const *

    * char * const

    Note: Ask the candidate whether the first declaration is pointing to a string or a singlecharacter. Both explanations are correct, but if he says that its a single character pointer,ask why a whole string is initialized as char* in C++. If he says this is a string declaration,ask him to declare a pointer to a single character. Competent candidates should not have

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 38/57

    problems pointing out why const char* can be both a character and a string declaration,incompetent ones will come up with invalid reasons.

    Youre given a simple code for the class Bank Customer. Write the followingfunctions:

    * Copy constructor

    * = operator overload

    * == operator overload

    * + operator overload (customers balances should be added up, as an example of

    joint account between husband and wife)

    Note:Anyone confusing assignment and equality operators should be dismissed from theinterview. The applicant might make a mistake of passing by value, not by reference. Thecandidate might also want to return a pointer, not a new object, from the additionoperator. Slightly hint that youd like the value to be changed outside the function, too, inthe first case. Ask him whether the statement customer3 = customer1 + customer2 wouldwork in the second case.

    What problems might the following macro bring to the application?#define sq(x) x*x

    Anything wrong with this code?T *p = new T[10];

    delete p;

    Everything is correct, Only the first element of the array will be deleted, The entire arraywill be deleted, but only the first element destructor will be called.

    Anything wrong with this code?T *p = 0;

    delete p;

    Yes, the program will crash in an attempt to delete a null pointer.

    How do you decide which integer type to use?It depends on our requirement. When we are required an integer to be stored in 1 byte(means less than or equal to 255) we use short int, for 2 bytes we use int, for 8 bytes weuse long int.

    A char is for 1-byte integers, a short is for 2-byte integers, an int is generally a 2-byte or 4-

    byte integer (though not necessarily), a long is a 4-byte integer, and a long long is a 8-byteinteger.

    hat does extern mean in a function declaration?Using extern in a function declaration we can make a function such that it can usedoutside the file in which it is defined.

    An extern variable, function definition, or declaration also makes the described variable orfunction usable by the succeeding part of the current source file. This declaration does not

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 39/57

    replace the definition. The declaration is used to describe the variable that is externallydefined.

    If a declaration for an identifier already exists at file scope, any extern declaration of thesame identifier found within a block refers to that same object. If no other declaration forthe identifier exists at file scope, the identifier has external linkage.

    What can I safely assume about the initial values of variables which are notexplicitly initialized?It depends on complier which may assign any garbage value to a variable if it is notinitialized.

    What is the difference between char a[] = string; and char *p = string;?In the first case 6 bytes are allocated to the variable a which is fixed, where as in thesecond case if *p is assigned to some other value the allocate memory can change.

    Whats the auto keyword good for?Answer1

    Not much. It declares an object with automatic storage duration. Which means the objectwill be destroyed at the end of the objects scope. All variables in functions that are notdeclared as static and not dynamically allocated have automatic storage duration bydefault.

    For exampleint main()

    {int a; //this is the same as writing auto int a;}

    Answer2

    Local variables occur within a scope; they are local to a function. They are often calledautomatic variables because they automatically come into being when the scope is enteredand automatically go away when the scope closes. The keyword auto makes this explicit,but local variables default to auto auto auto auto so it is never necessary to declaresomething as an auto auto auto auto.

    What is the difference between char a[] = string; and char *p = string; ?Answer1a[] = string;char *p = string;

    The difference is this:p is pointing to a constant string, you can never safely sayp[3]=x';however you can always say a[3]=x';

    char a[]=string; - character array initialization.char *p=string ; - non-const pointer to a const-string.( this is permitted only in the caseof char pointer in C++ to preserve backward compatibility with C.)

    Answer2

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 40/57

    a[] = string;char *p = string;

    a[] will have 7 bytes. However, p is only 4 bytes. P is pointing to an adress is either BSS orthe data section (depending on which compilerGNU for the former and CC for thelatter).

    Answer3char a[] = string;char *p = string;

    for char a[].using the array notation 7 bytes of storage in the static memory block aretaken up, one for each character and one for the terminating nul character.

    But, in the pointer notation char *p.the same 7 bytes required, plus N bytes tostore the pointer variable p (where N depends on the system but is usually a minimum of2 bytes and can be 4 or more)

    How do I declare an array of N pointers to functions returning pointers to functionsreturning pointers to characters?Answer1If 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

    Answer2char* (* (*a[N])())()Here a is that array. And according to question no function will not take any parametervalue.

    What does extern mean in a function declaration?It tells the compiler that a variable or a function exists, even if the compiler hasnt yet seenit in the file currently being compiled. This variable or function may be defined in anotherfile or further down in the current file.

    How do I initialize a pointer to a function?This is the way to initialize a pointer to a functionvoid fun(int a)

    {

    }

    void main()

    {void (*fp)(int);fp=fun;fp(1);

    }

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 41/57

    How do you link a C++ program to C functions?By using the extern "C" linkage specification around the C function declarations.

    Explain the scope resolution operator.It permits a program to reference an identifier in the global scope that has been hidden byanother identifier with the same name in the local scope.

    What are the differences between a C++ struct and C++ class?The default member and base-class access specifier are different.

    How many ways are there to initialize an int with a constant?Two.There are two formats for initializers in C++ as shown in the example that follows. The firstformat uses the traditional C notation. The second format uses constructor notation.int foo = 123;int bar (123);

    How does throwing and catching exceptions differ from using setjmp and

    longjmp?The throw operation calls the destructors for automatic objects instantiated since entry tothe try block.

    What is a default constructor?Default constructor WITH arguments class B { public: B (int m = 0) : n (m) {} int n; }; intmain(int argc, char *argv[]) { B b; return 0; }

    What is a conversion constructor?A constructor that accepts one argument of a different type.

    What is the difference between a copy constructor and an overloaded assignment

    operator?A copy constructor constructs a new object by using the content of the argument object.An overloaded assignment operator assigns the contents of an existing object to anotherexisting object of the same class.

    When should you use multiple inheritance?There are three acceptable answers: "Never," "Rarely," and "When the problem domaincannot be accurately modeled any other way."

    Explain the ISA and HASA class relationships. How would you implement each in aclass design?A specialized class "is" a specialization of another class and, therefore, has the ISA

    relationship with the other class. An Employee ISA Person. This relationship is bestimplemented with inheritance. Employee is derived from Person. A class may have aninstance of another class. For example, an employee "has" a salary, therefore the Employeeclass has the HASA relationship with the Salary class. This relationship is bestimplemented by embedding an object of the Salary class in the Employee class.

    When is a template a better solution than a base class?When you are designing a generic class to contain or otherwise manage objects of othertypes, when the format and behavior of those other types are unimportant to their

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 42/57

    containment or management, and particularly when those other types are unknown (thus,the generosity) to the designer of the container or manager class.

    What is a mutable member?One that can be modified by the class even when the object of the class or the memberfunction doing the modification is const.

    What is an explicit constructor?A conversion constructor declared with the explicit keyword. The compiler does not use anexplicit constructor to implement an implied conversion of types. Its purpose is reservedexplicitly for construction.

    What is the Standard Template Library (STL)?A library of container templates approved by the ANSI committee for inclusion in thestandard C++ specification.A programmer who then launches into a discussion of the generic programming model,iterators, allocators, algorithms, and such, has a higher than average understanding of thenew technology that STL brings to C++ programming.

    Describe run-time type identification.The ability to determine at run time the type of an object by using the typeid operator orthe dynamic_cast operator.

    What problem does the namespace feature solve?Multiple providers of libraries might use common global identifiers causing a namecollision when an application tries to link with two or more such libraries. The namespacefeature surrounds a librarys external declarations with a unique namespace thateliminates the potential for those collisions.

    This solution assumes that two library vendors dont use the same namespace identifier,of course.

    Are there any new intrinsic (built-in) data types?Yes. The ANSI committee added the bool intrinsic type and its true and false valuekeywords.

    Will the following program execute?void main()

    {void *vptr = (void *) malloc(sizeof(void));vptr++;

    }

    Answer1It will throw an error, as arithmetic operations cannot be performed on void pointers.

    Answer2It will not build as sizeof cannot be applied to void* ( error Unknown size )

    Answer3How can it execute if it wont even compile? It needs to be int main, not void main. Also,cannot increment a void *.

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 43/57

    Answer4According to gcc compiler it wont show any error, simply it executes. but in general wecant do arthematic operation on void, and gives size of void as 1

    Answer5

    The program compiles in GNU C while giving a warning for void main. The program runswithout a crash. sizeof(void) is 1? hence when vptr++, the address is incremented by 1.

    Answer6Regarding arguments about GCC, be aware that this is a C++ question, not C. So gcc willcompile and execute, g++ cannot. g++ complains that the return type cannot be void andthe argument of sizeof() cannot be void. It also reports that ISO C++ forbids incrementing apointer of type void*.

    Answer7in C++voidp.c: In function `int main():

    voidp.c:4: error: invalid application of `sizeof to a void typevoidp.c:4: error: malloc undeclared (first use this function)voidp.c:4: error: (Each undeclared identifier is reported only once for each function itappears in.)voidp.c:6: error: ISO C++ forbids incrementing a pointer of type `void*

    But in c, it work without problems

    void main()

    {

    char *cptr = 0?2000;

    long *lptr = 0?2000;

    cptr++;

    lptr++;

    printf( %x %x, cptr, lptr);

    }Will it execute or not?Answer1For Q2: As above, wont compile because main must return int. Also, 02000 cannot beimplicitly converted to a pointer (I assume you meant 02000 and not 0?2000.)

    Answer2Not Excute.

    Compile with VC7 results following errors:error C2440: initializing : cannot convert from int to char *error C2440: initializing : cannot convert from int to long *

    Not Excute if it is C++, but Excute in C.The printout:2001 2004

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 44/57

    Answer3In C++[$]> g++ point.cpoint.c: In function int main():point.c:4: error: invalid conversion from `int to `char*point.c:5: error: invalid conversion from `int to `long int*

    in C[$] etc > gcc point.cpoint.c: In function main:point.c:4: warning: initialization makes pointer from integer without a castpoint.c:5: warning: initialization makes pointer from integer without a cast[$] etc > ./a.exe2001 2004

    What is the difference between Mutex and Binary semaphore?semaphore is used to synchronize processes. where as mutex is used to provide

    synchronization between threads running in the same process.

    In C++, what is the difference between method overloading and method

    overriding?Overloading a method (or function) in C++ is the ability for functions of the same name tobe defined as long as these methods have different signatures (different set of parameters).Method overriding is the ability of the inherited class rewriting the virtual method of thebase class.

    What methods can be overridden in Java?In C++ terminology, all public methods in Java are virtual. Therefore, all Java methods canbe overwritten in subclasses except those that are declared final, static, and private.

    What are the defining traits of an object-oriented language?The defining traits of an object-oriented langauge are:* encapsulation* inheritance* polymorphism

    Write a program that ask for user input from 5 to 9 then calculate the averageint main()

    {int MAX=4;int total =0;

    int average=0;int numb;coutnumb;if((numb 9))cout

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 45/57

    total = total + numb;average= total /MAX;

    }cout

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 46/57

    leak because derived classs objects will not get freed.Destructors are declared virtualso as to bind objects to the methods at runtime so that appropriate destructor is called.

    What is "mutable"?Answer1."mutable" is a C++ keyword. When we declare const, none of its data members can

    change. When we want one of its members to change, we declare it as mutable.

    Answer2.A "mutable" keyword is useful when we want to force a "logical const" data member to haveits value modified. A logical const can happen when we declare a data member as non-const, but we have a const member function attempting to modify that data member. Forexample:class Dummy {public:bool isValid() const;private:mutable int size_ = 0;

    mutable bool validStatus_ = FALSE;// logical const issue resolved};

    bool Dummy::isValid() const// data members become bitwise const

    {if (size > 10) {validStatus_ = TRUE; // fine to assignsize = 0; // fine to assign

    }}

    Answer2."mutable" keyword in C++ is used to specify that the member may be updated or modifiedeven if it is member of constant object. Example:class Animal {private:string name;string food;mutable int age;public:void set_age(int a);

    };

    void main() {const Animal Tiger(Fulffy,'antelope,1);Tiger.set_age(2);// the age can be changed since its mutable

    }

  • 7/29/2019 hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdsk

    http:///reader/full/hsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnbhidjfnknfhsjdhkhdskhkcnb 47/57

    Differences of C and C++Could you write a small program that will compile in C but not in C++ ?In C, if you can a const variable e.g.const int i = 2;you can use this variable in other module as followsextern const int i;

    C compiler will not complain.

    But for C++ compiler u must writeextern const int i = 2;else error would be generated.

    Bitwise Operations - Given inputs X, Y, Z and operations | and & (meaning bitwise

    OR and AND, respectively), what is output equal to in?output = (X & Y) | (X & Z) | (Y & Z);

    C++ Object-Oriented Interview Questions And Answers

    What is a modifier?A modifier, also called a modifying function is a member function that changes the valueof at least one data member. In other words, an operation that modifies the state of anobject. Modifiers are also known as mutators. Example: The function mod is a modifier inthe following code snippet:

    class test

    {int x,y;

    public:test()

    {x=0; y=0;

    }void mod()

    {x=10;y=15;

    }};

    What is an accessor?An accessor is a class operation that does not modify the state of an object. The accessorfunctions need to be declared as const operations

    Differentiate between a template class and class template.Temp