C++ Programming MCQ (Multiple Choice Questions) : Here Are 1000 Mcqs On C++ (Chapterwise)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

C++ Programming MCQ (Multiple Choice Questions)

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
Answer: d
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
Answer: c
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
Answer: b
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
Answer: d
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 user-defined header file extension used in c++?


a) hg
b) cpp
c) h
d) hf
View Answer
Answer: c
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. 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
Answer: a
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
Answer: d
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
Answer: c
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
Answer: d
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
Answer: b
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
Answer: c
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
Answer: d
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
Answer: c
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
Answer: d
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
Answer: c
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?

1. #include <iostream>
2. #include <string>
3. #include <algorithm>
4. using namespace std;
5. int main()
6. {
7. string s = "spaces in text";
8. s.erase(remove(s.begin(), s.end(), ' ' ), s.end() ) ;
9. cout << s << endl;
10. }
a) spacesintext
b) spaces in text
c) spaces
d) spaces in
View Answer
Answer: a
Explanation: In this program, We formed a algorithm to remove spaces in the string.
Output:

You might also like