250+ TOP MCQs on C++ Concepts – 3 and Answers

C++ Programming Question Paper on “C++ Concepts – 3”.

1. Which of the following statement is correct?
a) Structure in C allows Constructor definition
b) Structure in C++ allows Constructor definition
c) Both allow Constructor definition
d) C allows constructor definition while C++ does not
Answer: b
Clarification: As C does not allow the programmer to define a function inside a structure and constructor itself is a function, therefore, the constructor definition is not allowed in C whereas such definitions are allowed in C++.

2. What happens if the following code is compiled on both C and C++?

#include
struct STRUCT
{
private:
	int a;
};
int main()
{
	printf("%dn", (int)sizeof(struct STRUCT));
	return 0;
}

a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++
Answer: b
Clarification: Access specifiers like private, protected and the public are used because the OOPs concept and as C is not an Object Oriented language, therefore, access specifiers are not defined in C and hence C gives error whereas C++ does not.

3. Which of the following is correct about this pointer in C++?
a) this pointer is passed as a hidden argument in all the functions of a class
b) this pointer is passed as a hidden argument in all non-static functions of a class
c) this pointer is passed as a hidden argument in all static functions of a class
d) this pointer is passed as a hidden argument in all static variables of a class
Answer: b
Clarification: 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.

4. Which of the following operator is used with this pointer to access members of a class?
a) .
b) !
c) ->
d) ~
Answer: c
Clarification: this pointer is a type of pointer and as we know pointer object uses the arrow(->) operator to access the members of the class, therefore, this pointer uses -> operator.

5. Why this pointer is used?
a) To access the members of a class which have the same name as local variables in that scope
b) To access all the data stored under that class
c) To access objects of other class
d) To access objects of other variables
Answer: a
Clarification: this pointer is used to access the members of a class which have the same name as local variables in that part of the code.

6. How many types of polymorphism are there?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two types of polymorphism in C++ namely compile-time polymorphism and run-time polymorphism.

7. What is the other name of compile-time polymorphism?
a) Static polymorphism
b) Dynamic polymorphism
c) Executing polymorphism
d) Non-executing polymorphism
Answer: a
Clarification: Compile-time polymorphism is also known as static polymorphism as it is implemented during the compile-time.

8. What is the other name of run-time polymorphism?
a) Static polymorphism
b) Dynamic polymorphism
c) Executing polymorphism
d) Non-executing polymorphism
Answer: b
Clarification: Run-time polymorphism is also known as dynamic polymorphism as it is implemented during the run-time of the program.

9. Which of the following is correct about static polymorphism?
a) In static polymorphism, the conflict between the function call is resolved during the compile time
b) In static polymorphism, the conflict between the function call is resolved during the run time
c) In static polymorphism, the conflict between the function call is never resolved during the execution of a program
d) In static polymorphism, the conflict between the function call is resolved only if it required
Answer: a
Clarification: The conflict between which function to call is resolved during the compile time in static polymorphism i.e. before the execution of the program starts.

10. Which of the following is correct about dynamic polymorphism?
a) In dynamic polymorphism, the conflict between the function call is resolved during the compile time
b) In dynamic polymorphism, the conflict between the function call is resolved during the run time
c) In dynamic polymorphism, the conflict between the function call is never resolved during the execution of the program
d) In dynamic polymorphism, the conflict between the function call is resolved at the beginning of the program
Answer: b
Clarification: The conflict between which function to call is resolved during the run time in dynamic polymorphism i.e. the conflict is resolved when the execution reaches the function call statement.

11. Which of the following operator(s) can be used with pointers?
i) – only
ii) +, *
iii) +, –
iv) +, -, *
v) /
vi) + only
a) i only
b) vi only
c) ii and v
d) iv
Answer: a
Clarification: The only arithmetic operator that can be used with a pointer is – subtraction operator. No arithmetic operator can be used with pointers.

12. What is std in C++?
a) std is a standard class in C++
b) std is a standard namespace in C++
c) std is a standard header file in C++
d) std is a standard file reading header in C++
Answer: b
Clarification: std is a standard namespace present in C++ which contains different stream classes and objects like cin, cout, etc. and other standard functions.

13. What will be the output of the following C++ code?

#include 
 
int main(int argc, char const *argv[])
{
	cout<<"Hello World";
	return 0;
}

a) Hello World
b) Compile-time error
c) Run-time error
d) Segmentation fault
Answer: b
Clarification: cout is defined under the namespace std and without including std namespace we cannot cout, therefore, the program gives an error.

14. Which of the following syntax can be used to use a member of a namespace without including that namespace?
a) namespace::member
b) namespace->member
c) namespace.member
d) namespace~member
Answer: a
Clarification: To use a member of a namespace without including the namespace is done by this syntax namespace::member.

15. Which of the following C++ code will give error on compilation?

================code 1=================
#include 
using namespace std;
int main(int argc, char const *argv[])
{
	cout<<"Hello World";
	return 0;
}
========================================
================code 2=================
#include 
int main(int argc, char const *argv[])
{
	std::cout<<"Hello World";
	return 0;
}
========================================

a) Both code 1 and code 2
b) Code 1 only
c) Code 2 only
d) Neither code 1 nor code 2
Answer: d
Clarification: Neither code 1 nor code2 will give 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.

Leave a Reply

Your email address will not be published. Required fields are marked *