Site icon DataFlair

Quiz on Encapsulation in Java

quiz on encapsulation in java

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.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

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

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question
    1. Which method is used for grouping the code and data together ?

    Correct
    Incorrect
  2. 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
  3. 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
  4. Question 4 of 15
    4. Question

    Which  two concepts are used to encapsulate a class ?

    Correct
    Incorrect
  5. 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
  6. 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
  7. Question 7 of 15
    7. Question

    Which method is used to make the fields read only or write only ?

    Correct
    Incorrect
  8. 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
  9. Question 9 of 15
    9. Question

    Which of the following is the correct syntax of the getter setter method ?

    Correct
    Incorrect
  10. Question 10 of 15
    10. Question

    Which of the following is not an advantage of Encapsulation ?

    Correct
    Incorrect
  11. 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
  12. 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
  13. Question 13 of 15
    13. Question

    Which keyword is used to implement encapsulation ?

    Correct
    Incorrect
  14. 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
  15. 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.

Exit mobile version