Java Question Bank
Java Question Bank
Illustrate the concept behind constructor overloading in Java with an example code in
detail.
Develop a Java class that uses multiple inner classes to simulate a banking system
where each inner class represents different types of accounts (e.g., savings, checking)
and has specific operations.
Build a class Book that has attributes title, author, and price. Demonstrate a method
applyDiscount(double percentage) that reduces the price of the book by the given
percentage. Create an object of Book and apply the discount.
Develop a Java program to determine the grade of a student based on marks using a
switch statement.
Write a constructor that uses this keyword to differentiate between instance variables
and constructor parameters
Demonstrate how to control access to private members using methods in the same class
with example.
Develop a class Person with attributes name and age, and write a method displayInfo() to
print the details of the person. Instantiate two objects of this class and call the
displayInfo() method.
```java
class Car {
String model;
int year;
void drive() {
System.out.println("The car is driving");
}
}
```
```java
Car car1 = new Car(); // car1 is an object of the Car class
```
What is inheritance in Java, and what are its types?
Inheritance is the process by which one class (child class) acquires the properties and
behaviors of another class (parent class). This allows code reuse and supports hierarchical
relationships between classes.
Java does not support multiple inheritance (a class cannot inherit from more than one class) to
avoid ambiguity. However, it supports multiple inheritance through interfaces.
```java
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
```java
class Calculator {
int add(int a, int b) {
return a + b;
}
**Method Overriding**:
- Methods have the same name, same return type, and same parameters but different
implementations in parent and child classes.
- It occurs between a superclass and a subclass.
- Runtime polymorphism.
```java
class Person {
private String name; // private field
**Abstract Class**:
- A class that cannot be instantiated and may contain abstract methods (methods without a
body) as well as concrete methods.
**Interface**:
- A reference type in Java, similar to a class, that can contain only abstract methods (before
Java 8) or default/static methods (from Java 8 onwards). A class can implement multiple
interfaces.
Example of an Interface:
```java
interface Animal {
void sound();
}
```java
class Car {
String model;
// Constructor
Car(String model) {
this.model = model;
}
}
Example:
```java
public class MainOverload {
public static void main(String[] args) {
System.out.println("Main with String[]");
main(5); // Calling overloaded main method
}
Example:
```java
Scanner sc = new Scanner(System.in);
```
What are some common methods of the `Scanner` class?
- **nextInt()**: Reads an `int` value.
- **nextDouble()**: Reads a `double` value.
- **nextLine()**: Reads a `String` until the end of the line.
- **next()**: Reads a single word (until a space).
- **hasNext()**: Returns true if there's another token.
- **hasNextInt()**: Returns true if the next token is an integer.
Example:
```java
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer: ");
int num = sc.nextInt();
How do you check if the input is of a specific type (e.g., integer) using `Scanner`?
You can use the `hasNextInt()`, `hasNextDouble()`, etc., to check if the next input is of the
expected type.
Example:
```java
Scanner sc = new Scanner(System.in);
Example:
```java
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer: ");
int num = sc.nextInt();
Example:
```java
Scanner sc = new Scanner(System.in);
try {
System.out.println("Enter an integer: ");
int num = sc.nextInt();
System.out.println("You entered: " + num);
} catch (InputMismatchException e) {
System.out.println("Invalid input, please enter an integer.");
}
```
Can you use the `Scanner` class to read input from a file?
Yes, you can use the `Scanner` class to read input from a file by passing a `File` object to the
`Scanner` constructor.
Example:
```java
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
**nextLine()**: Reads the entire line, including spaces, and moves the scanner to the next line.
Example:
```java
Scanner sc = new Scanner(System.in);
Example:
```java
Scanner sc = new Scanner(System.in);
// Use scanner...
sc.close(); // Close the scanner when done
.