Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Understanding Java Encapsulation

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

What is encapsulation in Java?

a)

Encapsulation is the process of converting Java code into machine code.

b)

Encapsulation refers to the use of multiple inheritance in Java.

c)

Encapsulation is the method of optimizing Java code for performance.

d)

Encapsulation is the bundling of data and methods that operate on that data, restricting access to some components.

2.

How does encapsulation improve code security?

a)

Encapsulation improves code security by restricting direct access to data and allowing controlled access through methods.

b)

Encapsulation allows unlimited access to all data fields.

c)

Encapsulation makes code slower and less efficient.

d)

Encapsulation eliminates the need for methods to access data.

3.

What are the main components of encapsulation?

a)

Static and dynamic typing

b)

Inheritance and polymorphism

c)

Compilation and interpretation

d)

Access modifiers and getter/setter methods

4.

Can you access private variables directly from outside the class?

a)

Private variables can be accessed using a public method.

b)

Private variables are accessible through inheritance.

c)

Yes, you can access private variables directly from outside the class.

d)

No, you cannot access private variables directly from outside the class.

5.

What is the purpose of getter and setter methods?

a)

The purpose of getter and setter methods is to provide controlled access to private class attributes.

b)

To directly modify private class attributes without restrictions.

c)

To automatically generate documentation for the class.

d)

To enhance the performance of class methods.

6.

How does encapsulation relate to data hiding?

a)

Data hiding is unrelated to encapsulation in object-oriented programming.

b)

Encapsulation allows full access to an object's internal state.

c)

Encapsulation prevents any form of data access or manipulation.

d)

Encapsulation enables data hiding by restricting access to an object's internal state.

7.

What keyword is used to define a class in Java?

a)

define

b)

object

c)

function

d)

class

8.

What is the difference between public, private, and protected access modifiers?

a)

Public: accessible everywhere; Private: accessible only within the class; Protected: accessible within the class and subclasses.

b)

Public: accessible only to the class; Private: accessible to subclasses; Protected: accessible everywhere.

c)

Public: accessible only to subclasses; Private: accessible to all classes; Protected: accessible only within the package.

d)

Public: accessible only within the package; Private: accessible everywhere; Protected: accessible only within the class.

9.

How can encapsulation lead to better code maintainability?

a)

Encapsulation requires all code to be rewritten for any change.

b)

Encapsulation makes code more complex and harder to understand.

c)

Encapsulation eliminates the need for documentation entirely.

d)

Encapsulation improves code maintainability by allowing changes to be made internally without impacting external code dependencies.

10.

Can encapsulation be applied to methods as well as variables?

a)

No, encapsulation can only be applied to variables.

b)

Yes, encapsulation can be applied to both methods and variables.

c)

Encapsulation is not relevant to methods or variables.

d)

Yes, but only to private methods.

11.

Composition is a

a)

uses-a relationship

b)

has-a relationship

c)

is-a relationship

d)

none of the above

12.

What is not a Java primitive data type?

a)

float

b)

byte

c)

Boolean

d)

char

13.

Which is not a valid way to define an array?

a)

int[3] i = new int[];

b)

int[ ] i = {1,2,3};

c)

int[ ] i = new int[ ]{1, 2, 3};

d)

int[ ] i = new int[3];

14.

Select the access level/s of a private modifier.

a)

Class

b)

Package

c)

Subclass

d)

World

15.

It keeps the data and the code safe from external interference.

a)

Polymorphism

b)

ArrayList

c)

Autoboxing

d)

Encapsulation

16.

Which data type is not supported by ArrayList?

a)

Integer

b)

String

c)

double

d)

Boolean

17.

What is an instance of a class?

a)

Class

b)

Object

c)

Reference

d)

Primitive

18.

Autoboxing converts primitive to corresponding object wrapper class

a)

true

b)

false

19.

What is the default value of a primitive int data type?

a)

-1

b)

null

c)

0

d)

garbage data

20.

Consider the given code. What would be the display on the screen?

a)

Class A

b)

Class B

c)

Class C

d)

Compile error