Encapsulation & Access Control

Last Updated :
Discuss
Comments

Question 1

Predict the output of following program.

CPP
#include <iostream>
using namespace std;
class A
{
protected:
    int x;
public:
    A() {x = 0;}
    friend void show();
};

class B: public A
{
public:
    B() : y (0) {}
private:
    int y;
};

void show()
{
    A a;
    B b;
    cout << "The default value of A::x = " << a.x << " ";
    cout << "The default value of B::y = " << b.y<<endl;
}
  • Compiler Error in show() because x is protected in class A

  • Compiler Error in show() because y is private in class b

  • The default value of A::x = 0 The default value of B::y = 0

  • Compiler Dependent

Question 2

If class A is a friend of class B and class B is a friend of class C, which of the following is true?

  • Class A is a friend of class C

  • Class B cannot be a friend of any other class

  • Class C is a friend of class A

  • None of the above

Question 3

Is the below statement correct?

In C++, structures provide data encapsulation or data hiding features.
  • True

  • False

Question 4

Which of the following access modifiers can be used to restrict access to a method or variable to only within the same class?

  • public 

  • protected

  • private

  • default

Question 5

Which of the following best describes the purpose of using access modifiers in class diagrams?

  • They are used to define the type of diagram being created.

  • They control the visibility of attributes and methods in a class.

  • They describe the inheritance hierarchy between classes.

  • They represent the relationships between different objects in the system.

Question 6

When declaring data members inside a class without specifying access modifiers, what is the default access level typically assigned?

  • Public — accessible by all

  • Private — accessible only within the class

  • Protected — accessible in the class and subclasses

  • Package-level — accessible within the package or module

Question 7

What is encapsulation in C++?


  • Wrapping data and functions into a single unit


  • Hiding the class from the user

  • Binding UI elements to data

  • Using inheritance to reuse code

Question 8

What is the main benefit of encapsulation in C++?


  • Faster execution


  •  Better UI design


  • Data security and abstraction

  • Simplified syntax

Question 9

Which OOPS principle restricts direct access to sensitive data using access modifiers?

  • Inheritance

  • Abstraction

  • Polymorphism

  • Encapsulation

Tags:

There are 9 questions to complete.

Take a part in the ongoing discussion