Java Practice Questions
Java Practice Questions
1. Class Fundamentals
Create a class `Book` with attributes: `title`, `author`, and `price`. Write methods to input book details
and display them. Create multiple objects and call methods to test.
2. Constructors
Create a class `Employee` with a constructor to initialize name, ID, and salary. Add a method to
display the employee details. Create at least two employee objects using the constructor.
Write a program that creates a class `Car`, and then instantiate it in the `main()` method using object
references. Demonstrate how to access fields and methods using the object reference.
Create a class `Calculator` with a static method `add()` and a non-static method `multiply()`. Show
how to call both methods from the main method and explain the difference in access.
Create a package named `bank`. Inside it, define a class `Account` with methods to deposit and
withdraw money. Access this class from another class in a different package.
6. Importing Packages
Use Java's built-in package `java.util` to create a program that uses the `Scanner` and `ArrayList`
7. Understanding CLASSPATH
Write two classes in different packages. Compile and run them using the command line by setting
the CLASSPATH manually. (You can include class-level comments on how to do this.)
8. Auto Boxing
Create a program that takes an `int` value and stores it in an `Integer` object using auto boxing.
Then demonstrate unboxing by converting the `Integer` object back to `int`.
9. String
Write a Java program that reads a sentence from the user and counts the number of vowels, words,
10. StringBuffer
Write a Java program that takes a string from the user and:
- Appends a message