C++ Interview Questions & Answers part 2

0
753
Q: – When does a name clash occur in C++?

A name clash occurs when a name is defined in more than one place. For example., two different class libraries could give two different classes the same name. If you try to use many class libraries at the same time, there is a fair chance that you will be unable to compile or link the program because of name clashes.

Q: – Why have references if pointers can do everything references can?

References are easier to use and to understand. The indirection is hidden, and no need exists to repeatedly dereference the variable.

C++ used for:
C++ is a powerful general-purpose programming language. It can be used to create small programs or large applications

Q: – Why have pointers if references are easier?

References cannot be null, and they cannot be reassigned. Pointers offer greater flexibility but are slightly more difficult to use.

Q: – How do you choose between recursion and iteration?

Some problems cry out for recursion, but most problems will yield to iteration as well. Put recursion in your back pocket; it may come in handy someday.

C++ used for:
C++ is a powerful general-purpose programming language. It can be used to create small programs or large applications

Q: – Is it better to use while (true) or [for (;;)?

No significant difference.

Q: – What is virtual constructors/destructors?

virtual constructors/destructors : Virtual destructors It is a Bassically same as vertual fuction, Its commonly used with inheritance. Since an abstract class must contain a pure virtual method that has to be overridden, a lot of developers commonly declare their destructors pure virtual, since a destructor is sure to be implemented in every subclass.Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. There is no virtual constructor coz virtual thing is in run time n constructor is compile time thing.

Q: – Why should I declare an object const if it limits what I can do with it?

As a programmer, you want to enlist the compiler in helping you find bugs. One serious bug that is difficult to find is a function that changes an object in ways that are not obvious to the calling function. Declaring an object const prevents such changes.

Q: – Why would you ever return by value from a function?

If the object being returned is local, you must return by value or you will be returning a reference to a non-existent object.

Q: – What is the difference between "overloading" and "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.overridding is runtime polymorphism while overloading is compile time polymorphism.

Submitted By:-Ankur Goyal            Email-ID: – goyal.ankur30@yahoo.in

SHARE

LEAVE A REPLY