Here are 1000 MCQs on C++ (Chapterwise).
1. Who invented C++?
a) Dennis Ritchie
b) Ken Thompson
c) Brian Kernighan
d) Bjarne Stroustrup
View Answer
Explanation: Bjarne Stroustrup is the original creator of C++ in 1979 at AT&T Bell Labs.
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
Explanation: C++ supports both procedural(step by step instruction) and object oriented programming (using the concept of classes and objects).
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
Explanation: C++ uses double quotes to include a user-defined header file. The correct syntax of including user-defined is #include “userdefinedname”.
4. Which of the following is used for comments in C++?
a) /* comment */
b) // comment */
c) // comment
d) both // comment or /* comment */
View Answer
Explanation: Both the ways are used for commenting in C++ programming. // is used for single line comments and /* … */ is used for multiple line comments.
5. Which of the following extension is used for user-defined header file in c++?
a) hg
b) cpp
c) h
d) hf
View Answer
Explanation: .h extensions are used for user defined header files. To include a user defined header file, one should use #include”name.h” i.e. it should be enclosed within double quotes.
6. Which of the following is a correct identifier in C++?
a) VAR_1234
b) $var_name
c) 7VARNAME
d) 7var_name
View Answer
Explanation: The rules for writing an identifier is as follows:
i) may contain lowercase/uppercase letters, digits or underscore(_) only
ii) should start with a non-digit character
iii) should not contain any special characters like @, $, etc.
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
Explanation: Friend function is not a constructor whereas others are a type of constructor used for object initialization.
8. Which of the following approach is used by C++?
a) Left-right
b) Right-left
c) Bottom-up
d) Top-down
View Answer
Explanation: C++ is an object-oriented language and OOL uses a bottom-up approach to solve/view a problem.
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
Explanation: Virtual inheritance is a C++ technique with which it ensures that a derived class contains only one copy of the base class’s variables. Refer Wikipedia for more info.
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
Explanation: The above statement is syntactically and semantically correct as C++ allows the programmer to delete a NULL pointer, therefore, the program is compiled and executed successfully.
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
Explanation: There is no operation defined for the addition of character array in C++ hence the compiler throws an error as it does not understood what to do about this expression.
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
Explanation: delete is used to delete a single object initiated using new keyword whereas delete[] is used to delete a group of objects initiated with the new operator.
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
Explanation: new is a keyword in C++, therefore, we cannot declare a variable with name new but as there is no such keyword new in C, therefore, the program is compiled and executed successfully in C.
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
Explanation: As the func(void) needs no argument during its call, hence when we are calling func(2) with 2 as passed as a parameter then this statement gives the error in both C++ and C compiler.
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
Explanation: As static functions are a type of global function for a class so all the object shares the common instance of that static function whereas all the objects have there own instance for non-static functions and hence they are passed as a hidden argument in all the non-static members but not in static members.
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
Explanation: In this program, We formed a algorithm to remove spaces in the string.
Output:
$ g++ dan.cpp $ a.out spacesintext
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
Explanation: Neither code 1 nor code 2 will give an error as both are syntactically correct as in first code we have included namespace std and in second one we have used scope resolution operator to resolve the conflict.
18. Which of the following type is provided by C++ but not C?
a) double
b) float
c) int
d) bool
View Answer
Explanation: C++ provides the boolean type to handle true and false values whereas no such type is provided in C.
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
Explanation: | means bitwise OR operation so x | y (0101 | 1010) will be evaluated to 1111 which is integer 15 and as a is true and b is false so a+b(1 + 0) = 1. So final value of expression in line #10 will be 15 + 1 = 16.
20. By default, all the files in C++ are opened in _________ mode.
a) Binary
b) VTC
c) Text
d) ISCII
View Answer
Explanation: By default, all the files in C++ are opened in text mode. They read the file as normal text.
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
Explanation: Using & on a register variable may be invalid, since the compiler may store the variable in a register, and finding the address of it is illegal.
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
Explanation: Because array variable and values need to be declared after the datatype only.
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
Explanation: Compiler wants to make CPU as more efficient in accessing the next value.
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
Explanation: cin is a variable hence overrides the cin object. cin >> cin has no meaning so no error.
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
Explanation: To distinguish between different parts of the program like comments, codes, etc.
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
Explanation: In the call by reference, it will just passes the reference of the memory addresses of passed values rather than copying the value to new memories which reduces the overall time and memory use.
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
Explanation: char* are terminated by a ‘\0’ character so the string “Hello\0World” will be cut down to “Hello”.
28. Which of the following is used to terminate the function declaration in C++?
a) ;
b) ]
c) )
d) :
View Answer
Explanation: ; semicolon is used to terminate a function declaration statement in C++.
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
Explanation: The literal value for 74 is J. So it will be printing J.
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
Explanation: The double had to truncate the approximation due to its limited memory, which resulted in a number that is not exactly 0.1.
Output:
$ g++ float2.out $ a.out 0.10000000000000001
31. Which keyword is used to define the macros in c++?
a) #macro
b) #define
c) macro
d) define
View Answer
Explanation: #define is the keyword that is used to define the macros in c++.
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
Explanation: Scope resolution operator(::) is used to access a static member of a class.
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
Explanation: Code that leads to the abnormal termination of the program should be written under the try block.
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
Explanation: Inheritance is the concept of OOPs in which new classes are derived from existing classes in order to reuse the properties of classes defined earlier.
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
Explanation: The a as a integer will be converted to float while calculating the size. The value of any variable doesn’t modify inside sizeof operator. Hence value of variable a will remain 5.
Output:
$ g++ size3.cpp $ a.out 4 5
36. Which of the following symbol is used to declare the preprocessor directives in C++?
a) $
b) ^
c) #
d) *
View Answer
Explanation: # symbol is used to declare the preprocessor directives.
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
Explanation: As this lambda expression is capturing the extrenal variable by value therefore the value of a cannot be changes inside the lambda expression hence the program gives error.
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
Explanation: As we are passing value by reference therefore the change in the value is reflected back to the passed variable number hence value of number is changed to 870.
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
Explanation: Polymorphism is literally meant class having many forms.
40. What will be the output of the following C++ program?
#include <iostream> #include <string> using namespace std; int main () { std::string str ("Sanfoundry."); str.back() = '!'; std::cout << str << endl; return 0; }
a) Sanfoundry!
b) Sanfoundry!.
c) Sanfoundry.
d) Sanfoundry.!
View Answer
Explanation: back() function modifies the last character of the string with the character provided.
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
Explanation: Inline are functions that are expanded when it is called. The whole code of the inline function gets inserted/substituted at the point of call. In this, they help in reducing the function call overheads. Also they save overhead of a return call from a function. Inline functions are generally kept small.
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
Explanation: We just casted this from void to int, so it prints 5
Output:
$ g++ poi1.cpp $ a.out 5
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
View Answer
Explanation: An abstract class is defined as a class which is specifically used as a base class. An abstract class should have atleast one pure virtual function.
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) Copy Assignment Operator
d) All of the mentioned
View Answer
Explanation: If a programmer does not define the above constructors in a class the C++ compiler by default provides these constructors to avoid error on basic operations.
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
View Answer
Explanation: The exception thrown by the inner try catch block is caught by the inner block hence “Inner Catch” is printed but as inner catch block again throws an exception further therefore the exception is thrown further which is caught by the outer catch block hence “Outer Catch” is also printed.
46. Which concept allows you to reuse the written code in C++?
a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation
View Answer
Explanation: Inheritance allows you to reuse your already written code by inheriting the properties of written code into other parts of the code, hence allowing you to reuse the already written code.
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
Explanation: In this program, we are divide and multiply the values.
Output:
$ g++ over3.cpp $ a.out 10 2.5
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
Explanation: Structure members are public by default whereas, class members are private by default. Both of them can have private and public members.
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
Explanation: We are using the ternary operator to evaluate this expression. It will return first option, if first condition is true otherwise it will return second
Output:
$ g++ ess1.cpp $ a.out 7
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
Explanation: C++ input and output are type safety that means we don’t need to specify the type of variable we are printing.
eg:
in C we need to specify %d showing that an integer will be printed, whereas in C++ we just cout the variable.
printf(“%d”, a);
cout<<a;
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
Explanation: We are adding all the elements in the array and printing it. Total elements in the array is 7, but our for loop will go beyond 7 and add a garbage value.
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) Sanfo
c) S
d) Sanfoundry
View Answer
Explanation: This program will terminate because the cout element is out of range.
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
View Answer
Explanation: In the above program we have first initiated five-pointer variables using new keyword hence fives time constructor will be called after that as we using delete[] (used for deleting multiple objects) to delete variables hence all the five objects created will be destroyed and hence five times destructor will be called.
Chapterwise Multiple Choice Questions on C++
- C++ Basic Concepts
- C++ Types, Pointers, Arrays & Structures
- C++ Functions, Namespaces & Exceptions
- C++ Source Files, Classes and Operator Overloading
- C++ Derived Classes, C++ Templates & Exception Handling
- C++ Class Hierarchies, C++ Library & Containers
- C++ Algorithms, Objects & Iterators
- C++ Strings, Streams & Numerics
- Advanced C++
1. C++ MCQ on Basic Concepts
The section contains C++ multiple choice questions and answers on basics, oops concepts, c++ concepts, static constant keyword, differences between c and c++.
|
|
2. C++ MCQ on Types, Pointers, Arrays & Structures
The section contains C++ questions and answers on integer, float, character and boolean types. It also contains arrays, pointers, references and structures.
3. C++ Programming Multiple Choice Questions on Functions, Namespaces & Exceptions
The section contains C++ MCQs on function declaration and overloading, operators and statements, values and arguments, macros, namespaces and exceptions.
4. C++ MCQ on Source Files, Classes and Operator Overloading
The section contains C++ multiple choice questions and answers on classes and functions, fiend function, objects and operators, operator overloading, constructors and destructors, subscripting and dereferencing and other string classes.
5. C++ Programming MCQ on Derived Classes, Templates & Exception Handling
The section contains C++ Programming questions and answers on different types of classes like abstract, derived and their hierarchies, different types of templates like simple string and function and their derivation and specialization. The section also has error and exception handling, different types of exceptions and their resource management.
6. C++ MCQ on Class Hierarchies, Library & Containers
The section contains C++ MCQs on different aspects of a container which includes creation and design of new containers, vectors and sequences, types of inheritance and various class hierarchies, sequences like seq_con array class, seq_con vector class, stl – pair and heap, vtable, vptr, generators, array type manipulations, tuples, complex library, valarray, bitset and class relationships.
7. Multiple Choice Questions on C++ Algorithms, Objects & Iterators
The section contains C++ multiple choice questions and answers on different types of algorithms like C style, standard library, modifying sequence and non modifying sequence, different types of iterators, stl algorithms, functors, sequences, containers and allocators.
8. C++ Programming MCQs on Strings, Streams & Numerics
The section contains C++ Programming questions and answers on basic strings and their characters, I/O streams, file and string streams, standard library and mathematical applications like numeric limits, file handling, vector arithmetic and random numbers.
9. Multiple Choice Questions on Advanced C++
The section contains C++ MCQs on lambda expressions and command line arguments.
|
|
Wish you the best in your endeavor to learn and master C++!
Important Links:
- C++ Books
- C++ Programming Books
- Object Oriented Programming in C++ Books
- C++ Online Test
- C++ Internship
- C Multiple Choice Questions
- Java Multiple Choice Questions
- Python Multiple Choice Questions
- C# Multiple Choice Questions
- Data Structures Multiple Choice Questions
- Programming Multiple Choice Questions
- Computer Science Multiple Choice Questions