TopicTestTillArrays

 

Java Test Paper

Variables, Loops, Conditional Statements, Inheritance, Method Overloading, Method Overriding, Encapsulation and Abstraction, and Interfaces in Java 8

 

Section A: Multiple Choice (1 mark each)

1.       Which keyword in Java is used to define a constant? a. static b. final c. const d. var

2.       Which loop construct in Java is used when the number of iterations is not known in advance? a. for loop b. while loop c. do-while loop d. if-else loop

3.       In Java, what does the super keyword refer to? a. The superclass of the current class. b. The subclass of the current class. c. A reserved keyword with no specific meaning. d. An interface in the current class.

4.       What is the primary purpose of method overloading in Java? a. To override methods in a superclass. b. To hide methods in a subclass. c. To create multiple methods with the same name but different parameters. d. To create a new class that extends another class.

5.       In Java, what is the main purpose of encapsulation? a. To achieve code reusability. b. To hide the internal details of an object and protect its integrity. c. To create abstract classes. d. To implement polymorphism.

6.       What is the primary advantage of using an abstract class in Java? a. It cannot be inherited by any subclass. b. It can be instantiated and used directly. c. It allows for multiple inheritance. d. It provides a blueprint for other classes and enforces method implementation.

7.       In Java 8, what is a functional interface? a. An interface with only one abstract method. b. An interface that cannot contain any methods. c. An interface with multiple abstract methods. d. An interface with only static methods.

 

Section B: True/False (1 mark each)

1.       True or False: In Java, a variable declared as final can be reassigned after its initial assignment.

2.       True or False: The do-while loop in Java always executes its code block at least once.

3.       True or False: Encapsulation is a mechanism for hiding the implementation details of a class.

4.       True or False: Method overriding in Java is the process of providing a new implementation for a method in a subclass.

5.       True or False: Inheritance in Java allows a subclass to inherit private members from its superclass.

6.       True or False: An abstract class can be instantiated (used to create objects) in Java.

7.       True or False: In Java 8, interfaces can have default and static methods in addition to abstract methods.

 

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

1.       Write a Java program that declares an integer variable age and uses conditional statements to determine whether a person is eligible to vote (age 18 and above).

2.       Create a class hierarchy for animals, starting with a base class "Animal." Define subclasses like "Mammal," "Reptile," and "Bird." Provide methods that demonstrate method overriding to produce unique behaviors for each subclass.

3.       Design a class "Product" with private attributes for name and price. Include a constructor and methods for setting and retrieving these attributes. Ensure proper encapsulation.

4.       Write a Java program that demonstrates method overloading by creating a class with overloaded methods to calculate the area of a rectangle, a circle, and a triangle.

5.       Implement 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.

6.       Create an abstract class "Shape" with a method "calculateArea." Implement this abstract class in subclasses like "Circle" and "Rectangle."

7.       Write a Java program that defines an interface "Drawable" with a method "draw." Create a class "Square" that implements the "Drawable" interface and provides an implementation of the "draw" method.

8.       Extend the animal hierarchy from question 16 by adding a "Fish" subclass. Provide an implementation for unique fish-related behaviors and characteristics.

9.       Design a class "Person" with private attributes for name, age, and address. Implement a constructor and methods for setting and retrieving these attributes. Ensure encapsulation.

10.   Create a Java program that demonstrates the use of functional interfaces in Java 8 by defining a functional interface with a single abstract method and implementing it with a lambda expression.

11.   Write a Java program that demonstrates the use of a for loop to print the Fibonacci series up to the 10th term.

12.   Create a class "Employee" with private attributes for name, employee ID, and salary. Implement a method to calculate and set the annual bonus based on the salary. Demonstrate encapsulation by restricting direct access to the attributes.

13.   Implement a Java program that uses a while loop to find the factorial of a given number. Include proper error handling for negative inputs.

14.   Design a class hierarchy for vehicles, starting with a base class "Vehicle." Define subclasses like "Car," "Bike," "Truck," and "Boat." Provide methods that demonstrate method overriding to display vehicle details.

15.   Write a Java program that demonstrates method overloading by creating a class with overloaded methods to calculate the area of a square (using side length), a pentagon (using apothem and side length), and a hexagon (using side length).

16.   Create a class "Library" that encapsulates a list of books. Provide methods to add and remove books, and a method to display the total number of books. Ensure proper encapsulation.

17.   Write a Java program that demonstrates the use of an abstract class and method. Create an abstract class "Bird" with an abstract method "makeSound." Create subclasses like "Eagle" and "Sparrow" that override the "makeSound" method to produce different bird sounds.

18.   Design a class "Bank" with private attributes for the bank name and location. Implement methods for setting and retrieving these attributes. Ensure encapsulation and provide a method to display bank information.

19.   Create a Java program that simulates a simple online shopping cart. Design a class for products with attributes like name, price, and quantity. Implement methods to add and remove products, calculate the total price, and display the cart contents.

20.   Extend the shape hierarchy from question 30 by adding a "Rhombus" subclass. Provide an implementation for calculating its area.

 

Section D: Bonus (3 marks)

25.   Explain the concept of polymorphism in Java and provide examples to illustrate its use in code.

Instructions:

·         Answer all questions.

·         Execute the solutions in the editor [ Eclipse ] and Save the same in the solutions word file

·         Be concise and clear in your code-related answers.

 

Good luck!

  

Section C: Code-related Questions - Algorithms

 

Question 1: Determining Voting Eligibility

Algorithm:

·         Declare an integer variable age.

·         Take user input to set the value of age.

·         Use conditional statements to determine eligibility:

·         If age is greater than or equal to 18, the person is eligible to vote.

·         If age is less than 18, the person is not eligible to vote.

Question 2: Animal Class Hierarchy with Method Overriding

Algorithm:

·         Create a base class "Animal" with methods for common animal behaviors (e.g., eat(), move()).

·         Create subclasses like "Mammal," "Reptile," and "Bird" that inherit from the "Animal" class.

·         In each subclass, override the methods from the base class with specific implementations.

·         For example, in the "Mammal" class, override move() to describe how mammals move.

·         Demonstrate method overriding by creating objects of each subclass and calling their specific methods.

Question 3: Product Class with Encapsulation

Algorithm:

·         Create a class "Product" with private attributes for name and price.

·         Implement a constructor that takes parameters for name and price and sets them.

·         Provide methods to set and retrieve the name and price attributes.

·         Implement setName() and setPrice() methods for setting the attributes.

·         Implement getName() and getPrice() methods for retrieving the attributes.

·         Ensure encapsulation by making the attributes private and accessing them through methods.

Question 4: Method Overloading for Shape Area Calculation

Algorithm:

·         Create a class with methods overloaded to calculate the area of different shapes (e.g., "calculateArea" method).

·         For each method:

·         Provide parameters specific to the shape (e.g., length and width for a rectangle).

·         Implement the area calculation based on the shape's formula.

·         Return the calculated area.

Question 5: BankAccount Class with Encapsulation

Algorithm:

·         Create a class "BankAccount" with private attributes for the balance.

·         Implement methods for depositing and withdrawing money.

·         Create a deposit() method that takes an amount and adds it to the balance.

·         Create a withdraw() method that takes an amount and deducts it from the balance.

·         Ensure encapsulation by restricting direct access to the balance attribute (make it private).

·         Demonstrate the functionality by creating a "BankAccount" object, depositing, and withdrawing money.

Question 6: Abstract Class "Shape" with Subclasses

Algorithm:

·         Create an abstract class "Shape" with an abstract method "calculateArea."

·         Create subclasses like "Circle" and "Rectangle" that inherit from "Shape."

·         In each subclass, provide an implementation for the "calculateArea" method based on the specific shape's formula.

·         When using the "Shape" class or its subclasses, call the "calculateArea" method to calculate the area based on the shape.

Question 7: Defining an Interface and Implementing "Drawable"

Algorithm:

·         Define an interface named "Drawable" with a method "draw()" that takes no arguments.

·         Create a class "Square" that implements the "Drawable" interface.

·         Provide an implementation for the "draw" method in the "Square" class.

·         This method should define how a square is drawn.

·         When using the "Square" class, call the "draw" method to demonstrate its implementation.

Question 8: Extending the Animal Hierarchy with "Fish"

Algorithm:

·         Create a subclass "Fish" that inherits from the "Animal" class in the animal hierarchy.

·         In the "Fish" class, provide specific methods and behaviors related to fish.

·         For example, implement a method to describe how fish swim.

·         Add attributes specific to fish, if needed (e.g., "finCount" or "waterType").

·         Demonstrate the use of the "Fish" subclass by creating a "Fish" object and calling its methods.

Question 9: Person Class with Encapsulation

Algorithm:

·         Create a class "Person" with private attributes for name, age, and address.

·         Implement a constructor that takes parameters for name, age, and address and sets them.

·         Provide methods to set and retrieve the name, age, and address attributes.

·         Implement setName(), setAge(), and setAddress() methods for setting the attributes.

·         Implement getName(), getAge(), and getAddress() methods for retrieving the attributes.

·         Ensure encapsulation by making the attributes private and accessing them through methods.

Question 10: Functional Interface with Lambda Expression

Algorithm:

·         Define a functional interface with a single abstract method (e.g., "MyFunctionalInterface" with "performAction()" method).

·         Create a class that demonstrates the use of the functional interface.

·         Implement the "performAction" method using a lambda expression.

·         For example, you can define a lambda expression to print a message when "performAction" is called.

·         Create an object of the class and call the "performAction" method to execute the lambda expression.

Question 11: Printing Fibonacci Series:

·         Initialize variables a and b as 0 and 1.

·         Print a to display the first term of the Fibonacci series.

·         For i from 2 to 10 (inclusive):

·         Calculate the next term as c = a + b.

·         Set a to b and b to c.

·         Print a to display the next term.

Question 12: Calculating Employee Annual Bonus:

·         Create a class "Employee" with private attributes name, employeeID, and salary.

·         Implement a method, calculateBonus(), that:

·         Takes no arguments.

·         Calculates the bonus based on the salary (e.g., 10% of the salary).

·         Sets the bonus attribute for the employee.

·         Ensure encapsulation by restricting direct access to attributes.

Question 13: Calculating Factorial with a While Loop:

·         Take user input for a positive integer.

·         Initialize a variable result to 1.

·         Check if the input is negative; if yes, display an error message.

·         If input is zero, set result to 1 and display the result.

·         Otherwise, for i from 1 to the input:

·         Multiply result by i.

·         Display the calculated factorial.

Question 14: Vehicle Class Hierarchy:

·         Create a class "Vehicle" with attributes such as make, model, and year.

·         Create subclasses (e.g., "Car," "Bike," "Truck," "Boat") that inherit from "Vehicle."

·         Implement a method in each subclass to display specific details of that vehicle.

·         Demonstrate method overriding by calling the methods for different vehicle objects.

Question 15: Method Overloading for Shape Area:

·         Create a class with methods overloaded to calculate the area of different shapes.

·         For each method:

·         Provide parameters specific to the shape (e.g., side length for a square, apothem and side length for a pentagon).

·         Implement the area calculation based on the shape's formula.

·         Display the calculated area.

Question 16: Library Class with Encapsulation:

·         Create a class "Library" with a private list or array to store books.

·         Implement methods to add and remove books and count the total number of books.

·         Ensure that the list of books is encapsulated and not directly accessible from outside the class.

Question 17: Abstract Class "Bird" with Method Override:

·         Create an abstract class "Bird" with an abstract method "makeSound."

·         Create subclasses like "Eagle" and "Sparrow."

·         In each subclass, implement the "makeSound" method to produce the unique sound for that bird (e.g., "Eagle" makes an eagle sound).

·         Demonstrate method overriding by calling the "makeSound" method for different bird objects.

Question 18: Bank Class with Encapsulation:

·         Create a class "Bank" with private attributes for the bank name and location.

·         Implement methods to set and retrieve these attributes.

·         Ensure encapsulation by providing access to these attributes only through methods.

·         Implement a method to display bank information (e.g., "Welcome to ABC Bank located at XYZ").

Question 19: Online Shopping Cart Simulation:

·         Design a class for products with attributes like name, price, and quantity.

·         Implement methods to:

·         Add products to the cart.

·         Remove products from the cart.

·         Calculate the total price based on the products in the cart.

·         Display the cart contents with names, prices, and quantities.

Question 20: Calculating Rhombus Area:

·         Extend the shape hierarchy from question 30 by adding a "Rhombus" subclass.

·         Provide an implementation for calculating the area of a rhombus based on its specific formula.

·         Display the calculated area.

 


Comments

Popular posts from this blog

FrontEnd - FAQs - Part 1

CoreJava - ClassesObjectsMethods - Assignment

Java Script - FAQs