Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Encapsulation is a cornerstone of object-oriented programming (OOP) in Java. It promotes data protection and code organization by bundling data (attributes) and methods (functions) that operate on that data into a single unit called a class. By controlling access to this data, encapsulation safeguards the integrity of your program and ensures proper data manipulation.
This quiz serves as a valuable tool to test your understanding of encapsulation concepts in Java. Take the short quiz to assess your knowledge and identify areas for potential improvement.
Whether you’re a beginner or an experienced Java developer, this quiz provides an opportunity to solidify your grasp of this fundamental OOP principle. Let’s test your knowledge.
Quiz Summary
0 of 15 Questions completed
Questions:
Information
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
Results
Results
0 of 15 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
Categories
- Not categorized 0%
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- Current
- Review / Skip
- Answered
- Correct
- Incorrect
-
Question 1 of 15
1. Question
- Which method is used for grouping the code and data together ?
CorrectIncorrect -
Question 2 of 15
2. Question
class Demo {
private int number ;
number = 5;
public void display() {
System.out.println(number);
}
}
class Main {
public static void main ( String args [ ] )
{
Demo d = new Demo();
d.display();
}
}
CorrectIncorrect -
Question 3 of 15
3. Question
class Sample {
protected String a = “DataFlair”;
}
class Example extends Sample {
public void print() {
System.out.println(a);
}
}
class Main {
public static void main ( String args [ ] )
{
Example e = new Example();
e.print();
}
}
CorrectIncorrect -
Question 4 of 15
4. Question
Which two concepts are used to encapsulate a class ?
CorrectIncorrect -
Question 5 of 15
5. Question
class EncapsulationDemo {
private String name;
public void getname() {
return name;
}
public void setname(string text) {
name = text;
System.out.println(name);
}
}
class Main {
public static void main ( String args [ ] )
{
EncapsulationDemo d = new EncapsulationDemo();
d.getname();
d.setname(“DataFlair”);
}
}
CorrectIncorrect -
Question 6 of 15
6. Question
class Encapsulation {
private int branches;
private string company;
public void GetBranches() {
return branches;
}
public void SetBranches()
System.out.println(branches);
}
public void GetCompany () {
return company;
}
public void SetName( String name ) {
company = name;
System.out.println(company);
}
}
class Main {
public static void main ( String args [ ] )
{
Encapsulation e = new Encapsulation();
e.GetBranches();
e.SetBranches(25);
e.GetBranches();
e.SetBranches(“DataFlair”);
}
}
CorrectIncorrect -
Question 7 of 15
7. Question
Which method is used to make the fields read only or write only ?
CorrectIncorrect -
Question 8 of 15
8. Question
class Rhombus {
private int side;
private int altitude;
public void calculate ( int perimeter , int a )
side = perimeter / 4;
Altitude= a;
int area = side * altitude;
System.out.println(area);
}
}
class Main {
public static void main ( String args [ ] )
{
Rhombus r = new Rhombus();
r.calculate(12,5);
}
}
CorrectIncorrect -
Question 9 of 15
9. Question
Which of the following is the correct syntax of the getter setter method ?
CorrectIncorrect -
Question 10 of 15
10. Question
Which of the following is not an advantage of Encapsulation ?
CorrectIncorrect -
Question 11 of 15
11. Question
class Demo {
private int a;
public void get() {
return a;
}
public void set() {
this.a = a;
}
}
class Main {
public static void main ( String args [ ] )
{
Demo obj = new Demo();
obj.set(5);
System.out.println(obj.set(5));
}
}
CorrectIncorrect -
Question 12 of 15
12. Question
class EncapsulationSample {
private String a = “DataFlair”;
}
class Inheritance extends EncapsulationSample {
public void display( ) {
System.out.println(a);
}
}
class Main {
public static void main ( String args [ ] )
{
Inheritance i = new Inheritance();
i.display();
}
}
CorrectIncorrect -
Question 13 of 15
13. Question
Which keyword is used to implement encapsulation ?
CorrectIncorrect -
Question 14 of 15
14. Question
class Sample {
private int length,breath;
public void Area(int l , int b ) {
private int area = l * b;
}
}
class Main {
public static void main ( String args [ ] )
{
Sample s = new Sample();
s.Area(2,3);
System.out.println(area);
}
}
CorrectIncorrect -
Question 15 of 15
15. Question
class Area {
private int a;
Scanner sc = new Scanner(System.in);
public void calculate () {
int Squarearea = a * a ;
}
}
class Main {
public static void main ( String args [ ] )
{
Area a = new Area();
a.calculate();
System.out.println(Squarearea);
}
}
CorrectIncorrect
Summary:
This blog post presented a concise quiz on encapsulation in Java. The quiz consisted of a single multiple-choice question focusing on the concept of grouping code and data together. While the quiz itself is brief, it serves as a springboard for further exploration.
Remember, encapsulation is a critical concept in Java programming. By effectively leveraging encapsulation principles, you can create well-structured, maintainable, and secure applications. For a deeper understanding of encapsulation, explore additional resources beyond the quiz, such as online tutorials, courses, and practice problems.
