Blog Archive

Theme images by Storman. Powered by Blogger.


Wednesday, July 20, 2016

C++ Latest Interview Questions and Answers.

C++ Latest Interview Questions and Answers. C++ Interview Questions and Answers For Freshers. Frequently Asked C++ Interview Questions and Answers. Latest Freshers Interview Questions and Answers.


Explain abstraction?

Simplified view of an object in user’s language is called abstraction. 
It is the simplest, well-defined interface to an object in OO and C++ that provides all the expected features and services to the user in a safe and predictable manner. 
It provides all the information that the user requires.
Good domain knowledge is important for effective abstraction. 
It separates specifications from implementation & keeps the code simpler and more stable.

What is the real purpose of class – to export data?

No, the real purpose of a class is not to export data. Rather, it is to provide services. Class provides a way to abstract behaviour rather than just encapsulating the bits.

What things would you remember while making an interface?

A class’s interface should be sensible enough. It should behave the way user expects it to. 
It should be designed from the outside in.

Explain the benefits of proper inheritance.

The biggest benefits of proper inheritance are : 

1. Substitutability 
2. Extensibility. 

1. Substitutability : 

The objects of a properly derived class can be easily and safely substituted for an object of its base class. 

2. Extensibility : 

The properly derived class can be freely and safely used in place of its base class even if the properly derived class is created a lot later than defining the user code. Extending the functionalities of a system is much easier when you add a properly derived class containing enhanced functionalities.

Does improper inheritance have a potential to wreck a project?

Many projects meet a dead end because of bad inheritance. So, it certainly has the potential to wreck a project. 
Small projects still have a scope to avoid the complete consequence of bad inheritance if the developers communicate and co-ordinate with an easy system design. This kind of a luxury is not possible in big projects, which means that the code breaks in a way difficult and at times impossible way to fix it.

How should runtime errors be handled in C++?

The runtime errors in C++ can be handled using exceptions. 
This exception handling mechanism in C++ is developed to handle the errors in software made up of independently developed components operating in one process and under synchronous control. 
According to C++, any routine that does not fulfil its promise throws an exception. The caller who knows the way to handle these exceptions can catch it.

When should a function throw an exception?

A function should throw an exception when it is not able to fulfil its promise. 
As soon as the function detects a problem that prevents it from fulfilling its promise, it should throw an exception. 
If the function is able to handle the problem, recover itself and deliver the promise, then the exception should not be thrown. 
If an event happens very frequently then exception handling is not the best way to deal with it. It requires proper fixation.

Where are setjmp and longjmp used in C++?

Setjmp and longjmp should not be used in C++. 
Longjmp jumps out of the function without unwinding the stack. This means that the local objects generated are not destructed properly. 
The better option is to use try/catch/throw instead. They properly destruct the local objects.

Are there any special rules about inlining?

Yes, there are a few rules about inlining :
1. Any source files that used the inline function must contain the function’s definition. 
2. An inline function must be defined everywhere. The easier way to deal with this to define the function once in the class header file and include the definition as required. The harder way is to redefine the function everywhere and learn the one-definition rule. 
3. Main() can not be inline.

Explain One-Definition Rule (ODR).

According to one-definition rule, C++ constructs must be identically defined in every compilation unit they are used in. 
As per ODR, two definitions contained in different source files are called to be identically defined if they token-for-token identical. The tokens should have same meaning in both source files. 
Identically defined doesn’t mean character-by-character equivalence. Two definitions can have different whitespace or comments and yet be identical.

What are the advantages of using friend classes?

Friend classes are useful when a class wants to hide features from users which are needed only by another, tightly coupled class. 
Implementation details can be kept safe by providing friend status to a tightly cohesive class.

What is the use of default constructor?

It is a constructor that does not accept any parameters. 
If there is no user-defined constructor for a class, the compiler declares a default parameterless constructor called default constructor. 
It is an inline public member of its class. 
When the compiler uses this constructor to create an object – the constructor will have no constructor initializer and a null body.


0 on: "C++ Latest Interview Questions and Answers."