C++



1. What is C++ Programming Language mean?  

C++ is an object oriented programming (OOP) language, developed by Bjarne Stroustrup, and is an extension of C language. It is therefore possible to code C++ in a "C style" or "object-oriented style." In certain scenarios, it can be coded in either way and is thus an effective example of a hybrid language.

C++ is a general purpose object oriented programming language. It is considered to be an intermediate level language, as it encapsulates both high and low level language features. Initially, the language was called 'C with classes' as it had all properties of C language with an additional concept of 'classes'. However, it was renamed to C++ in 1983.

It is pronounced "C-Plus-Plus".


2. Why main function is special in C++?  

Whenever a C++ program is executed, execution of the program starts and ends at main(). The main is the driver function of the program. If it is not present in a program, no execution can take place.



3. What is difference between C and C++?  

  1. C++ is Multi-Paradigm ( not pure OOP, supports both procedural and object oriented) while C follows procedural style programming.
  2. In C data security is less, but in C++ you can use modifiers for your class members to make it inaccessible from outside.
  3. C follows top-down approach ( solution is created in step by step manner, like each step is processed into details as we proceed ) but C++ follows a bottom-up approach ( where base elements are established first and are linked to make complex solutions ).
  4. C++ supports function overloading while C does not support it.
  5. C++ allows use of functions in structures, but C does not permit that.
  6. C++ supports reference variables ( two variables can point to same memory location ). C does not support this.
  7. C does not have a built in exception handling framework, though we can emulate it with other mechanism. C++ directly supports exception handling, which makes life of developer easy.


4. What is a class?  

Class defines a datatype, it's type definition of category of thing(s). But a class actually does not define the data, it just specifies the structure of data. To use them you need to create objects out of the class. Class can be considered as a blueprint of a building, you can not stay inside blueprint of building, you need to construct building(s) out of that plan. You can create any number of buildings from the blueprint, similarly you can create any number of objects from a class.
class Vehicle
{
    public:
        int numberOfTyres;
        double engineCapacity;
        void drive(){
            // code to drive the car
        }
};



5. What is run-time error, logical error and syntax error?  

Syntax error - The errors which are traced by the compiler during compilation, due to wrong grammar for the language used in the program, are called syntax errors.

For example, cin<

Run time Error - The errors encountered during execution of the program, due to unexpected input or output are called run-time error.

For example - a=n/0; // division by zero

Logical Error - These errors are encountered when the program does not give the desired output, due to wrong logic of the program.

For example : remainder = a+b // instead of using % operator + operator is used.



6. What is an Object/Instance?  

Object is the instance of a class, which is concrete. From the above example, we can create instance of class Vehicle as given below
Vehicle vehicleObject;
We can have different objects of the class Vehicle, for example we can have Vehicle objects with 2 tyres, 4 tyres etc. Similarly different engine capacities as well.


7. What is the role of #include directive in C++?  

The preprocessor directive #include tells the complier to insert another file into your source file. In effect, #include directive is replaced by the contents of the file indicated.



8. What is compiler and linker?  

Compiler - It is a program which converts the program written in a programming language to a program in machine language.

Linker - It is a program which links a complied program to the necessary library routines, to make it an executable program.



9. Why is char often treated as integer data type in C++?  

The memory implementation of char data type is in terms of the number code. Therefore, it is said to be another integer data type.



10. What is type conversion in C++?  

When two operands of different data types are encountered in the same expression, the variable of lower data type is automatically converted to the data tpes of variable with higher data type, and then the expression is calculated.

For example: int a=98; float b=5; cout<<a/3.0; //converts to float type, since 3.0 is of float type.

cout<<a/b; // converts a temporarily to float type, since b is of float type, and gives the result 19.6.


11. What is type casting in C++?  

Type casting refers to the data type conversions specified by the programmer, as opposed to the automatic type conversions. This can be done when the compiler does not do the conversions automatically. Type casting can be done to higher or lower data type.

For example : cout<<(float)12/5; //displays 2.4, since 12 is converted to float type.



12. What is the effect of absence of break in switch case in C++?  

The break keyword causes the entire switch statement to exit, and the control is passed to statement following the switch.. case construct. Without break, the control passes to the statements for the next case. The break statement is optional in switch..case construct.



13. In control structure switch-case what is the purpose of default in C++?  

This keyword gives the switch..case construct a way to take an action if the value of the switch variable does not match with any of the case constants. No break statement is necessary after default case, since the control is already at the end of switch..case construct. The default is optional in case of switch..case construct.



14. What is the difference between while and do-while loop?  

While is an Entry Controlled Loop, the body of the loop may not execute even once if the test expression evaluates to be false the first time, whereas in do..while, the loop is executed at least once whether the condition holds true the first time or not.



15. What is the difference between call by value and call by reference in a user defined function in C++?  

The value of the actual parameters in the calling function do not get affected when the arguments are passed using call by value method, since actual and formal parameters have different memory locations.

The values of the formal parameters affect the values of actual parameters in the calling function, when the arguments are passed using call by reference method. This happens since the formal parameters are not allocated any memory, but they refer to the memory locations of their corresponding actual parameters.



16. What is preprocessor directive?  

A preprocessor directive is an instruction to the complier itself. A part of compiler called preprocessor deals with these directives, before real compilation process. # is used as preprocessor directive in C++.



17. What is the difference between local variable and global variable?  

Local variables are those variables which are declared within a function or a compound statement and these variables can only be used within that function/scope. They cannot be accessed from outside the function or a scope of it's declaration. This means that we can have variables with the same names in different functions/scope. Local variables are local to the function/scope in which they are declared.

Global variables are those variables which are declared in the beginning of the program. They are not declared within a function. So,these variables can be accessed by any function of the program. So,global variables are global to all the functions of the program.



18. What is the role of #define in C++?  

It is a preprocessor directive to define a macro in a C++ program. Macros provide a mechanism for token replacement with or without a set of formal, function line parameters.

For example :
#define PIE 3.1416
#define AVG(A,B,C) (A+B+C)/3


19. What are the major differences between Object Oriented Programming and Procedural Programming?  

Object Oriented Programming
*Emphasis on data
*Follow bottom up approach in program design
*Concept of Data hiding prevents accidental change in the data
*Polymorphism, inheritance, Data Encapsulation possible

Procedural Programming
*Emphasis on doing things (function)
*Follow top-down approach in program design
*Due to presence of global variables, there are possibilities of accidental change in data.


20. What do you mean by C++ access specifiers?  

Access specifiers are used to define how the members (functions and variables) can be accessed outside the class. There are three access specifiers defined which are public, private, and protected.

private:
Members declared as private are accessible only with in the same class and they cannot be accessed outside the class they are declared.

public:
Members declared as public are accessible from any where.

protected:
Members declared as protected can not be accessed from outside the class except a child class. This access specifier has significance in the context of inheritance.


.Net Interview Question

PHP Interview Question

Java Interview Question

AngularJS Interview Questions