java_interview
java_interview
Here are detailed Java Programming Interview Questions and Answers, especially
focused on core Java and OOP concepts — ideal for freshers and entry-level Android developers.
🔹 1. What is Java?
Answer:
Java is a high-level, object-oriented programming language developed by Sun Microsystems. It is
platform-independent due to the Java Virtual Machine (JVM), making it "write once, run
anywhere".
// Overriding
class Animal {
void sound() { System.out.println("Sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
Here are more advanced and in-depth Java programming and OOP interview questions with
detailed answers, perfect for building your understanding as a fresher aiming for Android or Java
development roles:
🔹 16. What is the difference between this and super keyword in Java?
Keyword Usage
this Refers to the current class object.
super Refers to the parent class object or constructor.
class Animal {
String sound = "Animal sound";
}
void printSound() {
System.out.println(this.sound); // Bark
System.out.println(super.sound); // Animal sound
}
}
🔹 17. What is constructor chaining in Java?
Answer:
Calling one constructor from another within the same class using this() or from parent class
using super().
class Person {
Person() {
this("John"); // calls another constructor
}
Person(String name) {
System.out.println("Name is " + name);
}
}
🔹 18. What is the difference between shallow copy and deep copy?
Type Description
Shallow Copy Copies references, not actual objects.
Deep Copy Creates copies of referenced objects too.
// Shallow copy
Employee e1 = new Employee("Aryan");
Employee e2 = e1; // same object reference
🔹 29. What is the difference between interface and abstract class in Java 8 and
above?
From Java 8+, interfaces can have:
• Default methods with body.
• Static methods.
interface MyInterface {
default void show() { System.out.println("Default"); }
}
✅
Would you like:
✅
✅
Java DSA Questions
Android-specific Java questions (Lifecycle, Threads, API handling)?
More OOP code examples in Java?
Let me know — I’ll send the next batch accordingly.
Great, Aryan! Here's a detailed and advanced set of Java interview questions and answers on topics
you just mentioned:
🔸
🔸
== checks reference
.equals() checks value
📱
Applications of Java:
🌐
• Mobile Development (Android)
• Web Apps (Spring Boot)
• ☁️Cloud Computing
🔧
🧠
• Embedded Systems
🏦
• AI/ML tools (with libraries)
• Banking & Enterprise Apps
🛠
Hardware
(e.g., Java Native Interface - JNI).
Java doesn’t access hardware directly like C/C++, but can bridge using JNI.
🧹
Java automatically clears unused memory using the Garbage Collector (GC).
GC checks for objects with no reference and frees memory.
protected void finalize() {
System.out.println("Object is destroyed");
}
🔹 38. What are Wrapper Classes in Java?
They convert primitives to objects.
Primitive Wrapper
int Integer
char Character
double Double
int x = 5;
Integer obj = Integer.valueOf(x);
Great Aryan! Let’s dive deep into these Java and DSA core topics one by one:
➤ Benefits:
• Avoid class name conflicts (e.g., java.util.List vs java.awt.List)
System.out.println("Welcome!");
➤ How it works:
String a = "Aryan";
String b = "Aryan";
📌
System.out.println(a == c); // false (new object in heap)
Java reuses strings in the pool for efficiency. Strings are immutable to make this safe.
➤ Key facts:
• Managed by JVM
• Has Young, Old, and PermGen generations (Garbage Collected)
• String pool is part of heap (specifically in PermGen before Java 8, now in Metaspace)
✅ 4. Threads in Java
Definition:
A thread is a lightweight subprocess, the smallest unit of execution.
➤ Ways to Create a Thread:
1. Extend Thread class
2. Implement Runnable interface
3. Using lambda with Runnable (Java 8)
public class MyThread extends Thread {
System.out.println("Running in thread!");
new Thread(r).start();
➤ Thread States:
• New
• Runnable
• Running
• Waiting/Blocked
• Terminated
Do you want:
• A separate deep dive on Heap vs Stack memory?
• Practical code examples for each data structure?
• Or shall I generate a formatted PDF version of all this?
Let me know how you’d like to continue!
Great choice, Aryan! Let’s break it into two parts:
🔸
GC (Garbage Collection) Not applicable
Code Example:
Handled by JVM
obj.display();
void display() {
🧠
}
🧠
x → Stack
🧠
obj → Reference in Stack, object in Heap
data → Stored in Heap with obj
System.out.println(i);
}
🔹 LinkedList
import java.util.LinkedList;
list.add("A");
list.add("B");
System.out.println(list); // [A, B]
🔹 Stack
import java.util.Stack;
stack.push(1);
stack.push(2);
System.out.println(stack.pop()); // 2
🔹 Queue
import java.util.LinkedList;
import java.util.Queue;
queue.add("A");
queue.add("B");
System.out.println(queue.poll()); // A
🔹 HashMap
import java.util.HashMap;
map.put("Aryan", 1);
System.out.println(map.get("Aryan")); // 1
Node(int val) {
this.val = val;
System.out.println(graph);
Great Aryan! Let’s now cover Sorting and Searching Algorithms with simple explanations and
Java code examples.
✅ 1. Linear Search
🔸 Description:
🔸
Searches an element one by one in an array.
Time Complexity:
• Best: O(1)
🔸
• Worst: O(n)
Code:
int linearSearch(int[] arr, int target) {
}
return -1;
✅
}
🔸
Divides the array in half repeatedly to search.
Time Complexity:
🔸
• O(log n)
Code:
int binarySearch(int[] arr, int target) {
return -1;
✅
}
3. Bubble Sort
🔸 Description:
🔸
Repeatedly swaps adjacent elements if they are in wrong order.
Time Complexity:
🔸
• Worst: O(n²)
Code:
void bubbleSort(int[] arr) {
arr[j + 1] = temp;
}
}
✅
}
4. Selection Sort
🔸 Description:
🔸
Find the minimum element and place it at the beginning.
Time Complexity:
🔸
• O(n²)
Code:
void selectionSort(int[] arr) {
int minIdx = i;
arr[i] = arr[minIdx];
arr[minIdx] = temp;
✅
}
5. Insertion Sort
🔸 Description:
🔸
Takes one element and inserts it into its correct position in the sorted part.
Time Complexity:
🔸
• Worst: O(n²)
Code:
void insertionSort(int[] arr) {
int j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
✅
}
🔸
• O(n log n)
Code:
void mergeSort(int[] arr, int left, int right) {
int i = 0, j = 0, k = l;
✅
}
Code:
void quickSort(int[] arr, int low, int high) {
quickSort(arr, pi + 1, high);
int i = low - 1;
i++;
return i + 1;
Great! Let’s dive into String Manipulations and then cover Global Scope in Java. These are
important concepts for interviews and real-world coding.
🔹
In Java, String is an immutable class, stored in the string pool (inside heap memory).
Declaring Strings:
String s1 = "Hello"; // String pool
sb.append(" World");
🔹
System.out.println(sb); // Output: Hello World
✅
}
✅
}
Summary
Topic Usage
String API Manipulate strings using length, substring, etc.
StringBuilder Use for mutable & efficient string operations
Global Scope Use public static, Singleton, or Constants