TopicTestInheritance
Java Test Paper
- Variables, Loops,
- Conditional Statements,
- Access Modifiers,
- and Inheritance
Section A: Multiple Choice (1 mark each)
What is the correct way to declare an integer variable in Java? a. int x = 5; b. integer x = 5; c. var x = 5; d. x = 5;
Which of the following loops is suitable when the number of iterations is known in advance? a. for loop b. while loop c. do-while loop d. switch loop
In Java, what does the
breakstatement 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.Which access modifier restricts a class member's visibility to only the class in which it is declared? a. public b. protected c. private d. package-private (default)
In Java, what is inheritance? a. A mechanism for accessing class members from other classes. b. A way to create objects. c. A process where a subclass inherits the properties and behaviors of a superclass. d. A method for defining variables.
Section B: True/False (1 mark each)
- True or False: Variables declared inside a block of code are only accessible within that block.
- True or False: The
do-whileloop always executes its code block at least once. - True or False: The
else ifblock in anifstatement is mandatory. - True or False: The
defaultaccess modifier allows access to a class member only within the same class and package. - True or False: Inheritance in Java allows a subclass to inherit members with
privateaccess from its superclass.
Section C: Code-related Questions (2 marks each)
Write a Java code snippet that declares a variable
countand initializes it to 0. Then, use aforloop to print the numbers from 1 to 5.Create a class named "Vehicle" with a
protectedmember variable "speed." Then, create a subclass named "Car" that inherits from "Vehicle" and provides a method named "getSpeed" to access and return the "speed" value.Write a Java code snippet that takes an integer input from the user and prints "Even" if the number is even and "Odd" if the number is odd.
Create a class named "Person" with
privatemember variables for "name" and "age." Then, create a subclass named "Employee" that inherits from "Person" and provides methods to set and retrieve these attributes.Write a Java code snippet that calculates and prints the sum of all even numbers from 1 to 50 using a
forloop.Create a class hierarchy for shapes, starting with a base class "Shape." Define subclasses like "Circle," "Rectangle," and "Triangle," and provide methods to calculate the area of each shape.
Write a Java program that uses a
forloop to print the Fibonacci series up to the 10th term.
Section D: Bonus (3 marks)
Explain the differences between the
public,protected,private, andpackage-private(default) access modifiers in Java. Provide examples to illustrate these differences.Extend the shape hierarchy from question 16 by adding an "EquilateralTriangle" subclass. Provide an implementation for calculating its area.

Comments
Post a Comment