C++ Programming Questions and Answers – C++ Concepts – 3

«
»

This set of C++ Programming Question Paper focuses 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
View Answer

Answer: b
Explanation: 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++.
advertisement

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

#include<stdio.h>
struct STRUCT
{
private:
	int a;
};
int main()
{
	printf("%d\n", (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++
View Answer

Answer: b
Explanation: 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
View Answer

Answer: b
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.
advertisement

4. Which of the following operator is used with this pointer to access members of a class?
a) .
b) !
c) ->
d) ~
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: Run-time polymorphism is also known as dynamic polymorphism as it is implemented during the run-time of the program.
advertisement

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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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++
View Answer

Answer: b
Explanation: 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?

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

a) Hellow World
b) Compile-time error
c) Run-time error
d) Segmentation fault
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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 <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) Both code 1 and code 2
b) Code 1 only
c) Code 2 only
d) Neither code 1 nor code 2
View Answer

Answer: d
Explanation: 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.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn