Lab on Java Programming
Journal Questions
Q.1 Write a Java program to find the largest of three numbers using if-else.
Q.2 Write a Java program to check whether a given year is a leap year or not.
Q.3 Write a Java program to classify a student’s grade based on marks using if-else.
Q.4 Write a Java program to perform a basic calculator using switch-case.
Q.5 Write a Java program to check whether a given character is a vowel or consonant using
switch-case.
Q.6 Write a Java program to print the factorial of a given number.
Q.7 Write a Java program to print the Fibonacci series up to N terms.
Q.8 Write a Java program to reverse a given number using while loop.
Q.9 Write a Java program to check whether a number is prime or not.
Q.10 Write a Java program to print all even numbers from 1 to 100.
Q.11 Write a Java program to create a class Student with attributes: rollNo, name, and marks.
Create objects of the class and display student details. Include a method to calculate grade based
on marks.
Q.12 Create a class Rectangle with attributes length and width. Use a parameterized constructor
to initialize the values. Write methods to calculate area and perimeter of the rectangle. Display
the results using an object of the class.
Q.13 Write a program to create a class Book with attributes: title, author, and price. Include a
default constructor and a parameterized constructor. Create objects using both constructors and
display book details.
Q. 14 Design a Java program to:
Accept an array of integers from the user.
Implement a method to sort the array in ascending order without using built-in sort
functions.
Implement a method to search for a given element using linear search and binary search.
Display the sorted array and the result of the searches.
Q. 15 Design a Java program to:
Create a two-dimensional integer array.
Traverse the array and display its elements in matrix form.
Implement a method to search for a specific value in the 2D array and return its position.
Handle the case when the element is not found.
Q.16 Design a Java program to:
Take two input strings from the user.
Perform and display results for the following operations: length, concatenation,
comparison (case-sensitive and case-insensitive), substring extraction, and replacement of
characters.
Include methods for each operation.
Q.17 Design a Java program to: Demonstrate the concept of the String Literal Pool in Java.
Create strings using literals and the new keyword. Compare the strings using == and .equals()
and explain the output with comments in the code.
Q.18 Create a class Employee with attributes: empId, name, and salary. Create a subclass
Manager that inherits from Employee and has an additional attribute department. Display details
of both Employee and Manager using objects.
Q.19 Create a class Person with name and age. Create a subclass Student with rollNo and marks.
Create another subclass GraduateStudent with degree and yearOfPassing. Display complete
details using objects.
Q.20 Design a Student<T> class using generics to store student details with a generic ID type
(Integer or String).
Store multiple students in an ArrayList and Search student by ID using Iterator.
Q.21 Design a Java program to manage a collection of books using a HashSet, where each book
has an ISBN, title, author, and price. The program should ensure that no two books with the
same ISBN are stored. Add at least five sample books to the collection. The program should then
display all the books sorted by title using a Comparator.
Q.22 Simulate a shopping cart using LinkedList<CartItem> where each item has itemName,
quantity, price.
Perform:
Add/Remove/Update quantity of items
Calculate total bill
Q.23 Design a Java program to store employee records using HashMap<Integer, Employee>,
where the key is the employee ID and the value is an Employee object with name, department,
and salary. Add at least five employees and display all the records.
Q.24 Create a TreeMap<String, Integer> where key=destination city, value=distance from your
city. Display cities in reverse alphabetical order.
Q. 25 Create a bilingual dictionary (LinkedHashMap<String, String>) where the user can add
word-meaning pairs.
Provide:
Search by word
Export dictionary in alphabetical order
Q.26 Design a Java program to create two threads:
Odd Thread – Prints all odd numbers from 1 to 20.
Even Thread – Prints all even numbers from 1 to 20.
Q.27 Create a program to calculate sum and product of an array using two separate threads.
Q.28 Create packages: [Link] and [Link]. Employee package contains Employee
class; Utils package has methods for calculating salary and bonus. Demonstrate accessing
package methods in the main program.
Q. 29 Write a java program to Accept marks for students.
Throw user-defined exception InvalidMarksException if marks are outside 0–100.
Calculate grade based on valid marks.
Q. 30 Write a java program to Input a paragraph from the user. Use String class methods to count
number of words, vowels, consonants, and sentences.
Q.31 Write a Java program to: Store marks of students in different subjects using a two-
dimensional array.
Calculate for each student:
Total marks
Average marks
Q. 32 Design a Java program to calculate the area and perimeter of different shapes using
interfaces. Create an interface named Shape with two methods: area() to return the area and
perimeter() to return the perimeter of a shape. Implement this interface in two classes: Circle,
which takes the radius as a parameter in its constructor, and Rectangle, which takes length and
width. In the main() method, create objects of Circle and Rectangle using hardcoded values (for
example, radius = 5, length = 4, and width = 6) and display the area and perimeter of each shape.