Table of Contents
What is C++?
C++ is a programming language that allows developers to create software applications. It is an extension of the C programming language and is known for its efficiency and flexibility. C++ is used to build a wide range of applications, from operating systems to video games, and is considered one of the most powerful and versatile programming languages.
Variables in C++
Variables are like containers that store information in a program. They have a name and a type, like “age” (integer) or “name” (string). You can store different values in variables and use them throughout your code. Variables make it easy to work with data and change it as needed.
Classes and Objects in C++
Classes are like blueprints that define the properties and behaviors of an object. They contain variables (properties) and functions (behaviors). Objects are instances of a class, like a specific car or person. Objects have their own values for the properties defined in the class. Classes and objects help organize code and make it easier to work with complex data.
50 C++ MCQ Questions and Answers
Here are 50 C++ MCQ questions for placement with answer. Prepare with these C++ MCQ to get ready for placement or exam, and manage time effectively. It also helps you find out where you need more practice.
1. What does the acronym “C++” stand for?
- A) Common Computer Programming
- B) Central Computing Platform
- C) C Plus Plus
- D) Complete Coding Program
Answer: C) C Plus Plus
2.Which of the following is the correct syntax for a single-line comment in C++?
- A) // This is a comment
- B) /* This is a comment */
- C) # This is a comment
- D) — This is a comment
Answer: A) // This is a comment
3. What is the output of the following code snippet?
int x = 5;
cout << “The value of x is: ” << x++ << endl;
- A) The value of x is: 5
- B) The value of x is: 6
- C) The value of x is: 4
- D) None of the above
4. Which operator is used for dynamic memory allocation in C++?
- A) new
- B) malloc
- C) allocate
- D) create
Answer: A) new
5. What is the output of the following code snippet?
int x = 10;
int y = x << 1;
cout << “Value of y: ” << y << endl;
- A) Value of y: 5
- B) Value of y: 20
- C) Value of y: 2
- D) Value of y: 10
Answer: B) Value of y: 20
6. What is the correct syntax to declare a pointer in C++?
- A) int pointer p;
- B) pointer int p;
- C) int *p;
- D) p = new int;
Answer: C) int p;
7. What does the keyword “const” indicate in C++?
- A) It signifies a constant variable that cannot be modified.
- B) It specifies a function that returns a constant value.
- C) It denotes a variable that is constant throughout the program.
- D) All of the above
Answer: D) All of the above
8. What is the correct way to define a class in C++?
- A) class MyClass;
- B) class MyClass {}
- C) class MyClass();
- D) class = MyClass;
Answer: B) class MyClass {}
9. What is the scope resolution operator in C++?
- A) ::
- B) .
- C) ->
- D) :
Answer: A) ::
10. What is the output of the following code snippet?
- A) Size of the array: 5
- B) Size of the array: 20
- C) Size of the array: 10
- D) Size of the array: 1
Answer: A) Size of the array: 5
11. Which of the following is the correct way to allocate dynamic memory for an array of integers in C++?
- A) int* arr = new int[10];
- B) int* arr = malloc(10 * sizeof(int));
- C) int arr[10];
- D) int arr = new int[10];
Answer: A) int* arr = new int[10];
12. What is the default access specifier for members of a class in C++?
- A) Public
- B) Private
- C) Protected
- D) None
Answer: B) Private
13. Which of the following is not a valid C++ loop construct?
- A) for
- B) while
- C) do-while
- D) repeat-until
Answer: D) repeat-until
14. What does the “this” pointer refer to in a class?
- A) The previous object
- B) The next object
- C) The current object
- D) The parent object
Answer: C) The current object
15. Which function is used to find the length of a string in C++?
- A) size()
- B) length()
- C) strlen()
- D) all of the above
Answer: D) all of the above
16. Which of the following operators cannot be overloaded in C++?
- A) +
- B) –
- C) ::
- D) ==
Answer: C) ::
17. What is the correct syntax for creating a reference variable in C++?
- A) int &ref = var;
- B) int ref = &var;
- C) int ref = var;
- D) int *ref = var;
Answer: A) int &ref = var;
18. Which of the following is used to terminate a loop?
- A) break
- B) continue
- C) exit
- D) end
Answer: A) break
19. What is the output of the following code?
int a = 10;
int b = 20;
swap(a, b);
cout << a << ” ” << b;
- A) 10 20
- B) 20 10
- C) Compiler error
- D) Runtime error
Answer: C) Compiler error
20. Which of the following data types is used to store true/false values in C++?
- A) int
- B) bool
- C) char
- D) float
Answer: B) bool
21. What is the return type of the main() function in C++?
- A) void
- B) int
- C) char
- D) float
Answer: B) int
22. Which header file is required to use the std::vector class?
- A) <vector>
- B) <array>
- C) <list>
- D) <stack>
Answer: A) <vector>
23. What is the output of the following code?
int x = 5;
int y = 10;
cout << x + y << endl;
- A) 5
- B) 10
- C) 15
- D) 0
Answer: C) 15
24. Which of the following is not a type of constructor in C++?
- A) Default constructor
- B) Copy constructor
- C) Move constructor
- D) Assign constructor
Answer: D) Assign constructor
25. Which of the following statements is correct about destructors in C++?
- A) They can be overloaded.
- B) They can have parameters.
- C) They do not have a return type.
- D) They are called manually.
Answer: C) They do not have a return type.
26. What is the correct syntax to inherit a class in C++?
- A) class Derived : public Base {};
- B) class Derived = Base {};
- C) class Derived inherits Base {};
- D) class Derived Base {};
Answer: A) class Derived : public Base {};
27. What is the output of the following code?
int x = 10;
x += 5;
cout << x << endl;
- A) 5
- B) 10
- C) 15
- D) 20
Answer: C) 15
28. Which keyword is used to handle exceptions in C++?
- A) catch
- B) throw
- C) try
- D) all of the above
Answer: D) all of the above
29. Which of the following is used to define a macro in C++?
- A) #define
- B) #macro
- C) #include
- D) #pragma
Answer: A) #define
30. What does the ‘new’ keyword do in C++?
- A) Allocates memory for a variable
- B) Deallocates memory for a variable
- C) Creates a new variable
- D) Deletes a variable
Answer: A) Allocates memory for a variable
31. What is the output of the following code?
int arr[] = {1, 2, 3, 4, 5};
cout << arr[2] << endl;
- A) 1
- B) 2
- C) 3
- D) 4
Answer: C) 3
32. What is a friend function in C++?
- A) A function that is a member of a class
- B) A function that has access to the private and protected members of a class
- C) A function that is defined inside a class
- D) A function that is called within another function
Answer: B) A function that has access to the private and protected members of a class
33. What is polymorphism in C++?
- A) The ability to create multiple classes
- B) The ability to process objects differently based on their data type
- C) The ability to use one function for different purposes
- D) The ability to inherit properties from multiple classes
Answer: B) The ability to process objects differently based on their data type
34. Which of the following is a valid way to declare an integer array of size 5?
- A) int arr[5];
- B) int arr = new int[5];
- C) int arr(5);
- D) int arr[] = {5};
Answer: A) int arr[5];
35. What is the output of the following code?
int a = 10;
int b = 20;
int c = a > b ? a : b;
cout << c << endl;
- A) 10
- B) 20
- C) 30
- D) 0
Answer: B) 20
36. Which of the following is not a storage class specifier in C++?
- A) auto
- B) register
- C) static
- D) volatile
Answer: D) volatile
37. Which of the following is true about virtual functions in C++?
- A) They can be static.
- B) They are defined using the keyword virtual.
- C) They can have a different signature in derived classes.
- D) They cannot be overridden.
Answer: B) They are defined using the keyword virtual.
38. Which of the following is a correct way to declare a function in C++?
- A) void myFunction();
- B) function void myFunction();
- C) myFunction() void;
- D) void = myFunction();
Answer: A) void myFunction();
39. What is encapsulation in C++?
- A) The ability to hide the internal details of an object
- B) The ability to create objects from classes
- C) The ability to inherit properties from another class
- D) The ability to define multiple functions with the same name
Answer: A) The ability to hide the internal details of an object
40. Which of the following is the correct way to declare a constant in C++?
- A) const int x = 10;
- B) int const x = 10;
- C) both A and B
- D) none of the above
Answer: C) both A and B
41. What is the purpose of the ‘using namespace std;’ statement in C++?
- A) It imports the standard library functions.
- B) It allows you to use standard library names without prefixing them with ‘std::’.
- C) It defines a new namespace called ‘std’.
- D) It includes the standard input/output functions.
Answer: B) It allows you to use standard library names without prefixing them with ‘std::’.
42. Which of the following is the correct way to define a pure virtual function in C++?
- A) virtual void myFunction() = 0;
- B) void virtual myFunction() = 0;
- C) pure virtual void myFunction();
- D) virtual void myFunction() {0};
Answer: A) virtual void myFunction() = 0;
43. What does the keyword ‘mutable’ signify in C++?
- A) It allows a member of an object to be modified even if the object is const.
- B) It declares a variable that can change its type at runtime.
- C) It indicates a variable that can only be modified once.
- D) It specifies a variable that is shared across all instances of a class.
Answer: A) It allows a member of an object to be modified even if the object is const.
44. Which of the following is not a feature of object-oriented programming in C++?
- A) Inheritance
- B) Polymorphism
- C) Encapsulation
- D) Compilation
Answer: D) Compilation
45. What is the output of the following code?
int arr[] = {10, 20, 30, 40, 50};
cout << arr[3] << endl;
- A) 10
- B) 20
- C) 30
- D) 40
Answer: D) 40
46. Which of the following cannot be used with the ‘this’ pointer in C++?
- A) Static member functions
- B) Non-static member functions
- C) Member variables
- D) Member functions
Answer: A) Static member functions
47. What is the correct syntax for declaring a function template in C++?
- A) template <class T> void myFunction(T param);
- B) template void myFunction<T>(T param);
- C) template <T> void myFunction(class param);
- D) template <class T> T void myFunction(T param);
Answer: A) template <class T> void myFunction(T param);
48. What is the output of the following code?
int x = 10;
int y = 20;
if (x < y) {
cout << “x is less than y” << endl;
}
- A) x is less than y
- B) x is greater than y
- C) x is equal to y
- D) No output
Answer: A) x is less than y
49. Which of the following correctly describes a constructor in C++?
- A) A function that is called when an object is destroyed.
- B) A function that is used to copy one object to another.
- C) A function that initializes the member variables of an object.
- D) A function that is used to perform some operation on an object.
Answer: C) A function that initializes the member variables of an object.
50. What is the output of the following code?
int a = 5;
int b = 10;
swap(a, b);
cout << a << ” ” << b << endl;
- A) 5 10
- B) 10 5
- C) Compiler error
- D) Runtime error
Answer: C) Compiler error
FAQs on C++
What is the advantage of C++?
The main advantage of C++ is its ability to provide a high level of control over system resources and memory. This allows developers to create efficient and scalable applications that can run on multiple platforms. Additionally, C++ supports object-oriented programming, which makes it easier to write reusable and maintainable code.
Is C++ very useful?
Yes, C++ is a very useful programming language. It is widely used in various industries such as finance, app development, game development, and virtual reality. C++ is also used in operating systems, graphical user interfaces, and embedded systems. Its versatility and performance make it a popular choice for many applications.
What is OOPS in C++?
Object-Oriented Programming (OOPS) in C++ is a programming paradigm that organizes code into objects that contain both data and functions. This allows developers to create reusable and maintainable code by defining classes and objects that encapsulate data and behavior. OOPS in C++ helps to keep the code DRY (Don't Repeat Yourself) and makes it easier to maintain, modify, and debug.
Who invented C++?
C++ was invented by Bjarne Stroustrup as an extension to the C language. He developed C++ in the 1980s to provide a more powerful and flexible programming language that could handle complex applications.
What is the basic concept of C++?
What is the basic concept of C++? The basic concept of C++ is to provide a high-level programming language that can be used to create efficient and scalable applications. C++ is designed to be portable and can be used to develop applications that can run on multiple platforms. The language is also designed to be flexible and can be used for a wide range of applications, from operating systems to graphical user interfaces.