Top 82 C++ Interview Questions
Top 82 C++ Interview Questions
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Share this:
Top 82 C++ Questions and Answers for Job Interview :
2. What is OOP?
Answer: Object-oriented programming (OOP) refers to a type of computer
programming in which programmers define not only the data type of a data structure,
but also the types of operations (functions) that can be applied to the data structure.
In this way, the data structure becomes an object that includes both data and functions.
In addition, programmers can create relationships between one object and another. For
example, objects can inherit characteristics from other objects.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
elements, and just as efficiently as in arrays. But unlike arrays, their size can change
dynamically, with their storage being handled automatically by the container.
7. What is a Constant?’
Answer: A constant, like a variable, is a memory location where a value can be
stored. Unlike variables, constants never change in value.
Examples of Constants:
75 //integer (decimal)
0113 //octal
0x4b //hexadecimal
3.142 //floating point
‘c’ //character constant
“Hello, World” //string constant
8. What is the difference between equal to (==) and Assignment Operator (=)?
Answer: The ‘=’ is the so-called assignment operator and is used to assign the result
of the expression on the right side of the operator to the variable on the left side. The
‘==’ is the so-called equality comparison operator and is used to check whether the
two expressions on both sides are equal or not.
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
• + addition
• – subtraction
• * multiplication
• / division
• % module
10. State the difference between Pre and Post Increment/Decrement Operations.
Answer:Pre increment operator is used to increment variable value by 1 before
assigning the value to the variable. Post increment operator is used to
increment variable value by 1 after assigning the value to the variable.
By default, the class members are private. So, if the visibility labels are missing then
by default all the class members are private.
• Private: Members declared as private are accessible only within the same class
and they cannot be accessed outside the class they are declared. Child classes
are also not allowed to access private members of parent.
• Public: Members declared as public are accessible from anywhere.
• Protected: Only the class and its child classes can access protected members.
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
temp.seconds += rhs.seconds;
temp.seconds %= 60;
temp.minutes += rhs.minutes;
temp.minutes %= 60;
temp.hours += rhs.hours;
return temp;
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Addition is a binary operation, which means it has two operands. In C++, the
arguments being passed are the operands, and the temp object is the returned value.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Any changes in the static data member through one-member function will reflect in all
other object’s member functions.
Declaration
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
modified by a public member function (the mutator method), which takes the desired
new value as a parameter, optionally validates it, and modifies the private member
variable.
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
• When a variable is defined as volatile, the program may not change the value
of the variable explicitly.
• But these variable values might keep on changing without any explicit
assignment by the program. These types of qualifiers are called volatile.
• For example, if global variable’s address is passed to clock routine of the
operating system to store the system time, the value in this address keep on
changing without any assignment by the program. These variables are named
as volatile variable.
• Syntax:
volatile data_type variable_name; (or) volatile data_type *variable_name;
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Answer: A shallow copy of an object copies all of the member field values. This
works well if the fields are values, but may not be what you want for fields that point
to dynamically allocated memory. The pointer will be copied. but the memory it
points to will not be copied — the field in both the original object and the copy will
then point to the same dynamically allocated memory, which is not usually what you
want. The default copy constructor and assignment operator make shallow copies.
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Reusability-> facility to use public methods of base class without rewriting the same
Extensibility-> extending the base class logic as per business logic of the derived class
Data hiding-> base class can decide to keep some data private so that it cannot be
altered by the derived class
Overriding-> With inheritance, we will be able to override the methods of the base
class so that meaningful implementation of the base class method can be designed in
the derived class.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
3) Object is also called instance in Java and every instance has different values of
instance variables. e.g. in
following code
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
}
}
Person p1 = new Person(“Rakesh”);
Person p2 = new Person(“Jimmy”);
Person p3 = new Person(“Peter”);
Here Person is a class as it defines design of Person objects i.e. How will a person
object look like, what properties it will have etc. By the way Class is declared by
keyword “class” in Java and p1, p2, p3 are different object of Person class. In natural
language you can say different person which has different names where name is a
property of Person Class. Another difference between Class and Object in Java is that
we have a class keyword to declare class in Java but there is no object keyword.
Objects are most notably created using new() operator, which calls constructor of
class to create and initialize object in Java.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
70. What is the need of a destructor? Explain with the help of an example.
Answer: – During construction of an object, resources may be allocated for use.
– For example, a constructor may have opened a file and memory area may be
allocated to it. These allocated resources must be deallocated before the object is
destroyed.
– A destructor performs all clean-up tasks like closing a file, deallocating and
releasing memory area etc. automatically.
– Example:
class X
{
int i, j;
public :
X( int m = 5, int n = 10)
{
i = m;
j = n;
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
}
~X() // destructor
{}
void print()
{
cout << i << ” ” <, j << endl;
}
};
Example:
X o1(4,5);
2. Explicit Calling:
By explicit calling, we mean that the constructor’s name is specified in the calling
statement.
General Form:
class_name object_name = constructor_name( value1, value2, …. );
Example :
X o1 = X(4,5);
72. What is the difference or relationship between a calling function and a called
function?
Answer: The called function contains the definition of the function i.e. what and how
to do.
The calling function contains the input (the actual parameters) which is given to the
called function which then works on them because it contains the definition, performs
the procedure specified and returns if anything is to be returned.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
· The rest of the identifier name can consist of letters (upper or lowercase),
underscores (‘_’) or digits (0-9).
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
· Linked lists let you insert elements at the beginning and end of the list.
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
· Extensibility — extending the base class logic as per business logic of the
derived class
· Data hiding — base class can decide to keep some data private so that it cannot
be altered by the derived class
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
· Also, with time, during maintenance adding new features both base as well as
derived classes are required to be changed. If a method signature is changed then we
will be affected in both cases (inheritance & composition)
· If a method is deleted in the “super class” or aggregate, then we will have to re-
factor in case of using that method. Here things can get a bit complicated in case of
inheritance because our programs will still compile, but the methods of the subclass
will no longer be overriding superclass methods. These methods will become
independent methods in their own right.
The “dreaded diamond” refers to a class structure in which a particular class appears
more than once in a class’s inheritance hierarchy.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
overloading allows you to define three methods with the same name and different
types of parameters to handle the array operations.
· Method overriding allows a sub class to use all the general definitions that a
super class provides and add specialized definitions through overridden methods.
3{
4 cout<<”Hello,World!”;
5 return 0;
6}
The first line that begins with “#” is a preprocessor directive. In this case, we are
using includeas a directive which tells the compiler to include a header while
“iostream.h” which will be used for basic input/output later in the program.
Next line is the “main” function that returns an Integer. The main function is the
starting point of execution for any C++ program. Irrespective of its position in the
source code file, the contents of the main function are always executed first by the
C++ compiler.
In the next line, we can see open curly braces that indicate the start of a block of a
code. After this, we see the programming instruction or the line of code that uses the
count which is the standard output stream (its definition is present in iostream.h).
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
This output stream takes a string of characters and prints it to a standard output
device. In this case it is, “Hello, World!”. Please note that each C++ instruction ends
with a semicolon (;), which is very much necessary and omitting it will result in
compilation errors.
Before closing the braces}, we see another line “return 0;”. This is the returning point
to the main function.
Every C++ program will have a basic structure as shown above with a preprocessor
directive, main function declaration followed by a block of code and then a returning
point to the main function which indicates successful execution of the program.
72. What are the Extraction and Insertion operators in C++? Explain with
examples.
Answer: In the iostream.h library of C++, cin, and cout are the two data streams that
are used for input and output respectively. Cout is normally directed to the screen and
cin is assigned to the keyboard.
“cin” (extraction operator): By using overloaded operator >> with cin stream, C++
handles the standard input.
1 int age;
2 cin>>age;
As shown in the above example, an integer variable ‘age’ is declared and then it waits
for cin (keyboard) to enter the data. “cin” processes the input only when the RETURN
key is pressed.
“cout” (insertion operator): This is used in conjunction with the overloaded <<
operator. It directs the data that followed it into the cout stream.
Example:
view source
print?
1 cout<<”Hello, World!”;
2 cout<<123;
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
3. // block of statement.
4. }
init – Initializes the variable at the beginning of the loop to some value. This value is
the starting point of the loop.
condition – Decides whether the loop will continue running or not. While this
condition is true, the loop will continue running.
increment – The part of the loop that changes the value of the variable created in the
variable declaration part of the loop. The increment statement is the part of the loop
which will eventually stop the loop from running.
Example :
1. #include <iostream>
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
6. {
7. cout<<i<<” “;
8. }
9. return 0;
10. }
While Loop :
1. while (condition)
2. {
3. statement(s);
4. }
5.
Example:
1. #include <iostream>
6. int x = 1;
7.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
9. while (x < 5)
10. {
13. x++;
14. }
15.
16. return 0;
17. }
Do While Loop:
A do-while loop is similar to a while loop, except that a do-while loop is guaranteed
to execute at least one time. The conditional expression appears at the end of the
loop, so the statement(s) in the loop execute once before the condition is tested.
Syntax:
1. do
2. {
3. statement(s);
4. } while (condition);
5.
6.
Example:
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
1. #include <iostream>
6. int a = 1;
7.
8. // do-while loop
9. do
10. {
12. a = a + 1;
15. return 0;
16. }
As you can see the advantages, you can perform bigger operation inside a loop
without writing the code many times.
74. What is a nested loop in C++? What is the concept behind a nested loop?
Answer: A nested loop is a loop inside another loop. It consists of an outer loop, and
an inner loop. In terms of the mechanics, an inner loop is no different than putting a
loop inside a code block for an if statement, for example. Here’s an example of an
outer loop and an inner loop:
1. // Loop A
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
5. // Loop B
11. }
12. else
13. {
15. }
16. }
17. printf(“\n”);
18. }
Note that both loops have the same form. There’s nothing special about Loop B. It’s
just a loop, like Loop A, except that Loop B’s behavior is changed by what changes in
Loop A (note I initialize j in Loop B to whatever i is set to in Loop A). Note that
it is important to keep each loop’s variables straight! In my case, i is for Loop A,
and j is for Loop B. Typically, this is what you’ll want to do with nested loops, have
the inner loop be affected by what’s going on with the outer loop.
What you will see with my above example is a nested sequence of numbers, with each
inner sequence starting with the current number in the outer sequence. The outer
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
sequence goes from 1 to 10, and each inner sequence goes from each number in the
outer sequence to 10.
76. What are Default Parameters? How are they evaluated in C++ function?
Answer: Default parameter is a value that is assigned to each parameter while
declaring a function.
This value is used if that parameter is left blank while calling to the function. To
specify a default value for a particular parameter, we simply assign a value to the
parameter in the function declaration.
If the value is not passed for this parameter during the function call, then the compiler
uses the default value provided. If a value is specified, then this default value is
stepped on and the passed value is used.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Each item in an array is called an element, and each element is accessed by its
numerical index. As shown in the preceding illustration, numbering begins with 0.
The 9th element, for example, would therefore be accessed at index 8.
2. It can be used to implement other data structures like linked lists, stacks,
queues, trees, graphs etc.
2. Array is static structure. It means that array is of fixed size. The memory which
is allocated to array cannot be increased or reduced.
3. Since array is of fixed size, if we allocate more memory than requirement then
the memory space will be wasted. And if we allocate less memory than requirement,
then it will create problem.
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
A ((doubly) linked) list, on the other hand, stores the items the following way: It
creates an own “list item” for each element; this “list item” holds the actual
element and a pointer/reference/hint/etc. to the next “list item”. If it is doubly linked,
it also contains a pointer/reference/hint/etc. to the previous “list item”:
This means, the elements can be spread all over the memory and are not stored at
specific memory locations.
Now that we know this, we can compare some usual operations on sequences of
elements:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
situation that our array is already completely filled (see //here comes other stuff), we
probably can’t put the new element there because there might already be something
else. So, maybe we have to copy the whole array. However, if the array is not filled,
we can simply put the element there.
Using a list, we have to generate a new “list item”, put the element into it and set
the next pointer of the currently last element to this new list item. Usually, we store a
reference to the currently last element so that we don’t have to search through the
whole list to find it. Thus, inserting a new element is no real problem with lists.
· Adding a new element somewhere in the middle: Let’s first consider arrays:
here, we might get into the following situation: We have an array with elements 1 to
1000:
1 | 2 | 3 | 4 | 5 | 6 | … | 999 | 1000 | free | free
Now we consider, how a list can perform this task: Let’s say we have currently the
following list:
1 -> 2 -> 3 -> 4 -> 5 -> … -> 999 -> 1000Again, we want to
insert 4.5 between 4 and 5. This can be done very easily: We generate a new list item
and update the pointers/references:
1 -> 2 -> 3 -> 4 5 -> … -> 999 -> 1000 | ^ +-> 4.5 -+We
have simply created a new list element and generated sort of “bypass” to insert it –
very cheap (as long as we have a pointer/reference to the list item the new element
will be inserted after).
So, let’s sum up: Linked lists are really nice when it comes to inserting at random
positions (as long as you have a pointer to the adequate list item). If your operation
involves adding lots of elements dynamically and traversing all elements anyway, a
list might be a good choice.
An array is very good when it comes to index accesses. If your application needs to
access elements at specific positions very often, you should rather use an array.
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials