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
- Q1Multiple choice1 markWhich of the following statements correctly identifies the primary purpose of data hiding in Java?
- (a)It allows methods to be executed concurrently using multiple threads.
- (b)It allows a class to inherit properties from multiple superclasses.
- (c)It automatically generates getter and setter methods during compilation.
- (d)It protects an object's internal data from unintended or unauthorized modifications outside its class.
Show answer and step marks
- Correct option: (d) It protects an object's internal data from unintended or unauthorized modifications outside its class.+1
Answer: (d) - Q2Multiple choice1 markWhich access specifier makes a class member accessible exclusively within its own class definition?
- (a)protected
- (b)private
- (c)public
- (d)default
Show answer and step marks
- Correct option: (b) private+1
Answer: (b) - Q3Multiple choice1 markA variable declared inside a method body without any static keyword is known as a:
- (a)Global variable
- (b)Class variable
- (c)Local variable
- (d)Instance variable
Show answer and step marks
- Correct option: (c) Local variable+1
Answer: (c) - Q4Multiple choice1 markWhich type of method is used to assign or modify the value of a private instance variable in an encapsulated class?
- (a)Setter method
- (b)Getter method
- (c)Default constructor
- (d)Static method
Show answer and step marks
- Correct option: (a) Setter method+1
Answer: (a) - Q5Long answer4 marksDefine 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
- Class declaration with private instance variables `private String bookTitle; private double price;`.+1
- Public constructor `public Book(String t, double p) { bookTitle = t; price = p; }`.+1
- `public void updatePrice(double newPrice)` changing `price` only when `newPrice > 0`.+1
- `public void display()` printing the title and the price.+0.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(); } } - Q6Long answer4 marksWrite 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 on the price if the price exceeds ₹ , 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
- Class header and private member variables `private int bcode; private String bname; private double price;`.+1
- Constructor `Book(int code, String name, double pr)` assigning `bcode = code; bname = name; price = pr;`.+1
- `calculate()`: `if (price > 1000) price = price - 0.10 × price;`, leaving the price unchanged otherwise.+1
- `display()` printing the book code, book name and final price.+0.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
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