Learnitweb

Category: Java interview questions

  • What is Memoization?

    1. Overview Memoization is a technique to store the results of expensive function calls and reuse those results when the same inputs occur again. It avoids redundant calculations, making programs faster and more efficient. Memoization is particularly useful in scenarios involving: 2. How does Memoization work? Memoization works by following these steps: This technique ensures…

  • What is the difference between Map and Object in JavaScript?

    In JavaScript, both Map and Object can store key-value pairs, but they have different features, performance characteristics, and use cases. Here’s a detailed comparison: Aspect Object Map Key Types Keys must be strings or symbols (other types are coerced to strings). Keys can be of any type, including objects, functions, and primitives. Order of Keys…

  • Can we make an array volatile in Java?

    Before answering this question, let us see two important points about volatile variable. Now, let us answer our original question, Can we make an array volatile in Java? Yes, we can make an array volatile in Java but only the reference variable pointing to the array will be volatile not the elements of the array.…

  • map() vs flatMap() in Java

    map() and flatmap() methods are found in Optional class and Stream interfaces in Java 8. We’ll discuss the use of both these methods with respect to Optional and Stream. 1. flapMap() and map() methods in Optional 1.1 Syntax of map() <U> Optional<U> map(Function<? super T, ? extends U> mapper) 1.2 Syntax of flatMap() <U> Optional<U>…

  • Difference between Streams and Collections in Java

    A collection is a group of elements. A collection is an in-memory data structure. Collection supports various operations such as sorting, searching, insertion, manipulation and deletion. In Java, collection framework provides many interfaces like Set, List, Queue and implementation classes like ArrayList, HashSet to represent different type of collections. Stream is basically a sequence of…

  • How to Convert a Map to a List in Java?

    A Map maps keys to values. Map provides following two methods to return keys and values of a map: public Collection<V> values() This method returns a Collection view of the values contained in this map. public Set keySet() This method returns a Set view of the keys contained in this map Output

  • Difference between DOM and SAX parser in Java

    DOM XML parser in Java DOM stands for Document Object Model and represents an XML document in tree format, each element representing tree branches. The XML file will be loaded as a whole and all its contents will be built as an in-memory representation of the tree the document represents. Parsing XML file using DOM…

  • Difference between HashTable and HashMap in Java

    Both HashMap and HashTable map keys to values. HashTable was introduced in JDK 1.0. As of JDK 1.2, this class was retrofitted to implement the Map interface. Following are the differences between HashTable and HashMap: HashMap HashTable HashMap extends java.util.AbstractMap HashTable extends java.util.Dictionary HashMap allows only one null key and any number of null values.…

  • Can a constructor be synchronized in Java?

    Constructors cannot be synchronized. Using the synchronized keyword with a constructor is a syntax error – Illegal modifier for the constructor in type your class name; only public, protected & private are permitted’. Synchronizing constructors doesn’t make sense, because only the thread that creates an object should have access to it while it is being…

  • Difference between Runnable and Callable

    Both Runnable and Callable interface are used to encapsulate task supposed to be executed by another thread. Runnable and Callable both run on a different thread than the calling thread. There are certain things which Runnable can not do like throwing checked exception and returning result after computation. Java team could have changed the signature…