C++ Programming MCQ (Multiple Choice Questions)
1000+ C++ MCQ are arranged chapterwise! Start practicing now for exams, online tests, quizzes, & interviews! C++ Programming MCQ PDF covers topics like C++
1. Who invented C++?
a) Dennis Ritchie
b) Ken Thompson
c) Brian Kernighan
d) Bjarne Stroustrup
View Answer
2. What is C++?
a) C++ is an object oriented programming language
b) C++ is a procedural programming language
c) C++ supports both procedural and object oriented programming language
d) C++ is a functional programming language
View Answer
3. Which of the following is the correct syntax of including a user defined header files in C++?
a) #include [userdefined]
b) #include “userdefined”
c) #include <userdefined.h>
d) #include <userdefined>
View Answer
4. Which of the following is used for comments in C++?
a) /* comment */
b) // comment */
c) // comment
d) both // comment or /* comment */
View Answer
.5. Which of the following user-defined header file extension used in c++?
a) hg
b) cpp
c) h
d) hf
View Answer
advertisement
6. Which of the following is a correct identifier in C++?
a) VAR_1234
b) $var_name
c) 7VARNAME
d) 7var_name
View Answer
7. Which of the following is not a type of Constructor in C++?
a) Default constructor
b) Parameterized constructor
c) Copy constructor
d) Friend constructor
View Answer
8. Which of the following approach is used by C++?
a) Left-right
b) Right-left
c) Bottom-up
d) Top-down
View Answer
9. What is virtual inheritance in C++?
a) C++ technique to enhance multiple inheritance
b) C++ technique to ensure that a private member of the base class can be accessed somehow
c) C++ technique to avoid multiple inheritances of classes
d) C++ technique to avoid multiple copies of the base class into children/derived class
View Answer
10. What happens if the following C++ statement is compiled and executed?
int *ptr = NULL; delete ptr;
a) The program is not semantically correct
b) The program is compiled and executed successfully
c) The program gives a compile-time error
d) The program compiled successfully but throws an error during run-time
View Answer
11. What will be the output of the following C++ code?
#include <iostream> #include <string> using namespace std; int main(int argc, char const *argv[]) { char s1[6] = "Hello"; char s2[6] = "World"; char s3[12] = s1 + " " + s2; cout<<s3; return 0; }
a) Hello
b) World
c) Error
d) Hello World
View Answer
12. What is the difference between delete and delete[] in C++?
a) delete is syntactically correct but delete[] is wrong and hence will give an error if used in any case
b) delete is used to delete normal objects whereas delete[] is used to pointer objects
c) delete is a keyword whereas delete[] is an identifier
d) delete is used to delete single object whereas delete[] is used to multiple(array/pointer of) objects
View Answer
13. What happens if the following program is executed in C and C++?
#include <stdio.h> int main(void) { int new = 5; printf("%d", new); }
a) Error in C and successful execution in C++
b) Error in both C and C++
c) Error in C++ and successful execution in C
d) A successful run in both C and C++
View Answer
14. What happens if the following program is executed in C and C++?
#include <stdio.h> void func(void) { printf("Hello"); } void main() { func(); func(2); }
a) Outputs Hello twice in both C and C++
b) Error in C and successful execution in C++
c) Error in C++ and successful execution in C
d) Error in both C and C++
View Answer
15. Which of the following is correct about this pointer in C++?
a) this pointer is passed as a hidden argument in all static variables of a class
b) this pointer is passed as a hidden argument in all the functions of a class
c) this pointer is passed as a hidden argument in all non-static functions of a class
d) this pointer is passed as a hidden argument in all static functions of a class
View Answer
16. What will be the output of the following C++ code?
- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
- int main()
- {
- string s = “spaces in text”;
- s.erase(remove(s.begin(), s.end(), ‘ ‘ ), s.end() ) ;
- cout << s << endl;
- }
a) spacesintext
b) spaces in text
c) spaces
d) spaces in
View Answer
17. Which of the following C++ code will give error on compilation?
================code 1================= #include <iostream> using namespace std; int main(int argc, char const *argv[]) { cout<<"Hello World"; return 0; } ======================================== ================code 2================= #include <iostream> int main(int argc, char const *argv[]) { std::cout<<"Hello World"; return 0; } ========================================
a) Code 1 only
b) Neither code 1 nor code 2
c) Both code 1 and code 2
d) Code 2 only
View Answer
18. Which of the following type is provided by C++ but not C?
a) double
b) float
c) int
d) bool
View Answer
19. What is the value of p in the following C++ code snippet?
- #include <iostream>
- using namespace std;
- int main()
- {
- int p;
- bool a = true;
- bool b = false;
- int x = 10;
- int y = 5;
- p = ((x | y) + (a + b));
- cout << p;
- return 0;
- }
a) 12
b) 0
c) 2
d) 16
View Answer
20. By default, all the files in C++ are opened in _________ mode.
a) Binary
b) VTC
c) Text
d) ISCII
View Answer
21. What will be the output of the following C++ function?
- int main()
- {
- register int i = 1;
- int *ptr = &i;
- cout << *ptr;
- return 0;
- }
a) Runtime error may be possible
b) Compiler error may be possible
c) 1
d) 0
View Answer
22. Which of the following correctly declares an array in C++?
a) array{10};
b) array array[10];
c) int array;
d) int array[10];
View Answer
23. What is the size of wchar_t in C++?
a) Based on the number of bits in the system
b) 2 or 4
c) 4
d) 2
View Answer
24. What will be the output of the following C++ code?
#include<iostream> using namespace std; int main () { int cin; cin >> cin; cout << "cin: " << cin; return 0; }
a) Segmentation fault
b) Nothing is printed
c) Error
d) cin: garbage value
View Answer
25. What is the use of the indentation in c++?
a) r distinguishes between comments and inner data
b) distinguishes between comments and outer data
c) distinguishes between comments and code
d) r distinguishes between comments and outer data
View Answer
26. Which is more effective while calling the C++ functions?
a) call by object
b) call by pointer
c) call by value
d) call by reference
View Answer
27. What will be the output of the following C++ program?
#include <iostream> #include <string> #include <cstring> using namespace std; int main(int argc, char const *argv[]) { const char *a = "Hello\0World"; cout<<a; return 0; }
a) Hello
b) World
c) Error
d) Hello World
View Answer
28. Which of the following is used to terminate the function declaration in C++?
a) ;
b) ]
c) )
d) :
View Answer
29. What will be the output of the following C++ code?
- #include <iostream>
- using namespace std;
- int main()
- {
- char c = 74;
- cout << c;
- return 0;
- }
a) I
b) J
c) A
d) N
View Answer
30. What will be the output of the following C++ program?
- #include <iomanip>
- #include <iostream>
- using namespace std;
- int main()
- {
- cout << setprecision(17);
- double d = 0.1;
- cout << d << endl;
- return 0;
- }
a) compile time error
b) 0.100001
c) 0.11
d) 0.10000000000000001
View Answer
31. Which keyword is used to define the macros in c++?
a) #macro
b) #define
c) macro
d) define
View Answer
32. What is the correct syntax of accessing a static member of a class in C++?
--------------------------- Example class: class A { public: static int value; } ---------------------------
a) A->value
b) A^value
c) A.value
d) A::value
View Answer
33. The C++ code which causes abnormal termination/behaviour of a program should be written under _________ block.
a) catch
b) throw
c) try
d) finally
View Answer
34. What is Inheritance in C++?
a) Deriving new classes from existing classes
b) Overloading of classes
c) Classes with same names
d) Wrapping of data into a single class
View Answer
35. What will be the output of the following C++ code?
- #include <iostream>
- using namespace std;
- int main()
- {
- int a = 5;
- float b;
- cout << sizeof(++a + b);
- cout << a;
- return 0;
- }
a) 2 5
b) 4 5
c) 4 6
d) 2 6
View Answer
36. Which of the following symbol is used to declare the preprocessor directives in C++?
a) $
b) ^
c) #
d) *
View Answer
37. What will be the output of the following C++ program?
#include<iostream> using namespace std; int main() { int a = 5; auto check = [=]() { a = 10; }; check(); cout<<"Value of a: "<<a<<endl; return 0; }
a) Segmentation fault
b) Value of a: 5
c) Value of a: 10
d) Error
View Answer
38. What will be the output of the following C++ code?
#include <iostream> using namespace std; void square (int *x, int *y) { *x = (*x) * --(*y); } int main ( ) { int number = 30; square(&number, &number); cout << number; return 0; }
a) 30
b) Error
c) Segmentation fault
d) 870
View Answer
39. What is meant by a polymorphism in C++?
a) class having only single form
b) class having four forms
c) class having many forms
d) class having two forms
View Answer
40. What will be the output of the following C++ program?
#include <iostream> #include <string> using namespace std; int main () { std::string str ("Quearn."); str.back() = '!'; std::cout << str << endl; return 0; }
a) Quearn!
b) Quearn!.
c) Quearn.
d) Quearn.!
View Answer
41. Pick the incorrect statement about inline functions in C++?
a) Saves overhead of a return call from a function
b) They are generally very large and complicated function
c) These functions are inserted/substituted at the point of call
d) They reduce function call overheads
View Answer
42. What will be the output of the following C++ program?
- #include <iostream>
- using namespace std;
- int main()
- {
- int n = 5;
- void *p = &n;
- int *pi = static_cast<int*>(p);
- cout << *pi << endl;
- return 0;
- }
a) 5
b) 6
c) compile time error
d) runtime error
View Answer
43. What is abstract class in C++?
a) Any Class in C++ is an abstract class
b) Class from which any class is derived
c) Class specifically used as a base class with atleast one virtual functions
d) Class specifically used as a base class with atleast one pure virtual functions
44. Which of the following constructors are provided by the C++ compiler if not defined in a class?
a) Copy constructor
b) Default constructor
c) Assignment constructor
d) All of the mentioned
View Answer
45. What will be the output of the following C++ program?
#include <iostream> using namespace std; int main() { try { try { throw 20; } catch (int n) { cout << "Inner Catch\n"; throw; } } catch (int x) { cout << "Outer Catch\n"; } return 0; }
a) Outer Catch
b)
Inner Catch Outer Catch
c) Error
d) Inner Catch
46. Which concept allows you to reuse the written code in C++?
a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation
View Answer
47. What will be the output of the following C++ code snippet?
- #include <iostream>
- using namespace std;
- int operate (int a, int b)
- {
- return (a * b);
- }
- float operate (float a, float b)
- {
- return (a / b);
- }
- int main()
- {
- int x = 5, y = 2;
- float n = 5.0, m = 2.0;
- cout << operate(x, y) <<“\t“;
- cout << operate (n, m);
- return 0;
- }
a) 10.0 5
b) 10 2.5
c) 10.0 5.0
d) 5.0 2.5
View Answer
48. How structures and classes in C++ differ?
a) Structures by default hide every member whereas classes do not
b) In Structures, members are public by default whereas, in Classes, they are private by default
c) Structures cannot have private members whereas classes can have
d) In Structures, members are private by default whereas, in Classes, they are public by default
View Answer
49. What will be the output of the following C++ code?
- #include <iostream>
- using namespace std;
- int main ()
- {
- int a, b, c;
- a = 2;
- b = 7;
- c = (a > b) ? a : b;
- cout << c;
- return 0;
- }
a) 12
b) 14
c) 6
d) 7
View Answer
50. What is the benefit of c++ input and output over c input and output?
a) Both Type safety & Exception
b) Sequence container
c) Exception
d) Type safety
View Answer
51. What will be the output of the following C++ code snippet?
- #include <stdio.h>
- #include<iostream>
- using namespace std;
- int main ()
- {
- int array[] = {0, 2, 4, 6, 7, 5, 3};
- int n, result = 0;
- for (n = 0; n < 8; n++)
- {
- result += array[n];
- }
- cout << result;
- return 0;
- }
a) 21
b) 27
c) 26
d) 25
View Answer
52. What will be the output of the following C++ program?
- #include <iostream>
- #include <string>
- using namespace std;
- int main ()
- {
- string str (“Sanfoundry”);
- for (size_t i = 0; i < str.length();)
- {
- cout << str.at(i-1);
- }
- return 0;
- }
a) runtime error
b) Que
c) S
d) Quearn
53. What will be the output of the following C++ program?
#include <iostream> using namespace std; class A{ public: A(){ cout<<"Constructor called\n"; } ~A(){ cout<<"Destructor called\n"; } }; int main(int argc, char const *argv[]) { A *a = new A[5]; delete[] a; return 0; }
a) Segmentation fault
b) “Constructor called” five times and then “Destructor called” five times
c) “Constructor called” five times and then “Destructor called” once
d) Error
Keywords: what is class in c++ mcq, c programming mcq with answers pdf, c programming mcq, c++ mcq