0% found this document useful (0 votes)
5 views16 pages

C++ Interview Questions and Concepts

Uploaded by

keerthi M S
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

C++ Interview Questions and Concepts

Uploaded by

keerthi M S
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Thanks for joining interview call.. Hope you are doing good..

1. Tell me about yourself.


2. How do you rate yourself in C++ in 1 to 5 range
3. Agile process.. how are they doing it
4. What is the difference between an Object and a Class?
5. Answer: Class is a blueprint of a project or problem to be solved and consists of
variables and methods. These are called the members of the class. We cannot
access methods or variables of the class on its own unless they are declared
static.
6. In order to access the class members and put them to use, we should create an
instance of a class which is called an Object. The class has an unlimited lifetime
whereas an object has a limited lifespan only.

Default constructors, parametrized constructors.


Overloaded constructors essentially have the same name (name of the
class) and different number of arguments. A constructor is called depending
upon the number and type of arguments passed. While creating the object,
arguments must be passed to let compiler know, which constructor needs to
be called.

What Is The Difference Between A Copy Constructor And


Assignment Operator?

class Test
{
public:
Test() {}
Test(const Test &t)
{
cout<<"Copy constructor called "<<endl;
}

Test& operator = (const Test &t)


{
cout<<"Assignment operator called "<<endl;
return *this;
}
};

int main()

Test t1, t2;


t2 = t1;

Test t3 = t1;

return 0;
}
Assignment operator called
Copy constructor called- for new object

Answer :
A copy constructor is used to declare and initialize an object from another object.
A Myobj;
[Link] = 345;
A Anotherobj = A( Myobj );

- An assignment operator doesnot invoke the copy constructor. It simply assigns the
values of an object to another, member by member.

class A{

int a;

public:

A(int i){

a = i;

void assign(int i){

a = i;

int return_value(){

return a;

}
};

int main(int argc, char const *argv[])

A obj;

[Link](5);

cout<<obj.return_value();
}

Compilation error
Explanation: As we have defined a constructor which takes an int parameter,
so when we are trying to declare an object obj of class A without supplying
any parameter then as a constructor is overwritten it will give an error saying
that no matching function found.

class A{

int a;

A(){

a = 5;

public:

void assign(int i){

a = i;

int return_value(){
return a;

};

int main(int argc, char const *argv[])

A obj;

[Link](10);

cout<<obj.return_value();
}

C Here the constructor is made private


Access specifier

Explain overloading and Overriding.


Polymorphism

At runtime : This is called late binding.

In case of virtual function calls using a Base reference, as in shown in the example of
question no: 2, compiler does not know which method will get called at run time. In
this case compiler will replace the reference with code to get the address of function
at runtime.
Dynamic binding is another name for late binding.

Define VPTR.

The address of the VTABLE stored in the object is known as VPTR.

using namespace std;


class Base {
public:
virtual Base() {}
};
int main() {
return 0;
}
Virtual constructor

Can I Overload Destructor?


Answer :
No

Virtual destructor ad constructor

Explain Multiple and multi level inheritence


In what circumstance will the Diamond problem arise?

What is the Diamond problem? How can we get around it?

The diamond problem refers to an issue when a base class is inherited by two subclasses,
and both subclasses are inherited by a fourth class. When this happens, we need to give
the compiler a bit of guidance about the exact structure of inheritance we want.

Questions about sizeof. If you don't get sizeof, you won't be able to do pointer arithmetic,
and nearly all modern C programs do at least some pointer arithmetic.

Can static functions be virtual in C+


+?
In C++, a static member function of a class cannot be virtual. For example,
below program gives compilation error.

What is Advantage and Use of THIS pointer in C++


Can we use THIS Pointer in static function

What is diff between malloc()/free() and new/delete?

• The malloc allocates memory for object in the heap but not invokes object's constructor for
initiallizing the object.

• new also allocates the memory and also invokes constructor to initialize the object.

Difference between delete and free.

Delete invokes destructor, free will not

What if memory allocation using new fails in C++ how to handle


Exception
Due to memory leak in C C++ project what issue you have faced
if we dynamically allocate memory on heap using new operator or malloc() function and
forget to de-allocate the previously allocated memory, then there will be a memory leak problem in
the program or application.

Your program crashed. How do you go about figuring out why it crashed and
how to fix it?

Write C++ algorithm to get previous date of given date:

1. Check the months, which have 31 days in last month, assign 31 to the day and
subtract month by 1.
2. If the month is 3 (March), check the leap year condition and assign 28 or 29 based
on year to day and subtract month by 1.
3. If the month is 1 (January) then assigns 31 to the day and assign 12 (December) to
the month, subtract year by 1.
4. If the month is 2 (February) then assign 31 to the day and subtract month by 1.
5. And for other months define 30 to the days and subtract month by 1.

if(*day==1)
{
//months which have 30 days in previous month
if(*month==4|| *month==6|| *month==9||
*month==11)
{
*day=31;
*month = *month -1;
}
//for MARCH, to define february last day
else if(*month==3)
{
if(*year%4==0)
*day=29;
else
*day=28;

*month = *month -1;


}
//for January, to define December last day
else if(*month==1)
{
*day=31;
*month = 12;
*year = *year - 1 ;
}
//for Feb, to define January last day
else if(*month==2)
{
*day=31;
*month = *month -1;
}
//for other months
else
{
*day=30;
*month = *month -1;
}
}
//other days of month
else
{
*day = *day-1;
}

Can I Use This Pointer In The Constructor?


Answer :
Yes, but try to avoid calling virtual function from the constructor and
passing this pointer from the initialization list to other classes.

Explain what is upcasting in C++?

Upcasting is the act of converting a sub class references or pointer into its super
class reference or pointer is called upcasting.

A cast is a special operator that forces one data type to be converted into another.
When is dynamic_cast is used

A dynamic_cast performs casts on polymorphic types and can cast a A* pointer into a B*
pointer only if the object being pointed to actually is a B object.

7. -only within the context of a class hierarchy. It can be used to cast the
pointer of a class into one of its subclasses. It is also capable of casting
class references in the same way.

The reinterpret_cast operator changes a pointer to any other type of pointer. It also allows
casting from pointer to an integer type and vice versa.

It is considered dangerous to use reinterpret_cast

because the compiler makes the assumption that the programmer knows
exactly what to expect. There is no compile-time or run-time validity check
performed. This is similar to the original C-style casting.*****

The static_cast operator performs a nonpolymorphic cast. For example, it can be


used to cast a base class pointer into a derived class pointer.

Name types of Design Patterns?


Design patterns can be classified in three categories: Creational, Structural and
Behavioral patterns.
 Creational Patterns - These design patterns provide a way to create objects
while hiding the creation logic, rather than instantiating objects directly using
new opreator. This gives program more flexibility in deciding which objects
need to be created for a given use case.
In Factory pattern, we create object without exposing the creation logic to the client
and refer to newly created object using a common interface
Singleton pattern involves a single class which is responsible to create an object
while making sure that only single object gets created.
Can we create a clone of a singleton object?
Yes.
How to prevent cloning of a singleton object?
Throw exception within the body of clone() method.

 Structural Patterns - These design patterns concern class and object


composition. Concept of inheritance is used to compose interfaces and define
ways to compose objects to obtain new functionalities.
 Behavioral Patterns - These design patterns are specifically concerned with
communication between objects.

Why can’t we use a static class instead of singleton?


One of the key advantage of singleton over static class is that it can implement interfaces

and extend classes while the static class cannot (it can extend classes, but it does not

inherit their instance members).

Singleton object stores in Heap but, static object stores in stack.

We can clone the object of Singleton but, we can not clone the static class object.

C++ END

DATA STRUCTURE

Difference Between Linked List and Array Data Structure

How do you find middle element of a linked list in a single pass?

linked list is 1->2->3->4->5 then output should be 3


To answer this programming question I would say you start with a simple solution on

which you traverse the LinkedList until you find the tail of linked list where it points to

null to find the length of the linked list and then reiterating till middle.

After this answer interviewer will ask you to find the middle element in single pass and

there you can explain that by doing space-time trade-off you can use two pointers

one incrementing one step at a time and other incrementing two-step a time, so

when the first pointer reaches end of linked second pointer will point to the middle

element.

#include <iostream>

int main(int argc, const char * argv[]) {


int a[] = {1, 2, 3, 4, 5, 6};
std::cout << (1 + 3)[a] - a[0] + (a + 1)[2];

5-1+4

#include <iostream.h>
int globalVar = 2;
int main()
{
int globalVar = 5;
cout<<globalVar<<endl;
}

When there are a Global variable and Local variable with the same name, how will
you access the global variable?

#include<iostream.h>

int x= 10;

int main()
{

int x= 2;

cout<<”Global Variable x = “<<::x;

cout<<”\nlocal Variable x= “<<x;

Explain Pass by Value and Pass by Reference.

What is wrong with this code?


T *p = new T[10];
delete p;

T *p = 0;
delete p;

The above code is syntactically correct and will compile [Link] only problem is
that it will just delete the first element of the array.

What is wrong with this code?


T *p = 0;
delete p;
Answer: In the above code, the pointer is a null pointer. Hence automatically, the
program will crash in an attempt to delete the null pointer.

What is dangling pointer in C?

Answer:
Generally, daggling pointers arise when the referencing object is
deleted or deallocated, without changing the value of the pointers.

What is the difference between an uninitialized pointer and


a null pointer?

Answer:
An uninitialized pointer is a pointer that points unknown memory
location. The behavior of the uninitialized pointer is undefined. If you
try to dereference the uninitialized pointer code behavior will
undefine.

When you try to dereference the null pointer then your code will
crash.

What is the stack overflow?

Answer:
If your program tries to access beyond the limit of the available stack
memory then stack overflow occurs. In other words, you can say that
a stack overflow occurs if the call stack pointer exceeds the stack
boundary.

If stack overflow occurs, the program can crash or you can say that
segmentation fault that is the result of the stack overflow.

Which of the following statements is correct?


A. Base class pointer cannot point to derived class.

B. Derived class pointer cannot point to base class.

C. Pointer to derived class cannot be created.

D. Pointer to base class cannot be created.


Answer: Option B
Which of the following problem causes an exception?

Which of the following problem causes an exception?

A. Missing semicolon in statement in main().

B.A problem in calling function.

C.A syntax error.

D.A run-time error.

Write an algorithm to get sum of all positive integers in a sentence.


Sample string:

CString str = “ There are 22 chairs, 45 desks, 2 blackboard and 4 fans”


73

API and MICROSERVICES

What is your understanding of what are RESTful web services?

REST, Representational State Transfer

also based on stateless client-server style architecture, which can be easily accessed
over the network and is identified by URIs i.e. Uniform Resource Identifier.

web services that use the HTTP method and are based on the architecture of REST.

The important aspects of this implementation include:


 Resources
 Request Headers
 Request Body
 Response Body
 Status codes

RESTFUL is referred for web services written by applying REST architectural


concept are called RESTful services

Enlist some of the HTTP methods with description.


Answer: Enlisted below is the list of HTTP methods with their descriptions:
GET
PUT
POST
DELETE
OPTIONS

 GET: This is a read-only operation that fetches the list of users on the server.
 PUT: This operation is used for the creation of any new resource on the server.
 POST: This operation is used for updating an old resource or for creating a new
resource.
 DELETE: As the name suggests, this operation is used for deleting any resource
on the server.
 OPTIONS: This operation fetches the list of any supported options of resources
that are available on the server.

What is the difference between the PUT method and the POST method?

Answer: The major difference between the PUT and POST method is that

the result generated with the PUT method is always the same no matter how many
times the operation is performed.

On the other hand, the result generated by POST operation is always different every
time.

Mention whether you can use GET request instead of PUT to create a
resource?

No, you are not supposed to use PUT for GET. GET operations should only have
view rights, while PUT resource is used for updating a data.

What is the purpose of HTTP Status Code?


HTTP Status code are standard codes and refers to predefined status of task done
at server. For example, HTTP Status 404 states that requested resource is not
present on server.

What are HTTP status codes? Enlist few with meaning.

Some of the HTTP status codes with their meaning are as follows:
 Code 200: This indicates success.
 Code 201: This indicates resource has been successfully created.
 Code 204: This indicates that there is no content in the response body.
 Code 404: This indicates that there is no method available.

Some of the disadvantages of REST are:

 Since there is no contract defined between service and client, it has to be


communicated through other means such as documentation or emails.
 Since it works on HTTP, there can’t be asynchronous calls.
 Sessions can’t be maintained.

You might also like