What is Enuma in C++?
“Enuma is abbreviation of Enumeration which assigns names to integer constant to make a program easy to read. Syntax for the same:
Enuma Enuma_ name{const1, const2, ……. };”
What is end l in C++?
Endl is a predefined object of o stream class to insert a new line characters.
How to save a file in C++?
“When you have written code in the file (notepad),save the file as “hello.cpp.” If you want to write in a file using C++ code, you can do it using iostream and f stream libraries in C++.
include
include
using namespace std;
int main () {
ofstream file_name;
file_name.open (“”sample.txt””);
file_name<< “”Write in the file””;
file_name.close();
return 0;
}”
C++ Interview Questions
How to include all libraries in C++?
The library in c++ is used to include all the libraries.
How to maximize turbo C++ window?
Alt+ Enter is the keyboard shortcut used to maximize (full screen) turbo C++.
What is an expression in C++?
“An expression is a combination of operators, constants and variables. There seven types of expressions for examples:
– Constant expressions: 89 +10/4.0
– Integral expressions: x * y
– Floating expressions: 17.89
– Relational expressions: a<=b – Logical expressions: a > b && a == 7
– Pointer expressions: *ptr
– Bitwise expressions: p << 5″
Advance C++ Interview Questions
How to write a class in C++?
“A class in C++ is the building block that leads to Object-Oriented programming and is a user-defined data type which holds data and functions. The syntax to write a class in C++ is as follows:
Class (keyword) Class_Name (this is user defined)
{
Access specifier: // private, public, protected
Data members //int, char, float, double etc. variables to be used
Member function() { } // Methods to access data members
}; //Class end
For example:
class Sample
{
// Access specifier
private:
// Data Members
string s;
// Member Functions()
void printname()
{
cout << s;
}
}; “
Which is the best C++ compiler?
“There are several good compilers for C++ such as:
– MinGW / GCC
– Borland c++
– Dev C++
– Embracadero
– Clang
– Visual C++
– Intel C++
– Code Block
GCC and clang are great compilers if the programmer’s target more portability with good speed.
Intel and other compilers target speed with relatively less emphasis on portability.”
How to use strcmp function in C++?
“strcmp() function is an in-built function of header file which takes two strings as arguments and compares these two strings lexicographically.
The syntax of the function is as follows:
int strcmp(const char *l, const char *r );
include
include
int main()
{
// z has greater ASCII value than g
char a[] = “”zfz””;
char b[] = “”gfg””;
int r = strcmp(a, b);
if (r==0)
printf(""Strings are equal"");
else
printf(""Strings are unequal"");
printf(""%d"" , r);
return 0;
} “
C++ Interview Questions
How to write to a file in C++?
“A file is read in c++ using a fstream header file.
include
include
using namespace std;
int main()
{
ofstream fout;
string r;
fout.open(""test.txt"");
while (fout) {
getline(cin, r);
if (r == ""-1"")
break;
fout << line << endl;
}
fout.close();
ifstream fin;
fin.open(""test.txt"");
while (fin) {
getline(fin, line);
cout << line << endl;
}
fin.close();
return 0;
} “
What is string stream in C++?
“String stream is a class in C++ which associates a string object with a stream allowing to read from the string as if it were a stream. Syntax is as follows:
stringstream string_name(str);
Basic operations are as follows:
clear()
str()
<<
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.
Advance C++ Interview Questions
How to write hello world in C++?
“Hello world in C++ is as follows:
include
int main()
{
std::cout << “”Hello, World!””;
return 0;
}”
How to calculate length of a string in C++?
“The length of a string can be calculated by using in-built functions such as length(), size(), strlen() and also by loops (while and for).
include
include
using namespace std;
main() {
string s = “”Hi I am Mr X””;
char arr[] = “”Hi I am Mr X””;
cout << s.length();
cout << s.size();
cout <<strlen(arr);
char c = arr; int count = 0; while(c != ‘\0’){
count++;
c++;
}
cout << count;
count = 0;
for(int i = 0; arr[i] != ‘\0’; i++){
count++;
}
cout << count;
}”
How to find length of string in C++?
“There is an in-built function- length()- in C++ to find the length of the string. The code snippet to find the length of string is as follows
string str_1= “abcd”;
cout << “The length of the string is: ” << str_1.length();”
C++ Interview Questions
What is class in C++?
“C language is not an object oriented programming language, so it is a constant attempt of C++ to introduce OOPs. Class is a user defined data type which defines a blueprint of data type. For example,
class Circle{
public:
float radius;
}”
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
}”
What is friend function in C++?
“A friend function has the access rights to all private and protected members of the class.
class Circle{
double radius;
public:
friend void printradius( Circle c );
};
void printradius(Circle c ) {
/* Because printradius() is a friend of Circle, it can
directly access any member of this class */
cout << “”Radius of circle: “” << c.width;
}
int main() {
Circle c;
// Use friend function to print the radius.
printradius( c);
return 0;
}”
Advance C++ Interview Questions
What is exception handling in C++?
Exceptions are errors that happen during execution of code. To handle them we use throw, try & catch keywords.
How to use vector in C++?
“A sample code to see how to use vector in C++ is as follows:
include
include
using namespace std;
int main()
{
vector vec_1;
vec_1.push_back(“”sample code””);
vec_1.push_back(“”change example””);
for(vector ::iterator i=vec_1.begin();i!=vec_1.end();++i)
cout<<*i;
return 0;
}
“
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 variable (number of elements)
For example:
vector rooms (9);”
C++ Interview Questions
What is scope resolution operator in C++?
“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”
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 (‘).
What are templates in C++?
A feature that allows functions and classes to operate with generic types that means a function or class can work on different data types without being rewritten is called template.
Advance C++ Interview Questions
How to sort vector in C++?
“
“”#include
using namespace std;
int main()
{
vector vec{ 1,9,4,3,2,8,5,7};
sort(vec.begin(), vec.end());
for (auto x : v)
cout << x << """" """";
return 0;
}
“””
What is pure virtual function in C++?
“A pure virtual function is a type of virtual function which does not have implementation, but is only declared. It is declared by assigning 0 in declaration. Syntax for the same is as follows:
class Test
{
// Data members of class
public:
virtual void show() = 0;
/* Other members */
}; “
How to use map in C++?
“Associative containers storing a combination of a key value or mapped value is called Maps. Syntax: map map_name;
include
include
include
using namespace std;
int main()
{
map test;
// inserting elements
test.insert(pair<int, int>(1, 2));
test.insert(pair<int, int>(2, 3));
map<int, int>::iterator itr;
for (itr = test.begin(); itr != test.end(); ++itr) {
cout << itr->first
cout << itr->second << '\n';
}
return 0;
)”
C++ Interview Questions
How to empty a vector in C++?
“Std::vector::empty tests whether a vector is empty or not. A sample code for illustrating the same is as follows:
include
include
int main ()
{
std::vector vec;
int add (0);
for (int i=1;i<=5;i++) vec.push_back(i);
while (!vec.empty())
{
add+= vec.back();
vec.pop_back();
}
std::cout << add;
return 0;
}”
What is visual C++?
C++ is a standardized language and Visual C++ is a product that implements the standard of C++. One can write portable C++ programs using Visual C++, but one can also use Microsoft-only extensions which destroys portability but enhances your productivity.
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. Below are the reasons and solutions for segmentation error:
Reason: Accessing an address that is freed
int* p = malloc(8);
*p = 100;
free(p);
*p = 110;
Solution: Before freeing the pointer check the assignment or any operation required to perform.
Reason: Accessing out of array index bounds
int arr[2];
arr[3] = 10;
Solution: Correcting the array bound
Reason: Improper use of scanf()
int n = 2;
scanf(“”%d””,n);
Solution: To avoid this is the only solution
Reason: Dereferencing uninitialized pointer
int p; printf(“”%d””,p);
Solution: A pointer must point to valid memory before accessing it.
Reason: Stack Overflow
Solution: It can be resolved by having a base condition to return from the recursive function.
“
Advance C++ Interview Questions
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 trans versing through elements of a container, e.g., vector::iterator.”
What is flush in C++?
std::flush synchronizes the stream buffer with its controlled output sequence.
How to initialize a 2d vector in C++?
“The syntax to initialize a 2d vector is as follows:
std::vector > name_of_vector;
For example: std::vector > v { { 1, 2, 1 },
{ 2, 6, 7 } };”
C++ Interview Questions
What is virtual function in C++?
“A function is said to be virtual if it is defined in base class and is expected to be redefined in derived class.
include
using namespace std;
class Base_Class {
public:
virtual void print_msg() {
cout << “”Base””;
}
};
class Derived_Class : public Base_Class {
public:
void print_msg() {
cout << “”Derived””;
}
};”
How to find length of array in C++?
“The length of an array in C++ can be calculated using sizeof() function. The code depicting the same is mentioned below.
include
using namespace std;
void main()
{
int a[] = {0,1,2,3,4,5};
int a_size = sizeof(a)/sizeof(a[0]);
cout << “”””Size of the array is: “””” << a_size;
}
“””
How to sort a string in C++?
“To sort a string, the sort function in C++ can be used. The sample code for the same is as follows.
include
include
using namespace std;
void str_sort(string &s)
{
sort(s.begin(), s.end());
cout << s;
}
int main()
{
string s = “”””anmbdfc””””;
str_sort(s);
return 0;
}
Output: abcdfmn
“””
Advance C++ Interview Questions
How to convert string to int in C++?
“This can be done using the stoi() or atoi() function:
int main()
{
string str_value = “”1122″”;
int int_value = stoi(str_value);
}
or
int main()
{
const char *str_ptr = “”1122″”;
int int_value = atoi(str_value);
}”
How to compare two strings in C++?
“Two string can be compared using strcmp() function. It return boolean value; if it returns ‘0’ then the strings are same and if it returns ‘1’ then the two strings are not same.
int main()
{
if (strcmp(str_value_a, str_value_b) == 0)
cout<<“”These strings are the same””;
else
cout<<“”These strings are not the same””;
}”
How to set precision in C++?
“Precision in C++ can be set by using the following functions- floor(), ceil(), round(), trunc() and setprecision(). Using set precision() in a program.
include
using namespace std;
void main()
{
double p = 8.04149;
cout << fixed << setprecision(2) << pi<<endl;
}
The output will be 8.04.”
C++ Interview Questions
How to find string length in C++?
“You can do this by using size() function.
int main()
{
string str[]=””””Hello World!””””;
int g = str.size();
cout<<“”””The size of the string is””””<<g;
}
or you can use the strlen function.”””
How to initialize vector in C++?
“There are multiple ways to do it:
You can do it like arrays:
vector value{ 11, 22, 33 };
or by pushing values one by one:
vector value;
value.push_back(11);
value.push_back(22);
value.push_back(33);
value.push_back(44);
value.push_back(55);”
How to use getline in C++?
“
“”int main()
{
string yourname;
getline (cin,yourname);
}”””
Advance C++ Interview Questions
How to take input in C++?
“You can use the cin function to take in values, like such:
int main()
{
int age;
cout<<“”””Enter your name!””””; cin>>age;
}”””
How to print a string in C++?
“
“”int main()
{
string str_value[]=””””Hello-World!””””;
cout<<“”””The string is:””””<<str_value;
}”””
How to use sort function in C++?
“
“”syntax: sort(the element of the array from where you want to start the sorting ,the element of the array from where you want to finish the sorting )
int main()
{
int int_value[5]={1,2,3,4,5};
sort(intr_value,intr_value+5);
cout<<intr_value;
}””
“
C++ Interview Questions