Top 40 Jenkins Interview Questions And Answers For Freshers/Experienced
If you are looking for a career in software development, then Jenkins is definitely worth exploring. This widely used …
As C++ is an important programming language, we have provided C++ Interview Questions For freshers and experienced professionals who are preparing for a job interview. Here you can go through the Top C++ Interview questions and answers and prepare well for the interview. Here we have covered basic, intermediate, and advanced interview questions with coding examples to make you properly understand the question and concept so learn from these questions and prepare yourself.
About C++: It is a cross-platform programming language that is used to create high-performance applications. Bjarne Stroustrup developed C++, as a superset of the C language. It provides a high level of control to developers over system resources and memory.
2. Name different data types present in C++?
3. Differentiate between C and C++?
4. Define class and object in C++?
5. Differentiate between Structure and class?
6. Explain operator overloading?
7. Explain polymorphism in C++?
8. What is a constructor in C++?
9. Explain the virtual function
10. Explain friend class and friend function?
11. Explain C++ access specifiers?
12. Explain the inline function
13. Explain a reference in C++?
14. Explain abstraction in C++?
15. Tell whether the deconstructor overloading is possible or not?
16. What do you understand by call by reference and call by value?
17. Explain abstract class and its use?
18. Explain destructors in C++?
19. Explain static members and their functions?
21. Explain a copy constructor?
22. Differentiate shallow copy and deep copy?
23. Explain virtual functions and pure virtual functions?
24. Tell the function of the keyword "Auto"?
25. Can a virtual function be called from a constructor?
28. Show the code of allocating and deallocating memory in C++?
29. Explain a token in C++ with examples
33. Explain the function of the keyword “Volatile”?
34. Explain and name some storage classes in C++?
35. Are we able to have a recursive inline function in C++?
37. How different is function overloading from operator overloading?
38. Can a C++ program be compiled without the main() function?
40. Can we overload a destructor?
41. Explain the default constructor?
42. Can we give one default constructor for our class?
43. Differentiate between C++ and Java
44. Explain Static member in C++?
45. Explain the Reference variable?
46. Explain mutable storage class specifier?
47. Do we have String primitive data type in C++?
48. Can access specifiers be used to achieving data hiding in C++?
49. Explain Block scope variable?
50. Tell some functions of the scope resolution operator?
2. Name different data types present in C++?
3. Differentiate between C and C++?
4. Define class and object in C++?
It is a user-defined data type that has data and member functions. Data members manage the data variables and member functions perform operations on these variables.
An object is an instance of a class. As we know that class is a user-defined data type so an object is called a variable of that data type.
A class is defined as-
class A{
Private:
int data;
Public:
void fun(){
}
};
5. Differentiate between Structure and class?
6. Explain operator overloading?
It is a very important element that runs the operations on user-defined data types. By using operator overloading you can change the default meaning to the operators like +, -, *, /, <=, etc.
For example:
By using operator overloading, Adding two complex numbers:
class complex{
Private:
float r, i;
Public:
complex(float r, float i){
this->r=r;
this->i=i;
}
complex(){}
void displaydata(){
cout«”real part = “«r«endl;
cout«”imaginary part = “«i«endl;
}
complex operator+(complex c){
return complex(r+c.r, i+c.i);
}
};
int main(){
complex a(2,3);
complex b(3,4);
complex c=a+b;
c.displaydata();
return 0;
}
7. Explain polymorphism in C++?
Polymorphism defines having many forms and different situations bring different behavior. And this happens when we have many classes that are co-related by inheritance.
Polymorphism types in C++ are:
8. What is a constructor in C++?
It is a member function that automatically executes when an object is made. Constructors and the class member have the same name so the compiler knows that the specific member function is a constructor. Constructors use no return type.
Example:
class A{
Private:
int val;
Public:
A(int x){ //one argument constructor
val=x;
}
A(){ //zero argument constructor
}
}
int main(){
A a(3);
return 0;
}
9. Explain the virtual function
10. Explain friend class and friend function?
A friend class can acquire protected, private, and public members of different classes in which it is claimed as friends. Same as friend class, friend function can also acquire protected, private, and public members but, friend functions are not member functions. For example -
class A{
private:
int data_a;
public:
A(int x){
data_a=x;
}
friend int fun(A, B);
}
class B{
private:
int data_b;
public:
A(int x){
data_b=x;
}
friend int fun(A, B);
}
int fun(A a, B b){
return a.data_a+b.data_b;
}
int main(){
A a(10);
B b(20);
cout«fun(a,b)«endl;
return 0;
}
11. Explain C++ access specifiers?
12. Explain the inline function
13. Explain a reference in C++?
A reference is another name of an already present variable or like a pointer. Once a reference name is set with a variable, then a variable is accessible by the reference or variable name.
For example-
int x=10;
int &ref=x; //reference variable
If we modify the value of ref it will be replicated in x. Once a reference variable is set it cannot denote to any other variable.
14. Explain abstraction in C++?
15. Tell whether the deconstructor overloading is possible or not?
16. What do you understand by call by reference and call by value?
call by reference: Here, we pass the address of the variable through which we are able to access the actual argument used by a function call. So modification in the parameter changes the passing argument.
call by value: Here, we permit a copy of the parameter that is passed to the functions. For the copied values a new memory is allocated and modifications made to these values do not show the variable in the main function.
17. Explain abstract class and its use?
18. Explain destructors in C++?
A constructor is itself called when an object is created. Likewise, when an object demolishes a function, the destructor itself gets called. A constructor and destructor have the same name but are preceded by a tilde.
Example:
class A{
private:
int val;
public:
A(int x){
val=x;
}
A(){
}
~A(){ //destructor
}
}
int main(){
A a(3);
return 0;
}
19. Explain static members and their functions?
When a class variable is stated static, then a specific space for it is assigned for the lifetime of the program. There is only one copy of the static member irrelevant of how many objects of that class have been made. So, all the objects of that class can access the static member.
If there is no object in the class then also a static member function can be called and accessed by only using the class name and the scope resolution operator::
21. Explain a copy constructor?
If a member function initializes an object by using different objects of the same class then it is called a copy constructor
Example-
class A{
int x,y;
A(int x, int y){
this->x=x;
this->y=y;
}
};
int main(){
A a1(2,3);
A a2=a1; //default copy constructor is called
return 0;
}
22. Differentiate shallow copy and deep copy?
23. Explain virtual functions and pure virtual functions?
Virtual function: It is a base class member function that redefines in a derived class and is declared by using the virtual keyword.
Example-
class base{
public:
virtual void fun(){
}
};
Pure virtual function: A function that has no implementation and is declared by assigning 0 makes a pure virtual function, also it has nobody.
Example-
class base{
public:
virtual void fun()=0;
};
24. Tell the function of the keyword "Auto"?
25. Can a virtual function be called from a constructor?
Yes, we are able to call a virtual function from a constructor. But the behavior is a little different in this case. When a virtual function is called, the virtual call is resolved at runtime. It is always the member function of the current class that gets called. That is the virtual machine doesn’t work within the constructor.
For example-
class base{
private:
int value;
public:
base(int x){
value=x;
}
virtual void fun(){
}
}
class derived{
private:
int a;
public:
derived(int x, int y):base(x){
base *b;
b=this;
b->fun(); //calls derived::fun()
}
void fun(){
cout«”fun inside derived class”«endl;
}
}
It is a pointer that has no data type. It can hold addresses of any type.
For example-
void *ptr;
char *str;
p=str; // no error
str=p; // error because of type mismatch
As we know that we can allocate a pointer of any type to a void pointer but the inverse is not true unless you write it as
str=(char*) ptr;
Every object member functions have a pointer that indicates to the object itself. The value is set to the address of the object for which it is called. It can access data in the object it points to.
Example
class A{
private:
int value;
public:
void setvalue(int x){
this->value=x;
}
};
int main(){
A a;
a.setvalue(5);
return 0;
}
28. Show the code of allocating and deallocating memory in C++?
A new operator allocates the memory and deletes the operator deallocates the memory in C++.
For example-
int value=new int; //allocates memory for storing 1 integer
delete value; // deallocates memory taken by value
int *arr=new int[10]; //allocates memory for storing 10 int
delete []arr; // deallocates memory occupied by arr
29. Explain a token in C++ with examples
A token is a name provided to the different functions in C++ programs. Examples of tokens can be a keyword, string, symbol, identifier, literal, constant, etc. The token code in C++ other than C, is displayed as.
asm bool catch class
const_cast delete dynamic_cast explicit
export false friend inline
mutable namespace new operator
private protected public reinterpret_cast
static_cast template this throw
true try typeid typename
using virtual wchar_t
33. Explain the function of the keyword “Volatile”?
34. Explain and name some storage classes in C++?
35. Are we able to have a recursive inline function in C++?
37. How different is function overloading from operator overloading?
38. Can a C++ program be compiled without the main() function?
40. Can we overload a destructor?
41. Explain the default constructor?
The compiler arranges a constructor for every class if a provider is not able to offer the same. This is when the developer gives no particular parameters to the constructor then it is called a default constructor. It is displayed as.
// Cpp program to illustrate the
// concept of Constructors
#include
using namespace std;
class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout « “a: " « c.a « endl
« “b: " « c.b;
return 1;
}
42. Can we give one default constructor for our class?
43. Differentiate between C++ and Java
44. Explain Static member in C++?
45. Explain the Reference variable?
It is the name provided to the present variables. The variable name and reference variable point share a similar memory through which it can update the original variable. The code can give you an example.
#include
using namespace std;
int main()
{
int x = 10;
// ref is a reference to x.
int& ref = x;
// Value of x is now changed to 20
ref = 20;
cout « “x = " « x « endl ;
// Value of x is now changed to 30
x = 30;
cout « “ref = " « ref « endl ;
return 0;
}
46. Explain mutable storage class specifier?
47. Do we have String primitive data type in C++?
48. Can access specifiers be used to achieving data hiding in C++?
49. Explain Block scope variable?
50. Tell some functions of the scope resolution operator?
The code of the scope resolution operator are as follows.
#include
using namespace std;
int my_var = 0;
int main(void) {
int my_var = 0;
::my_var = 1; // set global my_var to 1
my_var = 2; // set local my_var to 2
cout « ::my_var « “, " « my_var;
return 0;
}
It is one of the popular languages that have high demand and vacancies. To get the job you really need to be well-prepared for the interview and the above given C++ interview questions will surely help you.
Want to prepare for these languages:
If you are looking for a career in software development, then Jenkins is definitely worth exploring. This widely used …
In this post, we will cover a few Linux interview questions and their answers. So, let’s get started. In this …