C/C interview question set 2/C/C Interview Questions and Answers for Freshers & Experienced

What is the role of the Static keyword for a class member variable?

The static member variable shares a common memory across all the objects created for the respective class. We need not refer to the static member variable using an object. However, it can be accessed using the class name itself.

What is a COPY CONSTRUCTOR and when is it called?

A copy constructor is a constructor that accepts an object of the same class as its parameter and copies its data members to the object on the left part of the assignment. It is useful when we need to construct a new object of the same class.

Example:

class A{
int x; int y;
public int color;
public A() : x(0) , y(0) {} //default (no argument) constructor
public A( const A& ) ;
};
A::A( const A & p )
{
this->x = p.x;
this->y = p.y;
this->color = p.color;
}
main()
{
A Myobj;
Myobj.color = 345;
A Anotherobj = A( Myobj ); // now Anotherobj has color = 345
}

What is the difference between an Object and a Class?

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.

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.

What is Name Mangling?

C++ compiler encodes the parameter types with function/method into a unique name. This process is called name mangling. The inverse process is called as demangling.

Example:

A::b(int, long) const is mangled as ‘b__C3Ail’.

For a constructor, the method name is left out.

That is A:: A(int, long) const is mangled as ‘C3Ail’.

Explain Register Storage Specifier.

“Register” variable should be used whenever the variable is used. When a variable is declared with a “register” specifier, then the compiler gives CPU register for its storage to speed up the lookup of the variable.

What is a Storage Class? Mention the Storage Classes in C++.

Storage class determines the life or scope of symbols such as variable or functions.

C++ supports the following storage classes:

Auto
Static
Extern
Register
Mutable

What is wrong with this code?

T *p = 0;
delete p;

In the above code, the pointer is a null pointer. Per the C++ 03 standard, it’s perfectly valid to call delete on a NULL pointer. The delete operator would take care of the NULL check internally.

Explain what is COPY CONSTRUCTOR and what is it used for?

COPY CONSTRUCTOR is a technique that accepts an object of the same class and copies its data member to an object on the left part of the assignment.

Explain what is pre-processor in C++?

Pre-processors are the directives, which give instruction to the compiler to pre-process the information before actual compilation starts.

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.

Explain what is C++ exceptional handling?

The problem that arises during execution of a program is referred as exceptional handling. The exceptional handling in C++ is done by three keywords.

a). Try: It identifies a block of code for which particular exceptions will be activated
b). Catch: The catch keyword indicates the catching of an exception by an exception handler at the place in a program
c). Throw: When a problem exists while running the code, the program throws an exception

What is the C-style character string?

The string is actually a one-dimensional array of characters that is terminated by a null character ''.

For example, to type hello word

#include
Using namespace std;
int main ()
{
char greeting[6] = { 'H' , 'e' , 'l' ,'l' , 'o' , ''};
cout << "Greeting message:" ;
cout << greeting << endl;
return 0;
}

Explain what are Access specifiers in C++ class? What are the types?

Access specifiers determine the access rights for the statements or functions that follow it until the end of class or another specifier is included. Access specifiers decide how the members of the class can be accessed. There are three types of specifiers.

1. Private
2. Public
3. Protected

Define basic type of variable used for a different condition in C++?

The variable used for a different condition in C++ are

Bool: Variable to store boolean values (true or false)
Char: Variable to store character types
int : Variable with integral values
float and double: Types of variables with large and floating point values

.Explain Function overloading with a different number of arguments with an example?

C++ enables two or more functions with the same name but with different types of arguments or a different sequences of arguments or a different numbers of arguments. It is also called “Function Polymorphism”

Eg)

void display(char c, int n)

{for ( ;n>0;n–)

cout<<c<<” “;}

void display(int a, int b)

{for (;b>0;b–)

cout<<a<<” “;}

Explain the declaration of variables in C++?

Variables should be declared anywhere within the program

How To Get The Current Position Of The File Pointer?


We can get the current position of the file pointer by using the tellp( ) member function of ostream class or tellg( ) member function of istream class. These functions return (in bytes) positions of put pointer and get pointer respectively.

What is an Inline function in C++?

Inline function is a function that is compiled by the compiler as the point of calling the function and the code is substituted at that point. This makes compiling faster. This function is defined by prefixing the function prototype with the keyword “inline”.

Such functions are advantageous only when the code of the inline function is small and simple. Although a function is defined as Inline, it is completely compiler dependent to evaluate it as inline or not.

What is object in C++?

Class in C++ provides a blueprint for object, that means, object is created from the class.

For example,

class Circle{

public:
float radius;
}

Circle C1;
Circle C2;

How to reverse a vector in C++?

syntax: reverse(the index you want to start reversing at,the index you want to end reversing at)
vector<int> value = {11,22,33};
reverse(value.begin(),value.end());

How to initialize vector in C++?

There are multiple ways to do it:
You can do it like arrays:
vector<int> value{ 11, 22, 33 };
or by pushing values one by one:
vector<int> value;
value.push_back(11);
value.push_back(22);
value.push_back(33);
value.push_back(44);
value.push_back(55);

What is a Friend class?

Similar to friend function one class can be made as a friend to another class. Let’s say X and Y are separate classes. When X is made friend to class Y, the member functions of Class X becomes friend functions to Class Y. Member functions of Class X can access the private and protected data of Class Y but Class Y cannot access the same of ClassX.

class X;
class Y
{
// class X is a friend class of class Y
friend class X;
... .. ...
}
class X
{
... .. ...
}

What are new and delete operator?

The new operator is used to dynamically allocate memory and delete operator is used to destroy the memory allocated by the new operator.

How to concatenate two strings in C++?


"#include <iostream>
using namespace std;
int main
{
string str_a = ""Hi, My name is"";
string str_b = ""Raj"";
string str_c = str_a + str_b;
cout<<""The string is:""<<str_c;
return 0;
}"

Explain briefly Classes & Objects?

CLASS IS A collection of data member and member function

The object is an entity, used to call the functions in a class

What is calloc() function? Describe the syntax?

calloc() used to allocate multiple blocks of contiguous memory in bytes. All the blocks are of same size.

Syntax:

Ptr_name= (*cast type) calloc (No.of blocks,int size);

What is the function of the keyword "Auto"?

The keyword “Auto” is used by default for various local variables to make function work automatically.

What are the functions of the scope resolution operator?

The functions of the scope resolution operator include the following.

1. It helps in resolving the scope of various global variables.
2. It helps in associating the function with the class when it is defined outside the class.
The code of the scope resolution operator can be displayed as follows.

#include <iostream>
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;
}

Define Block scope variable?

A Block scope variable is the one that is specified as a block using the C++ that can be declared anywhere within the block.

Can we have a String primitive data type in C++?

No, we cannot have a String Primitive data type in C++. Instead, we can have a class from the Standard Template Library (STL).

What is a mutable storage class specifier? How can they be used?

A mutable storage class specifier is applied only on the class's non-static and non-constant member variable. It is used for altering the constant class object's member by declaring it. This can be done by using a storage class specifier.

What do you mean by ‘void’ return type?

All functions should return a value as per the general syntax.

However, in case, if we don’t want a function to return any value, we use “void” to indicate that. This means that we use “void” to indicate that the function has no return value or it returns “void”.

Example:

void myfunc()
{
Cout<<”Hello,This is my function!!”;
}
int main()
{
myfunc();
return 0;
}

How to initialize a 2d vector in C++?

The syntax to initialize a 2d vector is as follows:
std::vector<std::vector<int> > name_of_vector;

For example: std::vector<std::vector<int> > v { { 1, 2, 1 },
{ 2, 6, 7 } };

What is stl in C++ with example?

STL in C++ is a library and abbreviation of Standard Template Library. STL is a generalized library that provides common programming data structures/ container classes, functions, algorithms, and iterators. STL has four components

– Algorithms: Searching and sorting algorithms such as binary search, merge sort etc.
– Containers: Vector, list, queue, arrays, map etc.
– Functions: They are objects that act like functions.
– Iterators: It is an object that allows transversing through elements of a container, e.g., vector<int>::iterator.

What is the use of pure virtual function?

A pure virtual function does not have implementation in base class. This will be useful during scenarios where no actual implementation is required in the base class context. For example let’s say we have a base class named Shape which is having a function “DrawShape()” as pure virtual. In base class there is actually no need to give a definition. Now, when a Class Square derives from Class Shape, it provides an actual definition for DrawShape() as drawing a square.

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 (‘).

Is there any advantage of using one over the other?

Though both codes will generate the same output, sample code 2 is a more performant option. This is due to the fact that the post-increment ‘itr++’ operator is more expensive than the pre-increment ‘++itr’ operator.

The post-increment operator generates a copy of the element before proceeding with incrementing the element and returning the copy. Moreover, most compilers will automatically optimize sample code 1 by converting it implicitly into sample code 2.

What is the difference between equal to (==) and Assignment Operator (=)?

In C++, equal to (==) and assignment operator (=) are two completely different operators.

Equal to (==) is an equality relational operator that evaluates two expressions to see if they are equal and returns true if they are equal and false if they are not.

The assignment operator (=) is used to assign a value to a variable. Hence, we can have a complex assignment operation inside the equality relational operator for evaluation.

What is an Abstract Class?

A class is said to be abstract when it contains at least one pure virtual function. Instantiation is not allowed for an abstract class. And the deriving class must implement or provide definition for the pure virtual functions.

Example:
class Base { public: virtual void display () = 0; };

How many ways are there to initialize an int with a Constant?

There are two ways:

1. The first format uses traditional C notation.
int result = 10;
2. The second format uses the constructor notation.
int result (10);

What is the precedence when there are a Global variable and a Local variable in the program with the same name?

Whenever there is a local variable with the same name as that of a global variable, the compiler gives precedence to the local variable.

Example:

#include <iostream.h>
int globalVar = 2;
int main()
{
int globalVar = 5;
cout<<globalVar<<endl;
}
The output of the above code is 5. This is because, although both the variables have the same name, the compiler has given preference to the local scope.

Comment on Local and Global scope of a variable.

The scope of a variable is defined as the extent of the program code within which the variable remains active i.e. it can be declared, defined or worked with.

There are two types of scope in C++:

1. Local Scope: A variable is said to have a local scope or is local when it is declared inside a code block. The variable remains active only inside the block and is not accessible outside the code block.
2. Global Scope: A variable has a global scope when it is accessible throughout the program. A global variable is declared on top of the program before all the function definitions.

Example:

#include <iostream.h>
Int globalResult=0; //global variable
int main()
{
Int localVar = 10; //local variable.
…..

}

Difference between Declaration and Definition of a variable.

The declaration of a variable is merely specifying the data type of a variable and the variable name. As a result of the declaration, we tell the compiler to reserve the space for a variable in the memory according to the data type specified.

Example:

int Result;
char c;
int a,b,c;

All the above are valid declarations. Also, note that as a result of the declaration, the value of the variable is undetermined.

Whereas, a definition is an implementation/instantiation of the declared variable where we tie up appropriate value to the declared variable so that the linker will be able to link references to the appropriate entities.

From above Example,

Result = 10;

C = ‘A’;

These are valid definitions.

What are the Comments in C++?

Comments in C++ are simply a piece of source code ignored by the compiler. They are only helpful for a programmer to add a description or additional information about their source code.

In C++ there are two ways to add comments:

//single-line comment
/* block comment */
The first type will discard everything after the compiler encounters “//”. In the second type, the compiler discards everything between “/*” and “*/”.

What is destructor?

A destructor is a special member function of a class that destroys the resources that have been allocated to the class object. The destructor class name is same as that of the class but prefixed with a ~ symbol.

For example:

Class Bird
{ char name [50];
public:
~Bird ()
{
printf(“
This is Destructor of Class Bird
”)
}
};

What is vector in C++?

A sequence of containers to store elements, a vector is a template class of C++. Vectors are used when managing ever-changing data elements. The syntax of creating vector.
vector <type> variable (number of elements)

For example:

vector <int> rooms (9);

What are the types of Polymorphism?

Polymorphism is classified into two types namely:

1. Compile-time Polymorphism or Overloading
2. Run-time Polymorphism or Overriding

What is Polymorphism?

Polymorphism is the concepts of having one functionality do multiple things. The words “poly”(many) and “morph” (forms) is itself explain the concept.

What is exception handling in C++?

Exceptions are errors that happen during execution of code. To handle them we use throw, try & catch keywords.

What is inline function in C++?

Inline functions are functions used to increase the execution time of a program. Basically, if a function is inline, the compiler puts the function code wherever the function is used during compile time. The syntax for the same is as follows:

inline return_type function_name(argument list) {
//block of code
}

Search
R4R Team
R4R provides C/C Freshers questions and answers (C/C Interview Questions and Answers) .The questions on R4R.in website is done by expert team! Mock Tests and Practice Papers for prepare yourself.. Mock Tests, Practice Papers,C/C interview question set 2,C/C Freshers & Experienced Interview Questions and Answers,C/C Objetive choice questions and answers,C/C Multiple choice questions and answers,C/C objective, C/C questions , C/C answers,C/C MCQs questions and answers Java, C ,C++, ASP, ASP.net C# ,Struts ,Questions & Answer, Struts2, Ajax, Hibernate, Swing ,JSP , Servlet, J2EE ,Core Java ,Stping, VC++, HTML, DHTML, JAVASCRIPT, VB ,CSS, interview ,questions, and answers, for,experienced, and fresher R4r provides Python,General knowledge(GK),Computer,PHP,SQL,Java,JSP,Android,CSS,Hibernate,Servlets,Spring etc Interview tips for Freshers and Experienced for C/C fresher interview questions ,C/C Experienced interview questions,C/C fresher interview questions and answers ,C/C Experienced interview questions and answers,tricky C/C queries for interview pdf,complex C/C for practice with answers,C/C for practice with answers You can search job and get offer latters by studing r4r.in .learn in easy ways .