You are on page 1of 18

C++ INTERVIEW QUESTIONS

What is encapsulation?? Containing and hiding information about an object, such as internal data structures and code. Encapsulation isolates the internal complexity of an object's operation from the rest of the application. For example, a client component asking for net re enue from a business object need not kno! the data's origin.

What is inheritance? "nheritance allo!s one class to reuse the state and beha ior of another class. #he deri ed class inherits the properties and method implementations of the base class and extends it by o erriding methods and adding additional properties and methods. What is $olymorphism?? $olymorphism allo!s a client to treat different objects in the same !ay e en if they !ere created from different classes and exhibit different beha iors. %ou can use implementation inheritance to achie e polymorphism in languages such as C&& and 'a a. (ase class object's pointer can in oke methods in deri ed class objects. %ou can also achie e polymorphism in C&& by function o erloading and operator o erloading. What is constructor or ctor? Constructor creates an object and initiali)es it. "t also creates table for irtual functions. "t is different from other methods in a class. What is destructor? *estructor usually deletes any extra resources allocated by the object. What is default constructor? Constructor !ith no arguments or all the arguments has default alues.

What is copy constructor? Constructor !hich initiali)es the it's object member ariables + by shallo! copying, !ith another object of the same class. "f you don't implement one in your class then compiler implements one for you. for example(oo .bj/+/0,1 22 calling (oo constructor (oo .bj3+.bj/,1 22 calling boo copy constructor (oo .bj3 4 .bj/122 calling boo copy constructor When are copy constructors called? Copy constructors are called in follo!ing casesa, !hen a function returns an object of that class by alue b, !hen the object of that class is passed by alue as an argument to a function c, !hen you construct an object based on another object of the same class d, When compiler generates a temporary object What is assignment operator? *efault assignment operator handles assigning one object to another of the same class. 5ember to member copy +shallo! copy, What are all the implicit member functions of the class? .r !hat are all the functions !hich compiler implements for us if !e don't define one.?? default ctor copy ctor assignment operator default destructor address operator What is con ersion constructor? constructor !ith a single argument makes that constructor as con ersion ctor and it can be used for type con ersion. for exampleclass (oo 6 public-

(oo+ int i ,1 71 (oo (oo.bject 4 /0 1 22 assigning int /0 (oo object What is con ersion operator?? class can ha e a public method for specific data type con ersions. for exampleclass (oo 6 double alue1 public(oo+int i , operator double+, 6 return alue1 7 71 (oo (oo.bject1 double i 4 (oo.bject1 22 assigning object to ariable i of type double. no! con ersion operator gets called to assign the alue. What is diff bet!een malloc+,2free+, and ne!2delete? malloc allocates memory for object in heap but doesn't in oke object's constructor to initialli)e the object. ne! allocates memory and also in okes constructor to initiali)e the object. malloc+, and free+, do not support object semantics *oes not construct and destruct objects string 8 ptr 4 +string 8,+malloc +si)eof+string,,, 9re not safe *oes not calculate the si)e of the objects that it construct :eturns a pointer to oid int 8p 4 +int 8, +malloc+si)eof+int,,,1 int 8p 4 ne! int1 9re not extensible ne! and delete can be o erloaded in a class ;delete; first calls the object's termination routine +i.e. its destructor, and then releases the space the object occupied on the heap memory. "f an array of objects !as created using ne!, then delete must be told that it is dealing !ith an array by preceding the name !ith an empty <=-> "nt?t 8my?ints 4 ne! "nt?t</0=1 ... delete <=my?ints1 What is the diff bet!een ;ne!; and ;operator ne!; ?

;operator ne!; !orks like malloc. What is difference bet!een template and macro?? #here is no !ay for the compiler to erify that the macro parameters are of compatible types. #he macro is expanded !ithout any special type checking. "f macro parameter has a postincremented ariable + like c&& ,, the increment is performed t!o times. (ecause macros are expanded by the preprocessor, compiler error messages !ill refer to the expanded macro, rather than the macro definition itself. 9lso, the macro !ill sho! up in expanded form during debugging. for example5acro@define min+i, j, +i A j ? i - j, templatetemplateAclass #B # min +# i, # j, 6 return i A j ? i - j1 7 What are C&& storage classes? auto register static extern auto- the default. Cariables are automatically created and initiali)ed !hen they are defined and are destroyed at the end of the block containing their definition. #hey are not isible outside that block register- a type of auto ariable. a suggestion to the compiler to use a C$D register for performance static- a ariable that is kno!n only in the function that contains its definition but is ne er destroyed and retains its alue bet!een calls to that function. "t exists from the time the program begins execution extern- a static ariable !hose definition and placement is determined !hen all object and library modules are combined +linked, to form the executable code file. "t can be isible outside the file !here it is defined. What are storage Eualifiers in C&& ? #hey are.. const

olatile mutable Const key!ord indicates that memory once initiali)ed, should not be altered by a program. olatile key!ord indicates that the alue in the memory location can be altered e en though nothing in the program code modifies the contents. for example if you ha e a pointer to hard!are location that contains the time, !here hard!are changes the alue of this pointer ariable and not the program. #he intent of this key!ord to impro e the optimi)ation ability of the compiler. mutable key!ord indicates that particular member of a structure or class can be altered e en if a particular structure ariable, class, or class member function is constant. struct data 6 char name<F0=1 mutable double salary1 7 const data 5yGtruct 4 6 ;Gatish Ghetty;, /000 71 22initli)ed by complier strcpy + 5yGtruct.name, ;Ghilpa Ghetty;,1 22 compiler error 5yGtruct.salaray 4 3000 1 22 complier is happy allo!ed What is reference ?? reference is a name that acts as an alias, or alternati e name, for a pre iously defined ariable or an object. prepending ariable !ith ;H; symbol makes it as reference. for exampleint a1 int Hb 4 a1 What is passing by reference? 5ethod of passing arguments to a function !hich takes parameter of type reference. for exampleoid s!ap+ int H x, int H y , 6 int temp 4 x1 x 4 y1 y 4 x1 7 int a43, b4I1 s!ap+ a, b ,1 (asically, inside the function there !on't be any copy of the arguments ;x; and ;y; instead they refer to original ariables a and b. so no extra memory needed to pass arguments and it

is more efficient. When do use ;const; reference arguments in function? a, Dsing const protects you against programming errors that inad ertently alter data. b, Dsing const allo!s function to process both const and non>const actual arguments, !hile a function !ithout const in the prototype can only accept non constant arguments. c, Dsing a const reference allo!s the function to generate and use a temporary ariable appropriately. When are temporary ariables created by C&& compiler? $ro ided that function parameter is a ;const reference;, compiler generates temporary ariable in follo!ing 3 !ays. a, #he actual argument is the correct type, but it isn't J alue double Cuberoot + const double H num , 6 num 4 num 8 num 8 num1 return num1 7 double temp 4 3.01 double alue 4 cuberoot + I.0 & temp ,1 22 argument is a expression and not a J alue1 b, #he actual argument is of the !rong type, but of a type that can be con erted to the correct type long temp 4 IJ1 double alue 4 cuberoot + temp,1 22 long to double con ersion What is irtual function? When deri ed class o errides the base class method by redefining the same function, then if client !ants to access redefined the method from deri ed class through a pointer from base class object, then you must define this function in base class as irtual function. class parent 6 oid Gho!+, 6 cout AA ;i'm parent; AA endl1 7 71 class child- public parent 6 oid Gho!+, 6 cout AA ;i'm child; AA endl1 7 71 parent 8 parent?object?ptr 4 ne! child1 parent?object?ptr>Bsho!+, 22 calls parent>Bsho!+, i

no! !e goto irtual !orld... class parent 6 irtual oid Gho!+, 6 cout AA ;i'm parent; AA endl1 7 71 class child- public parent 6 oid Gho!+, 6 cout AA ;i'm child; AA endl1 7 71 parent 8 parent?object?ptr 4 ne! child1 parent?object?ptr>Bsho!+, 22 calls child>Bsho!+, What is pure irtual function? or !hat is abstract class? When you define only function prototype in a base class !ithout and do the complete implementation in deri ed class. #his base class is called abstract class and client !on't able to instantiate an object using this base class. %ou can make a pure irtual function or abstract class this !ay.. class (oo 6 oid foo+, 4 01 7 (oo 5y(oo1 22 compilation error What is 5emory alignment?? #he term alignment primarily means the tendency of an address pointer alue to be a multiple of some po!er of t!o. Go a pointer !ith t!o byte alignment has a )ero in the least significant bit. 9nd a pointer !ith four byte alignment has a )ero in both the t!o least significant bits. 9nd so on. 5ore alignment means a longer seEuence of )ero bits in the lo!est bits of a pointer. What problem does the namespace feature sol e? 5ultiple pro iders of libraries might use common global identifiers causing a name collision !hen an application tries to link !ith t!o or more such libraries. #he namespace feature surrounds a library's external declarations !ith a uniEue namespace that eliminates the potential for those collisions. namespace <identifier= 6 namespace>body 7 9 namespace declaration identifies and assigns a name to a declarati e region. #he identifier in a namespace declaration must be uniEue in the declarati e region in

!hich it is used. #he identifier is the name of the namespace and is used to reference its members. What is the use of 'using' declaration? 9 using declaration makes it possible to use a name from a namespace !ithout the scope operator. What is an "terator class? 9 class that is used to tra erse through the objects maintained by a container class. #here are fi e categories of iterators- input iterators, output iterators, for!ard iterators, bidirectional iterators, random access. 9n iterator is an entity that gi es access to the contents of a container object !ithout iolating encapsulation constraints. 9ccess to the contents is granted on a one>at>a>time basis in order. #he order can be storage order +as in lists and Eueues, or some arbitrary order +as in array indices, or according to some ordering relation +as in an ordered binary tree,. #he iterator is a construct, !hich pro ides an interface that, !hen called, yields either the next element in the container, or some alue denoting the fact that there are no more elements to examine. "terators hide the details of access to and update of the elements of a container class. Gomething like a pointer. What is a dangling pointer? 9 dangling pointer arises !hen you use the address of an object after its lifetime is o er. #his may occur in situations like returning addresses of the automatic ariables from a function or using the address of the memory block after it is freed. What do you mean by Gtack un!inding? "t is a process during exception handling !hen the destructor is called for all local objects in the stack bet!een the place !here the exception !as thro!n and !here it is caught. Kame the operators that cannot be o erloaded?? si)eof, ., .8, .>B, --, ?What is a container class? What are the types of container classes? 9 container class is a class that is used to hold objects in memory or external storage. 9 container class acts as a generic holder. 9 container class has a predefined beha ior and a !ell>kno!n interface. 9 container class is a supporting class !hose 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 container is called a heterogeneous container1 !hen the

container is holding a group of objects that are all the same, the container is called a homogeneous container. What is inline function?? #he ??inline key!ord tells the compiler to substitute the code !ithin the function definition for e ery instance of a function call. Lo!e er, substitution occurs only at the compiler's discretion. For example, the compiler does not inline a function if its address is taken or if it is too large to inline. What is o erloading?? With the C&& language, you can o erload functions and operators. . erloading is the practice of supplying more than one definition for a gi en function name in the same scope. > 9ny t!o functions in a set of o erloaded functions must ha e different argument lists. > . erloading functions !ith argument lists of the same types, based on return type alone, is an error. What is . erriding? #o o erride a method, a subclass of the class that originally declared the method must declare a method !ith the same name, return type +or a subclass of that return type,, and same parameter list. #he definition of the method o erriding isM 5ust ha e same method name. M 5ust ha e same data type. M 5ust ha e same argument list. . erriding a method means that replacing a method functionality in child class. #o imply o erriding functionality !e need parent and child classes. "n the child class you define the same method signature as one defined in the parent class. What is ;this; pointer? #he this pointer is a pointer accessible only !ithin the member functions of a class, struct, or union type. "t points to the object for !hich the member function is called. Gtatic member functions do not ha e a this pointer. When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function. For example, the follo!ing function call my*ate.set5onth+ I ,1 can be interpreted this !ayset5onth+ Hmy*ate, I ,1 #he object's address is a ailable from !ithin the member function as the this pointer. "t is legal, though unnecessary, to use the this pointer !hen referring to members of the class. What happens !hen you make call ;delete this1; ??

#he code has t!o built>in pitfalls. First, if it executes in a member function for an extern, static, or automatic object, the program !ill probably crash as soon as the delete statement executes. #here is no portable !ay for an object to tell that it !as instantiated on the heap, so the class cannot assert that its object is properly instantiated. Gecond, !hen an object commits suicide this !ay, the using program might not kno! about its demise. 9s far as the instantiating program is concerned, the object remains in scope and continues to exist e en though the object did itself in. GubseEuent dereferencing of the pointer can and usually does lead to disaster. %ou should ne er do this. Gince compiler does not kno! !hether the object !as allocated on the stack or on the heap, ;delete this; could cause a disaster. Lo! irtual functions are implemented C&&? Cirtual functions are implemented using a table of function pointers, called the table. #here is one entry in the table per irtual function in the class. #his table is created by the constructor of the class. When a deri ed class is constructed, its base class is constructed first !hich creates the table. "f the deri ed class o errides any of the base classes irtual functions, those entries in the table are o er!ritten by the deri ed class constructor. #his is !hy you should ne er call irtual functions from a constructorbecause the table entries for the object may not ha e been set up by the deri ed class constructor yet, so you might end up calling base class implementations of those irtual functions What is name mangling in C&&?? #he process of encoding the parameter types !ith the function2method name into a uniEue name is called name mangling. #he in erse process is called demangling. For example Foo--bar+int, long, const is mangled as Nbar??CIFooil'. For a constructor, the method name is left out. #hat is Foo--Foo+int, long, const is mangled as N??CIFooil'. What is the difference bet!een a pointer and a reference? 9 reference must al!ays refer to some object and, therefore, must al!ays be initiali)ed1 pointers do not ha e such restrictions. 9 pointer can be reassigned to point to different objects !hile a reference al!ays refers to an object !ith !hich it !as initiali)ed. Lo! are prefix and postfix ersions of operator&&+, differentiated? #he postfix ersion of operator&&+, has a dummy parameter of type int. #he prefix ersion does not ha e dummy parameter. What is the difference bet!een const char 8my$ointer and char 8const my$ointer?

Const char 8my$ointer is a non constant pointer to constant data1 !hile char 8const my$ointer is a constant pointer to non constant data. Lo! can " handle a constructor that fails? thro! an exception. Constructors don't ha e a return type, so it's not possible to use return codes. #he best !ay to signal constructor failure is therefore to thro! an exception. Lo! can " handle a destructor that fails? Write a message to a log>file. (ut do not thro! an exception. #he C&& rule is that you must ne er thro! an exception from a destructor that is being called during the ;stack un!inding; process of another exception. For example, if someone says thro! Foo+,, the stack !ill be un!ound so all the stack frames bet!een the thro! Foo+, and the 7 catch +Foo e, 6 !ill get popped. #his is called stack un!inding. *uring stack un!inding, all the local objects in all those stack frames are destructed. "f one of those destructors thro!s an exception +say it thro!s a (ar object,, the C&& runtime system is in a no>!in situation- should it ignore the (ar and end up in the 7 catch +Foo e, 6 !here it !as originally headed? Ghould it ignore the Foo and look for a 7 catch +(ar e, 6 handler? #here is no good ans!er >> either choice loses information. Go the C&& language guarantees that it !ill call terminate+, at this point, and terminate+, kills the process. (ang you're dead. What is Cirtual *estructor? Dsing irtual destructors, you can destroy objects !ithout kno!ing their type > the correct destructor for the object is in oked using the irtual function mechanism. Kote that destructors can also be declared as pure irtual functions for abstract classes. if someone !ill deri e from your class, and if someone !ill say ;ne! *eri ed;, !here ;*eri ed; is deri ed from your class, and if someone !ill say delete p, !here the actual object's type is ;*eri ed; but the pointer p's type is your class. Can you think of a situation !here your program !ould crash !ithout reaching the breakpoint !hich you set at the beginning of main+,? C&& allo!s for dynamic initiali)ation of global ariables before main+, is in oked. "t is possible that initiali)ation of global !ill in oke some function. "f this function crashes the crash !ill occur before main+, is entered. Kame t!o cases !here you 5DG# use initiali)ation list as opposed to assignment in constructors. (oth non>static const data members and reference data members cannot be assigned alues1 instead, you should use initiali)ation list to initiali)e them.

Can you o erload a function based only on !hether a parameter is a alue or a reference? Ko. $assing by alue and by reference looks identical to the caller. What are the differences bet!een a C&& struct and C&& class? #he default member and base class access specifiers are different. #he C&& struct has all the features of the class. #he only differences are that a struct defaults to public member access and public base class inheritance, and a class defaults to the pri ate access specifier and pri ate base class inheritance. What does extern ;C; int func+int 8, Foo, accomplish? "t !ill turn off ;name mangling; for func so that one can link to code compiled by a C compiler. Lo! do you access the static member of a class? AClassKameB--AGtatic5emberKameB What is multiple inheritance+ irtual inheritance,? What are its ad antages and disad antages? 5ultiple "nheritance is the process !hereby a child can be deri ed from more than one parent class. #he ad antage of multiple inheritance is that it allo!s a class to inherit the functionality of more than one base class thus allo!ing for modeling of complex relationships. #he disad antage of multiple inheritance is that it can lead to a lot of confusion+ambiguity, !hen t!o base classes implement a method !ith the same name. What are the access pri ileges in C&&? What is the default access le el? #he access pri ileges in C&& are pri ate, public and protected. #he default access le el assigned to members of a class is pri ate. $ri ate members of a class are accessible only !ithin the class and by friends of the class. $rotected members are accessible by the class itself and it's sub>classes. $ublic members of a class can be accessed by anyone. What is a nested class? Why can it be useful? 9 nested class is a class enclosed !ithin the scope of another class. For example22 Example /- Kested class 22 class .uterClass 6 class KestedClass 6 22 ... 71 22 ... 71 Kested classes are useful for organi)ing code and controlling access and dependencies. Kested classes obey access rules just like other parts of a class do1 so, in Example /, if KestedClass is public then any code can name it as .uterClass--KestedClass. .ften nested

classes contain pri ate implementation details, and are therefore made pri ate1 in Example /, if KestedClass is pri ate, then only .uterClass's members and friends can use KestedClass. When you instantiate as outer class, it !on't instantiate inside class. What is a local class? Why can it be useful? local class is a class defined !ithin the scope of a function >> any function, !hether a member function or a free function. For example22 Example 3- Jocal class 22 int f+, 6 class JocalClass 6 22 ... 71 22 ... 71 Jike nested classes, local classes can be a useful tool for managing code dependencies. Can a copy constructor accept an object of the same class as parameter, instead of reference of the object? Ko. "t is specified in the definition of the copy constructor itself. "t should generate an error if a programmer specifies a copy constructor !ith a first argument that is an object and not a reference. +From 5icrosoft, 9ssume " ha e a linked list contains all of the alphabets from O9P to OQP. " !ant to find the letter ORP in the list, ho! does you perform the search to find the ORP? Lo! do you !rite a function that can re erse a linked>list? +Cisco Gystem, oid re erselist+ oid, 6 if+head440, return1 if+head>Bnext440, return1 if+head>Bnext44tail, 6 head>Bnext 4 01 tail>Bnext 4 head1 7 else 6 node8 pre 4 head1 node8 cur 4 head>Bnext1

node8 curnext 4 cur>Bnext1 head>Bnext 4 01 cur>Bnext 4 head1 for+1 curnextS401 , 6 cur>Bnext 4 pre1 pre 4 cur1 cur 4 curnext1 curnext 4 curnext>Bnext1 7 curnext>Bnext 4 cur1 7 7 Lo! do you find out if a linked>list has an end? +i.e. the list is not a cycle, %ou can find out by using 3 pointers. .ne of them goes 3 nodes each time. #he second one goes at / nodes each time. "f there is a cycle, the one that goes 3 nodes each time !ill e entually meet the one that goes slo!er. "f that is the case, then you !ill kno! the linked>list is a cycle. Lo! can you tell !hat shell you are running on DK"T system? %ou can do the Echo U:9K*.5. "t !ill return a undefined ariable if you are from the C>Ghell, just a return prompt if you are from the (ourne shell, and a V digit random numbers if you are from the Worn shell. %ou could also do a ps >l and look for the shell !ith the highest $"*. What is (oyce Codd Kormal form? 9 relation schema : is in (CKF !ith respect to a set F of functional dependencies if for all functional dependencies in F& of the form a>Bb, !here a and b is a subset of :, at least one of the follo!ing holdsM a>Bb is a tri ial functional dependency +b is a subset of a, M a is a superkey for schema : Could you tell something about the Dnix Gystem Wernel? #he kernel is the heart of the DK"T openrating system, itPs reponsible for controlling the computerPs resouces and scheduling user jobs so that each one gets its fair share of resources. What is a 5ake file? 5ake file is a utility in Dnix to help compile large programs. "t helps by only compiling the portion of the program that has been changed Lo! do you link a C&& program to C functions? (y using the extern ;C; linkage specification around the C function declarations.

Explain the scope resolution operator. *esign and implement a Gtring class that satisfies the follo!ingGupports embedded nulls $ro ide the follo!ing methods +at least, Constructor *estructor Copy constructor 9ssignment operator 9ddition operator +concatenation, :eturn character at location :eturn substring at location Find substring $ro ide ersions of methods for Gtring and for char8 arguments Guppose that data is an array of /000 integers. Write a single function call that !ill sort the /00 elements data <333= through data <I3/=. 9ns!er- Euicksort ++data & 333,, /00,1 What is a modifier? What is an accessor? *ifferentiate bet!een a template class and class template. When does a name clash occur? *efine namespace. What is the use of OusingP declaration. What is an "terator class? Jist out some of the ..*(5G a ailable. Jist out some of the object>oriented. What is an incomplete type? What is a dangling pointer? *ifferentiate bet!een the message and method. What is an adaptor class or Wrapper class? What is a Kull object? What is class in ariant? What do you mean by Gtack un!inding?

*efine precondition and post>condition to a member function. What are the conditions that ha e to be met for a condition to be an in ariant of the class? What are proxy objects? Kame some pure object oriented languages. Kame the operators that cannot be o erloaded. What is a node class? What is an orthogonal base class? What is a container class? What are the types of container classes? What is a protocol class? What is a mixin class? What is a concrete class? What is the handle class? What is an action class? When can you tell that a memory leak !ill occur? What is a parameteri)ed type? *ifferentiate bet!een a deep copy and a shallo! copy? What is an opaEue pointer? What is a smart pointer? What is reflexi e association? What is slicing? What is name mangling? What are proxy objects? What is cloning? *escribe the main characteristics of static functions. Will the inline function be compiled as the inline function al!ays? 'ustify. *efine a !ay other than using the key!ord inline to make a function inline. Lo! can a '--' operator be used as unary operator? What is placement ne!? What do you mean by analysis and design? What are the steps in ol ed in designing?

What are the main underlying concepts of object orientation? What do u meant by ;G("; of an object? *ifferentiate persistent H non>persistent objects? What do you meant by acti e and passi e objects? What is meant by soft!are de elopment method? What do you meant by static and dynamic modeling? Lo! to represent the interaction bet!een the modeling elements? Why generali)ation is ery strong? *ifferentiate 9ggregation and containment? Can link and 9ssociation applied interchangeably? What is meant by ;method>!ars;? Whether unified method and are same or different? Who !ere the three famous amigos and !hat !as their contribution to the object community? *ifferentiate the class representation of (ooch, :umbaugh and D5J? What is an DGEC9GE? Why it is needed? Who is an 9ctor?

What is guard condition? *ifferentiate the follo!ing notations? DGEC9GE is an implementation independent notation. Lo! !ill the designer gi e the implementation details of a particular DGEC9GE to the programmer? Guppose a class acts an 9ctor in the problem to represent it in the static model? Why does the function arguments are called as ;signatures;?

You might also like