JAVA Question Bank
JAVA Question Bank
They form the building blocks of a Java program, enabling the creation
of classes, control structures, and logic.
3. Illustrate the concept of single and double-dimensional arrays in
Java, providing examples to demonstrate their usage.
4. Define Java tokens, and classify the four types of tokens with
examples.
Examples of statements:
Java
// Class Definition
class Car {
// Fields
String model;
int year;
// Method
void startEngine() {
System.out.println("Engine started");
}
}
// Object Creation
public class Main {
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car();
Key Points:
● Class: Car defines the blueprint with fields like model and year
and a method startEngine().
● Object: myCar is an object of the Car class.
● Fields: Specific data like "Tesla Model S" and 2022 is assigned
to myCar.
● Methods: The startEngine() method is called using the
myCar object.
java
class Animal { }
class Dog extends Animal { }
Inheritance No Yes
Required
Return Can have different Must have the same return type
Type return types as the superclass
● Definition:
An abstract class in Java is a class that cannot be
instantiated and may contain abstract methods (methods without
a body) as well as non-abstract methods (with an
implementation).
Syntax:
java
●
● Key Features:
○ Can have both abstract and concrete methods.
○ Can define constructors and maintain state through
instance variables.
○ Can have access modifiers like private, protected,
public.
○ Used when you want to provide partial implementation that
can be shared by subclasses.
Interfaces
● Definition:
An interface in Java is a completely abstract class that
defines a set of methods that implementing classes must
provide. Since Java 8, interfaces can also contain default
methods with a body.
Syntax:
java
interface Animal {
void sound();
default void sleep() {
System.out.println("Sleeping...");
}
}
class Dog implements Animal {
public void sound() {
System.out.println("Barks");
}
}
●
● Key Features:
○ All methods are abstract (prior to Java 8) or can have
default implementations (post Java 8).
○ Interfaces provide full abstraction.
○ No constructors or instance variables allowed.
○ Multiple inheritance through interfaces is possible.
Comparison:
1. Data Hiding:
Sensitive data is hidden from unauthorized access by
making fields private.
2. Control Access: You can control how the fields of a class are
accessed and modified using getter and setter methods.
3. Increased Flexibility: By controlling the access to data,
encapsulation allows changes to the internal state of an object
without modifying external code.
4. Security: Encapsulation enhances security by ensuring that only
valid data is assigned to the fields through controlled access.
5. Code Maintainability: Since data is hidden and access is
controlled through methods, modifying code is easier, leading to
better maintainability.
15. Demonstrate the use of this and super keywords in Java, and
assess their functionality through examples.
Example:
java
class Dog {
private String breed;
Example:
java
class Animal {
String color = "white";
void sound() {
System.out.println("Animal makes sound");
}
}
void display() {
System.out.println("Dog color: " + color);
// brown
System.out.println("Animal color: " +
super.color); // white
}
void sound() {
super.sound(); // Calls parent class method
System.out.println("Dog barks");
}
}
1. Single Inheritance:
One class inherits from another.
○ Example: class Dog extends Animal { }
2. Multilevel Inheritance: A class is derived from another derived
class.
○ Example: class Puppy extends Dog { }
3. Hierarchical Inheritance: Multiple classes inherit from one base
class.
○ Example: class Cat extends Animal, class Dog
extends Animal { }
4. Multiple Inheritance (via Interfaces): A class can implement
multiple interfaces.
Example:
java
java
Mutability
import java.util.StringTokenizer;
System.out.println(tokenizer.nextToken());
}
}
}
Key Points:
20. Define access modifiers in Java and list the four types of access
modifiers available.