1.What is Java?
Answer: Java is a high-level, object-oriented programming language that is platform-independent due to its ability to run on any device equipped with a Java Virtual Machine (JVM). It is widely used for building enterprise-scale applications, mobile applications, and web services .
2.What are the main features of Java?
Answer:
Platform Independence: Java code can run on any platform without modification.
Object-Oriented: Supports encapsulation, inheritance, and polymorphism.
Robust and Secure: Strong memory management and exception handling.
Multithreading: Allows concurrent execution of multiple threads for efficient CPU utilization .
3.Explain the difference between JDK, JRE, and JVM.
Answer:
JVM (Java Virtual Machine): Executes Java bytecode and provides a runtime environment.
JRE (Java Runtime Environment): Contains the JVM and libraries necessary to run Java applications but does not include development tools.
JDK (Java Development Kit): A full-featured software development kit that includes the JRE, JVM, and development tools like compilers .
4.What is a class and an object in Java?
Answer: A class is a blueprint for creating objects that encapsulates data for the object. An object is an instance of a class that contains state (attributes) and behavior (methods). For example:
java
class Dog {
String breed;
void bark() {
System.out.println(“Woof!”);
}
}
Dog myDog = new Dog(); // myDog is an object of class Dog
.
5.What are the four principles of Object-Oriented Programming (OOP)?
Answer:
Encapsulation: Bundling data with methods that operate on that data.
Inheritance: Mechanism where one class can inherit fields and methods from another class.
Polymorphism: Ability to present the same interface for different underlying forms (data types).
Abstraction: Hiding complex implementation details and showing only essential features .
6.What is exception handling in Java?
Answer: Exception handling in Java is a mechanism to handle runtime errors, allowing the program to continue execution without crashing. It uses try, catch, finally, and throw statements to manage exceptions gracefully .
java
try {
int result = 10 / 0; // This will throw an exception
} catch (ArithmeticException e) {
System.out.println(“Cannot divide by zero.”);
} finally {
System.out.println(“This block always executes.”);
}
.
7.What is the difference between String, StringBuilder, and StringBuffer?
Answer:
String: Immutable; once created, cannot be changed.
StringBuilder: Mutable; not synchronized, hence faster but not thread-safe.
StringBuffer: Mutable; synchronized, making it thread-safe but slower than StringBuilder .
8.Explain method overriding in Java.
Answer: Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method must have the same name, return type, and parameters as in the parent class .
java
class Animal {
void sound() {
System.out.println(“Animal makes sound”);
}
}
class Dog extends Animal {
void sound() {
System.out.println(“Dog barks”);
}
}
9.What are access modifiers in Java?
Answer: Access modifiers determine the visibility of classes, methods, and variables. The main access modifiers are:
public: Accessible from any other class.
protected: Accessible within the same package or subclasses.
default (no modifier): Accessible only within the same package.
private: Accessible only within the same class .
10.What is multithreading in Java?
Answer: Multithreading is a feature that allows concurrent execution of two or more threads within a program. It improves application performance by utilizing CPU resources efficiently. Threads can be created by extending Thread class or implementing Runnable interface .
java
class MyThread extends Thread {
public void run() {
System.out.println(“Thread running”);
}
}
MyThread t1 = new MyThread();
t1.start(); // Starts the thread