CPP Interview Ques
CPP Interview Ques
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
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.
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:
Define 'std'.
return (a+b);
A function in C++ is a block of code that can be referenced from anywhere in the system and
that serves a specific purpose.
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>();
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);
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.
Stream refers to a stream of characters to be transferred between program thread and i/o.
It is a header file that includes basic objects such as cin, cout, cerr, clog.
In C++ programming, the space can be given using the following code.
cout << ” ” ;
Bool is a data type in C++ which takes two values- True and False. Syntax is as follows:
bool b1 = true;
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.
There are three methods for converting char variable to int type variable. These are as follows: –
atoi()
– sscanf()
– typecasting
Conio.h is a header file used for console input and output operations and is used for creating
text based user interfaces.
Any object which has an ability to iterate through elements of the range it has been pointing to
is called iterator.
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.
Character constant are members of the character set in which a program is written which is
surrounded by single quotation marks (‘).
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.
The access specifiers are used to define how to functions and variables can be accessed outside the
class.
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
elements.
o Array memory allocation is static and continuous while List memory allocation is dynamic
and random.
o There is no need to allocate the memory while using "new" but in malloc() you have to use
sizeof().
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
A Constructor is a special method that initializes an object. Its name must be same as class name.
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.
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.
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
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.
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.
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
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
An inline function when called expands in line. When you call this function, the whole code of the
// function code
Java Virtual Machine is a virtual machine that enables the computer to run the Java program
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
exceptions in java refer to an unwanted or unexpected event, which occurs during the execution of
a program. ArrayIndexOutOfBoundException