Java Technical Interview Cheat Sheet
1. What is Java?
Java is a high-level, object-oriented programming language. It is platform-
independent due to the
JVM (Java Virtual Machine).
2. Difference between JDK, JRE, JVM
- JDK: Java Development Kit for development.
- JRE: Java Runtime Environment to run Java apps.
- JVM: Java Virtual Machine executes bytecode.
3. Features of Java
- Object-Oriented
- Platform Independent
- Secure
- Robust
- Multithreaded
- High Performance
4. Class vs Object
- Class: Blueprint for objects.
- Object: Instance of a class.
5. Overloading vs Overriding
- Overloading: Same method name, different parameters.
- Overriding: Redefining superclass method in subclass.
6. Final Keyword
- final variable: Value can't be changed.
- final method: Can't be overridden.
- final class: Can't be inherited
7. Inheritance
Allows child class to inherit methods and fields from a parent class.
Example:
class Dog extends Animal
8. Interface
Interface in Java is used to achieve abstraction and multiple
inheritance.
9. Exception Handling
Use try, catch, finally blocks to handle exceptions.
Example: try { int a = 10/0; } catch (Exception e) {}
10. ArrayList vs LinkedList
- ArrayList: Fast random access, slow inserts/deletes.
- LinkedList: Fast inserts/deletes, slow random access.
Code Example: Java Practical Coding Questions & Answers
---
✅ 1. Check Prime Number
public class PrimeCheck {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
System.out.println(isPrime ? "Prime" : "Not Prime");
}
---
✅ 2. Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "Kamarta";
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
System.out.println(reversed);
---
✅ 3. Factorial using Recursion
public class Factorial {
static int fact(int n) {
if (n == 0) return 1;
return n * fact(n - 1);
}
public static void main(String[] args) {
System.out.println("Factorial: " + fact(5));
---
✅ 4. Palindrome String Check
public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
if (str.equals(rev)) {
System.out.println("Palindrome");
} else {
System.out.println("Not Palindrome");
}
---
✅ 5. Fibonacci Series (n terms)
public class FibonacciSeries {
public static void main(String[] args) {
int n = 10, a = 0, b = 1;
for (int i = 1; i <= n; i++) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
Java Technical Interview Cheat Sheet
1. OOP Concepts in Java
1.1 Encapsulation
Wrapping data (variables) and code (methods) together into a single unit
(class).
class Student {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
1.2 Inheritance
Acquiring properties and behaviors of a parent class.
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
1.3 Polymorphism
Same method name behaving differently based on object.
class Shape {
void draw() {
System.out.println("Drawing shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing circle");
}
}
class Test {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}
1.4 Abstraction
Hiding implementation details and showing only functionality.
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car started");
}
}
2. Java Collections
2.1 ArrayList Example
Dynamic array to store elements.
import java.util.ArrayList;
class TestList {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
for (String s : list) {
System.out.println(s);
}
}
}
2.2 HashMap Example
Stores key-value pairs.
import java.util.HashMap;
class TestMap {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "One");
map.put(2, "Two");
System.out.println(map.get(1));
}
}
3. Real-World Java Use Cases
3.1 Login System
Simple login check with username and password.
import java.util.Scanner;
class LoginSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String user = "admin", pass = "1234";
System.out.print("Username: ");
String u = sc.next();
System.out.print("Password: ");
String p = sc.next();
if (u.equals(user) && p.equals(pass)) {
System.out.println("Login successful!");
} else {
System.out.println("Invalid credentials!");
}
}
}
3.2 Bank Account Operations
Simple bank operations using class and object.
class BankAccount {
int balance = 1000;
void deposit(int amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
void withdraw(int amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Insufficient balance");
}
}
void checkBalance() {
System.out.println("Balance: " + balance);
}
}