TopicTestOOPs

 


Java Test Paper

            Variables, Loops, Conditional Statements, Inheritance, Method Overloading, Method Overriding, Encapsulation, and Abstraction

Section A: Multiple Choice (1 mark each)

  1. What is the correct way to declare a constant in Java?

a. var constantValue = 5; b. final int constantValue = 5; c. const constantValue = 5; d. static final constantValue = 5;

  1. Which loop construct is most appropriate when you want to iterate through a collection of elements, like an array or a list?

 a. for loop b. while loop c. do-while loop d. foreach loop

  1. In Java, what does the break statement do?

a. Terminates the program. b. Exits the loop or switch statement. c. Skips the current iteration of a loop. d. Skips the next iteration of a loop.

  1. In the context of inheritance, what does it mean when we say a subclass "overrides" a method from its superclass?

a. The subclass provides a new implementation of the method with the same name and parameters. b. The subclass inherits the method from the superclass without any changes. c. The subclass creates a completely new method unrelated to the superclass method. d. The subclass removes the method from the superclass.

  1. What is the purpose of the abstract keyword in Java?

a. To make a class immutable. b. To prevent a class from being extended (subclassed). c. To declare a class as an abstract class, allowing for abstract methods. d. To hide class members from other classes.

  1. In Java, which access modifier is used to restrict access to only the same class and any subclasses?

a. public b. protected c. private d. package-private (default)

Section B: True/False (1 mark each)

  1. True or False: In Java, variables declared as final can be modified after their initial assignment.
  2. True or False: Method overloading is a feature that allows multiple methods in the same class with the same name but different parameter lists.
  3. True or False: Encapsulation is the practice of bundling data (attributes) and methods (behaviors) that operate on the data into a single unit.
  4. True or False: Inheritance in Java allows a subclass to inherit private members from its superclass.
  5. True or False: An abstract class can be instantiated (used to create objects) in Java.

Section C: Code-related Questions (2 marks each)

  1. Write a Java class named "Rectangle" that represents a rectangle. It should have private attributes for length and width, a constructor to initialize these attributes, and methods to calculate the area and perimeter.
  2. Create a class hierarchy for shapes, starting with a base class "Shape." Define subclasses like "Circle," "Rectangle," "Triangle," "Square," and "Ellipse," and provide methods to calculate the area of each shape.
  3. Create a Java class "BankAccount" with private attributes for the account balance. Include methods for depositing and withdrawing money. Demonstrate encapsulation by restricting direct access to the balance.
  4. Write a Java program that demonstrates method overloading by creating a class with multiple methods named "findMax" that can find the maximum of different data types (e.g., int, double, String).
  5. Implement the concept of method overriding by creating a superclass "Vehicle" with a method named "start," and then create subclasses like "Car" and "Bike" that provide their own implementations of the "start" method.
  6. Design a class "Book" with private attributes for the title, author, and price. Create a subclass "EBook" that inherits from "Book" and adds an attribute for the file size in MB. Implement a constructor for "EBook" that sets the title, author, price, and file size.
  7. Write a Java program that demonstrates the use of the abstract keyword by creating an abstract class "Shape" with an abstract method "calculateArea." Create two subclasses, "Circle" and "Rectangle," that provide implementations of the "calculateArea" method.
  8. Create a class hierarchy representing vehicles. Start with a base class "Vehicle" and create subclasses like "Car," "Bike," "Boat," "Truck," and "Airplane." Include attributes and methods relevant to each type of vehicle.
  9. Implement an interface named "Drawable" with a method "draw" that takes no arguments. Create a class "Circle" that implements the "Drawable" interface and provides an implementation of the "draw" method.
  10. Extend the shape hierarchy from question 13 by adding a "Trapezoid" subclass. Provide an implementation for calculating its area.
  11. Write a Java program that uses an abstract class "Animal" with abstract methods for "eat" and "makeSound." Create subclasses for specific animals like "Dog," "Cat," and "Cow" that implement these methods.
  12. Write a Java class "Student" with private attributes for name and age. Provide a method to set the age, ensuring it is between 18 and 30. Demonstrate encapsulation by restricting direct access to the age attribute.
  13. Create a Java program that implements a simple ATM machine. Define classes for Account, Customer, and ATM. Use encapsulation to protect sensitive account information.
  14. Write a Java program that demonstrates the use of method overriding. Create a superclass "Animal" with a method "makeSound." Create subclasses "Dog" and "Cat" that override the "makeSound" method to produce appropriate sounds.
  15. Design a class "Employee" with private attributes for name, employee ID, and salary. Provide methods to set and get the salary. Create a subclass "Manager" that inherits from "Employee" and overrides the method to get the salary. Managers receive a 10% bonus.
  16. Write a Java program that demonstrates the use of interfaces for abstraction. Create an interface "Shape" with a method "calculateArea." Implement this interface in classes "Circle" and "Rectangle."
  17. Create a Java class hierarchy for vehicles. Define a superclass "Vehicle" with attributes for make, model, and year. Create subclasses "Car" and "Bike." Demonstrate method overriding by overriding a method to display vehicle details.
  18. Write a Java program that demonstrates method overloading by creating a class with overloaded methods to calculate the area of a circle (using radius), a rectangle (using length and width), and a triangle (using base and height).
  19. Design a class "Bank" that encapsulates a list of bank accounts. Provide methods to add a new account, get the total balance of all accounts, and find the account with the highest balance.
  20. Extend the shape hierarchy from question 27 by adding a "Square" subclass. Provide an implementation for calculating its area.
  21. Write a Java program that demonstrates the use of an abstract class and method. Create an abstract class "Fruit" with an abstract method "calculatePrice." Create subclasses like "Apple" and "Banana" that override the "calculatePrice" method to provide different pricing logic.
  22. Explain the difference between abstraction and encapsulation in Java. Provide examples to illustrate each concept.
  23. Explain the principles of polymorphism and how it is achieved in Java. Provide examples to illustrate polymorphic behavior.

Instructions:

·         Answer all questions.

·         You have 240 minutes to complete the test.

·         Be concise and clear in your code-related answers and add Algorithm for each Answer.

·         Good luck!

 

 

 

 

 


Comments

Popular posts from this blog

FrontEnd - FAQs - Part 1

CoreJava - ClassesObjectsMethods - Assignment

Java Script - FAQs