ICSE · Class 10 · Computer Applications

Class 10 Encapsulation Mock Test (ICSE)

6 practice questions from Encapsulation, in the style of your board paper, each with a worked answer and the marks for every step. Then take a full chapter test on paper, snap the pages and get every step marked.

Topics in Encapsulation

  • Data hiding
  • Access specifiers and their scope
  • Getter and setter methods
  • Scope of variables: local, instance and class

Encapsulation practice questions with answers

  1. Q1Multiple choice1 mark
    Which of the following statements correctly identifies the primary purpose of data hiding in Java?
    1. (a)It allows methods to be executed concurrently using multiple threads.
    2. (b)It allows a class to inherit properties from multiple superclasses.
    3. (c)It automatically generates getter and setter methods during compilation.
    4. (d)It protects an object's internal data from unintended or unauthorized modifications outside its class.
    Show answer and step marks
    1. Correct option: (d) It protects an object's internal data from unintended or unauthorized modifications outside its class.+1
    Answer: (d)
  2. Q2Multiple choice1 mark
    Which access specifier makes a class member accessible exclusively within its own class definition?
    1. (a)protected
    2. (b)private
    3. (c)public
    4. (d)default
    Show answer and step marks
    1. Correct option: (b) private+1
    Answer: (b)
  3. Q3Multiple choice1 mark
    A variable declared inside a method body without any static keyword is known as a:
    1. (a)Global variable
    2. (b)Class variable
    3. (c)Local variable
    4. (d)Instance variable
    Show answer and step marks
    1. Correct option: (c) Local variable+1
    Answer: (c)
  4. Q4Multiple choice1 mark
    Which type of method is used to assign or modify the value of a private instance variable in an encapsulated class?
    1. (a)Setter method
    2. (b)Getter method
    3. (c)Default constructor
    4. (d)Static method
    Show answer and step marks
    1. Correct option: (a) Setter method+1
    Answer: (a)
  5. Q5Long answer4 marks
    Define a class named `Book` with the following specifications: - Private instance variables: `bookTitle` (String) and `price` (double). - A public parameterized constructor `Book(String t, double p)` to initialize the instance members. - A public method `void updatePrice(double newPrice)` to modify the price if the new price is positive. - A public method `void display()` to output the book details. Write the complete Java class with a `main` method to create an object and test these methods.
    Show answer and step marks
    1. Class declaration with private instance variables `private String bookTitle; private double price;`.+1
    2. Public constructor `public Book(String t, double p) { bookTitle = t; price = p; }`.+1
    3. `public void updatePrice(double newPrice)` changing `price` only when `newPrice > 0`.+1
    4. `public void display()` printing the title and the price.+0.5
    5. `main` creating a `Book` object with `new` and calling `updatePrice()` and `display()` on it.+0.5
    Answer: class Book { private String bookTitle; private double price; public Book(String t, double p) { bookTitle = t; price = p; } public void updatePrice(double newPrice) { if (newPrice > 0) price = newPrice; } public void display() { System.out.println("Title: " + bookTitle); System.out.println("Price: " + price); } public static void main(String args[]) { Book b = new Book("Computer Basics", 300.0); b.display(); b.updatePrice(350.0); b.display(); b.updatePrice(-20.0); b.display(); } }
  6. Q6Long answer4 marks
    Write a Java class named `Book` with the following specifications: Member variables: - `bcode` - of type integer to store the book code - `bname` - of type String to store the book name - `price` - of type double to store the price of the book Use appropriate private access specifiers for data hiding. Member methods: 1. `Book(int code, String name, double pr)` - Parameterized constructor to initialize the member variables. 2. `void calculate()` - To compute a discount of 10%10\% on the price if the price exceeds ₹ 10001000, and update the price. Otherwise, the price remains unchanged. 3. `void display()` - To display the book code, book name, and the final price. Write the complete class along with a `main` method to create an object and invoke the methods.
    Show answer and step marks
    1. Class header and private member variables `private int bcode; private String bname; private double price;`.+1
    2. Constructor `Book(int code, String name, double pr)` assigning `bcode = code; bname = name; price = pr;`.+1
    3. `calculate()`: `if (price > 1000) price = price - 0.10 × price;`, leaving the price unchanged otherwise.+1
    4. `display()` printing the book code, book name and final price.+0.5
    5. `main` creating an object with `new` and calling `calculate()` and then `display()`.+0.5
    Answer: class Book { private int bcode; private String bname; private double price; Book(int code, String name, double pr) { bcode = code; bname = name; price = pr; } void calculate() { if (price > 1000) price = price - 0.10 × price; } void display() { System.out.println("Book code: " + bcode); System.out.println("Book name: " + bname); System.out.println("Final price: " + price); } public static void main(String args[]) { Book b = new Book(101, "Physics Guide", 1200.0); b.calculate(); b.display(); } }

Take a full Encapsulation test

PrepitUp writes a fresh paper on this chapter at the length you pick. Write it on paper with a timer, photograph the pages, and see the marks for every step and what to practise next.

  • 15 min10 marks
  • 30 min20 marks
  • 1.5 hr40 marks
  • 3 hr80 marks
See a sample marked answer sheet

Get your Encapsulation answers marked

Sign in, pick this chapter and a length, write the test on paper and get every step marked.

Sign in to take this test

More Class 10 Computer Applications mock tests