Question Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Questions Miscellaneous

1. Constructor:

Question:

 Create a Java class named Person with attributes name (String) and age (int). Implement
a parameterized constructor that initializes these attributes when an object is created.
Provide an example of how you would create an instance of the Person class using this
constructor.

 Imagine a class Person with attributes name (String) and age (int). Implement a default
constructor for the Person class that sets the name to "Unknown" and age to 0 when an
object is created without providing any values. Show how you would create an instance
of the Person class both with and without providing explicit values and explain when the
default constructor is invoked.

 Book class has 3 fields title, author and pages. Write a parameterized constructor for the
Book class that initializes the title, author, and pages when an object is created. Also,
provide an example of how you would create an instance of the Book class using this
constructor.
 Now, create an overloaded constructor that allows the creation of a book object with only
the title and author, assuming the pages are unknown. Demonstrate how constructor
overloading works by creating an instance of the Book class using both constructors.
Explain the concept of constructor overloading.

2. Inheritance:

Question:

 Define a base class Vehicle with attributes model (String) and year (int). Create a derived
class Car that inherits from Vehicle and has an additional attribute fuelType (String).
Demonstrate the use of inheritance by creating an object of the Car class and setting
values for its attributes. Explain how the derived class inherits the attributes from the
base class.
 You are tasked with designing a simple library system. Create a base class LibraryItem with
attributes title, author, and item_id. The LibraryItem class should have a method
display_info to print information about the item. Then, create two derived classes: Book and
DVD. The Book class should have an additional attribute genre, and the DVD class should have
an additional attribute duration.
 You are designing a system to model different types of animals. Create a base class Animal with
attributes name and sound. Then, create two derived classes: Mammal and Bird. The Mammal
class should have an additional attribute num_legs, and the Bird class should have an
additional attribute can_fly. Demonstrate the use of inheritance by creating objects of the
Mammal and Bird classes, setting values for their attributes, and explaining how the derived
classes inherit from the base class.

3. Abstract Class:

Question:

 Design an abstract class Shape with an abstract method calculateArea(). Create two
concrete classes Circle and Rectangle that extend the Shape class and provide
implementations for the calculateArea() method. Demonstrate how to create objects of
both Circle and Rectangle classes and calculate their respective areas.

4. Method Overriding:

Question:

 Create a base class Animal with a method makeSound(). Define a derived class Dog that
extends Animal and overrides the makeSound() method to print "Woof! Woof!". Create
an object of the Dog class and demonstrate how method overriding works by calling the
makeSound() method on the Dog object. Explain the concept of method overriding in the
context of inheritance.

Interface

Create a Java program that demonstrates the use of interfaces. Define an interface named Shape
with methods calculateArea() and calculatePerimeter(). Implement this interface in two
classes: Circle and Rectangle. The Circle class should have a constructor that takes the
radius, and the Rectangle class should have a constructor that takes the length and width. In the
main method, create objects of both Circle and Rectangle, and invoke the calculateArea()
and calculatePerimeter() methods on each object. Print the results to the console.

What are the use cases of a final variable.

Fill the following table with yes/no

Privat No Protecte Public


e Modifier d
Same class Yes Yes Yes Yes

Same package
subclass

Same package
non-subclass

Different package
Subclass

Different package
non-subclass

Write the difference between abstract class and interface


Write the difference between Exceptions and Errors
Create a Java program that intentionally triggers a ArithmeticException (divide by zero exception).
Use a try-catch block to handle the exception gracefully.

Write a note on Importing package with examples.

Other theory question from the Java Notes.

Write a JAVA Program to calculate the average of an integer array elements.


Write a Java Program to print the numbers 1 to 16 in a 2-dimensional array of 4X4.
Write a java program to print pyramid using 2 dimensional array.
Write a program to write the data to a file and read data from a file using FileInputStream and
FileOutputStream.

You might also like