Questions For Practice
Questions For Practice
1. Primary Key:
○ A primary key is a column or combination of columns
that uniquely identifies each row in a table
○ Example: Student ID in a students table
○ Cannot be NULL or duplicate
2. DELETE vs TRUNCATE:
○ DELETE removes specific rows and can be rolled back
(recoverable)
○ TRUNCATE removes all rows at once, can't be rolled
back (faster but non-recoverable)
3. WHERE vs HAVING:
○ WHERE filters individual rows before grouping
○ HAVING filters groups after GROUP BY
○ Example: WHERE salary > 5000 (filters rows) vs
HAVING AVG(salary) > 5000 (filters groups)
4. ACID Properties:
○ Atomicity: Transaction completely succeeds or
completely fails
○ Consistency: Database remains in a valid state
○ Isolation: Transactions don't interfere with each other
○ Durability: Completed transactions are permanent
5. DDL vs DML:
○ DDL (Data Definition Language): CREATE, ALTER,
DROP, TRUNCATE
○ DML (Data Manipulation Language): SELECT, INSERT,
UPDATE, DELETE
6. Foreign Key:
○ Links two tables together
○ Example: In Orders table, CustomerID references ID in
Customers table
○ Maintains referential integrity
7. GROUP BY:
○ Groups rows with similar values
○ Used with aggregate functions (COUNT, SUM, AVG)
○ Example: GROUP BY department to find average
salary per department
8. Normalization:
○ Process of organizing data to reduce redundancy
○ Helps maintain data integrity
○ Divides larger tables into smaller, related tables
9. UNION vs UNION ALL:
○ UNION: Combines results and removes duplicates
○ UNION ALL: Combines results and keeps duplicates
(faster)
10. Index:
○ Speeds up data retrieval
○ Like a book's index
○ Trade-off: Faster reads, slower writes
11. CHAR vs VARCHAR:
○ CHAR: Fixed length, padded with spaces
○ VARCHAR: Variable length, saves space
○ Example: CHAR(10) always uses 10 bytes,
VARCHAR(10) uses only needed bytes
12. JOINs:
○ INNER JOIN: Matches only rows with matching values
in both tables
○ LEFT JOIN: All rows from left table, matching from right
○ RIGHT JOIN: All rows from right table, matching from
left
○ FULL JOIN: All rows from both tables
13. NULL:
○ Represents absence of value
○ Different from zero or empty string
○ Can't be compared using = or !=, must use IS NULL
14. DISTINCT:
○ Removes duplicate values from results
○ Example: SELECT DISTINCT city FROM customers
15. View:
○ Virtual table based on SQL statement
○ Doesn't store data physically
○ Can simplify complex queries
16. DELETE vs DROP:
○ DELETE removes rows from table
○ DROP removes entire table structure
17. Transaction:
○ Unit of work performed against database
○ Must be ACID compliant
○ Example: Transfer money between accounts
18. ORDER BY:
○ Sorts results
○ ASC for ascending (default)
○ DESC for descending
19. Data Redundancy:
○ Same data stored in multiple places
○ Wastes storage
○ Can lead to inconsistencies
20. Referential Integrity:
○ Ensures relationships between tables remain valid
○ Prevents orphaned records
○ Enforced by foreign keys
OOP Answers:
class Animal {
void eat() { }
}
class Dog extends Animal {
void bark() { }
}
4. Method Overloading:
○ Same method name, different parameters
○ Happens in same class
void add(int a, int b) { }
void add(int a, int b, int c) { }
5. Method Overriding:
○ Same method in parent and child class
○ Same signature
class Animal {
void makeSound() { }
}
class Dog extends Animal {
void makeSound() { // bark }
}
6. Encapsulation:
○ Wraps data and code together
○ Protects data from outside access
○ Uses private variables with public getters/setters
7. Access Modifiers:
○ public: Accessible everywhere
○ private: Only within same class
○ protected: Within same package and subclasses
8. Polymorphism:
○ Object taking multiple forms
○ Example: Method behaving differently in different
classes
○ Compile-time (overloading) and Runtime (overriding)
9. Compile-time vs Runtime Polymorphism:
○ Compile-time: Method overloading
○ Runtime: Method overriding
10. Constructor:
○ Special method to initialize objects
○ Same name as class
○ Called automatically when object created
11. Abstract Class vs Interface:
○ Abstract class: Can have implementation
○ Interface: Only method declarations
○ Class can implement multiple interfaces but extend only
one abstract class
12. Super Keyword:
○ Refers to parent class
○ Calls parent class methods
○ Access parent class constructor
13. This Keyword:
○ Refers to current instance
○ Differentiates instance variables from parameters
○ Used to call current class methods
14. Multiple Inheritance:
○ Class inheriting from multiple classes
○ Java doesn't support through classes
○ Possible through interfaces
15. Static vs Non-static:
○ Static: Belongs to class
○ Non-static: Belongs to instance
○ Static methods can't access non-static members
directly
16. Already covered in 4 and 5
17. Abstraction vs Encapsulation:
○ Abstraction: Hiding implementation
○ Encapsulation: Wrapping data and methods
○ Example: Car steering (abstraction) vs Car engine
(encapsulation)
18. Getter/Setter:
○ Methods to access private variables
○ Getter returns value
○ Setter sets value
19. Final Keyword:
○ Variable: Can't change value
○ Method: Can't override
○ Class: Can't inherit
20. Default Constructor:
○ Created by compiler if no constructor defined
○ No parameters
○ Initializes instance variables to default values
DSA Answers:
9. Linear vs Non-linear:
○ Linear: Arrays, lists (one after another)
○ Non-linear: Trees, graphs (branching)
10. Bubble Sort Time Complexity:
○ O(n²) - worst and average case
○ O(n) - best case (already sorted)
11. Binary Tree:
○ Each node has at most 2 children
○ Left child < parent < right child
○ Used for efficient searching
12. Stack vs Queue:
○ Stack: LIFO (plates stack)
○ Queue: FIFO (people in line)
○ Different use cases
13. Infix, Prefix, Postfix:
○ Infix: a + b
○ Prefix: +ab
○ Postfix: ab+
14. Static vs Dynamic Arrays:
○ Static: Fixed size
○ Dynamic: Can grow/shrink
○ Example: Array vs ArrayList in Java
15. Doubly Linked List:
○ Nodes have next and previous pointers
○ Can traverse both directions
○ More memory than singly linked list
16. Heap:
○ Complete binary tree
○ Max-heap or min-heap
○ Used in priority queues
17. Array Insertion Time:
○ At end: O(1)
○ At beginning: O(n)
○ At middle: O(n)
18. Circular Queue:
○ Regular queue with connected ends
○ Better memory utilization
○ Used in scheduling
19. Tree vs Graph:
○ Tree: Hierarchical, no cycles
○ Graph: Can have cycles
○ Tree is a special type of graph
20. Bubble Sort Best Case:
○ O(n) when array is already sorted
○ Needs one pass to confirm
○ Can be optimized with flag