{"id":84658,"date":"2021-10-06T09:00:23","date_gmt":"2021-10-06T03:30:23","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=84658"},"modified":"2021-10-06T09:00:23","modified_gmt":"2021-10-06T03:30:23","slug":"c-interview-questions-and-answers","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/","title":{"rendered":"C++ Interview Questions and Answers"},"content":{"rendered":"<p>In this tutorial, we are going to discuss C++ Interview questions and Answers which will help you to prepare better for interviews and placements. This will also help you to build your basic knowledge of C++ programming language. Let&#8217;s start!!!<\/p>\n<h3>Basic Interview Questions on C++ Programming Language<\/h3>\n<p><strong>Q.1<\/strong> Write a program to check for equality without using arithmetic and comparison operators?<br \/>\n<strong>Ans.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;iostream&gt;\nusing namespace std;\nint main(){\n   int a = 16;\n   int b = 13;\n   if ( (a ^ b) )\n  \tcout&lt;&lt;\"a is not equal to b\";\n   else\n  \tcout&lt;&lt;\"a is equal to b\";\n  \treturn 0;\n}\n<\/pre>\n<p><strong>Output-<\/strong><br \/>\na is not equal to b<\/p>\n<p><strong>Q.2<\/strong> What is the output of the following program?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">main()\n {\nextern int i;\n cout&lt;&lt;i&lt;&lt;endl;\n }\nint i=20;\n<\/pre>\n<p><strong>Ans.<\/strong> 20<\/p>\n<p><strong>Q.3<\/strong> What is the output of the following program?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint foo(unsigned int n, unsigned int r) {\n  if (n  &gt; 0) return (n%r +  foo (n\/r, r ));\n  else return 0;\n}\nint main()\n{\nint res;\nres = foo(22,30);\ncout&lt;&lt;res;\nreturn 0;\n}\n<\/pre>\n<p><strong>Ans.<\/strong> 22<\/p>\n<p><strong>Q.4<\/strong> How to allocate a 2d array dynamically in C++?<br \/>\n<strong>Ans.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nint main()\n{\n  int row = 2, col = 2;\n  int* a =  new int[row * col];\n   \n  int i, j, count = 0;\n  for (i = 0; i &lt;  row; i++)\n  \tfor (j = 0; j &lt; col; j++)\n     \t*(a+ i*col + j) = count++;\n   \n  for (i = 0; i &lt;  row; i++)\n  \tfor (j = 0; j &lt; col; j++)\n     \tprintf(\"%d \", *(a + i*col + j));\n   \n  delete[ ] a;\n  return 0;\n}\n<\/pre>\n<p><strong>Q.5<\/strong> Can you write a program to generate random numbers in C++?<br \/>\n<strong>Ans.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\n#include &lt;random&gt;\nusing namespace std;\nint main()\n{\n   int max=15, min=5,i;\n   int range = max - min + 1;\n   for (i=min; i&lt;max;i++)\n  {\n    \tint num = rand() % range + min;\n    \tcout&lt;&lt;num;\n  }\n  return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><br \/>\nIt will generate 8 digit random numbers.<\/p>\n<p><strong>Q.6<\/strong> Why do you use the namespace std in C++?<\/p>\n<p><strong>Ans.<\/strong> A computer needs to know the code for the cout, cin functionalities and it also needs to know which namespace they are defined. But it is not necessary to write namespace, you can simply use scope resolution(::). It works the same as the namespace. Usage of namespace is a bad practice. The problem with putting \u2018using namespace\u2019 in the header files of your classes is that it forces anyone who wants to use your classes to also be using those other namespaces.<\/p>\n<p><strong>Q.7<\/strong> How to remove segmentation faults in C++?<\/p>\n<p><strong>Ans.<\/strong> It basically means that whenever a piece of code tries to perform a read and write operation in a read only location in memory. It indicates an error in memory corruption.<br \/>\nSome reasons and their solutions behind segmentation fault:-<\/p>\n<ul>\n<li>Reason:- Accessing an address that is freed.<\/li>\n<li>Solution:- Before freeing the pointer, check the assignment or any operation which is required to perform.<\/li>\n<li>Reason:- Accessing out of array index bounds.<\/li>\n<li>Solution:- Correct the array bound.<\/li>\n<li>Reason:- Dereferencing uninitialized pointer<\/li>\n<li>Solution:- A pointer must point to valid memory before accessing it.<\/li>\n<li>Reason:- Stack Overflow<\/li>\n<li>Solution:- Having a base condition to return from the recursive function.<\/li>\n<\/ul>\n<p><strong>Q.8<\/strong> How to sort a string using C++?<\/p>\n<p><strong>Ans.<\/strong> You can use the sort() function to sort a string in C++.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\n#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\nint main ()\n{\n  string str;\n  cout&lt;&lt;\"Enter String\"&lt;&lt;endl;\n  cin&gt;&gt;str;\n  sort(str.begin(),str.end());\n  cout&lt;&lt;\"Sorted String \"&lt;&lt;endl;\n  cout&lt;&lt;str;\n  return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><br \/>\nEnter String<br \/>\nabdjndod<br \/>\nSorted String<br \/>\nabdddjno<\/p>\n<p><strong>Q.9<\/strong> Why is C++ an object oriented programming language?<\/p>\n<p><strong>Ans.<\/strong> C++ always views a problem in terms of objects rather than the procedure of doing it. In object oriented programming, objects represent an entity that can store data and has its interface through functions. The advantages of oop programming is that it makes the program less complex and enhances readability. Components are reusable and extendible. It is also very much desired for maintenance and long term support.<\/p>\n<p>All the member functions of an object may not be used thus it introduces code overhead. If you want to write large business logics and large applications or games then you can make use of the oop programming.<\/p>\n<h3>Intermediate Interview Questions on C++ Language<\/h3>\n<p><strong>Q.10<\/strong> How would you implement the Isa and Hasa class relationships?<\/p>\n<p><strong>Ans.<\/strong> A specialized class has Isa relationship with another class. It is best implemented with inheritance. A class may have an instance of another class. For example, an employee \u2018has\u2019 a salary, therefore the employee has the \u2018HASA\u2019 relationship with the salary class. It is best implemented by setting an object of the Salary class in the Employee class.<\/p>\n<p>There are other relationships also. When one class uses the service of another then it is known as the USESA relationship.<\/p>\n<p><strong>Q.11<\/strong> Why a template is a better solution than a base class?<\/p>\n<p><strong>Ans.<\/strong> When you design a generic class to contain or manage objects of other types and when the format and the behaviour of the other types are not important to their management then you can use a template rather than using a base class. You can also use templates when those other types are not known to the designer of the container or managed class.<\/p>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;  \nusing namespace std;  \ntemplate&lt;class T&gt; T add(T &amp;i,T &amp;j)  \n{  \n  T res = i+j;  \n  return res;\n}  \nint main()  \n{  \n  int x =2;  \n  int y =3;  \n  cout&lt;&lt;\"The value of the addition of is: \"&lt;&lt;add(x,y);  \n  return 0;  \n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<p>The value of the addition of is: 5<\/p>\n<p><strong>Q.12<\/strong> In C++, how to write to a file?<br \/>\n<strong>Ans.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\n#include &lt;fstream&gt;\nusing namespace std;\nint main()\n{\nfstream exp;\nexp.open(\"Tech.txt\",ios::out);  \nif(!exp)\n{\ncout&lt;&lt;\"File creation failed!\";\n}\nelse\n{\ncout&lt;&lt;\"Created a new file!\";\nexp&lt;&lt;\"TechVidvan Tutorial: C++ File Handling!\";    \nexp.close();\n}   \nreturn 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<p>Created a new file!<\/p>\n<p>Name of the file:- Tech.txt<br \/>\nContents:- TechVidvan Tutorial: C++ File Handling!<\/p>\n<p>In the above example, we created a new file named Tech.txt. We used the stream insertion operator(\u201c&lt;&lt;\u201d) to put data into that file. And the data is \u201cTechVidvan Tutorial: C++ File Handling!\u201d.<\/p>\n<p><strong>Q.13<\/strong> Instead of reference of the object, can a copy constructor accept an object of the same class as a parameter?<\/p>\n<p><strong>Ans.<\/strong> No, it cannot be done. It will generate an error if you specify a copy constructor with a first argument which is an object and not a reference. This is specified in the definition of the copy constructor itself.<\/p>\n<p><strong>Copy constructor by value:<\/strong><\/p>\n<p>Syntax :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Tech(const Tech ob)\n<\/pre>\n<p>It will throw you a compile error.<\/p>\n<p><strong>Copy constructor by reference:<\/strong><br \/>\nSyntax :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Tech(const Tech&amp; ob)\n<\/pre>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Tech{\npublic:\n    Tech(){}\n    Tech(const Tech&amp; ob){\n   \t printf(\"Testing\\n\");\n    }\n};\n\nint main()\n{\n    Tech ob1;\n    Tech ob2=ob1;\n    return 0;\n}\n<\/pre>\n<p><strong>Q.14<\/strong> If a constructor fails, how can you handle it?<\/p>\n<p><strong>Ans.<\/strong> You can throw an exception to handle the failure of a constructor. Constructor don\u2019t have a return type. That\u2019s why it is not possible to use return types. All you can do to handle the failure of a constructor is to throw an exception.<\/p>\n<p>You should use exceptions to signal failure in constructors. The memory related with the object is cleaned up if the constructor finishes by throwing an exception.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void fun()\n{\n  A a;\n  B* p = new B();  \n}\n<\/pre>\n<p>If A:A() throws an error then the memory for x will not leak. And if B:B() throws an error then the memory for *p will not leak.<\/p>\n<p><strong>Q.15<\/strong> State the difference between a copy constructor and an overloaded assignment operator?<\/p>\n<p><strong>Ans.<\/strong> An overloaded assignment operator helps you to assign the contents of an existing object to another existing object of the same class.<\/p>\n<p>A copy constructor helps you to construct a new object by using the content of the argument object. It is called when an object is passed by value. Copy constructor is itself a function. You can also perform a user-defined copy constructor.<\/p>\n<p><strong>Q.16<\/strong> Can you call a destructor explicitly on a local variable?<\/p>\n<p><strong>Ans.<\/strong> No, you cannot call a destructor explicitly on a local variable. It will be called again at the close } of the block in which the local is created. It happens automatically. And there is no way to stop it from occurring.<br \/>\nYou will get some bad results if you call a destructor on the same object.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void Example(){\nFred a;\n Fred b;\n \/\/ ...\n}\n<\/pre>\n<p>In the above example, the destructor of b will be executed first then the destructor of a will be executed.<\/p>\n<p><strong>Q.17<\/strong> Why should you use the \u201cplacement_new\u201d?<\/p>\n<p><strong>Ans.<\/strong> There are many types of placement_new. Though a simple way to use this is to place an object at a particular location in memory. It can be done by supplying the place as a pointer parameter to the new part of a new expression.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">include \/\/ Must #include this to use \"placement new\"\n#include \"Fred.h\"\nvoid func()\n{\nchar mem[sizeof(Fred)];\nvoid* place = memory;\nFred* f = new(place) Fred();\n\n}\n<\/pre>\n<p><strong>NOTE:-<\/strong> Use it only when you really care that an object is placed at a particular location in memory. You have to take full responsibility that the pointer which you passed to the \u201cplacement_new\u201d operator points to a region of memory that is big enough.<\/p>\n<p><strong>Q.18<\/strong> State the difference between List x; and List x();?<\/p>\n<p><strong>Ans.<\/strong> There is a big difference between these two. Let\u2019s say that List is the name of the same class. The function func() declares a local list object called x:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void func()\n{\nList x; \/\/ Local object named x \n}\nvoid fun()\n{\nList x(); \/\/ Function named x \n...\n}\n<\/pre>\n<p>In the above example, the func() function declares a local List object called x. And, the function fun() declares a function named x() that returns a List. So, a function within another function.<\/p>\n<p><strong>Q.19<\/strong> What will happen when you create and destroy a derived-class object?<\/p>\n<p><strong>Ans.<\/strong> The constructor of the base class is called to initialize the data members inherited from the base class. The constructor of the derived class is called to initialize the data members which are added in the derived class. It is then reusable.<\/p>\n<p>When the object is destroyed then the destructor of the derived class is called on the object first. Then the destructor of the base class is called on the object.<\/p>\n<p>At last, the allocated space is reused for the full object. Space is allocated on the stack or heap for the full object.<\/p>\n<p><strong>Q.20<\/strong> Why should you use new instead of malloc()?<\/p>\n<p><strong>Ans.<\/strong> Constructor and Destructors:- Unlike malloc(sizeof(Anything)), new Anything() calls Anything\u2019s constructor. Also delete p calls the destructor of *p.<\/p>\n<p>Type Safety:- malloc() returns a void* which is not type safe. new returns a pointer of the right type.<br \/>\nOverridability:- The class can override the new operator. And malloc() is not overridable.<\/p>\n<p><strong>Q.21<\/strong> How to allocate or deallocate an array of things?<br \/>\n<strong>Ans.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Use p = new T[n] and delete[] p:\n\nAnything* p = new Anything[100];\n\u2026\ndelete[] p;\n<\/pre>\n<p>So, when you allocate an array of objects using new, you must use [] in the delete statement. It is useful to differentiate between a pointer to a thing and a pointer to an array of things.<\/p>\n<p><strong>Q.22<\/strong> Can you tell what a dangling pointer is?<\/p>\n<p><strong>Ans.<\/strong> It takes place when you use the address of an object after its lifetime is over. It can happen in situations such as returning addresses of the automatic variables from a function or using the address of the memory block after it is freed.<\/p>\n<p>This is the most common bug that is related to pointers and memory management. It is also known as wild pointers.<br \/>\nYou can avoid the errors caused by the dangling pointer by initializing the pointer to the NULL value.<\/p>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;  \nint *f()  \n{  \nstatic int x=10;  \nreturn &amp;b;  \n}  \nint main()  \n{  \nint *ptr=f();  \nprintf(\"%d\", *ptr);  \nreturn 0;  \n} \n<\/pre>\n<p><strong>Q.23<\/strong> Write a program to check if the word is palindrome or not!<br \/>\n<strong>Ans.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\n#include &lt;string.h&gt;\nusing namespace std;\nint main()\n{\nchar s1[20]=\"nun\", s2[20];\nint i, j, len = 0, flag = 0;\nlen = strlen(s1) - 1;\nfor (i = len, j = 0; i &gt;= 0 ; i--, j++)\ns2[j] = s1[i];\nif (strcmp(s1, s2))\nflag = 1;\ncout &lt;&lt; \"The word is: \" &lt;&lt; s1&lt;&lt;endl;\nif (flag == 1)\ncout &lt;&lt; \"It is not a palindrome!\";\nelse\ncout  &lt;&lt; \"It is a palindrome!\";\nreturn 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><br \/>\nThe word is: nun<br \/>\nIt is a palindrome!<\/p>\n<h3>Advance Interview Questions on C++<\/h3>\n<p><strong>Q.24<\/strong> How to create a thread using C++ programming language?<br \/>\n<strong>Ans.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\n#include &lt;pthread.h&gt;\nusing namespace std;\n\nchar* st = \"Child thread\";\n\nvoid* fun(void *st)\n{\n  cout &lt;&lt; \"Created child thread: \" &lt;&lt; (char*)st;\n}\nint main()\n{\n  pthread_t t;    \n  pthread_create(&amp;t, NULL, &amp;fun, (void*)st);\n  cout &lt;&lt; \"Created Main thread!\" &lt;&lt; endl;\n  pthread_join(t, NULL);\n  exit(EXIT_SUCCESS);\n  return 0;\n}\n<\/pre>\n<p><strong>Output:<\/strong><br \/>\nCreated Main thread!<br \/>\nCreated child thread: Child thread<\/p>\n<p><strong>Q.25<\/strong> State the difference between object oriented programming and procedural programming.<br \/>\n<strong>Ans.<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Procedural Programming<\/b><\/td>\n<td><b>Object Oriented Programming<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">In Procedural programming, there is no data security.<\/span><\/td>\n<td><span style=\"font-weight: 400\">Object oriented programming provides data security by using access specifiers and it is more secure and efficient.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">In procedural programming, the program is divided into functions.<\/span><\/td>\n<td><span style=\"font-weight: 400\">The program is divided into small blocks called objects in object oriented programming.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">It works on the top-down approach.<\/span><\/td>\n<td><span style=\"font-weight: 400\">It works on the bottom-up approach.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">C supports procedural programming.<\/span><\/td>\n<td><span style=\"font-weight: 400\">C++ supports object oriented programming.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Function is more important than data.<\/span><\/td>\n<td><span style=\"font-weight: 400\">Here, data is more important than functions.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">It is based on an unreal world.<\/span><\/td>\n<td><span style=\"font-weight: 400\">It is based on the real world.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Q.26<\/strong> Can you predict the output of the following program?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\n#include &lt;ctype.h&gt;\nusing namespace std;\nint main()\n{\nchar let = 'T', new_let;\nint val = 20, res;\nif(islower(let))\nnew_let = toupper(let);\nelse\nnew_let = val + let;\nres = new_let + 21;\ncout &lt;&lt; res &lt;&lt;endl;\nreturn 0;\n}\n<\/pre>\n<p><strong>Ans.<\/strong> 125<\/p>\n<p><strong>Q.27<\/strong> Write an algorithm to reverse a linked list.<br \/>\n<strong>Ans.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">template\nvoid linkt::reversing()\n{\nnode ptr= head;\nnode nextp= ptr-&gt;_next;\nwhile(nextp)\n{\nnode tmp = nextp-&gt;_next;\nnextp-&gt;_next = ptr;\nptr = nextp;\nnextp = tmp;\n}\nhead-&gt;_next = 0;\nhead = ptr;\n}\n<\/pre>\n<p><strong>Q.28<\/strong> Can you tell me your way of debugging when you came across a problem?<br \/>\n<strong>Ans.<\/strong> You can debug with amazing tools like:-<\/p>\n<ul>\n<li>GDB, DBG, Visual Studio.<\/li>\n<li>You can make use of tusc to trace the last system call before crashing.<\/li>\n<li>Analyzing the core dump<\/li>\n<li>It is very helpful to put debug statements in the program code.<\/li>\n<li>Proofreading your code carefully.<\/li>\n<\/ul>\n<p><strong>Q.29<\/strong> Can you find the error in the following code?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int main()\n{\nconst int data = 230;\nconst int *const ptr = &amp;data;\n(*ptr)++;\nint value = 20;\nptr = &amp;value;\n}\n<\/pre>\n<p><strong>Ans.<\/strong> It is not possible to change the value of a constant variable through the entire code. The statements \u2018(*ptr)++\u2019 and \u2018ptr=&amp;value\u2019 cannot modify a constant object as well.<\/p>\n<p><strong>Q.30<\/strong> In C++, which operators can or cannot be overloaded?<\/p>\n<p><strong>Ans.<\/strong> In C++, we can overload most of the operators except :: and .*. Below is an example of a subscript operator which returns a reference.<\/p>\n<p>Without operator overloading:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Arr{\npublic:\nint&amp; data(unsigned i) { if (i &gt; 99) error(); return val[i]; }\nprivate:\nint val[100];\n};\n\nint main()\n{\nArr a;\na.data(15) = 35;\na.data(11) += a.data(12);\n...\n} \n<\/pre>\n<p>Same one with Operator Overloading:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Arr{\npublic:\nint&amp; operator[] (unsigned i) { if (i &gt; 99) error(); return value[i]; }\nprivate:\nint value[100]; };\n\nint main()\n{\nArr a;\na[15] = 35;\na[11] += a[12];\n...\n}\n<\/pre>\n<p><strong>Q.31<\/strong> Can you guess the output of the following program code?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint main()\n{\nint positive = -2;\ntry\n{\nif (positive &lt; 0)\n{\nthrow \"It must be a positive number!\";\n}\ncout &lt;&lt; positive;\n}\ncatch(const char *msg)\n{\ncout &lt;&lt; \"error: \" &lt;&lt; msg;\n}\nreturn 0;\n}\n<\/pre>\n<p><strong>Ans.<\/strong> error: It must be a positive number!<\/p>\n<p><strong>Q.32<\/strong> What will be the output of the following program?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\n#include &lt;string&gt;\nusing namespace std;\ntemplate&lt;typename Tech&gt;\nvoid display(Tech out)\n{\ncout &lt;&lt; out &lt;&lt; endl;\n}\nint main()\n{\nstring s(\"Have a nice day!\");\ndisplay(s);\nreturn 0;\n}\n<\/pre>\n<p><strong>Ans.<\/strong> Have a nice day!<\/p>\n<p><strong>Q.33<\/strong> What will be the output of the following program?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nclass Tech\n{\nint t;\npublic:\nvirtual void print() = 0;\n};\nclass Random: public Tech\n{\npublic:\nvoid print(){\ncout&lt;&lt;\"Random Class\"&lt;&lt;endl;\n}    \n};\nint main()\n{\nTech t;\na.print();\nreturn 0;\n}\n<\/pre>\n<p><strong>Ans.<\/strong> The above program will give you an error because C++ does not allow you to declare a normal object for an abstract class.<\/p>\n<p><strong>Q.34<\/strong> What will be the output of the following program code?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\n#include &lt;bitset&gt;\nusing namespace std;\nint main()\n{\n    bitset&lt;8&gt; out(200);\n    cout&lt;&lt;out.any();\n}\n<\/pre>\n<p><strong>Ans.<\/strong> 1<\/p>\n<p><strong>Q.35<\/strong> Can you guess the output of the following program code?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint main(){\nint num, val = 3;\nnum = (100 % val ? val + 1 : val - 1);\ncout &lt;&lt; num &lt;&lt;  endl;\nreturn 0;\n}\n<\/pre>\n<p><strong>Ans.<\/strong> 4<\/p>\n<h3>Summary<\/h3>\n<p>In this tutorial, we discussed some advanced interview or job level questions of the C++ programming language and we also gave their answers. These questions will help you to prepare better for the interview.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we are going to discuss C++ Interview questions and Answers which will help you to prepare better for interviews and placements. This will also help you to build your basic knowledge&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":85253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[4335,4339,4340],"class_list":["post-84658","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-c-interview-questions","tag-c-interview-questions-and-answers","tag-c-programming-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ Interview Questions and Answers - TechVidvan<\/title>\n<meta name=\"description\" content=\"Check the most common C++ Interview Questions and Answers. These contain basc, intermediate and advance level C++ interview questions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Interview Questions and Answers - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Check the most common C++ Interview Questions and Answers. These contain basc, intermediate and advance level C++ interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-06T03:30:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/c-interview-questions-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Interview Questions and Answers - TechVidvan","description":"Check the most common C++ Interview Questions and Answers. These contain basc, intermediate and advance level C++ interview questions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/","og_locale":"en_US","og_type":"article","og_title":"C++ Interview Questions and Answers - TechVidvan","og_description":"Check the most common C++ Interview Questions and Answers. These contain basc, intermediate and advance level C++ interview questions.","og_url":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-10-06T03:30:23+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/c-interview-questions-1.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"C++ Interview Questions and Answers","datePublished":"2021-10-06T03:30:23+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/"},"wordCount":2040,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/c-interview-questions-1.jpg","keywords":["C Interview Questions","C++ Interview Questions and Answers","C++ Programming Interview Questions and Answers"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/","url":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/","name":"C++ Interview Questions and Answers - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/c-interview-questions-1.jpg","datePublished":"2021-10-06T03:30:23+00:00","description":"Check the most common C++ Interview Questions and Answers. These contain basc, intermediate and advance level C++ interview questions.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/c-interview-questions-1.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/c-interview-questions-1.jpg","width":1200,"height":628,"caption":"c++ interview questions and answers"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/c-interview-questions-and-answers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"C++ Interview Questions and Answers"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=84658"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84658\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/85253"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=84658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=84658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=84658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}