0% found this document useful (0 votes)
6 views

CPP Interview Ques

The document discusses various C++ concepts like differences between C and C++, namespaces, using namespace std, inputting strings, scope resolution operator, virtual functions, this pointer, templates, functions, destructors, type casting, streams, structures vs classes, clearing screen, iostream header file, goto statement, bool data type, iterators, enums, endl, operator overloading, including all libraries, character constants, segmentation fault, tokens, delete vs delete [], access specifiers, arrays vs lists, new vs malloc, friend functions, constructors, function overloading vs operator overloading, and copy constructors.

Uploaded by

Selina Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CPP Interview Ques

The document discusses various C++ concepts like differences between C and C++, namespaces, using namespace std, inputting strings, scope resolution operator, virtual functions, this pointer, templates, functions, destructors, type casting, streams, structures vs classes, clearing screen, iostream header file, goto statement, bool data type, iterators, enums, endl, operator overloading, including all libraries, character constants, segmentation fault, tokens, delete vs delete [], access specifiers, arrays vs lists, new vs malloc, friend functions, constructors, function overloading vs operator overloading, and copy constructors.

Uploaded by

Selina Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C++:

What are the differences between C and C++?


1) C++ is a kind of superset of C, most of C programs except few exceptions (See this and this)
work in C++ as well.
2) C is a procedural programming language, but C++ supports both procedural and Object
Oriented programming.
3) Since C++ supports object oriented programming, it supports features like function
overloading, templates, inheritance, virtual functions, friend functions. These features are absent
in C.

Define namespace in C++.

The namespace is a logical division of the code which is designed to stop the naming conflict.
The namespace starts with the keyword “namespace”. The syntax for the same is as follows:

namespace namespace_name {

// code declarations

What is using namespace std in C++?

Using namespace std in C++ tells the compiler that you will be making use of the name space
called ‘std’. The ‘std’ namespace contains all the features of the standard library. You need to
put this statement at the start of all your C++ codes if you don’t want to keep on writing std::
infront of every variable/string or whatever standard library feature you are making use of, as it
becomes tedious to do so.

How to input string in C++?


There are three ways to input a string, using cin, get, and getline. All three methods are
mentioned in the sample program below.

What is scope resolution operator in C++?

A scope resolution operator(::) is used to define the member function outside the class.

Scope resolution operator in c++ is denoted by double colon (::). It can be used:

– when there is a local variable with same name as of global variable


– When a function has to be defined outside a class
– When class’s static variables needs to be accessed
– When a class inside another class has to be referred
– In case of multiple Inheritance

Define 'std'.

Std is the default namespace standard used in C++.

What are virtual functions – Write an example?


Virtual functions are used with inheritance, they are called according to the type of the object
pointed or referred to, not according to the type of pointer or reference. In other words, virtual
functions are resolved late, at runtime. The virtual keyword is used to make a function virtual.

What is this pointer?


The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is
available as a local variable within the body of all nonstatic functions.

free( ) is used on resources allocated by malloc( ), or calloc( ) in C


Delete is used on resources allocated by new in C++

What is template in C++?


A template in C++ is used to pass data types as parameters . These make it easier and more
simpler to use classes and functions.

template <typename T>

int fun (T a,T b)

return (a+b);

What is function in C++?

A function in C++ is a block of code that can be referenced from anywhere in the system and
that serves a specific purpose.

What is destructor in C++?

Destructors in c++ are special function/methods that are used to remove memory allocation for
objects. They are called usually when the scope of an object ends. eg. when a function ends you
can call a destructor. They are of the same name as the class – syntax – ~<classname>();

How to convert integer to string in C++?

int n= 1; string s= to_string(n); cout << s;

What is stl in C++?

Stl is the standard template library. It is a library that allows you to use a standard set of
templates for things such as: Algorithms, functions, Iterators in place of actual code.

queue<int> Q;
for(k=0;k<10;k++)

Q.push(k);

What is type casting in C++?

Type casting in C is used to change the data type. They are of two types: Implicit Type
Conversion: It is automatic. Explicit Type Conversion: It is user-defined.

What is stream in C++?

Stream refers to a stream of characters to be transferred between program thread and i/o.

What is the difference between structure and class in C++?

The difference between structure and class is as follows:


– By default, the data members of class are private whereas data members of structure are
public.
– While implementing inheritance, the access specifier for struct is public whereas for class its
private.
– Structures do not have data hiding features whereas class does.

How to clear screen in C++?

One can clear screen using – clrscr() or system(“clear”).

What is iostream in C++?

It is a header file that includes basic objects such as cin, cout, cerr, clog.

28. How to give space in C++?

In C++ programming, the space can be given using the following code.
cout << ” ” ;

How to use goto statement in C++ ?

Goto statement provided unconditional jump in the code.

Which operator cannot be overloaded in C++ ?


Some of the operators that cannot be overloaded are as follows:

– Dot operator- “.”


– Scope resolution operator- “::”
– “sizeof” operator
– Pointer to member operator- “.*”

What is bool in C++?

Bool is a data type in C++ which takes two values- True and False. Syntax is as follows:
bool b1 = true;

How to set decimal places in C++ ?

For limiting the decimal places in C++ there are five functions : floor(), ceil(), trunc(), round() and
setprecision(). Out of these five, only setprecision() function is used for setting the decimal
places to put as output.

How to convert char to int in C++ ?

There are three methods for converting char variable to int type variable. These are as follows: –
atoi()
– sscanf()
– typecasting

c = (int)(d); // Using typecasting printf("c : %d", c);

What is conio.h in C++?

Conio.h is a header file used for console input and output operations and is used for creating
text based user interfaces.

What is iterator in C++?

Any object which has an ability to iterate through elements of the range it has been pointing to
is called iterator.

49. What is enum in C++?

enum is abbreviation of Enumeration which assigns names to integer constant to make a


program easy to read. Syntax for the same:

enum enum_name{const1, const2, ……. };

50. What is endl in C++?


Endl is a predefined object of ostream class to insert a new line characters.

Which operators can be overloaded in C++?

List of operators that can be overloaded are:

+ , – , * , / , % , ^, & , | , ~ , !, =, ++ , –, ==, != , && , ||


+= , -= , /= , %= , ^= , &=, |= , *= , = , [] , (), ->, ->* , new , new [] , delete , delete []

53. How to include all libraries in C++?

The library <bits/stdc++.h> in c++ is used to include all the libraries.

Why namespace std is used in C++?

If the program does not have using namespace std; then when you write cout <<; you would
have to put std::cout <<; same for other functions such as cin, endl etc.

75. What are character constants in C++?

Character constant are members of the character set in which a program is written which is
surrounded by single quotation marks (‘).

How to remove segmentation fault in C++?

Segmentation fault indicates an error memory corruption. In layman terms, when a piece of
code tries to do read and write operation in a read only location in memory.

Define token in C++.

A token in C++ can be a keyword, identifier, literal, constant and symbol.

How delete [] is different from delete?

Delete is used to release a unit of memory, delete[] is used to release an array.

What is the full form of STL in C++?

STL stands for Standard Template Library.

What are the C++ access specifiers?

The access specifiers are used to define how to functions and variables can be accessed outside the
class.

There are three types of access specifiers:


o Private: Functions and variables declared as private can be accessed only within the same

class, and they cannot be accessed outside the class they are declared.

o Public: Functions and variables declared under public can be accessed from anywhere.

o Protected: Functions and variables declared as protected cannot be accessed outside the

class except a child class. This specifier is generally used in inheritance

What is the difference between an array and a list?

o An Array is a collection of homogeneous elements while a list is a collection of heterogeneous

elements.

o Array memory allocation is static and continuous while List memory allocation is dynamic

and random.

What is the difference between new() and malloc()?

o new() is a preprocessor while malloc() is a function.

o There is no need to allocate the memory while using "new" but in malloc() you have to use

sizeof().

Define friend function.

Friend function acts as a friend of the class. It can access the private and protected members of the
class.

What is overloading?

o When a single object behaves in many ways is known as overloading. A single object has the

same name, but it provides different versions of the same function.

o Overloading is of two types:


What is a constructor?

A Constructor is a special method that initializes an object. Its name must be same as class name.

What is the difference between function overloading and operator overloading?

Function overloading is a feature in C++ where two or more functions can have the same
name but different type of parameters and different number of parameters.

Note: Overloading of functions with different return types are not allowed.

Operating overloading allows us to make operators work for user-defined classes. For example,
we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by
just using +.

Other example classes where arithmetic operators may be overloaded are Complex Number,
Fractional Number, Big Integer, etc.

What is a copy constructor?

A copy constructor is the constructor which take same class object reference as the parameter. It gets
automatically invoked as soon as the object is initialized with another object of the same class at the
time of its creation.

Can we make copy constructor private?


Yes, a copy constructor can be made private

What is Static Member?


Static is a keyword in C++ used to give special characteristics to an element. Static elements are
allocated storage only once in a program lifetime in static storage area. And they have a scope
till the program lifetime. Static Keyword can be used with following,

Interesting facts about Static Members Functions in C++


• static member functions do not have this pointer.
• A static member function cannot be virtual
• Member function declarations with the same name and the name parameter-
type-list cannot be overloaded if any of them is a static member function
declaration.

What is a preprocessor?

Preprocessor is a directive to the compiler to perform certain things before the actual compilation
process begins.

The while loop verifies the condition; if it’s true, then it iterates the loop till the condition becomes
false.

The do-while loop first iterates the loop body once, then it checks for the condition

What are pointers in C++?


Pointers are the variables that store the memory address of another variable. The type of the

variable must correspond with the type of pointer.

Syntax: type *name

Discuss the difference between prefix and postfix?

In prefix (++i), first, it increments the value, and then it assigns the value to the expression.

In postfix (i++), it assigns the value to the expression, and then it increments the variable's value.

Can you compile a program without the main function?

Yes, you can compile a program without the main function, but you cannot run or execute the

program because the main() function is the entry point, from where all the execution begins.

What is call by value and call by reference in C++?

In the call by value method, you pass the copies of actual parameters to the function's formal

parameters. This means if there is any change in the values inside the function, then that change

will not affect the actual values.

In the call-by-reference method, the reference or address of actual parameters is sent to the

function's formal parameters. This means any change in values inside the function will be reflected

in the actual values.

What are the differences between references and pointers?


Both references and pointers can be used to change the local variables of one function inside
another function.

References are less powerful than pointers


1) Once a reference is created, it cannot be later made to reference another object; it cannot be
reseated. This is often done with pointers.
2) References cannot be NULL. Pointers are often made NULL to indicate that they are not
pointing to any valid thing

15. What is an inline function?

An inline function when called expands in line. When you call this function, the whole code of the

inline function gets inserted or substituted at the inline function call.


The syntax for defining the function inline is:

inline return-type function-name(parameters)

// function code

C++ is a Platform dependent while Java is a platform-independent programming language. C++ is

only a compiled language while Java is both compiled and interpreted.

Java Virtual Machine is a virtual machine that enables the computer to run the Java program

Why is String class considered immutable?


The String class is immutable, so that once it is created a String object cannot be changed. Since
String is immutable it can safely be shared between many threads ,which is considered very
important for multithreaded programming.

What is an Interface?
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting
the abstract methods of the interface.

In java, both Errors and Exceptions are the subclasses of java.lang.Throwable class.

Error refers to an illegal operation performed by the user which results in the abnormal working of

the program. Stack overflow, out of memory

exceptions in java refer to an unwanted or unexpected event, which occurs during the execution of

a program. ArrayIndexOutOfBoundException

You might also like