1/17
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What is the difference between an interface and an abstract class in Java?
An abstract class can have instance variables, constructors, and concrete methods — a class can extend only one. An interface can only have constants and abstract methods (pre-Java 8) or default/static methods (Java 8+), and a class can implement many. Use abstract class for shared state/behavior; interface for defining a contract.
What is the JVM and how does Java achieve platform independence?
The JVM (Java Virtual Machine) is a runtime that executes Java bytecode. You compile Java source → .class bytecode (platform-neutral), then the JVM on each OS interprets/JIT-compiles that bytecode to native instructions. This is the 'write once, run anywhere' promise.
Explain the Java memory model: heap vs. stack.
The stack holds method frames, local variables, and references — each thread has its own, fast, LIFO. The heap is shared across threads and holds all object instances and arrays; it's managed by the Garbage Collector. Stack memory is allocated/freed automatically with method calls; heap memory is GC'd when objects are no longer reachable.
What are Java generics and why are they useful?
Generics let you write type-safe, reusable code. List<String> guarantees the list holds only Strings, catching type mismatches at compile time instead of runtime ClassCastException. Generics are erased at runtime (type erasure), so no overhead — they're purely a compile-time check.
What is the difference between == and .equals() in Java?
== compares reference identity (same object in memory). .equals() compares logical equality as defined by the class. For String, == may return false for two identical strings in different objects, while .equals() returns true. Always use .equals() for object comparison.
What is multithreading in Java? How do you create a thread?
Multithreading allows concurrent execution. You create a thread by (1) extending Thread and overriding run(), or (2) implementing Runnable and passing it to a Thread, or (3) using ExecutorService / Callable for thread pools. The Runnable/ExecutorService approach is preferred in production.
What does the 'synchronized' keyword do in Java?
synchronized ensures only one thread at a time can execute a block or method on a given object monitor. It prevents race conditions when multiple threads access shared mutable state. It has overhead — modern code often prefers java.util.concurrent utilities like ReentrantLock, AtomicInteger, or ConcurrentHashMap.
What is the difference between ArrayList and LinkedList?
ArrayList uses a dynamic array — O(1) random access, O(n) insert/delete in the middle. LinkedList uses a doubly-linked list — O(n) random access, O(1) insert/delete at known positions. For most use cases ArrayList is faster due to cache locality. Use LinkedList only if you need frequent insertions/deletions at the head.
What is exception handling in Java? Checked vs. unchecked?
Checked exceptions (e.g., IOException) must be declared in the method signature or caught — the compiler enforces this. Unchecked exceptions (extend RuntimeException, e.g., NullPointerException) don't require explicit handling. Best practice: use checked for recoverable conditions, unchecked for programming errors.
What are Java Streams and what problem do they solve?
Introduced in Java 8, Streams provide a declarative, functional-style API for processing collections. Instead of imperative loops, you chain operations: list.stream().filter(...).map(...).collect(...). They support lazy evaluation and can be parallelized with .parallelStream() with no code changes.
What is a Python decorator and give a real-world use case?
A decorator is a function that wraps another function to extend its behavior without modifying it. Syntax: @my_decorator above a function definition. Real use: @login_required in Flask/Django checks auth before running a view, or @staticmethod to define a class method that doesn't receive self.
What is the GIL (Global Interpreter Lock) in Python?
The GIL is a mutex in CPython that allows only one thread to execute Python bytecode at a time, even on multi-core systems. This means CPU-bound multi-threaded Python code won't run in true parallel. Workarounds: use multiprocessing (separate processes, each with its own GIL) or write C extensions. I/O-bound tasks are unaffected because the GIL is released during I/O waits.
What's the difference between a list and a tuple in Python?
Lists are mutable (you can change elements); tuples are immutable. Tuples are slightly faster and can be used as dictionary keys or set elements (since they're hashable). Convention: use tuples for heterogeneous fixed-structure data (a point: (x, y)), lists for homogeneous collections.
Explain list comprehensions vs. generator expressions.
[x*2 for x in range(1000)] builds the entire list in memory. (x*2 for x in range(1000)) (generator) yields values lazily one at a time — much more memory-efficient for large data. Use generators when you just need to iterate, not random access.
What are *args and *kwargs in Python?
*args captures any number of positional arguments as a tuple. **kwargs captures any number of keyword arguments as a dict. Example: def f(*args, **kwargs) lets you call f(1, 2, name='Madelyn'). Widely used in decorators and flexible API wrappers.
What is duck typing in Python?
Python doesn't care about an object's type — only whether it has the methods/attributes you need. 'If it quacks like a duck, it's a duck.' If you call obj.speak(), Python just looks for a speak attribute at runtime. This is the basis of Python's flexibility and is why interfaces aren't enforced by the language (though you can use ABCs or type hints for documentation/static checking).
How does Python manage memory? What is garbage collection?
Python uses reference counting as the primary mechanism — when an object's reference count drops to 0, it's freed immediately. The gc module handles cyclic references (objects pointing to each other) using a generational garbage collector. Memory-heavy applications should be aware of reference cycles and use weakref or explicitly break cycles.
What is the difference between deep copy and shallow copy?
copy.copy() creates a shallow copy — a new object but nested objects are still shared references. copy.deepcopy() recursively copies all nested objects, fully independent. For mutable nested structures (lists of lists, dicts of lists), deep copy is what you usually need to avoid unintended mutations.