Made with ScholarlyMake flashcards like these from your own notes
Question:What is a dynamically typed language?
Answer:Variables can hold values of different types, with type checking during program execution.
Question:What characterizes a statically typed language like Java?
Answer:Variables can only hold values of the declared type, checked at compile time.
Question:What is the difference between strong and weak typing?
Answer:Strong typing enforces strict type rules for safety at compile time; weak typing is more permissive.
Question:What is a variable an abstraction of?
Answer:A name referring to a location in computer memory.
Question:What are the primitive types in Java and their subtype relationships?
Answer:byte < short < int < long < float < double; char < int; boolean.
Question:What does S <: T mean in subtyping?
Answer:S is a subtype of T, allowing code for S to be used safely on T.
Question:What is widening type conversion?
Answer:Automatic conversion from subtype S to supertype T.
Question:What is narrowing type conversion and its risks?
Answer:Requires explicit casting from supertype to subtype; risks ClassCastException if incompatible.
Question:What is Run Time Type (RTT) versus Compile Time Type (CTT)?
Answer:RTT is the actual type at runtime; CTT is the declared type at compile time.
Question:How does a range-based for loop work in Java?
Answer:Iterates over collections implementing Iterable where U converts to loop variable type T.
Question:What is the Liskov Substitution Principle (LSP)?
Answer:Subtypes S of T must be substitutable for T without altering program properties.
Question:What is abstraction in OOP?
Answer:Hiding implementation details to reveal intentions, enabling simple use and code reuse.
Question:What is the abstraction barrier?
Answer:Separation between implementer (internal details) and client (external interface).
Question:What is encapsulation in OOP?
Answer:Bundling data and methods within a class, using private fields and public methods.
Question:What role do constructors play in encapsulation?
Answer:Initialize private fields during object creation, controlling access to internal state.
Question:What is the 'this' keyword in Java?
Answer:Refers to the current object instance.
Question:What is the default value for uninitialized reference variables?
Answer:null.
Question:What is the 'Tell, Don’t Ask' principle?
Answer:Instruct objects to perform actions rather than querying and manipulating externally.
Question:What are static methods and fields?
Answer:Associated with the class, not instances; declared with 'static' keyword.
Question:What is the main method signature in Java?
Answer:public static void main(String[] args).
Question:What is a 'has-a' relationship in OOP?
Answer:Composition, where one class contains another as a field.
Question:What is an 'is-a' relationship?
Answer:Inheritance using 'extends' for subtyping.
Question:What does every Java class implicitly inherit from?
Answer:The Object class, root of the hierarchy.
Question:How is the superclass constructor called?
Answer:Using 'super(arguments)' in the subclass constructor.
Question:Why avoid overusing inheritance?
Answer:Ensure it preserves subtyping meaning; prefer composition when possible.
Question:What is polymorphism?
Answer:Ability to change code behavior without modifying existing code, via dynamic binding.
Question:What is dynamic binding?
Answer:Method invocation determined at runtime based on object type.
Question:What is method overloading?
Answer:Static polymorphism: same name, different parameters (types/number/order).
Question:What is method overriding?
Answer:Dynamic polymorphism: same signature in subclass as in superclass.
Question:What comprises a method signature?
Answer:Method name, parameter types, number, and order; excludes return type and exceptions.
Question:What return type rule applies to overriding methods?
Answer:Can be a subtype of the overridden method's return type, not supertype.
Question:Which methods cannot be overridden?
Answer:private, static, final methods.
Question:How is method invocation resolved?
Answer:Compile time: most specific descriptor; runtime: based on object's RTT.
Question:What are abstract classes?
Answer:Cannot be instantiated; may contain abstract methods requiring implementation.
Question:What is an interface?
Answer:A type defining capabilities (methods) that classes can implement; often ends with '-able'.
Question:How do concrete classes implement interfaces?
Answer:Override all abstract methods with the same signatures.
Question:What is the stack in Java's memory model?
Answer:Stores variables (primitives and references) in frames; deallocated on method return.
Question:What is the heap?
Answer:Stores all objects; persists until garbage collected when unreferenced.
Question:What is call by value for primitives in Java?
Answer:Copies the value to the method's stack frame.
Question:What is call by reference for objects?
Answer:Passes the reference copy; modifications affect the original object.
Question:What are wrapper classes?
Answer:Classes encapsulating primitives, like Integer for int, enabling object treatment.
Question:What is autoboxing?
Answer:Automatic conversion from primitive to wrapper object.
Question:Why are wrapper classes inefficient?
Answer:Immutable; updates create new objects, incurring allocation and GC costs.
Question:What does 'private' access modifier mean?
Answer:Accessible only within the same class.
Question:What does 'protected' mean?
Answer:Accessible within package or via subclasses outside package.
Question:What is a final variable?
Answer:Can be assigned only once, becoming immutable after initialization.
Question:What is a final class?
Answer:Cannot be extended or inherited from.
Question:What is a final method?
Answer:Cannot be overridden in subclasses.
Question:When is casting safe?
Answer:When you can prove the runtime type matches the target type.
Question:What is type variance?
Answer:How subtype relationships in components affect complex types like arrays.
Question:What is covariant typing for arrays in Java?
Answer:If S <: T, then S[] <: T[].
Question:What is heap pollution?
Answer:Runtime error from storing incompatible types in a covariant array.
Question:What happens in a try-catch-finally block?
Answer:Try: executes code; catch: handles exceptions; finally: always runs.
Question:How are exceptions propagated?
Answer:Passed up the call stack until caught; code after exception skips.
Question:How to create a custom exception?
Answer:Extend Exception or a subclass, like IllegalArgumentException.
Question:What rule applies to overriding methods throwing checked exceptions?
Answer:Overriding method can throw same or more specific exceptions (subtype).
Question:What does 'throw' do in a method?
Answer:Immediately returns, propagating the exception unless finally runs first.
Question:What are unchecked exceptions?
Answer:Runtime errors from programmer mistakes, like ClassCastException; no handling required.
Question:What are checked exceptions?
Answer:External errors like FileNotFoundException; must be handled or declared to compile.
Question:What do generics provide?
Answer:Type-safe classes/methods without using Object, catching errors at compile time.
Question:Why are generics invariant in Java?
Answer:Subtype relationships don't propagate; List not subtype of List .
Question:Where is the type parameter declared in a generic method?
Answer:Before the return type, like boolean contains(T[] arr, T obj).
Question:What is type erasure?
Answer:Compile-time replacement of type parameters with Object or bounds.
Question:What are reifiable types?
Answer:Types with full information available at runtime, unlike generics due to erasure.
Question:What is @SuppressWarnings used for?
Answer:Suppresses specific compiler warnings, like 'unchecked' for raw types.
Question:What are raw types?
Answer:Generic types without type arguments, like List instead of List .
Question:What is an upper-bounded wildcard?
Answer:? extends T for covariant use, reading from collections of subtypes.
Question:What is a lower-bounded wildcard?
Answer:? super T for contravariant use, writing to collections of supertypes.
Question:What is the PECS principle?
Answer:Producer Extends, Consumer Super: use extends for reading, super for writing.
Question:What is type inference in generics?
Answer:Compiler deduces type parameters from context for safety and convenience.
Question:What is the diamond operator <>?
Answer:Allows omitting type arguments in generic instantiation, inferred from context.
Question:What makes a class immutable?
Answer:No visible state changes outside; final class with private final defensive fields.
Question:What is copy-on-write semantics?
Answer:Create new copies only when mutation is needed, sharing until then.
Question:What are advantages of immutability?
Answer:Easier reasoning, safe sharing, space savings, thread-safety.
Question:What are varargs?
Answer:Variable-length method parameters, passed as an array.
Question:What is @SafeVarargs?
Answer:Annotation for varargs methods with generics to suppress warnings.
Question:What is a static nested class?
Answer:Class defined inside another, associated with the outer class; accesses static members.
Question:What is an inner class?
Answer:Non-static nested class, associated with an outer instance; accesses all members.
Question:What are local classes?
Answer:Classes defined inside methods, accessing effectively final locals and enclosing members.
Question:What is variable capture?
Answer:Local/anonymous classes copy effectively final variables to persist beyond method scope.
Question:What are anonymous classes?
Answer:Unnamed classes instantiated inline, implementing interfaces or extending classes.
Question:What is a function in functional programming?
Answer:Deterministic mapping from domain to codomain, treatable as first-class values.
Question:What is referential transparency?
Answer:Expressions can be replaced with their values without changing program behavior.
Question:What is a pure function?
Answer:Deterministic, no side effects, total (always returns), maps inputs to outputs.
Question:What is a functional interface?
Answer:Interface with exactly one abstract method, annotated @FunctionalInterface.
Question:How do lambda expressions work?
Answer:Concise implementations of functional interfaces, inferring types from context.
Question:What is method reference notation?
Answer:Double colon :: for referencing existing methods as lambdas.
Question:What is currying?
Answer:Transforming multi-argument functions into chained single-argument functions.
Question:What is a closure?
Answer:Function with its enclosing environment, capturing effectively final variables.
Question:What is lazy evaluation?
Answer:Delaying computation until the value is needed, using lambdas.
Question:What is memoization?
Answer:Caching results of expensive functions to avoid recomputation.
Question:What is a monad?
Answer:Structure with of() and flatMap(), obeying identity and associativity laws; carries value and context.
Question:What is the left identity law for monads?
Answer:of(x).flatMap(f) equals f(x).
Question:What is the right identity law?
Answer:monad.flatMap(x -> of(x)) equals monad.
Question:What is the associative law for monads?
Answer:Associates flatMap applications regardless of grouping.
Question:What is a functor?
Answer:Structure with of() and map(), preserving identity and composition; no side info.
Question:What is an InfiniteList?
Answer:Lazy list with potentially infinite elements, using producers for delayed computation.
Question:What are stream pipelines?
Answer:Source -> intermediate ops -> terminal op; declarative and consumable once.
Question:What is the difference between concurrency and parallelism?
Answer:Concurrency: interleaving tasks; parallelism: simultaneous execution on multiple cores.
Question:When can streams be parallelized?
Answer:Stateless, non-interfering operations without side effects; use collect() for results.
Question:What is associativity in parallel streams?
Answer:Accumulator combines within substreams; combiner merges across; must be associative and compatible.
Question:What preserves order in streams?
Answer:distinct(), sorted(); use .unordered() for efficiency in parallels.
Question:What is a thread in Java?
Answer:A single flow of execution; created via Thread or Runnable.
Question:How do you start a thread?
Answer:Call start() on Thread instance; run() is for direct execution.
Question:What does Thread.sleep() do?
Answer:Pauses current thread for specified milliseconds.
Question:What is CompletableFuture?
Answer:Monad for asynchronous computations, representing a future value (promise).
Question:How to create a CompletableFuture?
Answer:completedFuture(value), runAsync(Runnable), supplyAsync(Supplier ).
Question:What is thenApply() in CompletableFuture?
Answer:Maps the result like functor's map.
Question:What is thenCompose()?
Answer:Flattens nested futures, like monad's flatMap.
Question:How to get results from CompletableFuture?
Answer:get() blocks with exceptions; join() blocks without checked exceptions.
Question:What is a thread pool?
Answer:Reusable threads executing queued tasks to avoid creation overhead.
Question:What is the fork-join model?
Answer:Divide tasks recursively (fork), solve base cases, combine results (join).
Question:What is ForkJoinPool?
Answer:Pool optimized for fork-join, with work-stealing for load balancing.
Question:How does fork() work in ForkJoinPool?
Answer:Adds task to executing thread's queue head for immediate processing.
Question:How does join() behave?
Answer:Computes if not started, returns result if done, waits or steals if running.