Search Header Logo

Understanding Encapsulation in OOP

Authored by nermin hamza

Computers

University

Used 9+ times

Understanding Encapsulation in OOP
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

9 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

what is the output :

class Box {

private:

int width;

public:

void setWidth(int w)

{ width = w; }

int getWidth()

{ return width; } };

int main() {

Box b; b.setWidth(10);

cout << "Width of box: " << b.getWidth() << endl; return 0; }

Compilation error

Width of box: 0

Width of box: 10

Runtime error

2.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

What is the main purpose of using private access specifier in the code below?

class Car {

private:

int speed;

public:

void setSpeed(int s) { speed = s; }

int getSpeed() { return speed; } };

To prevent the direct modification of speed from outside the class

To allow direct access to the speed variable

To allow the speed variable to be public

To implement function overloading for setSpeed

3.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

What is the purpose of the private access specifier in the following code?

class Rectangle {

private:

int length;

public:

void setLength(int len) { length = len; }

int getLength() { return length; } };

To allow direct access to the length variable from outside the class

To make sure the length variable cannot be modified directly from outside the class

To enable inheritance from the Rectangle class

To allow multiple constructors

4.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

What is the difference between encapsulation and abstraction?

Encapsulation is about creating user interfaces, while abstraction is about data storage.

Encapsulation allows for multiple inheritance, whereas abstraction restricts it.

Encapsulation focuses on data hiding and bundling, while abstraction focuses on simplifying complex systems by exposing only the essential features.

Encapsulation is only used in object-oriented programming, while abstraction is used in all programming paradigms.

5.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

Which of the following best describes encapsulation in C++?

A process of defining the blueprint of an object

A mechanism to combine data and functions that operate on that data into a single unit

A way to inherit properties from one class to another

A feature that allows a function to be defined multiple times with different parameters

6.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

In the following program, what happens if you try to access balance directly from main()?

class Bank {

private:

double balance;

public:

void deposit(double amount) { balance += amount; } };

int main() { Bank b; b.deposit(500.00);

cout << b.balance << endl;

return 0; }

The program will compile and print the balance

The program will throw a runtime error

The program will fail to compile due to the direct access of the private member

The balance will be printed as 500.00 without any issue

7.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

What will happen if you try to access the length variable directly from outside the Rectangle class?

class Rectangle {

private:

int length;

public:

void setLength(int len) { length = len; }

int getLength() { return length; } };

The program will compile and allow access to length

The program will throw a runtime error

The program will fail to compile due to the direct access of the private member

The length will be printed without any issue

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Microsoft

Continue with Microsoft

or continue with

Facebook

Facebook

Apple

Apple

Others

Others

Already have an account?