0% found this document useful (0 votes)
14 views35 pages

120 C++ Toughts

Uploaded by

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

120 C++ Toughts

Uploaded by

mohanraj105497
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 35
5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning Githeburses ~ FReecouRSES ~ —INTERVIEWQUESTIONS ~ CAREER —VISUALSTORIES, 2022 y Great Learning Team ~ Jon 6.2021 (CH programming is a general purpose programming language that was created by Bjarne Stroustrup. Itis essential to know C++ if you want to work in the software development domain. ++ is an extension of C programming language. The first set, of questions and answers are curated for freshers and talks about the basic C+ questions. The blog will cover C++ Interview Questions for advanced levels, CH programming interview questions, and more, These questions cover all the basics of C+ and will help showcase your expertise in the subject. The questions are divided into groups as follows. + Basic C++ Interview Questions + Advanced C++ interview Questions + C++ Programming Interview Questions + OOPS Interview Questions C++ Basic C++ Interview Questions 1, What is C++? As an extension of the C language, C++ was developed by Bjarne Stroustrup as a general purpose cross-platform language which gives programmers a high level of control over system resources and memory. 2. What is namespace in C++? hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 1185 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning YIPEoGases ~ FREECOURSES ~ INTERVIEW QUESTIONS ~ CAREER Y _VISUALSTORIES Q then hon . . picture, A namespace defines a scope and difforentiates functions, classes, variables ote, with the same name available in different libraries. The namespace starts with the keyword "namespace". The syntax for the same is as follows: | namespace namespace_nane { 2 // code declarations 3} 3. How to input string in C++? There are three ways to input a string, using cin, get, and gotline, All three methods ‘are mentioned in the sample program below. 1] #include 2| using namespace std; 4| ant main() 5 6 char s[10]; 7 8 cout << "Enter a string: 9 cin >> str; 1e 1 cout << "\n€nter another string: " 2 cin.get(s, 10); B 14 getline(cin, str); 15 16 return 05 7) > 4. What is operator overloading in C++? Operator overloading in C+ is an overloaded declaration is a declaration in the ‘samo scope of function or operator declared with the same name more than once. 5. How to learn C++? C++ is a programming language which is an extension of ©. Thus, one should prefer toloarn C first i's not necessary), After learning C, then understand the basic difference between ¢ and C++. Implement allthe basic programs you learnt in C in C+ also. Then dive into the OOP concept of C+, Do as many hands-on as possible to understand basic 00Ps, and then dive into advanced lovel OOPS, When all the basics are clear, builel «smell game to understand the structure and remain concepts if any, By following ail these steps one can learn C+ 6. What is the ference between C and C++? The difference between c and c++ is that C+ is a object-oriented language, which means that it has all the features of C as well as its own thing that is the concept of OOP. C+ has many functionalities of OOP that are missing from ¢ such as encapsulation, abstraction, classes, objects, ete. hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 2138 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning GLULCOURSES ~ FREECOURSES v INTERVIEWQUESTIONS ~ CAREER Y VISUAL STORIES Q Cisaprercuue vienou poy Gre wun eee EIKoe language. programming language. C does not support data hiding, (C+ support data hiding. Cisa subset of C++ CH is a suporset of C. C doost not support Function and C++ support Function and operator operator overloading overloading Funetions can not be defined inside Functions can be defined inside structuros. structures. 7. How to reverse a string in C++? To reverse a string, a sample code is mentioned below. 1 | #includectostream> 2| #includecstring.h> 3 | using nanespace std; a) ine main () 5| ¢ 6 char n[5@], ts 7 8 9 int i, 35 cout << "Enter a string : "3 gets(n); 20 i= strlen(n) - 25 uu for (j= 0; 4 2 3 int fun (T a,T b) 4 { 5 return (a+b); 6 x 7 8 int main(){ 9 cout<(11,22)5 18 y 9. Whatis ut 1g 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 te put this statement at the start of all your C+ codes if hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 3135 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning YOUGLSUeses ~ FREECOURSES ~ INTERVIEWQUESTIONS CAREER Y _VISUALSTORIES Q standare 10. How to download turbo C++ for windows 10? To download turbo c++ follow the steps mentioned below: Step-t Download turbo C++ from http:/uwu:turboc8. com/p/downloadhtm| Step-2: Extract Turbo.0:3.2zip filo, Step-3: Run sotup.oxe filo. tep-4 Follow the instructions mentioned Tl. How to paste in turbo C++? Paste in turbo C++ can be done by two techniques: 1 Shifttinsert 2. Open the file in notepad with cpp extension. Make the changes and save it. After saving the file, you can open it from the Turbo C+ application file menu from where you stored the opp file. 12. What is pointer in C++? Pointers in C+ are a data type that store the memory address of another variable. For eg, 1] char *str = "Hi, How are you?"; 2 Here the pointer variable *str points to the string 3 4 or 5 6 int age; 7 int *int_value; 8 9 *int_value = &age; 1e n coute<"Enter your age please:” 2 cin>>ages 2B 14 cout<<"\n Your age is:"<<*int_value; 15 16 // this will print your age as the variable is pointin > 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. int fun(){ int a= 11; return 11; } int main(){ int b = fun(); hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 4138 sv9r2a, 2:57 PM “on 120% C+ inleriew Questions and Answers 2022 | Great Learning T4uMideses » FREECOURSES ~ INTERVIEWQUESTIONS ~ CAREER Y _VISUALSTORIES Q 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 ~0: class ~ syntax 15. Who invented C++? Bjarne Stroustrup invented C+ in 1985. 16. How to convert integer to string in C++? There are 2 approaches to convert integer variables to string. Both the approaches with a sample code are mentioned below. 3] Approach-2 2| #includeciostrean> 5 | #includecstring> 4 | Using nanespace std; 5) void main() 6] ¢ 7 int 8 string 9 cout << 55 20| 3 a 12 | Approach-2 14) #includeciostream> 45 | #include 16 | include 17 | using namespace std; 38] int main() 39) ¢ 2e int n = 175 21 2 // declaring output string stream 23 ostringstream st; 24 25 // Sending @ nunber as a stream into output str 26 s<< nj 27 7/ the str() converts number into string 28 string fin = s.ste(); 29 // Displaying the string 30 cout << fin; 31 return 0; 32| } 17. What is function overloading in C++? Function Overloading happens in C+ when two or more functions share the same name. They can be differentiated on the basis of the type of data they are passing as parameters or even the number of parameters they are passing. eg. int fun(char «): & int fun(int b); & void fun(int a, int b) 18. What is stl in C++? hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 5135 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning STA WSueses ~ FREECOURSES ~ INTERVIEWQUESTIONS ~ CAREER Y _VISUALSTORIES of temple code, 1 | queuecint> 95 2 3 For (k=053k<10;k++) 4 t 5 @.push(k) ; 6 cmd? 19. How to run C++ program 1] verify gcc installtion using the command: 2 $ gcc -v 3 4| then go to your working directory or folder where your code is: 5 $ cd 6 7| then build the file containing your ¢ code as such: 8 $ gcc main. cpp 9 28 or a 2 $ gH -0 main main.cpp B 14 | then run the executable generated in your system: 35 $ main.exe 20. What is type casting in C++? Type casting in Cis used to change the data type. They are of two types: Implicit Type Conversion: It is automatic, Explicit Type Conversion: Itis user-defined, 21. How to use string in C++? Astring is a sequence of characters. In C+, string is a data type as well as a header file. This header file consists of powerful functions of string manipulation. A variable of string is declared as follows: string st fell ‘And to use string one needs to include the header file. // Include the string Library include // Create a string string str= "Hello"; ariable 22. How to input string in C++ with spaces? The code to input a string in C++ with spaces is as follows: include include using namespace std; int main() hitpssiwwnzmygreatlearning comvbloglepp-sntervew-questions! 2135 5/0122, 2:57 PM. “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning 6 ALL COURSES ¥ FREECOURSES ¥ —INTERVIEWQUESTIONS ~ CAREER Y VISUAL STORIES. Q 8 9 cout << “Enter the sentence"; 10 getline(cin, 5); 11 cout << str} 2 return 0; Bl 23. What is stream in C++? Stroam refors to a stream of characters to be transforred between program throad and ifo. 24. 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 inhoritance, the access specifior for struct is public whereas for class its private. ~ Structures do not have data hiding features whereas class does. = Structures contain only data members whereas class contains data members as well as member functions. = In structure, data members are not initialized with a value whereas in class, data members can be initialised, ~ Structures are stored as stack in memory whereas class is stored as heap in memory. 25. How to clear screen in C++? One can clear screen using ~ clrser() or system(‘clear") 26. Who developed C++? Bjarne Stroustrup in 1998 at Boll Labs developed the lanuage C++. 27. How to compile and run C program in notepad++ To compile and run ¢ program in notopad++ follow the stops montioned below: ‘Step-: Download and install notepad+ ‘Step-2: Download and install MinGw gee along with gcc. ‘Step-3: Configure notepad+ for gee. This step can be further divided into two sub- steps. A: Create C compiler tool in Notepad++ B: Creating C execution tool ‘Step-4: Execute C program in Notepad++ 28. How many keywords in C++? hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 7138 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning THEIR MUCCOURses ~ —FREECOURSES ~ INTERVIEWQUESTIONS ~ CAREER _VISUALSTORIES overload 29. What iostream in C++? Itis a header file that includes basic objects such as cin, cout, cer, clog. 30, How to give space in C++? In C+ programming, the space can be given using the following code. cout «**; 31. How to dynamically allocate a 2d array in C++ ? There are several methods by which one can allocate memory to 2D array dynamically one of which is as follows, 2] include 2) int main() 5 a] ‘ant row = 2, col = 25 5 int* a new int[row * col]; é 7) ane 4, 3, court = 8; | for Ses d < rons i++) 9 for (j = @; j < col; j++) Pr) sav dcol + 3) 2 countes; u 12 for (i i< row; it+) B for (j= 0; 4 < cols j+#) 14 printf("Ad ", *(a + i*col + j))3 15 16 delete[ ] a; y| return 0: 18} } 32. How to use goto statement in C++? Goto statement provided unconditional jump in the code. The syntax is: goto label; 1| label: statements 2 3| #include 4| using namespace std; 6 | void main () { 7 float d, avg, add = 0.0; 8 int 3, 5 9 cin >} nj 10 ett for(j = 1; j <= nj +43) 2 { 3 cout << “Enter nunber” << 15 aa cin >> 45 a5 36 if(d < 0.0) v7 { 38 goto jump; 29 } 20 addt= 4; a } 22 hitpssiwwnzmygreatlearning comvbloglepp-sntervew-questions! 8135 5/0122, 2:57 PM. 23 25 - 21} 33. What is function overriding in C++? 3 AWLCOURSES ~ FREECOURSES ~ —INTERVIEWQUESTIONS ~ CAREER ~ “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning VISUAL STORIES When a function with same name is present in both parent and child class then it is called function overriding. 1] include 2| using namespace std; 3| class parent { 4| public: 5 void display(){ 6 cout<<"Parent Class"; 7 y 8) hs 9| class child: public parent{ 36 | public: n void display() { 2 cout<<"Child Class"; B } 34a] 3 15 | int main() ¢ 16 child 0 = parent(); v7 o.display(); 18 return @; a9| 3 34. 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- “*" 35. How to copy and paste in turbo C++ ? Pross Ctrl + Insert to copy. Press Shift + Insert to paste. 36. Why C++? The use of C+ is varied such as = Itis used in developing graphic user interface based applications ike adobe photoshop. = Itis used in developing games as it overrides the complexity of 3D games. = There are many animated softwares developed in C++ Most of the compilers are written in C+. Google Chrome, Mozilla Firefox otc. web browser are developed using C++ There are many more such uses that make C++ a desired language. hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 9138 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning 3ZeWibases ~ FREECOURSES ~ INTERVIEW QUESTIONS ~ CAREER» _VISUALSTORIES Bool is a data type in C++ which takes two values- True and False. Syntaxis as follows: bool bi true; A sample code is as follows: 1] #includeciostream> 2| using namespace std; 3 | int main() 4 5 int a= 60, b= 70; 6 bool c, dj 7 c= ass b; // false 8 9 c= ac bs // true 10 11 cout < 38. What is exception in C++? Runtime aisnormal conditions that occur in the program are called exceptions. These are of 2 typos: = synchronous ~ Asynchronous CH has 3 specific keywords for handling these exceptions: -0y ~eateh = throw 39. How to set decimal places in C++? For limiting the decimal places in C++ there are five functions :tloor(), ceil, trunc0, round() and setprecision() Out of these five, only setprecision() function is used for sotting the decimal places to put as output All the functions are mentioned in the following sample code. 1 #includecbits/stdct+.h> 2| using namespace std; 3 4| ant main() 6 float a =2.33333; 7 cout << floor(a) << endl; 8 cout << ceil(a) << endl; 9 cout << trunc(a) << endl; 10 cout << round(a) << endl; 11 cout << setprecision(2) <«< aj 2 return 0; 2B 40. How to get absolute value in C++? hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 10135 s10122, 2:57 PM “Top 1204 C++ inlenviw Questions and Answers 2022 | Great Learning ingtesb, ‘AiLEdUnses ~ FREECOURSES ~ INTERVIEW QUESTIONS ~ CAREER VISUAL STORIES Q ofthe int ~abs() ~labs() = llabs() The syntax for all the functions is same ~ function name(integer value) The difference lies in the range for integer value being passed as an argument. For abs() its type int in C++. For labs) its type long int in C++ and for llabs() its long long intin +4, ‘Sample code for the illustrating the three functions is as follows: 1] #include 2| #include 3 4| using namespace std; 5 6 | int main() 7 8 int a, b, ¢5 9 1e a = abs(22); n b= labs (123435SL); 2 c= Labs (1234863552LL) ; 3 cout << a; 14 cout << b; 15 cout<< c3 16 return 0; v7) > 41. What is the difference between C++ and Java? The difference between c++ and java are as follows: — C++ supports goto statements whereas Java does not ~ C++ is majorly used in system programming whereas Java is majorly used in application programming ~ C++ supports multiple inheritance whereas Java does not support multiple inheritance — C++ supports operator overloading whereas Java does not support operator overloading. ~ C++ has pointers which can bo used in the program whereas Java has pointers but internally ~ C++ uses a compiler only whereas Java uses both compiler and interpreter. ~ C++ has both call by volue and call by reference whereas Java supports only call by value — C++ supports structures and joins whereas Java does not support structure and joins — Java supports unsigned right shift operator (>>) whereas C++ does not. ~ C++ is intoractive with hardware wheroas Java is not that interactive with hardware, hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 11138 si9122, 257 PM “Top 120+ C++ Inlerview Questions and Answers in 2022 | Great Learning 4PxWGinses ~ FREECOURSES ~ INTERVIEW QUESTIONS ~ CAREER ~ _VISUALSTORIES The strings in C++ can be concatenated in two ways- one considering them string objects and second concatenating them C style strings. 1] #include 2| using namespace std; 3 4| ant main() 5) ¢ 6 string s1, s_2, fin; 7 cout << Enter string"; 8 getline (cin, s_1); 9 cout << “enter string 2e getline (cin, s 2); 11 fine s1+ 2; 2 cout << fins B 14 char str1(s0], str2[se], fin[100]; 1s 16 cout << “Enter string"; v7 cin.getline(str1, 52); 18 19 cout << “Enter string"; 20 cin.getline(str2, 58) 21 2 streat(stri, str2); 23 24 cout << "str = " << sted << endl 25 cout << "str2 =" << stra; 26 27 return 0; 28| + 43. 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() =sscant() ~typecasting ‘A sample code depicting all three functions are as follows: 1 | #includecstdio.h> 2| #includecstdlib. b> 3] int main() { 4) "char *s s| char | int a,b,c5 7 | — sscant(s, fa); // Using sscant | — printe("a : xd", a)3 10 a] b= atoi(s);_// Using atoi() 32] printf("b : xa", b); 3 14] c= (int)(a)3 // Using typecasting a5] printf("e : a", cs a6 37] return 05 38] } 44. How to generate random numbers in C++ with a range? hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 12135 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning nat UGNEEBGrses ~ FREECOURSES ~ INTERVIEW QUESTIONS ~ CAREER» _VISUALSTORIES 1 #include 2| #include 3| int main() 4 5 int max=10@, mi 6 int range ='max - min'+ 1; 7 for (i=mins icmax;ir+) 8 { 9 int num = rand() % range + min; 18 cout< #include using namespace std; int main() { int 323.456; int x = abs(a); cout << x; return @; 48. How to exit from turbo C++? To exit Turbo C++, use the Quit option under the File Menu, or press Alt + X. 49. What erator in C++? Any object which has an ability to iterate through elements of the range it has been pointing to is called iterator. hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 13935 si9122, 257 PM “Top 120+ C++ Inlerview Questions and Answers in 2022 | Great Learning BArWibases ~ FREECOURSES ~ INTERVIEW QUESTIONS ~ CAREER Y VISUAL STORIES Q is called a scope resolution operator which is used to access global variables with the same name as of local variables, for defining functions outside the class, for accessing static variables, and for referring to a class inside of another class. 51. 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{const, const2, 52. What is endl in C++? Endlis a predefined object of ostream class to insert a new line characters, 53. How to save a file in C++? When you have written code in the file (notepad).save the fle as “hellopp: if you want to write in a fle using C++ code, you can do it using iostream and fstream, libraries in C+. 1] #include 2| include 3| using namespace std; 4 5 int main () { 6| — ofstrean #ile_name; 7| file_name.open (“sanple.txt"); a| file-namece "write in the file"; 9 | File_name.close(); 1e| _ return 9; ul) 54. Which operators can be overloaded in C++? List of operators that can be overloaded are: =.=, [].0,.->* new, new [] delete , delete [] 55. How to include all libraries in C++? The library in e+ is used to include all the libraries 56. How to maximize turbo C++ window? Aittnter is the keyboard shortcut used to maximize (full screen) turbo C++ 57. What is an expression in C++? hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 14135 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning AMUEBGRsEs ~ FREE COURSES ¥ INTERVIEW QUESTIONS » CAREER ¥ types of ~ Constant expressions: 89 +10/40 Integral expressions:x*y ~ Floating expressions: 1789 ~ Relational expressions: ae=b ~ Logical expressions: a> b &&a==7 ~ Pointer exprossions: “ptr = Bitwise expressions: p «5 58. How to write a class in C++? VISUAL STORIES Aclass 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 ts 1/Class enc For example: class Sample { JI Access specifier private: // Data Menbers string s3 1 2 3 4 5 6 7 8 8 11 Menber Functions() 10 void printname() 1 { 2 B y 14 cout << 53 59. Which is the best C++ compiler? There are several good compilers for C++ such as: ~Minew /@cc Borland o++ -Devc++ - Embracadero -Clang = Visual C++ = Intel C+ - Code Block Data menbers //int, char, float, double etc. variables to be used Member function() { } // Methods to access data menbers CC and clang are great compilers if the programmer's target more portability with hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 15135 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning S9REEBSnses ~ FREECOURSES ~ INTERVIEWQUESTIONS CAREER Y _VISUALSTORIES Intel and 60. How to use strcmp function in C++? stremp(/function is an in-built function of 2| #includecstring.h> 3] int main() 4a) ¢ 5 JJ z has greater ASCII value than g 6 char a[] = "zf2"; 7 char b[] = “gfe”; 8 9 int r= stremp(a, b)5 1e n if (r==0) 2 printf("Strings are equal"); 2B else 14 printf("Strings are unequal"); 15 16 printf("Kd" 7); 7 18 return 0; 19| 3 61. How to write to a file in C++? A filo is read in c++ using a fstroam header file, 1| include 2| include 3| using nanespace std; 4) ant main() 5| ¢ 6 ofstream fout; ? string rs 8 9 fout .open("test.txt"); 20 ett while (fout) { 2 getline(cin, r)3 3 if (r ) 14 break; a5 fout << Line << endl; 36 } v7 fout..close(); 38 9 ifstrean fin; 20 Fin.open("test txt"); a while (Fin) ( 22 getline(fin, Line); 2B cout << line << endl 24 d 25 Fin.close()3 26 return 8; 27| } hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 16135 sv9r2a, 2:57 PM “on 120% C+ inleriew Questions and Answers 2022 | Great Learning 62x Wlnses ~ FREECOURSES ~ INTERVIEW QUESTIONS ~ CAREER ~ VISUAL STORIES Stringstream 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 s as follows: stringstroam string_namo(str): Basic operations are as follows: clear() str 63. Why namespace std is used in C++? Ifthe 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 ete. 64. How to write hello world in C++? Hello world in 0+ is as follows: 1| #include 2| int main() 3 4| * std::cout << "Hello, World! 5 | return @; oly 65. How to calculate length of a string in C++? The length of a string can be caloulated by using in-built functions such as length0, sizeQ, strlen() and also by loops (while and for) 1 | #includeciostrean> 2| #includecestring> 3| using namespace std; 4) main() ¢ 5 string s = “Hi T am Mr x"; 6 char are] = "Hi I an Me x 7 cout << s.Length(); 8 cout << s.size(); 9 cout < 2| #includecvector> 3| using namespace std; 4| int main() s| ¢ 6 | vector vec_1; 7| vec_1.push_back("sample code"); 8 | vec“t.push_back("change example"); 9| for(vector : :iterator isvec_1.begin();i 20 | coutec*i; u1| return 03 2] ¥ fec_l.end();++4) 75. What is vector in C++? A sequence of containers to store elements, a vectors 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 rooms (9); 76. 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: 77. 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 mars () 78. What are templates in C++? A feature that allows functions and classes to operate with generic types that means function or class can work on different data types without being rewritten is called template. hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 20135 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning TGutdGeses ~ FREECOURSES ~ INTERVIEWQUESTIONS CAREER Y _VISUALSTORIES Q 2 | “include vec{ 1,9,4,3,2,8,5,7}5 é 7 sort(vec.begin(), vec.end()); 8 5| for (auto x : vy 2 cout << x Ft 32| return 03 ay u 77. 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. itis declared by assigning 0 in declaration. ‘syntax for the same is as follows: class Test { // data members of class public: virtual void show() = @: /* Other menbers */ us 78. How to use map in C++? Associative containers storing a combination of a key value or mapped value is called Maps. Syntax map 2| #include 3| #include 4 5| using namespace std; 6 7| Ant main() 8 9 mapcint, int> test; 10 rey // inserting elements 2 test.insert(paircint, int>(1, 2))5 2B test.insert (paircint, int>(2, 3))3 14 15 mapcint, int>::iterator itr; 16 for (itr = test.begin(); itr != test.end(); +#itr) { 7 cout << itr“ofirst 38 cout << itr->second << ‘\n's 19 x 2e| return @; al) 79. How to empty a vector in C++? hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 21138 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning StiVESURsEs ~ FREECOURSES ¥ INTERVIEW QUESTIONS » CAREER Y VISUAL STORIES a ilustratin. 1| #include 2| include 3 4| ant main () 5 6 |” sté::vectorcint> vecs 7 int add (8); 8 9 | for (int inisiceS;is+) vec.push_back(i)$ 20 a1} while (Ivec.enpty()) al) ¢ 13 add+= vec.back(); 14 vec.pop_back(); a5 16 a7| std::cout << add; 18 19] — return @; 20| } 80. What is visual C++? CHis a standardized language and Visual C+ is a product that implements the standard of CH. One can write portable C+ programs using Visual C++, but one can also use Microsoft-only extensions which destroys portability but enhances your productivity. 81. 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: 1] Reason: Accessing an address that is freed 2| int* p = malloc(8); 3 | *p = 100; 4 5 free(p); 6 | *p = 110; 7 8| Solution: Before freeing the pointer check the assignment or any opera 9 1@| Reason: Accessing out of array index bounds a1| int arr[2]; 32) arr[3] = 10; 13| Solution: correcting the array bound 45 | Reason: Improper use of scanf() 36 | int n = 25 17 | scanF("Xd" yn); 18 | Solution: To avoid this 1s the only solution 19 20| Reason: Dereferencing uninitialized pointer 21 | int *p; 22| printF("xa",*p)s 23 | Solution: A’ pointer must point to valid memory before accessing it. 25 | Reason: Stack Overflow 26| Solution: It can be resolved by having a base condition to return from hitps:iiwwn-mygreatlearning comvbloglepp-ntervew-questions! 22138 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning ALLCOURSES ¥ FREECOURSES ~ INTERVIEWQUESTIONS ~ CAREER _VISUALSTORIES 82. What Is stl in C++ with exampiey STLin C+ is a library and abbreviation of Standard Template Library. STLis a generalized library that provides commen programming data structures/ container classes, functions, algorithms, and iterators, STL has four components = Algorithms: Searching and sorting algorithms such as binary search, merge sort ote, ~ Containers: Vector. list, quoue, arrays, map ete. = Functions: They are objects that act like functions. ~Itorators: tis an object that allows transvorsing through elements of a container, 2g, vector >» name_of_vector: For oxamplo: stc:vectorv { {1.2.1}, {2.6.7}}; C++ Programming Interview Questions 85. How to input a string in C++? There are two ways to Input a string in C+. Way a | string str_as 2| cout << “Enter the string"; 3| cin >> stra; Way 2: a| string str_as 2| cout << "Enter the string"; 3 | getline (cin, str_1) 86. Whatis virtual function in C++? A function is said to be virtual if it is dofined in base class and is expected to be redefined in dorived class. 2 | #include hitpssiwwnzmygreatlearning comvbloglepp-sntervew-questions! 23135 5/0122, 2:57 PM. “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning 2) u AULEOURSES ¥ FREECOURSES ~ —INTERVIEWQUESTIONS ~ CAREER —_VISUALSTORIES a) c ‘ s| “public: 6 virtual void nse() ¢ 7 cout << 8 } 9] 20 11 | class Derived_Class : public Base_Class { 32| * “pubii B void print_nsg() { u cout << "Derived"; 15 } as| 3s 87. 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 al] = (@,1,2,3,4,5}5 int a_size = sizeof (a)/sizeof(a[0]); cout << ""Size of the array i << a_size; 88. How to convert int to string in C++? There are 2 approaches to convert integer variables to string. Both the approaches with a sample code are mentioned below. Approach 1 1 | #includeciostream> 2| #includecstring> 3| using namespace std; 4| void main() 5] 4 6 int n= 15 7 string s= to_string(n); 8 cout << 3 ely Approach 2: 1] #includeciostream> 2| include 3| #include 4| using namespace std; 5 | int main() 6 7 int n = 175 8 9 // declaring output string stream 16 ostringstream s1; n 2 // Sending 2 nunber as a stream into output str hitpssiwwnzmygreatlearning comvbloglepp-sntervew-questions! 24138 5/0122, 2:57 PM. “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning 23 ALL COURSES ¥ FREECOURSES Y INTERVIEW QUESTIONS ~ CAREER Y VISUAL STORIES. Q 35 rate 16 77 Displaying the string v7 cout << fins 38 return 0; 19] } 89. How to sorta string in C++? To sort a string, the sort function in c++ can be used, The sample code for the same is. as follows. 1 | #includeciostream> 2| include 3 using namespace std; 4| void str_sort(string &) 5 { 6 sort(s.begin(), s.end()); 7| cout ce s3 8} ) 9 28 | int main() uu n string s = "“annbdfe" B str_sort(s); 14 return 85 a5| > as 17 | output: abcéfan as] * 90. How to convert string to intin C++? This can be done using the stoi0 or atoi0 function 2] ant main() 2 3 string str_value = 4 int int_value = stoi(str_value); 5 + 6 or 7 int main() 8 9 const char *str_ptr 1 int int_value = atoi(str_value); u y 91. How to compare two strings in C++? Two string can be compared using stremp() function. it return boolean value if i returns 0’ then the strings are same and if it returns T then the two strings are not same. 2 | ant main() 2 3 if (stremp(str_value_a, str_value_b) == 0) 4 cout<<*These strings are the same"; 5 else 6 coutc<"These strings are not the same vl hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 25135 sio, 2:57 PM “Top 120% C++ Inlarvew Questions an Answers in 2022 | Great Learning S2rldGases ~ FREECOURSES ~ INTERVIEW QUESTIONS ~ CAREER ~ _VISUALSTORIES Q Precision in C++ can be set by using the following functions- floor(), ceil(), round(), trunc() and setprecision(, Using setprecision( in a program. wincludecbits/stdc++.h> using namespace std; void main() double p = 8.04149; cout << fixed << setprecision(2) << piccendl; y The output will be 8.04. Bowvausunn 93. What is this pointer in C++? Using this pointer, every object is provided access to its own address. It is an implicit parameter to all member functions. Only member functions have ai this pointer and not a friend function, 1 | #includeciostrean> 2| using nanespace std; 3 4| class sanple 5| ¢ 6 | private: 7) "ant as 8| public 9| void set_var (int a) wo} ¢ ett this->a zl oy 23| void print) 4 { 15 cout << x 5 is > 1) ds 38 39} int main() 20 21] * sample obj; 22) int a = 23 23] obj.set_var(a); 2a} 0b3.print()$ 25) return 0; 26| } 94. How to concatenate two strings in C++? “#include using namespace std; int main 1 2 3 al ¢ 5 string stra 6 string str_b 7 8 9 e string strc > 7 he string is:"*< valuef 1,22, 23; or by pushing values one by one: vectorsint> value valueppush_back(n): valueppush_back(22) valueppush_back(33) valuepush_back(44): valueppush_back(s8) 97. How to use getline in C++? “int main() string yournane; getline (cin, yourname); 98. How to take input in C++? You can use the cin function to take in values, like such: a | int main() 2 3 int age; 4 cout<<""Enter your name! 5 cino>age; 6| ¥ 99. How to print a string in C++? “int main() string str_value[ ]=""Hello-World!""; coute<""The string is:""< 2| #includecvector> 3 4| using namespace std; 5 © | vector fun(vector value_c) 7| ¢ 3 vectorcint> value: 9 for(int j=25 jevalue_c.size();3++) 10 uu value_d.push_back(value_c[3]#4); 2 B return valued; 14 //eeturning the vector a5] } 16 17) int main() a a9 20 vectorcint> value_a,value_bs 2 2 J/Me put values in vector value_a 23 Value_a.push_back(1)s 24 value_a.push_back(2); 2s value_a.push_back(3); 26 27 28 value_b = fun(value_a); 29 Iiveceiving the vector 8 31 return @5 »| > 103. How to reverse an array in C++? 1] “Pincludeciostream> 2| using namespace std; 3 4) int main() 5] ¢ 6 | int a3 3 | cin 53 ns 8) int a[n]; 9| ant 33 1@| for(i = @; i > arr[i]; 2 14 | for(i = nets 4 >= as 16 | cout << ar[i] ce 5 hitpsilwwn-mygreatlearning convbloglepp-ntervew-questions! 28135 5/0122, 2:57 PM. “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning a7} AULCOURSES ¥ FREECOURSES ~ —INTERVIEWQUESTIONS ~ CAREER —_VISUALSTORIES a9] ry 20 21 22 104. How to split a string in C++? 1| “include 2| #include 3 4) ant main () 5 6 |” char * flag pointer; 7| char string value[10]=""Great Learning 8| — flag_pointer =strtok (string value,” 9 10 | while (flag_pointer!=NULL) 11 2 coutc include using namespace std; void str_sort(string &s) 1 2 3 4 5) ¢ 6 sort(s.begin(), s.end()); 7 cout << 3 8] } 9 10 | nt main() 11 2 string s = ""annbdfc" B str_sort(s); 4 return 0; as| ) 16 17 | output: abedfan a8] * OOPS Interview Questions C++ 106. What is constructor in C++? Constructor in C++ is a method in class which has the same name as that of class ands followed by parentheses 0. tis automatically called when an object of a class is created, 1 class Hello { —// The class 2| — public: // Recess specifier 3 Hello() ( —// Constructor hitpssiwwnzmygreatlearning comvbloglepp-sntervew-questions! 29135 5/0122, 2:57 PM. “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning 4 ALLCOURSES ¥ FREECOURSES Y INTERVIEW QUESTIONS ~ CAREER Y VISUAL STORIES. Q 6] > 7 8 | int main() ¢ 9| Hello obj; // Create an object of Hello (this will call the const 10| return 0; un} } 2 107. What is inheritance in C++? Inheritance in C++ is just like a child inherits some features and attributes from his parent similarly a class inherit attributes and methods from another class. The parent class is called base class and the child class is called derived class. 1] // Base class 2| class Food_Iter{ 3| public: 4 void taste() { 5 cout << “The taste of every food item is different. \n"" 5 6] } 7) 8 9| // Derived class 10| class Chips: public Food_Tten{ a1] public: 2 void taste() { B cout << “The taste of chips is salty \n"" ; } a] ys 108. What is object in C++? Class in C++ provides a blueprint for object, that means, object is created from the class, For example, class Circlet public: 1 2 3 4 5 float radius; 6| > 7 a 9 Circle 1; Circle C2; 109. What is encapsulation in C++? To prevent access to data directly, Encapsulation is the procoss that combines data variables and functions in a class, This is achieved by doing the following: 1. Making all data variables private. 2. Creating getter and setter functions for data variables, 110. What is abstraction in C++? hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 30135 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning AGT ESUeses ~ FREECOURSES ~ INTERVIEWQUESTIONS ~ CAREER Y _VISUALSTORIES Q Program, . world and only showing what is absolutely necessary for the outside world to use. ‘9g. Classes use the abstraction concept to only show relevant data types or elements. This is done through access specifiers such as: public, private, protected. IN. What is oops in C++? 00? or Object Orionted Programming in C++ is a type of programming in which you create objects and classes to emulate real world concepts such as Abstraction, Polymorphism, Encapsulation, inheritance. Here classes are data types that allow you list several types of data within it and even functions. You can access these classes with the help of class objects. 112. What is member function in C++? Member functions are those functions that you declare within a class, they are members of the class. You can reference them using class objects. Eg a| class a 2] 4 3 publi 4 int add(int b) 5 é a=b* 10; 7 return a5 8 bh 9] ys 113. What is virtual base class in C++? Let's understand this with an example. You Have 4 classes: WXYZ Here X & Y inhorit from W, So they both have similar features being inherited from W, Now, Z inherits from both x & ¥ Hore Z may inherit similar features from X & Y as they both have inherited them from W. This can cause issues and thatt's why we Use virtual base classes as they stop multiple features of a class from appearing in another class. 114. How to access private members of a class in C++? Private members of the class are not accessible by object or function outside the class. Only functions inside the class can access them or friend functions. Howover, pointers can be used to access private data members outside the class. Sample code is as follows: include using namespace std; pune class sample_test{ hitpssiwwnzmygreatlearning comvbloglepp-sntervew-questions! 31135 5/0122, 2:57 PM. “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning 5] p AULEOURSES ¥ FREECOURSES ~ —INTERVIEWQUESTIONS ~ CAREER —_VISUALSTORIES 7 8] public: 3 | Sample test() { n = 45; } 18 int display() { 11 return ng 12 ? Bly 115. How to call base class constructor from derived class in C++? Abase class constructor will be called whenever the derived class constructor is called. Upon the creation of a dervied class object the order of constructor execution is: base class constructor then Default class constructor. 16. What is an abstract class in C++? An abstract class in C++ is such that cannot be used directly and is used to form a base class for others to inherit from. Ifyou create an object for an abstract class the compiler will throw an error at you. 117. Whatis containership in C++? Containership in C+ is a relationship in which a class's object is nested within another class, The class that contains the object is called a container class and the class whose object is stored is called a contained class. 118. What is data hiding in C++? An object oriented technique of hiding data members is called data hiding. In other words, giving restricted access to the data members so as to maintain object integrity. 119. What is runtime polymorphism in C++? Polymorphism means having many forms either itis a function or operator in programming. Runtime polymorphism is achieved by function overriding. include using namespace sti ¢ 1 2 3 4| class parent 5 6 | public: 7 8 void print() 9 { coutc< "base class’ 1e| 3 n 12| class child:public parent a) ¢ 14| public: 15 16 void print() hitpssiwwnzmygreatlearning comvbloglepp-sntervew-questions! 32135 5/0122, 2:57 PM. “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning v7 AULEOYRSES ~ FREECOURSES ~ INTERVIEW QUESTIONS ~ CAREER» VISUAL STORIES Q 39 20 | int wain() 2| ¢ 2 parent *p; 23 child ¢5 24 P= Bc 25 26 //virtal function, binded at runtime (Runtine polyrorphism) 27 p->print(); 28 return 0; 23| } 30 120. What is copy constructor in C++? A copy constructor is a constructor which creates an object by initializing it with an. object of the same class, which has been created previously. The syntax for copy constructor is as follows: ‘classname (const classname &obj) { || body of constructor } 121. How is modularity introduced in C++? Modularity is a way of mapping encapsulated abstractions into real and physical modules which is closoly related to Encapsulation It is a concept in which separate programs are divided into separate modules. For example, when building a house it is built in modular way. First foundation is lai, then structure is made and so on 122. Whatis the size of empty class in C++? Size of an empty class is 1 byte generally just to ensure that the two different objects will have different addresses. C++ Interview Questions FAQS @: What are the important topics in c++? A: The significant topics in C++ are abstraction, control statements, constructor, objects, and ck 05, inheritance, destructor, static, polymorphism, abstract class, interface, namespace, exception handling, oncapsulation, arrays, strings, and File 10, toname afew. @: How dol prepare for c++? A: As you start preparing for C++, you need to be sure that you are the basic data structures as well as algorithms. These are the basic things that you are most Ikely to hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 33135 5/0122, 2:57 PM “Top 120+ G++ Interview Questions and Answers in 2022 | Great Learning PGAEBGases ~ FREECOURSES ~ INTERVIEW QUESTIONS ~ CAREER Y _VISUALSTORIES Q tobever, Q: Whatis C++ best used for? A: C+ is used especially when a low-level programming language is needed. C+ is mostly used for graphics-heavy software, which includes photo and video editing ‘apps, games, browsers; while, C is usually used for OS kernels and embedded dovices. @: Whatare the basics of C+#? A: The basic elements of C+ are input, output, conditional, arithmetic, and looping. To know more about it, you first need to understand the concept of C++. How to code in c++? ‘A: You have to start it by choosing a file name that explains the purpose of the program, The next step will include building the Executable Code: Compile and Link (aka Build) the source code" hello. cpp "into executable code ("hello.exe" in Winclows or * hello "in UNIx/Linux/Mac). On IDE (Ike CodeBlocks), push the “Build” button Q: Whatis C++ andits features? A: C++ is basically an object-oriented programming (OOP) language that many. consider as the best language that helps in creatting large-scale applications. The features of C++ include Object Oriented, Simple, Platform Dependent, Mic-level programming language, Structured programming language, Rich Library, Memory Management, and Powerful & Fast. QIs C++ hard tolearn? A: The syntax of C+ is not difficult to understand for most people, And it becomes especially easy to learn if you already know what C is, However, the versatility of C+ makes it « powerful language, which is why some find it a bit hard to learn. QIs Java better than c++? A: Java is usually @ high-level, object-oriented, and interpreted language, which uses objects. On the other hand, C uses functions. Java is mostly found easior to learn by many since it is high level, while C performs faster since it is closer to machine code. @: Why is C++ called OOP? A: The reason why C++ is called OOP or Object Oriented Programming is because CH sees a problem as objects that are involved rather than the process to do it hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 34138 5/0122, 2:57 PM ‘Top 120+ G++ Interview Questions and Answer in 2022 | Great Learning THILO Gases ~ FREECOURSES ~ —INTERVIEWQUESTIONS ~ CAREER Y _VISUALSTORIES well-equ. Wondering where to learn the highly coveted in demand skills for free? Chock out the courses on Great Learning Academy. Also Read Top 25 Common Interview Questions Great Learning Team ‘Groat Learning’ Slog covers the latest developments an innovations in technology that ean be lovoraged to build rewerding caroors Youll ind caroor guides, toch tutorials and industry nows to Koop ‘yourself updated withthe fast-changing world of tech and business. hitpsslwwn-mygreatlearning comvbloglepp-ntervew-questions! 35135

You might also like