WorksheetsUnderstanding Java Encapsulation
Total questions: 20
Worksheet time: 10mins
What is encapsulation in Java?
Encapsulation is the process of converting Java code into machine code.
Encapsulation refers to the use of multiple inheritance in Java.
Encapsulation is the method of optimizing Java code for performance.
Encapsulation is the bundling of data and methods that operate on that data, restricting access to some components.
How does encapsulation improve code security?
Encapsulation improves code security by restricting direct access to data and allowing controlled access through methods.
Encapsulation allows unlimited access to all data fields.
Encapsulation makes code slower and less efficient.
Encapsulation eliminates the need for methods to access data.
What are the main components of encapsulation?
Static and dynamic typing
Inheritance and polymorphism
Compilation and interpretation
Access modifiers and getter/setter methods
Can you access private variables directly from outside the class?
Private variables can be accessed using a public method.
Private variables are accessible through inheritance.
Yes, you can access private variables directly from outside the class.
No, you cannot access private variables directly from outside the class.
What is the purpose of getter and setter methods?
The purpose of getter and setter methods is to provide controlled access to private class attributes.
To directly modify private class attributes without restrictions.
To automatically generate documentation for the class.
To enhance the performance of class methods.
How does encapsulation relate to data hiding?
Data hiding is unrelated to encapsulation in object-oriented programming.
Encapsulation allows full access to an object's internal state.
Encapsulation prevents any form of data access or manipulation.
Encapsulation enables data hiding by restricting access to an object's internal state.
What keyword is used to define a class in Java?
define
object
function
class
What is the difference between public, private, and protected access modifiers?
Public: accessible everywhere; Private: accessible only within the class; Protected: accessible within the class and subclasses.
Public: accessible only to the class; Private: accessible to subclasses; Protected: accessible everywhere.
Public: accessible only to subclasses; Private: accessible to all classes; Protected: accessible only within the package.
Public: accessible only within the package; Private: accessible everywhere; Protected: accessible only within the class.
How can encapsulation lead to better code maintainability?
Encapsulation requires all code to be rewritten for any change.
Encapsulation makes code more complex and harder to understand.
Encapsulation eliminates the need for documentation entirely.
Encapsulation improves code maintainability by allowing changes to be made internally without impacting external code dependencies.
Can encapsulation be applied to methods as well as variables?
No, encapsulation can only be applied to variables.
Yes, encapsulation can be applied to both methods and variables.
Encapsulation is not relevant to methods or variables.
Yes, but only to private methods.
Composition is a
uses-a relationship
has-a relationship
is-a relationship
none of the above
What is not a Java primitive data type?
float
byte
Boolean
char
Which is not a valid way to define an array?
int[3] i = new int[];
int[ ] i = {1,2,3};
int[ ] i = new int[ ]{1, 2, 3};
int[ ] i = new int[3];
Select the access level/s of a private modifier.
Class
Package
Subclass
World
It keeps the data and the code safe from external interference.
Polymorphism
ArrayList
Autoboxing
Encapsulation
Which data type is not supported by ArrayList?
Integer
String
double
Boolean
What is an instance of a class?
Class
Object
Reference
Primitive
Autoboxing converts primitive to corresponding object wrapper class
true
false
What is the default value of a primitive int data type?
-1
null
0
garbage data
Consider the given code. What would be the display on the screen?
Class A
Class B
Class C
Compile error
