Logo/

Java Core Concepts Flashcards

Log in

Made with ScholarlyMake flashcards like these from your own notes

Java Core Concepts Flashcards

Questions and answers

115 cards
  1. Question:What is a dynamically typed language?

    Answer:Variables can hold values of different types, with type checking during program execution.

  2. Question:What characterizes a statically typed language like Java?

    Answer:Variables can only hold values of the declared type, checked at compile time.

  3. 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.

  4. Question:What is a variable an abstraction of?

    Answer:A name referring to a location in computer memory.

  5. Question:What are the primitive types in Java and their subtype relationships?

    Answer:byte < short < int < long < float < double; char < int; boolean.

  6. 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.

  7. Question:What is widening type conversion?

    Answer:Automatic conversion from subtype S to supertype T.

  8. Question:What is narrowing type conversion and its risks?

    Answer:Requires explicit casting from supertype to subtype; risks ClassCastException if incompatible.

  9. 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.

  10. 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.

  11. Question:What is the Liskov Substitution Principle (LSP)?

    Answer:Subtypes S of T must be substitutable for T without altering program properties.

  12. Question:What is abstraction in OOP?

    Answer:Hiding implementation details to reveal intentions, enabling simple use and code reuse.

  13. Question:What is the abstraction barrier?

    Answer:Separation between implementer (internal details) and client (external interface).

  14. Question:What is encapsulation in OOP?

    Answer:Bundling data and methods within a class, using private fields and public methods.

  15. Question:What role do constructors play in encapsulation?

    Answer:Initialize private fields during object creation, controlling access to internal state.

  16. Question:What is the 'this' keyword in Java?

    Answer:Refers to the current object instance.

  17. Question:What is the default value for uninitialized reference variables?

    Answer:null.

  18. Question:What is the 'Tell, Don’t Ask' principle?

    Answer:Instruct objects to perform actions rather than querying and manipulating externally.

  19. Question:What are static methods and fields?

    Answer:Associated with the class, not instances; declared with 'static' keyword.

  20. Question:What is the main method signature in Java?

    Answer:public static void main(String[] args).

  21. Question:What is a 'has-a' relationship in OOP?

    Answer:Composition, where one class contains another as a field.

  22. Question:What is an 'is-a' relationship?

    Answer:Inheritance using 'extends' for subtyping.

  23. Question:What does every Java class implicitly inherit from?

    Answer:The Object class, root of the hierarchy.

  24. Question:How is the superclass constructor called?

    Answer:Using 'super(arguments)' in the subclass constructor.

  25. Question:Why avoid overusing inheritance?

    Answer:Ensure it preserves subtyping meaning; prefer composition when possible.

  26. Question:What is polymorphism?

    Answer:Ability to change code behavior without modifying existing code, via dynamic binding.

  27. Question:What is dynamic binding?

    Answer:Method invocation determined at runtime based on object type.

  28. Question:What is method overloading?

    Answer:Static polymorphism: same name, different parameters (types/number/order).

  29. Question:What is method overriding?

    Answer:Dynamic polymorphism: same signature in subclass as in superclass.

  30. Question:What comprises a method signature?

    Answer:Method name, parameter types, number, and order; excludes return type and exceptions.

  31. Question:What return type rule applies to overriding methods?

    Answer:Can be a subtype of the overridden method's return type, not supertype.

  32. Question:Which methods cannot be overridden?

    Answer:private, static, final methods.

  33. Question:How is method invocation resolved?

    Answer:Compile time: most specific descriptor; runtime: based on object's RTT.

  34. Question:What are abstract classes?

    Answer:Cannot be instantiated; may contain abstract methods requiring implementation.

  35. Question:What is an interface?

    Answer:A type defining capabilities (methods) that classes can implement; often ends with '-able'.

  36. Question:How do concrete classes implement interfaces?

    Answer:Override all abstract methods with the same signatures.

  37. Question:What is the stack in Java's memory model?

    Answer:Stores variables (primitives and references) in frames; deallocated on method return.

  38. Question:What is the heap?

    Answer:Stores all objects; persists until garbage collected when unreferenced.

  39. Question:What is call by value for primitives in Java?

    Answer:Copies the value to the method's stack frame.

  40. Question:What is call by reference for objects?

    Answer:Passes the reference copy; modifications affect the original object.

  41. Question:What are wrapper classes?

    Answer:Classes encapsulating primitives, like Integer for int, enabling object treatment.

  42. Question:What is autoboxing?

    Answer:Automatic conversion from primitive to wrapper object.

  43. Question:Why are wrapper classes inefficient?

    Answer:Immutable; updates create new objects, incurring allocation and GC costs.

  44. Question:What does 'private' access modifier mean?

    Answer:Accessible only within the same class.

  45. Question:What does 'protected' mean?

    Answer:Accessible within package or via subclasses outside package.

  46. Question:What is a final variable?

    Answer:Can be assigned only once, becoming immutable after initialization.

  47. Question:What is a final class?

    Answer:Cannot be extended or inherited from.

  48. Question:What is a final method?

    Answer:Cannot be overridden in subclasses.

  49. Question:When is casting safe?

    Answer:When you can prove the runtime type matches the target type.

  50. Question:What is type variance?

    Answer:How subtype relationships in components affect complex types like arrays.

  51. Question:What is covariant typing for arrays in Java?

    Answer:If S <: T, then S[] <: T[].

  52. Question:What is heap pollution?

    Answer:Runtime error from storing incompatible types in a covariant array.

  53. Question:What happens in a try-catch-finally block?

    Answer:Try: executes code; catch: handles exceptions; finally: always runs.

  54. Question:How are exceptions propagated?

    Answer:Passed up the call stack until caught; code after exception skips.

  55. Question:How to create a custom exception?

    Answer:Extend Exception or a subclass, like IllegalArgumentException.

  56. Question:What rule applies to overriding methods throwing checked exceptions?

    Answer:Overriding method can throw same or more specific exceptions (subtype).

  57. Question:What does 'throw' do in a method?

    Answer:Immediately returns, propagating the exception unless finally runs first.

  58. Question:What are unchecked exceptions?

    Answer:Runtime errors from programmer mistakes, like ClassCastException; no handling required.

  59. Question:What are checked exceptions?

    Answer:External errors like FileNotFoundException; must be handled or declared to compile.

  60. Question:What do generics provide?

    Answer:Type-safe classes/methods without using Object, catching errors at compile time.

  61. Question:Why are generics invariant in Java?

    Answer:Subtype relationships don't propagate; List not subtype of List .

  62. Question:Where is the type parameter declared in a generic method?

    Answer:Before the return type, like boolean contains(T[] arr, T obj).

  63. Question:What is type erasure?

    Answer:Compile-time replacement of type parameters with Object or bounds.

  64. Question:What are reifiable types?

    Answer:Types with full information available at runtime, unlike generics due to erasure.

  65. Question:What is @SuppressWarnings used for?

    Answer:Suppresses specific compiler warnings, like 'unchecked' for raw types.

  66. Question:What are raw types?

    Answer:Generic types without type arguments, like List instead of List .

  67. Question:What is an upper-bounded wildcard?

    Answer:? extends T for covariant use, reading from collections of subtypes.

  68. Question:What is a lower-bounded wildcard?

    Answer:? super T for contravariant use, writing to collections of supertypes.

  69. Question:What is the PECS principle?

    Answer:Producer Extends, Consumer Super: use extends for reading, super for writing.

  70. Question:What is type inference in generics?

    Answer:Compiler deduces type parameters from context for safety and convenience.

  71. Question:What is the diamond operator <>?

    Answer:Allows omitting type arguments in generic instantiation, inferred from context.

  72. Question:What makes a class immutable?

    Answer:No visible state changes outside; final class with private final defensive fields.

  73. Question:What is copy-on-write semantics?

    Answer:Create new copies only when mutation is needed, sharing until then.

  74. Question:What are advantages of immutability?

    Answer:Easier reasoning, safe sharing, space savings, thread-safety.

  75. Question:What are varargs?

    Answer:Variable-length method parameters, passed as an array.

  76. Question:What is @SafeVarargs?

    Answer:Annotation for varargs methods with generics to suppress warnings.

  77. Question:What is a static nested class?

    Answer:Class defined inside another, associated with the outer class; accesses static members.

  78. Question:What is an inner class?

    Answer:Non-static nested class, associated with an outer instance; accesses all members.

  79. Question:What are local classes?

    Answer:Classes defined inside methods, accessing effectively final locals and enclosing members.

  80. Question:What is variable capture?

    Answer:Local/anonymous classes copy effectively final variables to persist beyond method scope.

  81. Question:What are anonymous classes?

    Answer:Unnamed classes instantiated inline, implementing interfaces or extending classes.

  82. Question:What is a function in functional programming?

    Answer:Deterministic mapping from domain to codomain, treatable as first-class values.

  83. Question:What is referential transparency?

    Answer:Expressions can be replaced with their values without changing program behavior.

  84. Question:What is a pure function?

    Answer:Deterministic, no side effects, total (always returns), maps inputs to outputs.

  85. Question:What is a functional interface?

    Answer:Interface with exactly one abstract method, annotated @FunctionalInterface.

  86. Question:How do lambda expressions work?

    Answer:Concise implementations of functional interfaces, inferring types from context.

  87. Question:What is method reference notation?

    Answer:Double colon :: for referencing existing methods as lambdas.

  88. Question:What is currying?

    Answer:Transforming multi-argument functions into chained single-argument functions.

  89. Question:What is a closure?

    Answer:Function with its enclosing environment, capturing effectively final variables.

  90. Question:What is lazy evaluation?

    Answer:Delaying computation until the value is needed, using lambdas.

  91. Question:What is memoization?

    Answer:Caching results of expensive functions to avoid recomputation.

  92. Question:What is a monad?

    Answer:Structure with of() and flatMap(), obeying identity and associativity laws; carries value and context.

  93. Question:What is the left identity law for monads?

    Answer:of(x).flatMap(f) equals f(x).

  94. Question:What is the right identity law?

    Answer:monad.flatMap(x -> of(x)) equals monad.

  95. Question:What is the associative law for monads?

    Answer:Associates flatMap applications regardless of grouping.

  96. Question:What is a functor?

    Answer:Structure with of() and map(), preserving identity and composition; no side info.

  97. Question:What is an InfiniteList?

    Answer:Lazy list with potentially infinite elements, using producers for delayed computation.

  98. Question:What are stream pipelines?

    Answer:Source -> intermediate ops -> terminal op; declarative and consumable once.

  99. Question:What is the difference between concurrency and parallelism?

    Answer:Concurrency: interleaving tasks; parallelism: simultaneous execution on multiple cores.

  100. Question:When can streams be parallelized?

    Answer:Stateless, non-interfering operations without side effects; use collect() for results.

  101. Question:What is associativity in parallel streams?

    Answer:Accumulator combines within substreams; combiner merges across; must be associative and compatible.

  102. Question:What preserves order in streams?

    Answer:distinct(), sorted(); use .unordered() for efficiency in parallels.

  103. Question:What is a thread in Java?

    Answer:A single flow of execution; created via Thread or Runnable.

  104. Question:How do you start a thread?

    Answer:Call start() on Thread instance; run() is for direct execution.

  105. Question:What does Thread.sleep() do?

    Answer:Pauses current thread for specified milliseconds.

  106. Question:What is CompletableFuture?

    Answer:Monad for asynchronous computations, representing a future value (promise).

  107. Question:How to create a CompletableFuture?

    Answer:completedFuture(value), runAsync(Runnable), supplyAsync(Supplier ).

  108. Question:What is thenApply() in CompletableFuture?

    Answer:Maps the result like functor's map.

  109. Question:What is thenCompose()?

    Answer:Flattens nested futures, like monad's flatMap.

  110. Question:How to get results from CompletableFuture?

    Answer:get() blocks with exceptions; join() blocks without checked exceptions.

  111. Question:What is a thread pool?

    Answer:Reusable threads executing queued tasks to avoid creation overhead.

  112. Question:What is the fork-join model?

    Answer:Divide tasks recursively (fork), solve base cases, combine results (join).

  113. Question:What is ForkJoinPool?

    Answer:Pool optimized for fork-join, with work-stealing for load balancing.

  114. Question:How does fork() work in ForkJoinPool?

    Answer:Adds task to executing thread's queue head for immediate processing.

  115. Question:How does join() behave?

    Answer:Computes if not started, returns result if done, waits or steals if running.