0% found this document useful (0 votes)
8 views2 pages

Java Programming Practice Questions

The document contains a series of Java programming practice questions covering fundamental concepts such as class creation, constructors, object declaration, static vs non-static methods, package creation and access, importing packages, CLASSPATH understanding, auto boxing, and string manipulation. Each question provides a specific task to implement, demonstrating key programming principles. The exercises are designed to enhance understanding and application of Java programming skills.

Uploaded by

kgond332007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Java Programming Practice Questions

The document contains a series of Java programming practice questions covering fundamental concepts such as class creation, constructors, object declaration, static vs non-static methods, package creation and access, importing packages, CLASSPATH understanding, auto boxing, and string manipulation. Each question provides a specific task to implement, demonstrating key programming principles. The exercises are designed to enhance understanding and application of Java programming skills.

Uploaded by

kgond332007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programming 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.

3. Declaring Objects (Object & Object Reference)

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.

4. Static and Non-Static Variables/Methods

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.

5. Creating and Accessing Packages

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 `[Link]` to create a program that uses the `Scanner` and `ArrayList`

classes. Demonstrate how to import and use classes from a package.

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,

and characters using various `String` class methods.

10. StringBuffer

Write a Java program that takes a string from the user and:

- Reverses it using `StringBuffer`

- Appends a message

- Deletes a portion of the string

Common questions

Powered by AI

To create a class with a constructor in Java, define the class with its attributes and then define a constructor method named after the class. For the Employee class, you would define attributes like `name`, `ID`, and `salary`. The constructor would be defined by `Employee(String name, int ID, double salary)` and used to initialize these fields like `this.name = name;`. The constructor method allows for setting initial values for objects when they are created.

The primary difference is that `String` objects are immutable, meaning once created, they cannot be changed. In contrast, `StringBuffer` objects are mutable, allowing modifications without creating a new object. For example, reversing a string can be done using `StringBuffer` by `StringBuffer buffer = new StringBuffer("example"); buffer.reverse();` whereas `String` would need reassignment after any change. This mutability makes `StringBuffer` more efficient for operations that modify strings often.

Static methods belong to the class and can be called on the class itself without creating an instance, using the class name, for example, `Calculator.add()`. Non-static methods require an instance of the class to be called, for example, by creating an object `Calculator calculator = new Calculator();` and then calling `calculator.multiply()`.

In Java, multiple objects of a class can be created by instantiating the class with the `new` keyword. For example, for the `Book` class, objects can be created as `Book book1 = new Book();` and `Book book2 = new Book();`. Methods like `inputBookDetails()` and `displayBookDetails()` can be invoked on these objects using dot notation, such as `book1.inputBookDetails(); book1.displayBookDetails();`. This shows how objects interact with the class's methods.

To manage classes in separate packages, you define each class within its own package using the `package` keyword at the beginning of the Java file. For accessing a class from a different package, first compile the classes, then import the desired class in the file where it needs to be accessed using the `import` statement, such as `import bank.Account;`. You can then instantiate and use the Account class in your application. This setup allows for better organization and modularity of code.

Auto boxing in Java is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an `int` to an `Integer`. Unboxing is the reverse process where the object wrapper class is converted back to a primitive type. An example is storing an `int` value `int num = 10;` into an `Integer` object `Integer numObject = num;` (auto boxing) and subsequently retrieving the primitive type `int num2 = numObject;` (unboxing)

In Java, object references are variables that hold the memory address of an object's location rather than the object itself. For instance, given a `Car` class, you would declare an object reference by `Car myCar;`. By instantiating the object with `myCar = new Car();`, the reference `myCar` can be used to access the fields and methods of `Car`, such as `myCar.startEngine();` or `myCar.make = "Toyota";`. This approach ensures that any modifications through the reference affect the actual object in memory.

To compute the number of vowels, words, and characters in a user-input sentence, you can use Java's `String` class methods like `split()` for word separation, `length()` for characters counting, and iterate through the string with a method like `toLowerCase()` combined with a loop and conditional checks to count vowels. For example, words can be split by spaces using `String.split(" ")`, characters can be counted directly with `String.length()`, and vowels can be iterated and counted through a filter like checking if `charAt(i)` is in 'aeiou'.

Classes from Java's `java.util` package can be imported at the beginning of a Java file using the `import` statement, such as `import java.util.Scanner;` or `import java.util.ArrayList;`. Once imported, these classes can be used directly in the code. For instance, a `Scanner` object can be created for receiving input as `Scanner scanner = new Scanner(System.in);`, and an `ArrayList` can be initialized with `ArrayList<String> list = new ArrayList<>();`. These utilities provide functionality for dynamic collections and input handling.

Setting CLASSPATH in Java instructs the Java compiler and JVM where to look for classes during compile and execute time. To use CLASSPATH for compiling and running classes, ensure that the directory containing the class files is included. For example, `set CLASSPATH=.;C:\path\to\classes` on Windows or `export CLASSPATH=.:/path/to/classes` on Unix systems. This setup allows for the execution of classes from different packages even when running from the command line without specifying full paths each time.

You might also like