Java is one of the most popular programming languages, and understanding constructors in Java is essential for mastering Object-Oriented Programming (OOP). If you are preparing for Java interviews, college exams, or simply want to improve your Java skills, this set of Java constructor practice questions will help you.
At Pivot Edu Unit, Dehradun, we provide hands-on Java training with real-world projects to enhance your programming expertise. Let’s dive into the best Java constructor questions to test and improve your coding skills.
What is a Constructor in Java?
A constructor in Java is a special method that gets called automatically when an object of a class is created. Constructors help initialize objects and can be of different types: default constructors, parameterized constructors, copy constructors, and overloaded constructors.
1. Basic Constructor Questions
Create a class Student with attributes name and age. Use a constructor to initialize these attributes and display them.
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
Write a class Car with attributes brand, model, and price. Implement a constructor to initialize the values and print the details.
class Car {
String brand, model;
double price;
Car(String brand, String model, double price) {
this.brand = brand;
this.model = model;
this.price = price;
}
void display() {
System.out.println("Brand: " + brand + ", Model: " + model + ", Price: " + price);
}
}
Design a Book class for a library system with attributes title, author, and ISBN. Initialize these attributes using a constructor and display the book details.
public class Book {
private String title;
private String author;
private String ISBN;
// Constructor
public Book(String title, String author, String ISBN) {
this.title = title;
this.author = author;
this.ISBN = ISBN;
}
// Method to display book details
public void displayDetails() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("ISBN: " + ISBN);
}
public static void main(String[] args) {
Book book = new Book("Effective Java", "Joshua Bloch", "978-0134685991");
book.displayDetails();
}
}
Create a Patient class for a hospital management system with attributes name, age, and patientID. Use a constructor to initialize these attributes and display the patient’s information.
public class Patient {
private String name;
private int age;
private String patientID;
// Constructor
public Patient(String name, int age, String patientID) {
this.name = name;
this.age = age;
this.patientID = patientID;
}
// Method to display patient information
public void displayInfo() {
System.out.println("Patient Name: " + name);
System.out.println("Age: " + age);
System.out.println("Patient ID: " + patientID);
}
public static void main(String[] args) {
Patient patient = new Patient("John Doe", 30, "P12345");
patient.displayInfo();
}
}
2. Parameterized Constructor Questions
Develop a BankAccount class that requires an accountNumber and initialBalance upon creation. Ensure that the balance cannot be negative.
public class BankAccount {
private String accountNumber;
private double balance;
// Constructor
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
if (initialBalance >= 0) {
this.balance = initialBalance;
} else {
System.out.println("Initial balance cannot be negative. Setting balance to 0.");
this.balance = 0;
}
}
// Method to display account details
public void displayAccountDetails() {
System.out.println("Account Number: " + accountNumber);
System.out.println("Balance: $" + balance);
}
public static void main(String[] args) {
BankAccount account = new BankAccount("123456789", 500.0);
account.displayAccountDetails();
}
}
Create a Movie class for a cinema booking system that initializes with title, genre, and duration (in minutes).
public class Movie {
private String title;
private String genre;
private int duration; // in minutes
// Constructor
public Movie(String title, String genre, int duration) {
this.title = title;
this.genre = genre;
this.duration = duration;
}
// Method to display movie details
public void displayMovieDetails() {
System.out.println("Title: " + title);
Implement a LibraryBook class that takes title, author, and ISBN as parameters in a constructor and displays book details.
public class LibraryBook {
private String title;
private String author;
private String ISBN;
// Parameterized Constructor
public LibraryBook(String title, String author, String ISBN) {
this.title = title;
this.author = author;
this.ISBN = ISBN;
}
public void displayBookInfo() {
System.out.println("Title: " + title + ", Author: " + author + ", ISBN: " + ISBN);
}
public static void main(String[] args) {
LibraryBook book = new LibraryBook("Effective Java", "Joshua Bloch", "978-0134685991");
book.displayBookInfo();
}
}
Create a Flight class that accepts flightNumber, destination, and departureTime as parameters and prints flight details.
public class Flight {
private String flightNumber;
private String destination;
private String departureTime;
// Parameterized Constructor
public Flight(String flightNumber, String destination, String departureTime) {
this.flightNumber = flightNumber;
this.destination = destination;
this.departureTime = departureTime;
}
public void displayFlightInfo() {
System.out.println("Flight Number: " + flightNumber +
", Destination: " + destination +
", Departure Time: " + departureTime);
}
public static void main(String[] args) {
Flight flight = new Flight("AI-202", "New York", "10:30 AM");
flight.displayFlightInfo();
}
}
Develop a Product class where the constructor initializes productName, category, and price. Print product details.
public class Product {
private String productName;
private String category;
private double price;
// Parameterized Constructor
public Product(String productName, String category, double price) {
this.productName = productName;
this.category = category;
this.price = price;
}
public void displayProductInfo() {
System.out.println("Product Name: " + productName +
", Category: " + category +
", Price: $" + price);
}
public static void main(String[] args) {
Product product = new Product("Laptop", "Electronics", 999.99);
product.displayProductInfo();
}
}
Create a Movie class that takes title, director, and releaseYear as parameters and prints the movie details.
public class Movie {
private String title;
private String director;
private int releaseYear;
// Parameterized Constructor
public Movie(String title, String director, int releaseYear) {
this.title = title;
this.director = director;
this.releaseYear = releaseYear;
}
public void displayMovieInfo() {
System.out.println("Title: " + title +
", Director: " + director +
", Release Year: " + releaseYear);
}
public static void main(String[] args) {
Movie movie = new Movie("Inception", "Christopher Nolan", 2010);
movie.displayMovieInfo();
}
}
Constructor Overloading Questions with Solutions
Create a Vehicle class with overloaded constructors that allow creating a vehicle with only brand, with brand and model, and with brand, model, and price.
public class Vehicle {
private String brand;
private String model;
private double price;
// Constructor with only brand
public Vehicle(String brand) {
this.brand = brand;
this.model = "Unknown";
this.price = 0.0;
}
// Constructor with brand and model
public Vehicle(String brand, String model) {
this.brand = brand;
this.model = model;
this.price = 0.0;
}
// Constructor with brand, model, and price
public Vehicle(String brand, String model, double price) {
this.brand = brand;
this.model = model;
this.price = price;
}
public void displayVehicleInfo() {
System.out.println("Brand: " + brand + ", Model: " + model + ", Price: $" + price);
}
public static void main(String[] args) {
Vehicle car1 = new Vehicle("Toyota");
Vehicle car2 = new Vehicle("Honda", "Civic");
Vehicle car3 = new Vehicle("BMW", "X5", 50000);
car1.displayVehicleInfo();
car2.displayVehicleInfo();
car3.displayVehicleInfo();
}
}
Develop an Employee class with overloaded constructors that allow creating an employee with only name, with name and ID, and with name, ID, and salary.
public class Employee {
private String name;
private int id;
private double salary;
// Constructor with only name
public Employee(String name) {
this.name = name;
this.id = 0;
this.salary = 0.0;
}
// Constructor with name and id
public Employee(String name, int id) {
this.name = name;
this.id = id;
this.salary = 0.0;
}
// Constructor with name, id, and salary
public Employee(String name, int id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
public void displayEmployeeInfo() {
System.out.println("Name: " + name + ", ID: " + id + ", Salary: $" + salary);
}
public static void main(String[] args) {
Employee emp1 = new Employee("Alice");
Employee emp2 = new Employee("Bob", 101);
Employee emp3 = new Employee("Charlie", 102, 75000);
emp1.displayEmployeeInfo();
emp2.displayEmployeeInfo();
emp3.displayEmployeeInfo();
}
}
Create a Hotel class with overloaded constructors to initialize hotelName, location, and rating. Allow object creation with different parameter sets.
public class Hotel {
private String hotelName;
private String location;
private int rating;
// Constructor 1: Default Constructor
public Hotel() {
this.hotelName = "Unknown";
this.location = "Not Specified";
this.rating = 0;
}
// Constructor 2: Only hotel name
public Hotel(String hotelName) {
this.hotelName = hotelName;
this.location = "Not Specified";
this.rating = 0;
}
// Constructor 3: Hotel name and location
public Hotel(String hotelName, String location) {
this.hotelName = hotelName;
this.location = location;
this.rating = 0;
}
// Constructor 4: Hotel name, location, and rating
public Hotel(String hotelName, String location, int rating) {
this.hotelName = hotelName;
this.location = location;
this.rating = rating;
}
public void displayHotelInfo() {
System.out.println("Hotel: " + hotelName +
", Location: " + location +
", Rating: " + rating + " stars");
}
public static void main(String[] args) {
Hotel hotel1 = new Hotel();
Hotel hotel2 = new Hotel("Grand Hyatt");
Hotel hotel3 = new Hotel("Taj Palace", "Mumbai");
Hotel hotel4 = new Hotel("The Oberoi", "Delhi", 5);
hotel1.displayHotelInfo();
hotel2.displayHotelInfo();
hotel3.displayHotelInfo();
hotel4.displayHotelInfo();
}
}
Create a Laptop class with overloaded constructors that allow creating a laptop with only brand, with brand and RAM size, and with brand, RAM size, and price.
public class Laptop {
private String brand;
private int ramSize;
private double price;
// Constructor 1: Only brand
public Laptop(String brand) {
this.brand = brand;
this.ramSize = 8; // Default RAM size
this.price = 0.0;
}
// Constructor 2: Brand and RAM size
public Laptop(String brand, int ramSize) {
this.brand = brand;
this.ramSize = ramSize;
this.price = 0.0;
}
// Constructor 3: Brand, RAM size, and price
public Laptop(String brand, int ramSize, double price) {
this.brand = brand;
this.ramSize = ramSize;
this.price = price;
}
public void displayLaptopInfo() {
System.out.println("Brand: " + brand +
", RAM: " + ramSize + "GB" +
", Price: $" + price);
}
public static void main(String[] args) {
Laptop laptop1 = new Laptop("Dell");
Laptop laptop2 = new Laptop("HP", 16);
Laptop laptop3 = new Laptop("Apple", 32, 2500);
laptop1.displayLaptopInfo();
laptop2.displayLaptopInfo();
laptop3.displayLaptopInfo();
}
}
Design a Restaurant class with overloaded constructors that allow creating an object with only name, with name and cuisine type, and with name, cuisine type, and average cost.
public class Restaurant {
private String name;
private String cuisineType;
private double avgCost;
// Constructor 1: Only name
public Restaurant(String name) {
this.name = name;
this.cuisineType = "Not Specified";
this.avgCost = 0.0;
}
// Constructor 2: Name and cuisine type
public Restaurant(String name, String cuisineType) {
this.name = name;
this.cuisineType = cuisineType;
this.avgCost = 0.0;
}
// Constructor 3: Name, cuisine type, and average cost
public Restaurant(String name, String cuisineType, double avgCost) {
this.name = name;
this.cuisineType = cuisineType;
this.avgCost = avgCost;
}
public void displayRestaurantInfo() {
System.out.println("Restaurant: " + name +
", Cuisine: " + cuisineType +
", Average Cost: $" + avgCost);
}
public static void main(String[] args) {
Restaurant rest1 = new Restaurant("Pizza Hut");
Restaurant rest2 = new Restaurant("Sushi House", "Japanese");
Restaurant rest3 = new Restaurant("Royal Dine", "Indian", 30);
rest1.displayRestaurantInfo();
rest2.displayRestaurantInfo();
rest3.displayRestaurantInfo();
}
}
3. Copy Constructor Questions with Solutions
Create a Smartphone class with a copy constructor to duplicate an object’s attributes (brand, model, price).
public class Smartphone {
private String brand;
private String model;
private double price;
// Parameterized Constructor
public Smartphone(String brand, String model, double price) {
this.brand = brand;
this.model = model;
this.price = price;
}
// Copy Constructor
public Smartphone(Smartphone phone) {
this.brand = phone.brand;
this.model = phone.model;
this.price = phone.price;
}
public void displaySmartphoneInfo() {
System.out.println("Brand: " + brand + ", Model: " + model + ", Price: $" + price);
}
public static void main(String[] args) {
Smartphone phone1 = new Smartphone("Apple", "iPhone 14", 999);
Smartphone phone2 = new Smartphone(phone1); // Using Copy Constructor
phone1.displaySmartphoneInfo();
phone2.displaySmartphoneInfo();
}
}
Implement a HotelRoom class with a copy constructor to clone a room’s attributes (roomNumber, roomType, price).
public class HotelRoom {
private int roomNumber;
private String roomType;
private double price;
// Parameterized Constructor
public HotelRoom(int roomNumber, String roomType, double price) {
this.roomNumber = roomNumber;
this.roomType = roomType;
this.price = price;
}
// Copy Constructor
public HotelRoom(HotelRoom room) {
this.roomNumber = room.roomNumber;
this.roomType = room.roomType;
this.price = room.price;
}
public void displayRoomInfo() {
System.out.println("Room Number: " + roomNumber + ", Room Type: " + roomType + ", Price: $" + price);
}
public static void main(String[] args) {
HotelRoom room1 = new HotelRoom(101, "Deluxe", 120);
HotelRoom room2 = new HotelRoom(room1); // Copying details
room1.displayRoomInfo();
room2.displayRoomInfo();
}
}
Create a Book class with a copy constructor that allows creating a duplicate book object.
public class Book {
private String title;
private String author;
private double price;
// Parameterized Constructor
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
// Copy Constructor
public Book(Book other) {
this.title = other.title;
this.author = other.author;
this.price = other.price;
}
public void displayBookInfo() {
System.out.println("Title: " + title +
", Author: " + author +
", Price: $" + price);
}
public static void main(String[] args) {
Book book1 = new Book("Java Programming", "James Gosling", 40.5);
Book book2 = new Book(book1); // Copying book1 into book2
book1.displayBookInfo();
book2.displayBookInfo();
}
}
Create a Person class with a copy constructor that copies personal details.
public class Person {
private String name;
private int age;
// Parameterized Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Copy Constructor
public Person(Person other) {
this.name = other.name;
this.age = other.age;
}
public void displayPersonInfo() {
System.out.println("Name: " + name +
", Age: " + age);
}
public static void main(String[] args) {
Person person1 = new Person("John Doe", 30);
Person person2 = new Person(person1); // Copying person1 into person2
person1.displayPersonInfo();
person2.displayPersonInfo();
}
}
Develop a BankAccount class where a copy constructor allows duplicating account details.
public class BankAccount {
private String accountHolder;
private int accountNumber;
private double balance;
// Parameterized Constructor
public BankAccount(String accountHolder, int accountNumber, double balance) {
this.accountHolder = accountHolder;
this.accountNumber = accountNumber;
this.balance = balance;
}
// Copy Constructor
public BankAccount(BankAccount other) {
this.accountHolder = other.accountHolder;
this.accountNumber = other.accountNumber;
this.balance = other.balance;
}
public void displayAccountInfo() {
System.out.println("Account Holder: " + accountHolder +
", Account Number: " + accountNumber +
", Balance: $" + balance);
}
public static void main(String[] args) {
BankAccount acc1 = new BankAccount("Alice Brown", 123456, 5000.75);
BankAccount acc2 = new BankAccount(acc1); // Copying acc1 into acc2
acc1.displayAccountInfo();
acc2.displayAccountInfo();
}
}
Implement a Movie class that supports copy constructor to create duplicate movie objects.
public class Movie {
private String title;
private String genre;
private int duration; // in minutes
// Parameterized Constructor
public Movie(String title, String genre, int duration) {
this.title = title;
this.genre = genre;
this.duration = duration;
}
// Copy Constructor
public Movie(Movie other) {
this.title = other.title;
this.genre = other.genre;
this.duration = other.duration;
}
public void displayMovieInfo() {
System.out.println("Movie: " + title +
", Genre: " + genre +
", Duration: " + duration + " min");
}
public static void main(String[] args) {
Movie movie1 = new Movie("Inception", "Sci-Fi", 148);
Movie movie2 = new Movie(movie1); // Copying movie1 into movie2
movie1.displayMovieInfo();
movie2.displayMovieInfo();
}
}
Create a Laptop class with a copy constructor that copies laptop specifications.
public class Laptop {
private String brand;
private int ram;
private double price;
// Parameterized Constructor
public Laptop(String brand, int ram, double price) {
this.brand = brand;
this.ram = ram;
this.price = price;
}
// Copy Constructor
public Laptop(Laptop other) {
this.brand = other.brand;
this.ram = other.ram;
this.price = other.price;
}
public void displayLaptopInfo() {
System.out.println("Brand: " + brand +
", RAM: " + ram + "GB" +
", Price: $" + price);
}
public static void main(String[] args) {
Laptop laptop1 = new Laptop("Dell", 16, 1200);
Laptop laptop2 = new Laptop(laptop1); // Copying laptop1 into laptop2
laptop1.displayLaptopInfo();
laptop2.displayLaptopInfo();
}
}
4. Constructor Chaining Questions with Solutions
Design a Company class where constructor chaining initializes companyName, location, and totalEmployees in a hierarchical manner.
public class Company {
private String companyName;
private String location;
private int totalEmployees;
// Constructor 1
public Company() {
this("Unknown Company");
}
// Constructor 2
public Company(String companyName) {
this(companyName, "Unknown Location");
}
// Constructor 3
public Company(String companyName, String location) {
this(companyName, location, 0);
}
// Constructor 4
public Company(String companyName, String location, int totalEmployees) {
this.companyName = companyName;
this.location = location;
this.totalEmployees = totalEmployees;
}
public void displayCompanyInfo() {
System.out.println("Company: " + companyName + ", Location: " + location + ", Employees: " + totalEmployees);
}
public static void main(String[] args) {
Company company1 = new Company();
Company company2 = new Company("Tech Corp");
Company company3 = new Company("Innovate Ltd.", "New York", 500);
company1.displayCompanyInfo();
company2.displayCompanyInfo();
company3.displayCompanyInfo();
}
}
Create an Employee class where constructor chaining initializes employee details.
public class Employee {
private String name;
private int id;
private double salary;
// Default Constructor
public Employee() {
this("Unknown", 0, 0.0); // Calls parameterized constructor
}
// Constructor with Name and ID
public Employee(String name, int id) {
this(name, id, 30000.0); // Calls full parameterized constructor
}
// Constructor with Name, ID, and Salary
public Employee(String name, int id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
public void display() {
System.out.println("Employee Name: " + name +
", ID: " + id +
", Salary: $" + salary);
}
public static void main(String[] args) {
Employee emp1 = new Employee();
Employee emp2 = new Employee("Alice", 101);
Employee emp3 = new Employee("Bob", 102, 50000);
emp1.display();
emp2.display();
emp3.display();
}
}
Develop a Smartphone class that demonstrates constructor chaining.
public class Smartphone {
private String brand;
private String model;
private double price;
// Default Constructor
public Smartphone() {
this("Unknown", "Unknown", 0.0);
}
// Constructor with Brand and Model
public Smartphone(String brand, String model) {
this(brand, model, 10000.0); // Default price
}
// Constructor with Brand, Model, and Price
public Smartphone(String brand, String model, double price) {
this.brand = brand;
this.model = model;
this.price = price;
}
public void display() {
System.out.println("Brand: " + brand +
", Model: " + model +
", Price: $" + price);
}
public static void main(String[] args) {
Smartphone phone1 = new Smartphone();
Smartphone phone2 = new Smartphone("Samsung", "Galaxy S21");
Smartphone phone3 = new Smartphone("Apple", "iPhone 15", 1200);
phone1.display();
phone2.display();
phone3.display();
}
}
Create a Vehicle class where constructor chaining initializes vehicle details.
public class Vehicle {
private String type;
private String brand;
private int wheels;
// Default Constructor
public Vehicle() {
this("Unknown", "Unknown", 4);
}
// Constructor with Type
public Vehicle(String type) {
this(type, "Unknown", 4);
}
// Constructor with Type and Brand
public Vehicle(String type, String brand) {
this(type, brand, 4);
}
// Constructor with Type, Brand, and Wheels
public Vehicle(String type, String brand, int wheels) {
this.type = type;
this.brand = brand;
this.wheels = wheels;
}
public void display() {
System.out.println("Vehicle Type: " + type +
", Brand: " + brand +
", Wheels: " + wheels);
}
public static void main(String[] args) {
Vehicle v1 = new Vehicle();
Vehicle v2 = new Vehicle("Bike");
Vehicle v3 = new Vehicle("Car", "Toyota");
Vehicle v4 = new Vehicle("Truck", "Volvo", 18);
v1.display();
v2.display();
v3.display();
v4.display();
}
}
Build a Hotel class that demonstrates constructor chaining for room booking.
public class Hotel {
private String name;
private String roomType;
private int nights;
private double price;
// Default Constructor
public Hotel() {
this("Default Hotel", "Standard", 1, 1000);
}
// Constructor with Name and Room Type
public Hotel(String name, String roomType) {
this(name, roomType, 1, 2000);
}
// Constructor with Name, Room Type, and Nights
public Hotel(String name, String roomType, int nights) {
this(name, roomType, nights, nights * 2000);
}
// Constructor with All Parameters
public Hotel(String name, String roomType, int nights, double price) {
this.name = name;
this.roomType = roomType;
this.nights = nights;
this.price = price;
}
public void display() {
System.out.println("Hotel: " + name +
", Room Type: " + roomType +
", Nights: " + nights +
", Total Price: $" + price);
}
public static void main(String[] args) {
Hotel h1 = new Hotel();
Hotel h2 = new Hotel("Luxury Inn", "Deluxe");
Hotel h3 = new Hotel("Grand Palace", "Suite", 3);
h1.display();
h2.display();
h3.display();
}
}
Implement a University class where constructor chaining initializes student details.
public class University {
private String studentName;
private String course;
private int duration; // in years
private double fee;
// Default Constructor
public University() {
this("Unknown", "General Studies", 3, 50000);
}
// Constructor with Name and Course
public University(String studentName, String course) {
this(studentName, course, 3, 70000);
}
// Constructor with Name, Course, and Duration
public University(String studentName, String course, int duration) {
this(studentName, course, duration, duration * 70000);
}
// Constructor with All Parameters
public University(String studentName, String course, int duration, double fee) {
this.studentName = studentName;
this.course = course;
this.duration = duration;
this.fee = fee;
}
public void display() {
System.out.println("Student: " + studentName +
", Course: " + course +
", Duration: " + duration + " years" +
", Fee: $" + fee);
}
public static void main(String[] args) {
University u1 = new University();
University u2 = new University("John Doe", "Computer Science");
University u3 = new University("Alice", "Mechanical Engineering", 4);
u1.display();
u2.display();
u3.display();
}
}
Mastering constructors in Java, including constructor chaining, overloading, and copy constructors, is essential for building robust and efficient Java applications. By practicing these real-world scenario-based questions, you can strengthen your OOP concepts and improve your coding skills for interviews and projects.
At Pivot Edu Unit, Dehradun, we offer industry-focused Java training with hands-on projects to help you become a proficient Java developer.
💡 Start your Java learning journey today! Visit Pivot Edu Unit and enhance your programming expertise. 🚀








