Tuesday, November 6, 2007

C++ Interview Questions

Repeatedly asking C++ Interview Questions

1
.What is public, protected, private?
Ans:
public, protected, private are access specifiers that is used to implement encapsulation of data at various level.

Private:

* Can be data or method members
* Are private to the class where they are declared
* Accessible ONLY by the member methods of the class where they are declared
* Only exception to the above rule is Friend (explanation of friends is beyond the scope of this topic
* In a C++ class, private is default for member declaration. That is, if you do not specify any access specifier (private, public, protected), the member is considered private

Public:

* Can be data or method members
* Are accessible by any function/method/application globally, so long as an instance of the class where the public members are declared is created.
* These members are accessible only thru an instance of the class where they are declared
* Generally used to define a C++ class behaviour and/or to access private data members (act as private data modifiers)

Protected

* Can be data or method members
* Act exactly as private members for all practical purposes, so long as they are referenced from within the class (and/or instances of the class)where they are declared
* Specifically used to define how certain data/method members of a class would behave in a child class (used to define their behaviour in inheritance)
* The protected members become private of a child class in case of private inheritance, public in case of public inheritance, and stay protected in case of protected inheritance.


2. What is a class?

Ans:

class is a user defined data type,in which data members and member functions are defined.A class can also be defined as a classification/category of objects that have similar
attributes and behaviour.For example, Automobile is a category of objects that have similar attributes, such as wheels, engine, doors, shape, color, cylinders etc., and behaviours,
such as start, run, move, turn etc. Car is an instance of automobile which has different values for the attributes (4 wheels, one engine, 2 or 4 doors, 4/6/8 cylinders, etc),

3.What is an object?

Ans:

In C++, Object is an instance of a Class that has a runtime state, and is associated with certain specific methods that can change its state.

4.Difference between realloc() and free()?

Ans:

Realloc() is used to reallocate the memory for variable.Realloc()used to resize the memory held by the pointer to the number of bytes specificed.If the new
size is larger than current size, new memory is allocated. If it is less, the remaining (additional) bytes are released to general OS/application consumption.

Free() is used to free the allocated memory of a variable.

5. What is function overloading and operator overloading?

Ans:

Function overloading: C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as their types
are concerned). This capability is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number,
types and order of the arguments in the call. Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types
For example, void Function_Test();
void Function_Test(int); // Overloaded
void Function_Test(int, int); // Overloaded

Operator overloading allows existing C++ operators to be redefined so that they work on objects of user-defined classes. Overloaded operators are syntactic sugar for equivalent function calls.
They form a pleasant facade that doesn't add anything fundamental to the language (but they can improve understandability and reduce maintenance costs).

6. What is virtual class and friend class?

Ans:

Virtual Base Class: Used in context of multiple inheritance in C++. If you plan to derive two classes from a class, and further derive one class from the two classes in the second level, you
need to declare the uppermost base class as 'virtual' in the inherited classes. This prevents multiple copies of the uppermost base class data members when an object of the class at the
third level of hierarchy is created.

Friend class: When a class declares another class as its friend, it is giving complete access to all its data and methods including private and protected data and methods to the friend
class member methods.
Friendship is not bi-directional. If A declares B as its friend it does NOT mean that A can access private data of B. It only means that B can access all data of A.

7. What is abstraction?

Ans:
Abstraction refers to the act of representing essential features without including the background details.That means abstraction is separating the logical properties from implementation details.
For example driving the car is a logical property and design of the engine is the implementation detail.

8. What do you mean by inline function?

Ans:
The idea behind inline functions is to insert the code of a called function at the point where 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) an increase in the size of the generated binary executables.When an inline Function is invoked the code of function is inserted instead of
jump to code of function.

9. What do you mean by pure virtual functions?

Ans:
A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero.

class Shape {

public:

virtual void draw() = 0;
};

10. What is virtual constructors/destructors?

Ans:
Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error.

Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object,
the base-class destructor function (matching the pointer type) is called on the object.

11. What is a scope resolution operator?

Ans:
A scope resolution operator (::) can be used to define the member functions of a class outside the class.Most generally a scope resolution operator is required when a data member is redefined by a derived class,
or an overriden method of the derived class wants to call the base class version of the same method.

12. what is difference between constructor and destructor?

Ans:
Constructor is the memeber function of the class which has the same name as that of class and it is invoked whenever the class object is instantiated.Using construtor we can allocate memory.

Destructor is also the member function of same class name and has ~ operator when ever declared in the function and it is used to destruct the object which has been constructed ,whenever we want to destroy it..

51 comments:

Harilal said...

Good Post Arun. It's very usefull to me.

Deepak said...

Software Testing jobs & study material.
Adding our link to your site is very easy. Simple copy the code between the dotted lines and add it to your Home Page:

Software Testing Jobs & Free Study Material>
"Software Testing Jobs & Free Study Material"


If you would like a link back to your site, please contact us.

once you will add it in your home page then please let me know your code so i can also put your name in my blog.

Deepak said...

<a href="http://www.istqbsoftwaretesting.blogspot.com/">Software Testing Jobs & Free Study Material>
"Software Testing Jobs & Free Study Material"
</a>

Brownie said...

Nice post. The answer for the virtual constructor is not very satisfying though. Here's a nice article about the subject: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=184

春天來嚕 said...

成人色情圖片激情聊天室火辣美眉520sex赤裸美女成人自拍貼圖18限性影片觀賞av色情影音聊天手淫激情成人聊天室成人色情圖片網sex520自拍走光照片淫慾18禁成人影音聊天美女台灣性網限制級女生手淫成人聊天春宮裙底風光情趣丁字褲極度震撼情色論壇露點成人視訊聊天avlive show愛愛明星露點台灣色情網站自慰少婦成人聊天網美女視訊成人頻道人妻熟女蕩婦一對多激情成人色情聊天室av成人上空秀巨奶視訊網愛聊天室一夜正妹色情貼片一對多免費美女視訊粉紅乳頭作愛影片美女聊天sexy成人色情圖片網女人胸部圖片視訊色情情色網站性關係辣妹聊天室大奶子

昱廷 said...

great msg for me, thanks a lot dude˙﹏˙

郭怡君 said...

人生就像一顆核桃,必須敲破它,才會顯出他的內容。 ..................................................

邱志誠 said...

你不能左右天氣,但你可以改變心情......................................................

奎峰 said...

愛情是一種發明,需要不斷改良。只是,這種發明和其他發明不一樣,它沒有專利權,隨時會被人搶走。......................................................

睿玄 said...

上床圖片av圖片免費色情圖台灣性網陰蒂淫妹貼圖色情激突成人論壇成人色情網激情成人聊天室情色影音鹹濕女生自慰影片淫慾自慰色情a片成人色情圖片網情人視訊網成人話題成人色情台灣成人淫蕩a圖片情色留言板女生奶頭情色成人全裸美女圖片情色寫真裸體照大奶子台灣色情成人網站一夜正妹美女裸體寫真g罩杯美女sex520情趣娃娃視訊打槍視訊辣妹uthome淫亂走光照av情色網女生高潮成人自拍火辣眉眉太太陰毛美女視訊聊天全裸入鏡火辣av三點全露

Ko212ryS_Dahle0 said...

笑口常開~~天天開心........................................

SungR_Auclair0佳亦 said...

I do like ur article~!!!..................................................

又陽 said...

It's great!!.............................................

LoganO_Kellam1 said...

A good medicine tastes bitter. .............................................

K910athrinA_Petrin0 said...

處順境須謹慎,處逆境要忍耐。.............................................

Tro0223y_Millan said...

我們老得太快,卻聰明得太遲。 ....................................................

FrederickBovee0722麗玲 said...

很喜歡你的文章~繼續加油唷^^. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

DedeJ文辰_Fe said...

If you can, you can. ............................................................

向霖 said...

大師手筆﹐果然不凡.................................................................

文群文群 said...

Pen and ink is wits plough.................................................................

江婷 said...

一棵樹除非在春天開了花,否則難望在秋天結果。.................................................................

于庭 said...

成熟,就是有能力適應生活中的模糊。.................................................................

web dagger said...

Hey thanks a lot for sharing such a nice and informative article........

for some more interview questions on C++ check this link: free Cpp interview questions

芳容222許林堅林芳容儀 said...

It takes all kinds to make a world.............................................................

吳婷婷 said...

當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。............................................................

ju吳phe宇te佳ns said...

格主的部落格內容真豐富~~看得很開心..................................................................

web dagger said...

Hey thanks a lot for sharing useful interview questions..... which will be very helpful while attending the interviews.....
glad i found ur site...really a very helpful site.....
by the way check out my collection of php interview questions from here: php interview questions

楊容茂楊容茂 said...

來打聲招呼-大家好!!!..................................................................

冠宇 said...

部落格很棒唷~ 支持你歐^^..................................................................

潘凱花潘凱花 said...

人不可以求其備,必捨其所短,取其所長............................................................

曾法幸 said...

旁觀自己的悲傷是解脫,主觀自己的悲傷是更加悲傷................................................

曾戴山柔曾戴山柔 said...

來拜訪囉~部落格很棒^^~幫你推個文............................................................

ToryO_Vis建銘 said...

你不能改變容貌~~但你可以展現笑容.................................................................

雅王任 said...

Lets cross the bridge when we come to it............................................................

建邱勳 said...

活是一種鍛鍊靈魂的東西..................................................................

恩宛玲如 said...

相見亦無事,不來常思君......................................................................

偉曹琬 said...

要保持更新呦,加油!!!期待你的新文章!!!............................................................

靜蔡蔡蔡蔡怡 said...

人不能像動物一樣活著,而應該追求知識和美德............................................................

怡靜怡靜怡靜怡雯 said...

No pains, no gains.......................................................................

Oszkár said...

Thanks for the nice questions.

I'd just note that protected members don't become public in the case of public inheritance, but remain protected.

mallorca said...

Hi

Tks very much for post:

I like it and hope that you continue posting.

Let me show other source that may be good for community.

Source: Behavioral interview questions

Best rgs
David

malouda said...

Hi

I read this post two times.

I like it so much, please try to keep posting.

Let me introduce other material that may be good for our community.

Source: Second interview questions

Best regards
Henry

apoorva said...

Great article.

Alex said...

This is not correct:

The protected members become private of a child class in case of private inheritance, public in case of public inheritance, and stay protected in case of protected inheritance.

Alex said...

This is not correct:
The protected members become private of a child class in case of private inheritance, public in case of public inheritance, and stay protected in case of protected inheritance.

Chaitanya Reddy said...

http://ccppcoding.blogspot.in/

Phil Braica said...

Not bad, but as another put it, part of the answer to question 1 is wrong, inheriting a class never elevates the visibility of the base class members only reduces it. Public inheritance won't make protected base class members accessible.
Also inlines are complex, they don't just copy, they only TEND to just copy, the compiler may do other things, static inlines and small ones are more likely to really just copy. There are actually 3 behaviors (treat like normal code, copy, or make stackless jump-returns to snippets where the code is already in the same register arrangement). Thus 30 calls might turn into 1 normal call in one place, 2 different stackless snippets jmp called from that handle 15 other calls, and the remaining might be strait copies of code. That kinda thing is rare but happens.

Tyler Thursby said...

Hello there! Thanks for providing such an awesome resource with your site here. I was hoping you might be interested in swapping links.
I’m working on a little site that specializes in providing job interview tips. It’s a humble operation and we’re trying to build our exposure up now.
Keep up the great work! Your site looks fantastic  Cheers!

career maker said...

thank u

anish mounish said...
This comment has been removed by the author.
asitbangalorereviews said...

good collection...
Core Java Interview Questions and Answers

Advanced Java Interview Questions and Answers

c++ interview questions and answers