Keyword

Result: 252 questions

What are the Data Types supported by Java ? What is Autoboxing and Unboxing ?

Answer:

The eight primitive data types supported by the Java programming language are:

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char

Autoboxing is the automatic conversion made by the Java compiler between the primitive types and their corresponding object wrapper classes. For example, the compiler converts an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this operation is called unboxing.

View

What is a Constructor, Constructor Overloading in Java and Copy-Constructor ? 

Answer:

A constructor gets invoked when a new object is created. Every class has a constructor. In case the programmer does not provide a constructor for a class, the Java compiler (Javac) creates a default constructor for that class. The constructor overloading is similar to method overloading in Java. Different constructors can be created for a single class. Each constructor must have its own unique parameter list. Finally, Java does support copy constructors like C++, but the difference lies in the fact that Java doesn’t create a default copy constructor if you don’t write your own.

View

What is the difference between an Interface and an Abstract class ?

Answer:

Java provides and supports the creation both of abstract classes and interfaces. Both implementations share some common characteristics, but they differ in the following features:

  • All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods.
  • A class may implement a number of Interfaces, but can extend only one abstract class.
  • In order for a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract.
  • Abstract classes can implement interfaces without even providing the implementation of interface methods.
  • Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
  • Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public.
  • An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be invoked if it contains a main method.

Also check out the Abstract class and Interface differences for JDK 8.

View

What is the difference between processes and threads ?

Answer:

A process is an execution of a program, while a Thread is a single execution sequence within a process. A process can contain multiple threads. A Thread is sometimes called a lightweight process.

View

Explain different ways of creating a thread. Which one would you prefer and why ?

Answer:

There are three ways that can be used in order for a Thread to be created:

  • A class may extend the Thread class.
  • A class may implement the Runnable interface.
  • An application can use the Executor framework, in order to create a thread pool.

The Runnable interface is preferred, as it does not require an object to inherit the Thread class. In case your application design requires multiple inheritance, only interfaces can help you. Also, the thread pool is very efficient and can be implemented and used very easily.

View

Explain the available thread states in a high-level.

Answer:

During its execution, a thread can reside in one of the following states:

  • NEW: The thread becomes ready to run, but does not necessarily start running immediately.
  • RUNNABLE: The Java Virtual Machine (JVM) is actively executing the thread’s code.
  • BLOCKED: The thread is in a blocked state while waiting for a monitor lock.
  • WAITING: The thread waits for another thread to perform a particular action.
  • TIMED_WAITING: The thread waits for another thread to perform a particular action up to a specified waiting time.
  • TERMINATED: The thread has finished its execution.
View

What’s a deadlock ?

Answer:

A condition that occurs when two processes are waiting for each other to complete, before proceeding. The result is that both processes wait endlessly.

View

What are the basic interfaces of Java Collections Framework ?

Answer:

Java Collections Framework provides a well designed set of interfaces and classes that support operations on a collections of objects. The most basic interfaces that reside in the Java Collections Framework are:

  • Collection, which represents a group of objects known as its elements.
  • Set, which is a collection that cannot contain duplicate elements.
  • List, which is an ordered collection and can contain duplicate elements.
  • Map, which is an object that maps keys to values and cannot contain duplicate keys.
View

Why Collection doesn’t extend Cloneable and Serializable interfaces ?

Answer:

The Collection interface specifies groups of objects known as elements. Each concrete implementation of a Collection can choose its own way of how to maintain and order its elements. Some collections allow duplicate keys, while some other collections don’t. The semantics and the implications of either cloning or serialization come into play when dealing with actual implementations. Thus, the concrete implementations of collections should decide how they can be cloned or serialized.

View

What is an Iterator ?

Answer:

The Iterator interface provides a number of methods that are able to iterate over any Collection. Each Java Collection contains the iteratormethod that returns an Iterator instance. Iterators are capable of removing elements from the underlying collection during the iteration.

View

What differences exist between Iterator and ListIterator ?

Answer:

The differences of these elements are listed below:

  • An Iterator can be used to traverse the Set and Listcollections, while the ListIterator can be used to iterate only over Lists.
  • The Iterator can traverse a collection only in forward direction, while the ListIterator can traverse a List in both directions.
  • The ListIterator implements the Iterator interface and contains extra functionality, such as adding an element, replacing an element, getting the index position for previous and next elements, etc.
View

What is difference between fail-fast and fail-safe ?

Answer:

The Iterator's fail-safe property works with the clone of the underlying collection and thus, it is not affected by any modification in the collection. All the collection classes in java.util package are fail-fast, while the collection classes in java.util.concurrent are fail-safe. Fail-fast iterators throw a ConcurrentModificationException, while fail-safe iterator never throws such an exception.

View

How HashMap works in Java ?

Answer:

HashMap in Java stores key-value pairs. The HashMap requires a hash function and uses hashCode and equals methods, in order to put and retrieve elements to and from the collection respectively. When the put method is invoked, the HashMap calculates the hash value of the key and stores the pair in the appropriate index inside the collection. If the key exists, its value is updated with the new value. Some important characteristics of a HashMap are its capacity, its load factor and the threshold resizing.

View

What is the importance of hashCode() and equals() methods ?

Answer:

In Java, a HashMap uses the hashCodeand equals methods to determine the index of the key-value pair and to detect duplicates. More specifically, the hashCodemethod is used in order to determine where the specified key will be stored. Since different keys may produce the same hash value, the equals method is used, in order to determine whether the specified key actually exists in the collection or not. Therefore, the implementation of both methods is crucial to the accuracy and efficiency of the HashMap.

View

What differences exist between HashMap and Hashtable ?

Answer:

Both the HashMap and Hashtable classes implement the Map interface and thus, have very similar characteristics. However, they differ in the following features:

  • HashMap allows the existence of null keys and values, while a Hashtable doesn’t allow neither null keys, nor null values.
  • Hashtable is synchronized, while a HashMap is not. Thus, HashMap is preferred in single-threaded environments, while a Hashtable is suitable for multi-threaded environments.
  • HashMap provides its set of keys and a Java application can iterate over them. Thus, a HashMap is fail-fast. On the other hand, a Hashtable provides an Enumeration of its keys.
  • The Hashtable class is considered to be a legacy class.
View

What is difference between Array and ArrayList ? When will you use Array over ArrayList ?

Answer:

The Arrayand ArrayList classes differ on the following features:

  • Arrays can contain primitive or objects, while an ArrayList can contain only objects.
  • Arrays have fixed size, while an ArrayList is dynamic.
  • An ArrayListprovides more methods and features, such as addAllremoveAlliterator, etc.
  • For a list of primitive data types, the collections use autoboxing to reduce the coding effort. However, this approach makes them slower when working on fixed size primitive data types.
View

What is difference between ArrayList and LinkedList ?

Answer:

Both the ArrayList and LinkedList classes implement the List interface, but they differ on the following features:

  • An ArrayList is an index based data structure backed by an Array. It provides random access to its elements with a performance equal to O(1). On the other hand, a LinkedList stores its data as list of elements and every element is linked to its previous and next element. In this case, the search operation for an element has execution time equal to O(n).
  • The Insertion, addition and removal operations of an element are faster in a LinkedList compared to an ArrayList, because there is no need of resizing an array or updating the index when an element is added in some arbitrary position inside the collection.
  • LinkedList consumes more memory than an ArrayList, because every node in a LinkedList stores two references, one for its previous element and one for its next element.

Check also our article ArrayList vs. LinkedList.

View

What is Comparable and Comparator interface ? List their differences.

Answer:

Java provides the Comparableinterface, which contains only one method, called compareTo. This method compares two objects, in order to impose an order between them. Specifically, it returns a negative integer, zero, or a positive integer to indicate that the input object is less than, equal or greater than the existing object. Java provides the Comparator interface, which contains two methods, called compare and equals. The first method compares its two input arguments and imposes an order between them. It returns a negative integer, zero, or a positive integer to indicate that the first argument is less than, equal to, or greater than the second. The second method requires an object as a parameter and aims to decide whether the input object is equal to the comparator. The method returns true, only if the specified object is also a comparator and it imposes the same ordering as the comparator.

View

What is Java Priority Queue ?

Answer:

The PriorityQueue is an unbounded queue, based on a priority heap and its elements are ordered in their natural order. At the time of its creation, we can provide a Comparator that is responsible for ordering the elements of the PriorityQueue. A PriorityQueue doesn’t allow null values, those objects that doesn’t provide natural ordering, or those objects that don’t have any comparator associated with them. Finally, the Java PriorityQueue is not thread-safe and it requires O(log(n)) time for its enqueing and dequeing operations.

View

What do you know about the big-O notation and can you give some examples with respect to different data structures ?

Answer:

The Big-O notation simply describes how well an algorithm scales or performs in the worst case scenario as the number of elements in a data structure increases. The Big-O notation can also be used to describe other behavior such as memory consumption. Since the collection classes are actually data structures, we usually use the Big-O notation to chose the best implementation to use, based on time, memory and performance. Big-O notation can give a good indication about performance for large amounts of data.

View

© 2017 QuizBucket.org