{"id":10096,"date":"2023-05-01T06:19:50","date_gmt":"2023-05-01T06:19:50","guid":{"rendered":"https:\/\/www.scmgalaxy.com\/tutorials\/?p=10096"},"modified":"2023-06-19T13:30:01","modified_gmt":"2023-06-19T13:30:01","slug":"top-50-c-interview-questions-with-answers-2","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/","title":{"rendered":"Top 50 C++ Interview Questions with Answers"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/05\/image-1.png\" alt=\"\" class=\"wp-image-10097\" width=\"660\" height=\"366\" srcset=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/05\/image-1.png 900w, https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/05\/image-1-300x167.png 300w, https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/05\/image-1-768x427.png 768w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><figcaption class=\"wp-element-caption\"><strong><em>C++ Interview Questions with Answers<\/em><\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">1) What is the size of a character in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) 8 bits<br>b) 16 bits<br>c) 32 bits<br>d) 64 bits<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 8 bits<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">2) Which of the following is the correct syntax for declaring a pointer?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) int *p;<br>b) *int p;<br>c) int p*;<br>d) *p int;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) int *p;<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">3) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int a = 10, b = 20;<br>a = a ^ b;<br>b = a ^ b;<br>a = a ^ b;<br>std::cout &lt;&lt; a &lt;&lt; &#8221; &#8221; &lt;&lt; b;<br>a) 20 10<br>b) 30 20<br>c) 10 20<br>d) 20 30<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 20 10<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">4) Which of the following is a correct way to allocate an integer array of size 10 on the heap?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) int array[10];<br>b) int *array = new int[10]();<br>c) int *array = new int(10);<br>d) int array = new int[10];<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) int *array = new int[10]();<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">5) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int x = 10, y = 5;<br>std::cout &lt;&lt; (x &gt; y ? (x &lt; 15 ? 1 : 2) : 0);<br>a) 0<br>b) 1<br>c) 2<br>d) Compilation error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 1<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">6) Which of the following statements is true about the const keyword in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) It can be used to declare a constant variable.<br>b) It can be used to declare a function parameter that cannot be modified.<br>c) It can be used to declare a function return value that cannot be modified.<br>d) All of the above<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) All of the above<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">7) Which of the following is the correct way to declare a virtual function in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) virtual int foo();<br>b) virtual int foo(void);<br>c) virtual int foo() = 0;<br>d) virtual int foo() const;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) virtual int foo();<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">8) Which of the following is the correct syntax to open a file in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) std::filestream myFile(&#8220;filename&#8221;, mode);<br>b) std::fstream myFile(&#8220;filename&#8221;, mode);<br>c) std::ifstream myFile(&#8220;filename&#8221;, mode);<br>d) Both b and c<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) Both b and c<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">9) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int a = 10, b = 5;<br>int &amp;refA = a, &amp;refB = b;<br>refA = refB;<br>std::cout &lt;&lt; a &lt;&lt; &#8221; &#8221; &lt;&lt; b;<br>a) 5 10<br>b) 10 5<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 5 10<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">10) Which of the following statements about templates in C++ is true?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) They allow generic programming in C++.<br>b) They provide a way to define a common functionality for different data types.<br>c) They can be used to define functions or classes.<br>d) All of the above<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) All of the above<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">11) Which of the following is the correct syntax to pass an array to a function in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) void func(int * arr, int size);<br>b) void func(int arr[], int size);<br>c) void func(int arr[10], int size);<br>d) All of the above<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) void func(int arr[], int size);<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">12) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int a = 10, b = 5, c = 2;<br>std::cout &lt;&lt; (a &gt; b) &lt;&lt; &#8221; &#8221; &lt;&lt; (a &lt; b) &lt;&lt; &#8221; &#8221; &lt;&lt; (a == b) &lt;&lt; &#8221; &#8221; &lt;&lt; (a &gt;= c) &lt;&lt; &#8221; &#8221; &lt;&lt; (b &lt;= c);<br>a) 1 0 0 1 0<br>b) 1 0 0 0 0<br>c) 0 1 0 1 1<br>d) Compilation error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 1 0 0 1 0<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">13) What is a destructor in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) A function that is called automatically when an object is created.<br>b) A function that is called automatically when an object is destroyed.<br>c) A function that is called manually to destroy an object.<br>d) A function that is used to initialize an object.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) A function that is called automatically when an object is destroyed.<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">14) Which of the following is the correct syntax to define a class in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) class MyClass { public: int foo(); };<br>b) class MyClass() { public: int foo(); };<br>c) class MyClass { int foo(); };<br>d) class { int foo(); };<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) class MyClass { public: int foo(); };<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">15) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">std::cout &lt;&lt; sizeof(&#8220;hello world&#8221;);<br>a) 11<br>b) 12<br>c) 13<br>d) Compilation error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 12<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">16) Which of the following is the correct syntax to declare an enumeration in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) enum { RED, GREEN, BLUE };<br>b) enum colors { RED, GREEN, BLUE };<br>c) enum colors { RED = 1, GREEN = 2, BLUE = 3 };<br>d) Both b and c<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) Both b and c<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">17) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int arr[] = {1, 2, 3, 4, 5};<br>std::cout &lt;&lt; sizeof(arr) \/ sizeof(arr[0]);<br>a) 1<br>b) 2<br>c) 3<br>d) 5<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) 5<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">18) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int arr[] = {1, 2, 3, 4, 5};<br>int *ptr = arr;<br>std::cout &lt;&lt; *ptr &lt;&lt; &#8221; &#8221; &lt;&lt; *(ptr + 3) &lt;&lt; &#8221; &#8221; &lt;&lt; *(arr + 4);<br>a) 1 3 5<br>b) 1 4 5<br>c) 1 4 4<br>d) 1 4 1<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 1 4 5<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">19) What is a template specialization in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) It is a way to define a function or class for a specific data type.<br>b) It is a way to define a function or class for a generic data type.<br>c) It is a way to define a function or class for multiple data types at once.<br>d) It is a way to define a function or class for a derived data type.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) It is a way to define a function or class for a specific data type.<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">20) Which of the following is an example of dynamic polymorphism in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) Function overloading<br>b) Constructor overloading<br>c) Operator overloading<br>d) Virtual functions<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) Virtual functions<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">21) Which of the following is not a type of inheritance in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) Public inheritance<br>b) Protected inheritance<br>c) Private inheritance<br>d) Multiple inheritance<br>e) All of the above are types of inheritance<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: e) All of the above are types of inheritance<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">22) Which of the following is the correct way to declare a member function of a class outside the class definition in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) void MyClass::foo() { }<br>b) void foo() { }<br>c) MyClass::void foo() { }<br>d) MyClass::foo() { }<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) void MyClass::foo() { }<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">23) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int a = 10, b = 20, c;<br>c = a++ + &#8211;b;<br>std::cout &lt;&lt; a &lt;&lt; &#8221; &#8221; &lt;&lt; b &lt;&lt; &#8221; &#8221; &lt;&lt; c;<br>a) 10 20 30<br>b) 11 20 30<br>c) 10 19 30<br>d) 11 19 30<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: c) 10 19 30<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">24) Which of the following is the correct syntax to define a destructor in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) ~ MyClass { }<br>b) ~MyClass() { }<br>c) MyClass::~() { }<br>d) Both a and b<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) ~MyClass() { }<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">25) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int a = 10, b = 20;<br>std::swap(a, b);<br>std::cout &lt;&lt; a &lt;&lt; &#8221; &#8221; &lt;&lt; b;<br>a) 10 20<br>b) 20 10<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 20 10<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">26) Which of the following is the correct syntax to create a thread in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) std::thread myThread = new std::thread(foo);<br>b) std::thread myThread(foo);<br>c) std::thread myThread = std::thread::create(foo);<br>d) None of the above<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) std::thread myThread(foo);<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">27) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int arr[] = {1, 2, 3, 4, 5};<br>for (int i : arr) {<br>std::cout &lt;&lt; i &lt;&lt; &#8221; &#8220;;<br>}<br>a) 1 2 3 4 5<br>b) 5 4 3 2 1<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 1 2 3 4 5<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">28) Which of the following is a correct way to include a header file in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) #include &#8220;myFile.h&#8221;<br>b) #include<br>c) #include<br>d) Both a and b<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) Both a and b<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">29) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int arr[] = {1, 2, 3, 4, 5};<br>std::cout &lt;&lt; arr &lt;&lt; &#8221; &#8221; &lt;&lt; &amp;arr[0];<br>a) Same memory address<br>b) Different memory addresses<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) Same memory address<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">30) Which of the following is the correct syntax to declare a constant pointer in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) int * const ptr = &amp;x;<br>b) const int * ptr = &amp;x;<br>c) const int * const ptr = &amp;x;<br>d) Both a and c<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) Both a and c<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">31) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int arr[] = {1, 2, 3, 4, 5};<br>int *ptr = arr;<br>std::cout &lt;&lt; arr + 4 &lt;&lt; &#8221; &#8221; &lt;&lt; ptr + 4;<br>a) Same memory address<br>b) Different memory addresses<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) Same memory address<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">32) Which of the following is the correct syntax for a lambda function in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) []() { }<br>b) [](int x) { return x * x; }<br>c) [x, y](int z) { return x + y + z; }<br>d) All of the above<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) All of the above<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">33) Which of the following is the correct syntax to declare a base class in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) class Base { protected: int x; }<br>b) class Base : protected { int x; }<br>c) class Base { public: int x; }<br>d) class Base : public { int x; }<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: c) class Base { public: int x; }<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">34) What is a singleton in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) It is a design pattern that restricts the instantiation of a class to one object.<br>b) It is a class that can be instantiated multiple times.<br>c) It is a class that is derived from another class.<br>d) It is a function that takes a single argument as input.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) It is a design pattern that restricts the instantiation of a class to one object.<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">35) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int a = {10}, b = {20};<br>std::cout &lt;&lt; a &lt;&lt; &#8221; &#8221; &lt;&lt; b;<br>a) 10 20<br>b) {10} {20}<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 10 20<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">36) Which of the following is the correct syntax for a copy constructor in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) MyClass(MyClass &amp;obj) { }<br>b) MyClass(MyClass obj) { }<br>c) MyClass(MyClass const &amp;obj) { }<br>d) Both a and c<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: c) MyClass(MyClass const &amp;obj) { }<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">37) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int arr[] = {1, 2, 3, 4, 5};<br>int *ptr = &amp;arr[2];<br>std::cout &lt;&lt; ptr[-2] &lt;&lt; &#8221; &#8221; &lt;&lt; *(ptr + 2);<br>a) 1 5<br>b) 2 4<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 2 4<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">38) Which of the following is the correct syntax to call a superclass constructor in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) SuperClass();<br>b) SuperClass::constructor();<br>c) SuperClass::SuperClass();<br>d) Both b and c<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: c) SuperClass::SuperClass();<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">39) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int x = 10;<br>int *ptr1 = &amp;x, *ptr2 = &amp;x;<br>std::cout &lt;&lt; (ptr1 == ptr2) &lt;&lt; &#8221; &#8221; &lt;&lt; (*ptr1 == *ptr2);<br>a) 1 1<br>b) 0 0<br>c) 1 0<br>d) Compilation error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 1 1<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">40) Which of the following is the correct syntax to declare a function that takes a variable number of arguments in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) void foo(\u2026);<br>b) void foo(int \u2026);<br>c) void foo(int, \u2026);<br>d) Both b and c<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: d) Both b and c<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">41) What is the output of the following code?<\/h3>\n\n\n\n<h1 class=\"wp-block-heading\">include<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">int foo(int x) { return x * x; }<br>int main() {<br>int arr[] = {1, 2, 3, 4, 5};<br>std::transform(arr, arr + 5, arr, foo);<br>for (int i : arr) {<br>std::cout &lt;&lt; i &lt;&lt; &#8221; &#8220;;<br>}<br>}<br>a) 1 4 9 16 25<br>b) 1 2 3 4 5<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) 1 4 9 16 25<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">42) Which of the following is a correct way to declare a constant reference in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) const int &amp; ref = x;<br>b) int const &amp; ref = x;<br>c) Both a and b<br>d) None of the above<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: c) Both a and b<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">43) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int a = 10, b = 20;<br>swap(a, b);<br>std::cout &lt;&lt; a &lt;&lt; &#8221; &#8221; &lt;&lt; b;<br>a) 10 20<br>b) 20 10<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 20 10<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">44) Which of the following is the correct syntax for a move constructor in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) MyClass(MyClass &amp;&amp;obj) { }<br>b) MyClass(MyClass obj &amp;&amp;) { }<br>c) MyClass(MyClass const &amp;&amp;obj) { }<br>d) Both a and c<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) MyClass(MyClass &amp;&amp;obj) { }<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">45) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int a = 10, b = 20;<br>int *ptr1 = &amp;a, *ptr2 = &amp;b;<br>std::swap(ptr1, ptr2);<br>std::cout &lt;&lt; *ptr1 &lt;&lt; &#8221; &#8221; &lt;&lt; *ptr2;<br>a) 10 20<br>b) 20 10<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 20 10<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">46) Which of the following is the correct syntax to call a virtual function in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) obj.foo();<br>b) MyClass::foo();<br>c) virtual obj.foo();<br>d) None of the above<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) obj.foo();<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">47) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int a = 10, b = 20;<br>int *ptr1 = &amp;a, *ptr2 = &amp;b;<br>ptr2 = ptr1;<br>*ptr2 = 30;<br>std::cout &lt;&lt; a &lt;&lt; &#8221; &#8221; &lt;&lt; b;<br>a) 10 20<br>b) 30 20<br>c) Compilation error<br>d) Runtime error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: b) 30 20<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">48) Which of the following is a correct way to declare a static member in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) class MyClass { public: static int x; };<br>b) class MyClass { static int x; public: };<br>c) class MyClass { static int x(); };<br>d) Both a and b<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: a) class MyClass { public: static int x; };<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">49) What is the output of the following code?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">int a = 10, b = 2;<br>double c = static_cast&gt;(a) \/ b;<br>std::cout &lt;&lt; c;<br>a) 5.0<br>b) 5<br>c) 5.00<br>d) Compilation error<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: c) 5.00<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">50) What is a virtual destructor in C++?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">a) It is a function that is called manually to destroy an object.<br>b) It is a function that is used to initialize an object.<br>c) It is a function that is called automatically when an object is destroyed.<br>d) It is a function that is used to create an object.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Answer: c) It is a function that is called automatically when an object is destroyed.<\/h4>\n","protected":false},"excerpt":{"rendered":"<p>1) What is the size of a character in C++? a) 8 bitsb) 16 bitsc) 32 bitsd) 64 bits Answer: [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10096","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Top 50 C++ Interview Questions with Answers - scmGalaxy<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Top 50 C++ Interview Questions with Answers - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"1) What is the size of a character in C++? a) 8 bitsb) 16 bitsc) 32 bitsd) 64 bits Answer: [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/profile.php?id=100003935402658\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-01T06:19:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-19T13:30:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/05\/image-1.png\" \/>\n<meta name=\"author\" content=\"Ashwani Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashwani Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/\"},\"author\":{\"name\":\"Ashwani Kumar\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/19f28f8f969c1679bfb37d4dad6adc31\"},\"headline\":\"Top 50 C++ Interview Questions with Answers\",\"datePublished\":\"2023-05-01T06:19:50+00:00\",\"dateModified\":\"2023-06-19T13:30:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/\"},\"wordCount\":2063,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2023\\\/05\\\/image-1.png\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/\",\"name\":\"Top 50 C++ Interview Questions with Answers - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2023\\\/05\\\/image-1.png\",\"datePublished\":\"2023-05-01T06:19:50+00:00\",\"dateModified\":\"2023-06-19T13:30:01+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/19f28f8f969c1679bfb37d4dad6adc31\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2023\\\/05\\\/image-1.png\",\"contentUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2023\\\/05\\\/image-1.png\",\"width\":900,\"height\":500},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/top-50-c-interview-questions-with-answers-2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Top 50 C++ Interview Questions with Answers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/19f28f8f969c1679bfb37d4dad6adc31\",\"name\":\"Ashwani Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g\",\"caption\":\"Ashwani Kumar\"},\"description\":\"There is no end to education. It is not that you read a book, pass an examination, and finish with education. The whole of life, from the moment you are born to the moment you die, is a process of learning.\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/profile.php?id=100003935402658\",\"https:\\\/\\\/www.instagram.com\\\/adarshashwani\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/adarsh-ashwani-a75b23130\\\/\"],\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/ashwani\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Top 50 C++ Interview Questions with Answers - scmGalaxy","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:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/","og_locale":"en_US","og_type":"article","og_title":"Top 50 C++ Interview Questions with Answers - scmGalaxy","og_description":"1) What is the size of a character in C++? a) 8 bitsb) 16 bitsc) 32 bitsd) 64 bits Answer: [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/","og_site_name":"scmGalaxy","article_author":"https:\/\/www.facebook.com\/profile.php?id=100003935402658","article_published_time":"2023-05-01T06:19:50+00:00","article_modified_time":"2023-06-19T13:30:01+00:00","og_image":[{"url":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/05\/image-1.png","type":"","width":"","height":""}],"author":"Ashwani Kumar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Ashwani Kumar","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/"},"author":{"name":"Ashwani Kumar","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/19f28f8f969c1679bfb37d4dad6adc31"},"headline":"Top 50 C++ Interview Questions with Answers","datePublished":"2023-05-01T06:19:50+00:00","dateModified":"2023-06-19T13:30:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/"},"wordCount":2063,"commentCount":0,"image":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/05\/image-1.png","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/","name":"Top 50 C++ Interview Questions with Answers - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/#primaryimage"},"image":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/05\/image-1.png","datePublished":"2023-05-01T06:19:50+00:00","dateModified":"2023-06-19T13:30:01+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/19f28f8f969c1679bfb37d4dad6adc31"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/#primaryimage","url":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/05\/image-1.png","contentUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2023\/05\/image-1.png","width":900,"height":500},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/top-50-c-interview-questions-with-answers-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Top 50 C++ Interview Questions with Answers"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/19f28f8f969c1679bfb37d4dad6adc31","name":"Ashwani Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b0fe597f4b8473400f95ce54bee0bdd62df08a319a198ad229b279aaab4f5410?s=96&d=mm&r=g","caption":"Ashwani Kumar"},"description":"There is no end to education. It is not that you read a book, pass an examination, and finish with education. The whole of life, from the moment you are born to the moment you die, is a process of learning.","sameAs":["https:\/\/www.facebook.com\/profile.php?id=100003935402658","https:\/\/www.instagram.com\/adarshashwani\/","https:\/\/www.linkedin.com\/in\/adarsh-ashwani-a75b23130\/"],"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/ashwani\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/10096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=10096"}],"version-history":[{"count":2,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/10096\/revisions"}],"predecessor-version":[{"id":10121,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/10096\/revisions\/10121"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=10096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=10096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=10096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}