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