Practice 70 Java Constructor Programming Questions
Q1. Write a Java program to create a constructor that initializes a string.
Input:
Enter a string: Hello, World!
Expected Output:
The string is: Hello, World!
Q2. Write a Java program to create a constructor that accepts two integers and calculates their sum.
Input:
Enter two integers: 5 10
Expected Output:
The sum is: 15
Q3. Write a Java program to demonstrate the use of a default constructor.
Expected Output:
Default Constructor Called
Q4. Write a Java program to create a constructor that accepts a name and age, then displays them.
Input:
Enter name: John
Enter age: 25
Expected Output:
Name: John
Age: 25
Q5. Write a Java program to create a constructor that initializes a circle’s radius and calculates its area.
Input:
Enter radius: 7
Expected Output:
Area of the circle is: 153.93804002589985
Q6. Write a Java program to create a constructor that initializes a rectangle’s length and width, then calculates its area.
Input:
Enter length: 5
Enter width: 3
Expected Output:
Area of the rectangle is: 15
Q7. Write a Java program to create a constructor that initializes an object with a string and an integer, then displays them.
Input:
Enter string: Java
Enter number: 10
Expected Output:
String: Java
Number: 10
Q8. Write a Java program to create a constructor that accepts two integers and calculates their product.
Input:
Enter two integers: 6 4
Expected Output:
The product is: 24
Q9. Write a Java program to demonstrate constructor overloading with two constructors.
Input:
Enter a number: 5
Expected Output:
Constructor with one parameter called
Q10. Write a Java program to create a constructor that initializes a person’s name, age, and city.
Input:
Enter name: Alice
Enter age: 30
Enter city: New York
Expected Output:
Name: Alice
Age: 30
City: New York
Q11. Write a Java program to create a constructor that initializes a vehicle’s brand and model.
Input:
Enter brand: Toyota
Enter model: Corolla
Expected Output:
Brand: Toyota
Model: Corolla
Q12. Write a Java program to create a constructor that accepts an array of integers and calculates their sum.
Input:
Enter the number of elements: 5
Enter elements: 1 2 3 4 5
Expected Output:
The sum of the array is: 15
Q13. Write a Java program to create a constructor that initializes a book’s title and price.
Input:
Enter title: Java Programming
Enter price: 29.99
Expected Output:
Title: Java Programming
Price: 29.99
Q14. Write a Java program to create a constructor that initializes an employee’s ID and salary.
Input:
Enter employee ID: 101
Enter salary: 50000
Expected Output:
Employee ID: 101
Salary: 50000
Q15. Write a Java program to create a constructor that initializes a student’s name, age, and grade.
Input:
Enter name: Bob
Enter age: 20
Enter grade: A
Expected Output:
Name: Bob
Age: 20
Grade: A
Q16. Write a Java program to create a constructor that accepts a date of birth and calculates the age.
Input:
Enter date of birth (yyyy-mm-dd): 2000-01-01
Expected Output:
Age is: 25
Q17. Write a Java program to create a constructor that initializes a bank account with an account number and balance.
Input:
Enter account number: 12345
Enter balance: 1000
Expected Output:
Account Number: 12345
Balance: 1000
Q18. Write a Java program to create a constructor that initializes a point’s x and y coordinates.
Input:
Enter x coordinate: 2
Enter y coordinate: 3
Expected Output:
Point coordinates: (2, 3)
Q19. Write a Java program to create a constructor that initializes a product’s name and price.
Input:
Enter product name: Laptop
Enter price: 700
Expected Output:
Product Name: Laptop
Price: 700
Q20. Write a Java program to create a constructor that initializes a circle’s radius and calculates its circumference.
Input:
Enter radius: 5
Expected Output:
Circumference of the circle is: 31.41592653589793
Q21. Write a Java program to create a constructor that accepts the radius and calculates the area and circumference of a circle.
Input:
Enter radius: 10
Expected Output:
Area: 314.1592653589793
Circumference: 62.83185307179586
Q22. Write a Java program to create a constructor that initializes a book with title, author, and year.
Input:
Enter title: Effective Java
Enter author: Joshua Bloch
Enter year: 2008
Expected Output:
Title: Effective Java
Author: Joshua Bloch
Year: 2008
Q23. Write a Java program to create a constructor that initializes a person’s first name and last name.
Input:
Enter first name: John
Enter last name: Doe
Expected Output:
Full name: John Doe
Q24. Write a Java program to create a constructor that initializes a car’s brand, model, and year.
Input:
Enter brand: Ford
Enter model: Mustang
Enter year: 2022
Expected Output:
Brand: Ford
Model: Mustang
Year: 2022
Q25. Write a Java program to create a constructor that initializes an employee’s name, position, and salary.
Input:
Enter name: Alice
Enter position: Manager
Enter salary: 80000
Expected Output:
Name: Alice
Position: Manager
Salary: 80000
Q26. Write a Java program to create a constructor that initializes a student’s roll number and grade.
Input:
Enter roll number: 101
Enter grade: A
Expected Output:
Roll number: 101
Grade: A
Q27. Write a Java program to create a constructor that accepts two numbers and finds the larger of the two.
Input:
Enter first number: 5
Enter second number: 8
Expected Output:
The larger number is: 8
Q28. Write a Java program to create a constructor that initializes a time in hours and minutes, then calculates total minutes.
Input:
Enter hours: 2
Enter minutes: 30
Expected Output:
Total minutes: 150
Q29. Write a Java program to create a constructor that accepts the name of a student and their marks in 3 subjects, and calculates the average.
Input:
Enter name: Jane
Enter marks for 3 subjects: 85 90 88
Expected Output:
Name: Jane
Average marks: 87.67
Q30. Write a Java program to create a constructor that initializes a product’s name, quantity, and price, and calculates total cost.
Input:
Enter product name: Phone
Enter quantity: 2
Enter price: 300
Expected Output:
Product: Phone
Total cost: 600
Q31. Write a Java program to create a constructor that initializes a rectangle with width and height, then calculates the perimeter.
Input:
Enter width: 4
Enter height: 6
Expected Output:
Perimeter of the rectangle is: 20
Q32. Write a Java program to create a constructor that initializes a person's name and date of birth, then calculates their age.
Input:
Enter name: David
Enter date of birth (yyyy-mm-dd): 1995-05-10
Expected Output:
Name: David
Age: 29
Q33. Write a Java program to create a constructor that accepts two floating-point numbers and performs subtraction.
Input:
Enter first number: 10.5
Enter second number: 4.3
Expected Output:
The result of subtraction is: 6.2
Q34. Write a Java program to create a constructor that initializes a book’s title, author, and price.
Input:
Enter title: Java Basics
Enter author: Jane Smith
Enter price: 25.5
Expected Output:
Title: Java Basics
Author: Jane Smith
Price: 25.5
Q35. Write a Java program to create a constructor that initializes a number and calculates its square.
Input:
Enter a number: 6
Expected Output:
The square of 6 is: 36
Q36. Write a Java program to create a constructor that accepts a person's first name and last name, then displays the full name.
Input:
Enter first name: Sarah
Enter last name: Connor
Expected Output:
Full name: Sarah Connor
Q37. Write a Java program to create a constructor that accepts an integer and calculates its factorial.
Input:
Enter an integer: 5
Expected Output:
The factorial of 5 is: 120
Q38. Write a Java program to create a constructor that initializes a cube’s side length and calculates its volume.
Input:
Enter side length: 3
Expected Output:
Volume of the cube is: 27
Q39. Write a Java program to create a constructor that initializes a book’s name and its number of pages, then displays the total pages.
Input:
Enter book name: Programming 101
Enter number of pages: 200
Expected Output:
Book: Programming 101
Pages: 200
Q40. Write a Java program to create a constructor that initializes a person’s name, city, and country.
Input:
Enter name: Olivia
Enter city: Paris
Enter country: France
Expected Output:
Name: Olivia
City: Paris
Country: France
Q41. Write a Java program to create a constructor that initializes a student's name and subject marks, then calculates total marks.
Input:
Enter name: Peter
Enter marks for 3 subjects: 78 89 95
Expected Output:
Name: Peter
Total Marks: 262
Q42. Write a Java program to create a constructor that initializes a time with hours and minutes and displays the total minutes.
Input:
Enter hours: 2
Enter minutes: 45
Expected Output:
Total minutes: 165
Q43. Write a Java program to create a constructor that accepts a salary and calculates the tax.
Input:
Enter salary: 5000
Expected Output:
Tax is: 500
Q44. Write a Java program to create a constructor that initializes a vehicle’s model and price.
Input:
Enter model: BMW X5
Enter price: 60000
Expected Output:
Model: BMW X5
Price: 60000
Q45. Write a Java program to create a constructor that accepts an integer and finds whether it is even or odd.
Input:
Enter a number: 7
Expected Output:
The number 7 is odd.
Q46. Write a Java program to create a constructor that initializes a phone’s brand, model, and price.
Input:
Enter brand: Samsung
Enter model: Galaxy S21
Enter price: 999.99
Expected Output:
Brand: Samsung
Model: Galaxy S21
Price: 999.99
Q47. Write a Java program to create a constructor that initializes a person's name and displays a greeting message.
Input:
Enter name: Emma
Expected Output:
Hello, Emma! Welcome to Java Programming.
Q48. Write a Java program to create a constructor that initializes a time in hours, minutes, and seconds and displays the total seconds.
Input:
Enter hours: 1
Enter minutes: 30
Enter seconds: 45
Expected Output:
Total time in seconds: 5445
Q49. Write a Java program to create a constructor that initializes a product’s name, price, and quantity, and calculates total cost.
Input:
Enter product name: Smartwatch
Enter price: 199.99
Enter quantity: 3
Expected Output:
Product: Smartwatch
Total cost: 599.97
Q50. Write a Java program to create a constructor that accepts a string and displays the number of characters in it.
Input:
Enter a string: JavaProgramming
Expected Output:
The string "JavaProgramming" has 15 characters.
Q51. Write a Java program to create a constructor that initializes a rectangle’s width and height and calculates its area.
Input:
Enter width: 6
Enter height: 8
Expected Output:
Area of the rectangle: 48
Q52. Write a Java program to create a constructor that accepts a student's name and subject marks and calculates the percentage.
Input:
Enter name: Michael
Enter marks for 5 subjects: 85 90 78 88 92
Expected Output:
Name: Michael
Percentage: 86.6%
Q53. Write a Java program to create a constructor that initializes a circle’s radius and calculates its area and circumference.
Input:
Enter radius: 4
Expected Output:
Area: 50.26548245743669
Circumference: 25.1327412287193
Q54. Write a Java program to create a constructor that initializes a triangle’s base and height and calculates its area.
Input:
Enter base: 10
Enter height: 5
Expected Output:
Area of the triangle is: 25
Q55. Write a Java program to create a constructor that initializes an address with street, city, and zip code.
Input:
Enter street: 123 Main St
Enter city: Los Angeles
Enter zip code: 90001
Expected Output:
Address: 123 Main St, Los Angeles, 90001
Q56. Write a Java program to create a constructor that initializes a book’s title and author and displays them.
Input:
Enter title: Java for Beginners
Enter author: Alice Johnson
Expected Output:
Title: Java for Beginners
Author: Alice Johnson
Q57. Write a Java program to create a constructor that initializes a student’s name and calculates the average marks.
Input:
Enter name: John
Enter marks for 3 subjects: 75 80 70
Expected Output:
Name: John
Average Marks: 75.0
Q58. Write a Java program to create a constructor that accepts a product’s name, price, and quantity, and calculates the total price.
Input:
Enter product name: Tablet
Enter price: 150
Enter quantity: 2
Expected Output:
Product: Tablet
Total price: 300
Q59. Write a Java program to create a constructor that initializes a person’s name and age and displays them.
Input:
Enter name: Peter
Enter age: 30
Expected Output:
Name: Peter
Age: 30
Q60. Write a Java program to create a constructor that accepts two integers and calculates their difference.
Input:
Enter first integer: 9
Enter second integer: 5
Expected Output:
The difference is: 4
Q61. Write a Java program to create a constructor that initializes a movie’s name and director and displays them.
Input:
Enter movie name: The Matrix
Enter director: Wachowskis
Expected Output:
Movie: The Matrix
Director: Wachowskis
Q62. Write a Java program to create a constructor that accepts a point’s x and y coordinates and calculates the distance from the origin.
Input:
Enter x coordinate: 3
Enter y coordinate: 4
Expected Output:
Distance from origin: 5.0
Q63. Write a Java program to create a constructor that initializes a city’s name and population, and displays them.
Input:
Enter city name: Paris
Enter population: 2148000
Expected Output:
City: Paris
Population: 2148000
Q64. Write a Java program to create a constructor that initializes a book’s title, author, and price, and displays them.
Input:
Enter title: Advanced Java
Enter author: Bob Martin
Enter price: 50.99
Expected Output:
Title: Advanced Java
Author: Bob Martin
Price: 50.99
Q65. Write a Java program to create a constructor that initializes a product’s name, price, and quantity and calculates the total cost.
Input:
Enter product name: Laptop
Enter price: 900.00
Enter quantity: 2
Expected Output:
Product: Laptop
Total cost: 1800.0
Q66. Write a Java program to create a constructor that initializes a book’s title, author, and year of publication and displays them.
Input:
Enter title: Java Programming
Enter author: Mark Lee
Enter year: 2021
Expected Output:
Title: Java Programming
Author: Mark Lee
Year: 2021
Q67. Write a Java program to create a constructor that accepts two numbers and calculates the average.
Input:
Enter first number: 12
Enter second number: 18
Expected Output:
Average: 15.0
Q68. Write a Java program to create a constructor that initializes a car’s brand, model, and price and displays them.
Input:
Enter brand: Tesla
Enter model: Model S
Enter price: 79999
Expected Output:
Brand: Tesla
Model: Model S
Price: 79999
Q69. Write a Java program to create a constructor that initializes a person's name and age and displays the name and calculates the decade.
Input:
Enter name: Lucy
Enter age: 25
Expected Output:
Name: Lucy
Decade: 20s
Q70. Write a Java program to create a constructor that initializes a product’s name, price, and quantity and calculates the total revenue.
Input:
Enter product name: Smartphone
Enter price: 400
Enter quantity: 3
Expected Output:
Product: Smartphone
Revenue: 1200
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!