Author: Editorial Team
-
How to avoid ConcurrentModificationException while iterating a collection?
ConcurrentModificationException is thrown if the collection is changed while a thread is traversing over it using iterator. ConcurrentModificationException can occur both in single threaded as well as multi-threaded programs. For example, following program will throw ConcurrentModificationException because we are trying to remove element from the list while iterating: Output To avoid ConcurrentModificationException Following example shows…
-
Java Program to find second largest number in Array
Approach 1 Sort the array in descending order and then return the second element from the array. The time complexity of this solution is O(nlogn). This solution has problem if first two numbers in sorted array are equal. Approach 2 Second and a better solution is to traverse the array twice. In first traversal find…
-
Java program to reverse a string without String class’s reverse() function.
We’ll use the following approach in this example: Output
-
How to clone an object in JavaScript?
Here we are considering only shallow copy of objects. We are not considering deep cloning of objects. Following are the common ways of cloning object in JavaScript: 1. Object.assign() method The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It…
-
Data types in JavaScript
The latest ECMAScript standard defines eight data types: Seven primitive data types: Boolean: Boolean represents a logical entity and can have two values: true, and false. Null: The Null type has exactly one value: null. Undefined: A variable that has not been assigned a value has the value undefined. Number: The Number type is a…
-
What is the difference between == and === operator?
JavaScript has both strict and type–converting comparisons. The equality operator converts the operands if they are not of the same type, then applies strict comparison. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory. The identity operator returns true if the operands…
-
How can you access the current thread in Java?
Sometimes we need to know about the currently executing thread. For example, for debugging purposes we want information about current Thread. Current thread here means the currently executing thread object in Java. currentThread() method returns a reference to the currently executing thread object. In the following example, currently executing thread is ‘main’ thread. We can get the…
-
What are important methods of Java Exception Class?
Following are some important methods of Exception class: If getLocalizedMessage returns null, then just the class name is returned.
-
What is the default capacity of collection framework classes in Java?
The answer to this questions is – it depends on the Java version. For example, in Java 8 default capacity of ArrayList is 0 until we add at least one object into the ArrayList object. But to answer this question as of Java 7, we mention following default capacity of collection framework classes in Java:
-
What is initial capacity, load factor and rehashing of a HashMap?
As per Java documentation: An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how…
