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.
Time limit: 0
Quiz Summary
0 of 15 Questions completed
Questions:
Information
You have already completed the quiz before. Hence you can not start it again.
Which method is used for grouping the code and data together ?
Correct
Incorrect
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();
}
}
Correct
Incorrect
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();
}
}
Correct
Incorrect
Question 4 of 15
4. Question
Which two concepts are used to encapsulate a class ?
Correct
Incorrect
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”);
}
}
Correct
Incorrect
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”);
}
}
Correct
Incorrect
Question 7 of 15
7. Question
Which method is used to make the fields read only or write only ?
Correct
Incorrect
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);
}
}
Correct
Incorrect
Question 9 of 15
9. Question
Which of the following is the correct syntax of the getter setter method ?
Correct
Incorrect
Question 10 of 15
10. Question
Which of the following is not an advantage of Encapsulation ?
Correct
Incorrect
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));
}
}
Correct
Incorrect
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();
}
}
Correct
Incorrect
Question 13 of 15
13. Question
Which keyword is used to implement encapsulation ?
Correct
Incorrect
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);
}
}
Correct
Incorrect
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);
}
}
Correct
Incorrect
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.
Did you know we work 24x7 to provide you best tutorials Please encourage us - write a review on Google
DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.