C++ OOPs Concepts | Set 1
1. Wrapping data and its related functionality into a single entity is known as _____________
a) Abstraction
b) Encapsulation
c) Polymorphism
d) Modularity
Answer: B
Explanation: In OOPs, the property of enclosing data and its related functions into a single entity(in C++ we call them classes) is called encapsulation.
2. How structures and classes in C++ differ?
a) In Structures, members are public by default whereas, in Classes, they are private by default
b) In Structures, members are private by default whereas, in Classes, they are public by default
c) Structures by default hide every member whereas classes do not
d) Structures cannot have private members whereas classes can have
Answer: A
Explanation: Structure members are public by default whereas, class members are private by default. Both of them can have private and public members.
3. What does polymorphism in OOPs mean?
a) Concept of allowing overiding of functions
b) Concept of hiding data
c) Concept of keeping things in differnt modules/files
d) Concept of wrapping things into a single unit
Answer: A
Explanation: In OOPs, Polymorphism is the concept of allowing a user to override functions either by changing the types or number of parameters passed.
4. Which concept allows you to reuse the written code?
a) Encapsulation
b) Abstraction
c) Inheritance
d) Polymorphism
Answer: C
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.
5. Which of the following explains Polymorphism?
a) int func(int, int); float func1(float, float);
b) int func(int); int func(int);
c) int func(float); float func(int, int, char);
d) int func(); int new_func();
Answer: C
Explanation: Polymorphism means overriding the same function by changing types or number of arguments. So we have only two options which has the same function names, but as one can observe that in one option types, name and number of parameters all are same which will lead to an error. Hence that is wrong so the option having same name and different types or number of parameters is correct.
6. Which of the following shows multiple inheritances?
a) A->B->C
b) A->B; A->C
c) A,B->C
d) B->A
Answer: C
Explanation: In multiple inheritance, a single class is inherited from two classes. So in A,B->C, Class C is inherited from A and B, whereas in A->B->C, C from B and B from A called simple inheritance, in A->B; A->C, B and C are inherited from A which is called hierarchical inheritance.
7. How access specifiers in Class helps in Abstraction?
a) They does not helps in any way
b) They allows us to show only required things to outer world
c) They help in keeping things together
d) Abstraction concept is not used in classes
Answer: B
Explanation: Abstraction is the concept of hiding things from the outer world and showing only the required things to the world, which is where access specifiers private, protected and public helps in keeping our knowledge hidden from the world.
8. C++ is ______________
a) procedural programming language
b) object oriented programming language
c) functional programming language
d) both procedural and object oriented programming language
Answer: D
Explanation: C++ supports both procedural(step by step instruction) and object oriented programming(using concept of classes and objects).
9. What does modularity mean?
a) Hiding part of program
b) Subdividing program into small independent parts
c) Overriding parts of program
d) Wrapping things into single unit
Answer: B
Explanation: Modularity means dividing a program into independent sub programs so that it can be invoked from other parts of the same program or any other program.
10. Which of the following feature of OOPs is not used in the following C++ code?
class A{
int i;
public:
void print() {cout << "hello" << i;}
}
class B : public A{
int j;
public:
void assign (int a ) {k = a;}
}
a) Abstraction
b) Encapsulation
c) Inheritance
d) Polymorphism
Answer: D
Explanation: As i and j members are private i.e. they are hidden from outer world therefore we have used the concept of abstraction. Next data members and there related functions are put together into single class therefore encapsulation is used. Also as class B is derived from A therefore Inheritance concept is used. But as no function is overloaded in any of the classes therefore, the concept of polymorphism is missing here.