Java Interview Question
Java Interview Question
Q1: What is Java, and what are some of its key features?
Java is a high-level, object-oriented, platform-independent programming language. Key
features include:
Q2: What are the differences between JDK, JRE, and JVM?
Q3: Explain the difference between a compiled and an interpreted language. Is Java
compiled or interpreted?
java
Copy code
public static void main(String[] args) { ... }
java
Copy code
class Example {
static int count = 0;
static void displayCount() {
System.out.println(count);
}
}
java
Copy code
int a = 10;
float b = (float) a; // Explicit casting
3. Control Structures
Q1: Describe the different control structures in Java (if, switch, loops).
Q2: What is the difference between for, while, and do-while loops?
java
Copy code
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other day");
}
If you want detailed explanations or coding examples for specific sections, let me know!
4o
You said:
explain remaning
ChatGPT said:
ChatGPT
Let’s continue with the remaining sections and questions from the list.
java
Copy code
class Employee {
private int salary; // Private variable
java
Copy code
class Parent {
void display() { System.out.println("Parent class method"); }
}
class Child extends Parent {
void show() { System.out.println("Child class method"); }
}
● Compile-time (Method Overloading): Methods with the same name but different
parameters.
● Runtime (Method Overriding): A subclass provides a specific implementation of a
method declared in its superclass.
● Achieved by:
○ Abstract classes (abstract keyword)
○ Interfaces
java
Copy code
import static java.lang.Math.PI;
System.out.println(PI);
● Constructor: Initializes an object. Has no return type and the same name as the
class.
● Method: Defines behavior. Can have a return type.
● Interface: Defines only abstract methods (by default). Allows multiple inheritance.
● Abstract Class: Can have both abstract and concrete methods.
Q3: What are default methods in interfaces, and when are they used?
Default methods allow adding new methods to interfaces without breaking existing
implementations.
Example:
java
Copy code
interface Example {
default void display() { System.out.println("Default Method"); }
}
7. Exception Handling
8. Collections Framework
4o
You said:
remaining explaning
ChatGPT said:
ChatGPT
9. Multithreading
Example:
java
Copy code
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
java
Copy code
synchronized void increment() {
// Critical section
}
Q1: What are lambda expressions, and how are they used in Java 8?
A lambda expression is a concise way to represent a functional interface (interface with a
single abstract method).
Example:
java
Copy code
List<Integer> numbers = Arrays.asList(1, 2, 3);
numbers.forEach(n -> System.out.println(n));
java
Copy code
numbers.forEach(n -> System.out.println(n));
12. Miscellaneous
● String: Immutable.
● StringBuilder: Mutable and not thread-safe.
● StringBuffer: Mutable and thread-safe.
Q2: How do you handle file I/O in Java?
Using classes like File, FileReader, FileWriter, and BufferedReader.
Example:
java
Copy code
File file = new File("example.txt");
FileWriter writer = new FileWriter(file);
writer.write("Hello, world!");
writer.close();
java
Copy code
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/db",
"user", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM table");
Q4: What is the enum type, and how do you use it?
An enum defines a set of constants.
Example:
java
Copy code
enum Day { MONDAY, TUESDAY, WEDNESDAY }
Day today = Day.MONDAY;
java
Copy code
final class MyClass { }
Q6: What is the ternary operator in Java?
It is a shorthand for if-else.
Example:
java
Copy code
int max = (a > b) ? a : b;
bash
Copy code
java -jar filename.jar
java
Copy code
if (obj instanceof String) { System.out.println("It's a String!"); }
Coding Problems