Showing posts with label Interview. Show all posts
Showing posts with label Interview. Show all posts

Kafka Interview Questions

Q1. What is Kafka?

Apache Kafka is an open-source distributed event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications.

Q2. Kafka Salient Features

High Throughput: Support for millions of messages with modest hardware

Scalability: Highly scalable distributed systems with no downtime.

Replication: Messages are replicated across the cluster to provide support for multiple subscribers and balance the consumers in case of failures.

Durability: Provides support for persistence of message to disk

Stream Processing: Used with real-time streaming applications like Apache Spark & Storm

Data Loss: Kafka with proper configurations can ensure zero data loss.

Q3. List the various components in Kafka.

The four major components of Kafka are:

Topic – a stream of messages belonging to the same type

Producer – that can publish messages on a topic

Brokers – a set of servers where the published messages are stored

Consumer – that subscribes to various topics and pulls data from the brokers.

Q4. Explain the role of the offset.

Messages contained in the partitions are assigned a unique ID number that is called the offset. The role of the offset is to uniquely identify every message within the partition.

Q5. What is a Consumer Group?

Consumer Groups is a concept exclusive to Kafka.  Every Kafka consumer group consists of one or more consumers that jointly consume a set of subscribed topics.

Q6. What is the role of the ZooKeeper?

Kafka uses Zookeeper to store offsets of messages consumed for a specific topic and partition by a specific Consumer Group.

Q7. Is it possible to use Kafka without ZooKeeper?

No, it is not possible to bypass Zookeeper and connect directly to the Kafka server. If for some reason, ZooKeeper is down, you cannot service any client request.

Q8. Explain the concept of Leader and Follower in Kafka.

In Kafka, each partition has one server that acts as a Leader and one or more servers that operate as Followers. The Leader is in charge of all read and writes requests for the partition, while the Followers are responsible for passively replicating the leader. In the case that the Leader fails, one of the Followers will assume leadership. The server's load is balanced as a result of this.

Q9. Why is Topic Replication important in Kafka? What do you mean by ISR in Kafka?

Topic replication is critical for constructing Kafka deployments that are both durable and highly available. When one broker fails, topic replicas on other brokers remain available to ensure that data is not lost and that the Kafka deployment is not disrupted. The replication factor specifies the number of copies of a topic that are kept across the Kafka cluster. It takes place at the partition level and is defined at the subject level. A replication factor of two, for example, will keep two copies of a topic for each partition.

Q10. Why are Replications critical in Kafka?

Replication ensures that published messages are not lost and can be consumed in the event of any machine error, program error, or frequent software upgrades.

Q11. If a Replica stays out of the ISR for a long time, what does it signify?

It means that the Follower is unable to fetch data as fast as data accumulated by the Leader.

Q12. What is the process for starting a Kafka server?

Since Kafka uses ZooKeeper, it is essential to initialize the ZooKeeper server, and then fire up the Kafka server.

To start the ZooKeeper server: > bin/zookeeper-server-start.sh config/zookeeper.properties

Next, to start the Kafka server: > bin/kafka-server-start.sh config/server.properties

Q13. How do you define a Partitioning Key?

Within the Producer, the role of a Partitioning Key is to indicate the destination partition of the message. By default, a hashing-based Partitioner is used to determine the partition ID given the key. Alternatively, users can also use customized Partitions.

Q14. In the Producer, when does QueueFullException occur?

QueueFullException typically occurs when the Producer attempts to send messages at a pace that the Broker cannot handle. Since the Producer doesn’t block, users will need to add enough brokers to collaboratively handle the increased load.

Q15. Explain the role of the Kafka Producer API.

The role of Kafka’s Producer API is to wrap the two producers – kafka.producer.SyncProducer and the kafka.producer.async.AsyncProducer. The goal is to expose all the producer functionality through a single API to the client.

Q16. Explain the four core API architectures that Kafka uses.

Following are the four core APIs that Kafka uses:

Producer API: The Producer API in Kafka allows an application to publish a stream of records to one or more Kafka topics.

Consumer API: An application can subscribe to one or more Kafka topics using the Kafka Consumer API. It also enables the application to process streams of records generated in relation to such topics.

Streams API: The Kafka Streams API allows an application to use a stream processing architecture to process data in Kafka. An application can use this API to take input streams from one or more topics, process them using stream operations, and generate output streams to transmit to one or more topics. The Streams API allows you to convert input streams into output streams in this manner.

Connect API: The Kafka Connector API connects Kafka topics to applications. This opens up possibilities for constructing and managing the operations of producers and consumers, as well as establishing reusable links between these solutions. A connector, for example, may capture all database updates and ensure that they are made available in a Kafka topic.

Q17. What is the maximum size of a message that Kafka can receive?

By default, the maximum size of a Kafka message is 1MB (megabyte). The broker settings allow you to modify the size. Kafka, on the other hand, is designed to handle 1KB messages as well.

Q18. What are some of the disadvantages of Kafka?

Following are the disadvantages of Kafka :

Kafka performance degrades if there is message tweaking. When the message does not need to be updated, Kafka works well.

Wildcard topic selection is not supported by Kafka. It is necessary to match the exact topic name.

Brokers and consumers reduce Kafka's performance when dealing with huge messages by compressing and decompressing the messages. This has an impact on Kafka's throughput and performance.

Certain message paradigms, including point-to-point queues and request/reply, are not supported by Kafka.

Kafka does not have a complete set of monitoring tools.

Q19. What is the purpose of partitions in Kafka?

Partitions allow a single topic to be partitioned across numerous servers from the perspective of the Kafka broker. This allows you to store more data in a single topic than a single server can. If you have three brokers and need to store 10TB of data in a topic, one option is to construct a topic with only one partition and store all 10TB on one broker. Another alternative is to build a three-partitioned topic and distribute 10 TB of data among all brokers. A partition is a unit of parallelism from the consumer's perspective.

Q20. What do you mean by multi-tenancy in Kafka?

Multi-tenancy is a software operation mode in which many instances of one or more programs operate in a shared environment independently of one another. The instances are considered to be physically separate yet logically connected. The level of logical isolation in a system that supports multi-tenancy must be comprehensive, but the level of physical integration can vary. Kafka is multi-tenant because it allows for the configuration of many topics for data consumption and production on the same cluster.

Q21. What are the ways to tune Kafka for optimal performance?

There are mainly three ways to tune Kafka for optimal performance:

Tuning Kafka Producers

Kafka Brokers Tuning

Tuning Kafka Consumers

Q22. Exaplain Tunning of Kafka Producers.

Data that the producers have to send to brokers is stored in a batch. When the batch is ready, the producer sends it to the broker. For latency and throughput, to tune the producers, two parameters must be taken care of: batch size and linger time. The batch size has to be selected very carefully. If the producer is sending messages all the time, a larger batch size is preferable to maximize throughput. However, if the batch size is chosen to be very large, then it may never get full or take a long time to fill up and, in turn, affect the latency. Batch size will have to be determined, taking into account the nature of the volume of messages sent from the producer. The linger time is added to create a delay to wait for more records to get filled up in the batch so that larger records are sent. A longer linger time will allow more messages to be sent in one batch, but this could compromise latency. On the other hand, a shorter linger time will result in fewer messages getting sent faster - reduced latency but reduced throughput as well.

Q23. Exaplain  Tuning of Kafka broker.

Each partition in a topic is associated with a leader, which will further have 0 or more followers. It is important that the leaders are balanced properly and ensure that some nodes are not overworked compared to others.

Q24. Exaplain Tuning of Kafka Consumers.

It is recommended that the number of partitions for a topic is equal to the number of consumers so that the consumers can keep up with the producers. In the same consumer group, the partitions are split up among the consumers.

Q25. What are the use cases of Kafka monitoring?

Following are the use cases of Apache Kafka monitoring:

Apache Kafka monitoring can keep track of system resource consumption such as memory, CPU, and disk utilization over time.

Apache Kafka monitoring is used to monitor threads and JVM usage. It relies on the Java garbage collector to free up memory, ensuring that it frequently runs, thereby guaranteeing that the Kafka cluster is more active.

It can be used to determine which applications are causing excessive demand, and identifying performance bottlenecks might help rapidly solve performance issues.

It always checks the broker, controller, and replication statistics to modify the partitions and replicas status if required.

Q26. When does the broker leave the ISR?

ISR is a set of message replicas that are completely synced up with the leaders. It means ISR contains all the committed messages, and ISR always includes all the replicas until it gets a real failure. An ISR can drop a replica if it deviates from the leader.

Q27. Define consumer lag in Apache Kafka.

Consumer lag refers to the lag between the Kafka producers and consumers. Consumer groups will have a lag if the data production rate far exceeds the rate at which the data is getting consumed. Consumer lag is the difference between the latest offset and the consumer offset.

Q28. What is meant by cluster id in Kafka?

Kafka clusters are assigned unique and immutable identifiers. The identifier for a particular cluster is known as the cluster id. A cluster id can have a maximum number of 22 characters and has to follow the regular expression [a-zA-Z0-9_\-]+. It is generated when a broker with version 0.10.1 or later is successfully started for the first time. The broker attempts to get the cluster id from the znode during startup. If the znode does not exist, the broker generates a new cluster id and creates a znode with this cluster id.

Q29. Mention some of the system tools available in Apache Kafka.

The three main system tools available in Apache Kafka are:

Kafka Migration Tool − This tool is used to migrate the data in a Kafka cluster from one version to another.

Kafka MirrorMaker − This tool copies data from one Kafka cluster to another.

Consumer Offset Checker − This tool displays the Consumer Group, Topic, Partitions, Off-set, logSize, and Owner for specific Topics and Consumer Group sets.

Q30. Mention some benefits of Apache Kafka.

High throughput: Kafka can handle thousands of messages per second. Due to low latency, Kafka supports incoming messages at a high volume and velocity.

Low latency: Apache Kafka offers as low as ten milliseconds. This is because it decoupled messages on the broker, allowing the consumer to pull them at any time.

Fault-tolerant: The use of replicas allows Apache Kafka to be fault-tolerant in cases of a failure within a cluster.

Durability: With the replication feature, Apache Kafka allows data to remain more persistent on the cluster rather than the disk, thus making it more durable.

Scalability: the ability of Kafka to handle messages of high volume and high velocity makes it very scalable.

Ability to handle real-time data: Kafka can handle real-time data pipelines.

Q31. What is the optimal number of partitions for a topic?

The optimal number of partitions a topic should be divided into must be equal to the number of consumers.

Q32. What is the Kafka MirrorMaker?

The Kafka MirrorMaker is a stand-alone tool that allows data to be copied from one Apache Kafka cluster to another. The Kafka MirrorMaker will read data from topics in the original cluster and write the topics to a destination cluster with the same topic name. The source and destination clusters are independent entities and can have different numbers of partitions and varying offset values.

Q33. What is the role of the Kafka Migration Tool?

The Kafka Migration tool is used to efficiently move from one environment to another. It can be used to move existing Kafka data from an older version of Kafka to a newer version.

Q34. What are the name restrictions for Kafka topics?

According to Apache Kafka, there are some legal rules to be followed to name topics, which are as follows:

The maximum length is 255 characters (symbols and letters). The length has been reduced from 255 to 249 in Kafka 0.10

. (dot), _ (underscore), - (hyphen) can be used. However, topics with a dot (.) and underscore ( _) could cause some confusion with internal data structures, and hence, it is advisable to use either but not both.

Q35. Where is the meta information about topics stored in the Kafka cluster?

Currently, in Apache Kafka, meta-information about topics is stored in the ZooKeeper. Information regarding the location of the partitions and the configuration details related to a topic are stored in the ZooKeeper in a separate Kafka cluster.

Q36. What is meant by Kafka Connect?

Kafka Connect is a tool provided by Apache Kafka to allow scalable and reliable streaming data to move between Kafka and other systems. It makes it easier to define connectors that are responsible for moving large collections of data in and out of Kafka. Kafka Connect is able to process entire databases as input. It can also collect metrics from application servers into Kafka topics so that this data can be available for Kafka stream processing.

Q37. Explain message compression in Apache Kafka.

In Apache Kafka, producer applications write data to the brokers in JSON format. The data in the JSON format is stored in string form, which can result in several duplicated records getting stored in the Kafka topic. This leads to an increased occupation of disk space. Hence, to reduce this disk space, compression of messages or lingering data is performed before sending the messages to Kafka. Message compression is done on the producer side, and hence there is no need to make any changes to the configuration of the consumer or the broker.

Q38. What is the need for message compression in Apache Kafka?

Message compression in Kafka does not require any changes in the configuration of the broker or the consumer. It is beneficial for the following reasons:

Due to reduced size, it reduces the latency in which messages are sent to Kafka.

Reduced bandwidth allows the producers to send more net messages to the broker.

When the data is stored in Kafka via cloud platforms, it can reduce the cost in cases where the cloud services are paid.

Message compression leads to reduced disk load, which will lead to faster read and write requests.

Q39. What are some disadvantages of message compression in Apache Kafka?

Producers end up using some CPU cycles for compression.

Consumers use some CPU cycles for decompression.

Compression and decompression result in greater CPU demand.

Q40. Can a consumer read more than one partition from a topic?

Yes, if the number of partitions is greater than the number of consumers in a consumer group, then a consumer will have to read more than one partition from a topic.

Q41. What do you know about log compaction in Kafka?

Log compaction is a method by which Kafka ensures that at least the last known value for each message key within the log of data is retained for a single topic partition. This makes it possible to restore the state after an application crashes or in the event of a system failure. It allows cache reloading once an application restarts during any operational maintenance. Log compaction ensures that any consumer processing the log from the start can view the final state of all records in the original order they were written.

Q42. Name the various types of Kafka producer API.

There are three types of Kafka producer API available-

Fire and Forget

Synchronous producer

Asynchronous producer

Q43. What is Kafka’s producer acknowledgment? What are the various types of acknowledgment settings that Kafka provides?

A broker sends an ack or acknowledgment to the producer to verify the reception of the message. Ack level is a configuration parameter in the Producer that specifies how many acknowledgments the producer must receive from the leader before a request is considered successful. The following types of acknowledgment are available:

acks=0

In this setting, the producer does not wait for the broker's acknowledgment. There is no way to know if the broker has received the record.

acks=1

In this situation, the leader logs the record to its local log file and answers without waiting for all of its followers to acknowledge it. The message can only be lost in this instance if the leader fails shortly after accepting the record but before the followers have copied it; otherwise, the record would be lost.

acks=all

A set leader in this situation waits for all in-sync replica sets to acknowledge the record. As long as one replica is alive, the record will not be lost, and the best possible guarantee will be provided. However, because a leader must wait for all followers to acknowledge before replying, the throughput is significantly lower.

Q44. How do you get Kafka to perform in a FIFO manner?

Kafka organizes messages into topics, which are then divided into partitions. The partition is an immutable list of ordered messages that is updated regularly. A message in the partition is uniquely recognized by a sequential number called offset. FIFO behavior is possible only within the partitions. Following the methods below will help you achieve FIFO behavior:

To begin, we first set the enable the auto-commit property to be false:

Set enable.auto.commit=false

We should not call the consumer.commitSync(); method after the messages have been processed.

Then we may "subscribe" to the topic and ensure that the consumer system's register is updated.

You should use Listener consumerRebalance, and call a consumer inside a listener.

seek(topicPartition, offset).

The offset related to the message should be kept together with the processed message once it has been processed.

Q45. How is load balancing maintained in Kafka?

Load balancing in Kafka is handled by the producers. The message load is spread out between the various partitions while maintaining the order of the message. By default, the producer selects the next partition to take up message data using a round-robin approach. If a different approach other than round-robin is to be used, users can also specify exact partitions for a message.


Exception Handling Interview Questions

DBMS Interview Questions Set -1

DBMS Interview Questions Set -2

SQL Interview Question Set -1

SQL Interview Question Set -2

JPA Interview Questions Set -1

JPA Interview Question Set -2

Hibernate Interview Questions

Spring Boot Interview Questions Set 1

Spring Boot Interview Questions Set 2

GIT Interview Questions

Redis Interview Questions

Core Java Interview Questions Set -1

Docker interview question Set -1

Docker interview question Set -2

Kubernetes Interview Question Set -1

Kubernetes Interview Question Set -2

Collection interview questions

Kafka Interview Questions

Collection interview questions

Q1. What is the Collection framework in Java?

A collection Framework is a combination of classes and interfaces, which is used to store and manipulate the data in the form of objects. It provides various classes such as ArrayList, Vector, Stack, HashSet, etc., and interfaces such as List, Queue, Set, etc. for this purpose.

Q2. What are the main differences between array and collection?

Array and Collection are somewhat similar regarding storing the references of objects and manipulating the data, but they differ in many ways. The main differences between the array and Collection are defined below:

Arrays are always of fixed size, i.e., a user can not increase or decrease the length of the array according to their requirement or at runtime, but In Collection, size can be changed dynamically as per need.

Arrays can only store homogeneous or similar type objects, but in Collection, heterogeneous objects can be stored.

Q3. Explain various interfaces used in the Collection framework?

The collection framework implements various interfaces, Collection interface and Map interface (java.util.Map) is the mainly used interfaces of the Java Collection Framework. A list of interfaces of Collection Framework is given below:

1. Collection interface: Collection (java.util.Collection) is the primary interface, and every collection must implement this interface.

Syntax:

public interface Collection<E>extends Iterable  

Where <E> represents that this interface is of Generic type

2. List interface: The list interface extends the Collection interface, and it is an ordered collection of objects. It contains duplicate elements. It also allows random access to elements.

Syntax:

public interface List<E> extends Collection<E>  

3. Set interface: Set (java.util.Set) interface is a collection that cannot contain duplicate elements. It can only include inherited methods of the Collection interface.

Syntax:

public interface Set<E> extends Collection<E>

4. Queue interface: Queue (java.util.Queue) interface defines the queue data structure, which stores the elements in the form FIFO (first in first out).

Syntax:

public interface Queue<E> extends Collection<E>  

5. Dequeue interface: it is a double-ended queue. It allows the insertion and removal of elements from both ends. It implants the properties of both Stack and queue so it can perform LIFO (Last in first out) stack and FIFO (first in first out) queue, operations.

Syntax:

public interface Dequeue<E> extends Queue<E>  

6. Map interface: A Map (java.util.Map) represents a key, value pair storage of elements. Map interface does not implement the Collection interface. It can only contain a unique key but can have duplicate elements. There are two interfaces that implement Map in java that are Map interface and Sorted Map.

Q4. What is the difference between ArrayList and Vector?

ArrayListVector
ArrayList is not synchronized.The vector is synchronized.
ArrayList is not a legacy class.Vector is a legacy class.
ArrayList increases its size by 50% of the array size.Vector increases its size by doubling the array size.
ArrayList is not thread-safe as it is not synchronized.Vector list is thread-safe as it's every method is synchronized.


Q5. What is the difference between ArrayList and LinkedList?

Below are some of the differences in ArrayList and LinkedList

ArrayListLinkedList
ArrayList uses a dynamic array.LinkedList uses a doubly linked list.
ArrayList is not efficient for manipulation because too much is required.LinkedList is efficient for manipulation.
ArrayList is better to store and fetch data.LinkedList is better to manipulate data.
ArrayList provides random access.LinkedList does not provide random access.
ArrayList takes less memory overhead as it stores only the objectLinkedList takes more memory overhead, as it stores the object as well as the address of that object.

Q6. How to measure the performance of an ArrayList?

The performance of ArrayList can be measured by:

Adding an element: We can add an element at the end of ArrayList using add(E e) method. It is O(1). In the worst scenario, it might go to O(n). 

Retrieving an element: We can access the array index using get(int index). The performance, in this case, can be measured using ArrayList get() is O(1).

Removing an element: In case, if the developers are removing an element using the remove(int index), then the performance of ArrayList can be calculated using said remove(int index) operation in the O(n – index) method.

Q7. What is the difference between Iterator and ListIterator?

Iterator traverses the elements in the forward direction only whereas ListIterator traverses the elements in forward and backward directions.

IteratorListIterator
The Iterator traverses the elements in the forward direction only. ListIterator traverses the elements in backward and forward directions both.
The Iterator can be used in List, Set, and Queue.ListIterator can be used in List only.
The Iterator can only perform remove operation while traversing the collection.ListIterator can perform add, remove, and set operation while traversing the collection.


Q8. Explain Linkedhashmap

LinkedHashMap is the implementation of the Map interface. It can also extend the HashMap class. Therefore, like HashMap, LinkedHashMap enables Java developers to allow one null key and more than one null value.

Q9. What is the difference between an Iterator and an Enumeration?

IteratorEnumeration
The Iterator can traverse legacy and non-legacy elements.Enumeration can traverse only legacy elements.
The Iterator is fail-fast.Enumeration is not fail-fast.
The Iterator is slower than Enumeration.Enumeration is faster than Iterator.
The Iterator can perform a remove operation while traversing the collection.The Enumeration can perform only traverse operation on the collection.


Q10. Explain RandomAccess interface

RandomAccess interface is used by List implementations for the indication that they are supporting fast. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.

Q11. What is the difference between a List and a Set?

The List and Set both extend the collection interface. However, there are some differences between both which are listed below.

The List can contain duplicate elements whereas the Set includes unique items.

The List is an ordered collection that maintains the insertion order whereas the Set is an unordered collection that does not preserve the insertion order.

The List interface can allow n number of null values whereas the Set interface only allows a single null value.

Q12. Explain diamond operator

The diamond operator enables the compiler to collect the type arguments of a generic class. In Java SE, we can substitute the parameterized constructor with an empty parameter set (<>) known as the diamond operator.

Q13. What is the difference between HashSet and TreeSet?

The HashSet and TreeSet, both classes, implement the Set interface. The differences between the both are listed below.

HashSet maintains no order whereas TreeSet maintains ascending order.

HashSet is impended by a hash table whereas TreeSet is implemented by a Tree structure.

HashSet performs faster than TreeSet.

HashSet is backed by HashMap whereas TreeSet is backed by TreeMap.

Q14. What is the difference between Set and Map?

The differences between the Set and Map are given below.

Set contains values only whereas Map contains key and values both.

Set contains unique values whereas Map can contain unique Keys with duplicate values.

Set holds a single number of null values whereas Map can include a single null key with n number of null values.

Q15. How to iterate a map?

The developer cannot directly iterate the map, but, this interface has two methods that give a view set of the map. These methods are:

Set<Map.Entry<K, V>>entrySet(): It is a method that returns a set having the entries mentioned in the map. These entries are generally objected, which has type Map. Entry.

Set<K>keySet(): This Java method returns a set that has the map key.

Q16. What is the difference between HashSet and HashMap?

The differences between HashSet and HashMap are listed below.

HashSet contains only values whereas HashMap includes the entry (key, value). HashSet can be iterated, but HashMap needs to convert into Set to be iterated.

HashSet implements the Set interface whereas HashMap implements the Map interface

HashSet cannot have any duplicate value whereas HashMap can contain duplicate values with unique keys.

HashSet contains only a single number of null values whereas HashMap can hold a single null key with n number of null values.

Q17. What is the difference between HashMap and TreeMap?

The differences between HashMap and TreeMap are given below.

HashMap maintains no order, but TreeMap maintains ascending order.

HashMap is implemented by a hash table whereas TreeMap is implemented by a Tree structure.

HashMap can be sorted by Key or value whereas TreeMap can be sorted by Key.

HashMap may contain a null key with multiple null values whereas TreeMap cannot hold a null key but can have multiple null values.

Q18. What is the difference between HashMap and Hashtable?

HashMapHashtable
HashMap is not synchronizedHashtable is synchronized.
HashMap can contain one null key and multiple null values.Hashtable cannot contain any null key or null value.
HashMap is not  thread-safe, so it is useful for non-threaded applications. Hashtable is thread-safe, and it can be shared between various threads.
HashMap inherits the AbstractMap classHashtable inherits the Dictionary class.


Q19. What is the difference between Collection and Collections?

The differences between the Collection and Collections are given below.

The Collection is an interface whereas Collections is a class.

The Collection interface provides the standard functionality of data structure to List, Set, and Queue. However, the Collections class is to sort and synchronize the collection elements.

The Collection interface provides the methods that can be used for data structure whereas the Collections class provides the static methods which can be used for various operations on a collection.

Q20. What is the difference between Comparable and Comparator?

ComparableComparator
Comparable provides only one sort of sequence.The Comparator provides multiple sorts of sequences.
It provides one method named compareTo().It provides one method named compare().
It is found in java.lang package.It is located in java.util package.
If we implement the Comparable interface, The actual class is modified.The actual class is not changed.


Q21. What do you understand by BlockingQueue?

BlockingQueue is an interface that extends the Queue interface. It provides concurrency in the operations like retrieval, insertion, and deletion. While retrieval of any element, it waits for the queue to be non-empty. While storing the elements, it waits for the available space. BlockingQueue cannot contain null elements, and the implementation of BlockingQueue is thread-safe.

Syntax:

public interface BlockingQueue<E> extends Queue <E>

Q21. What does the hashCode() method?

The hashCode() method returns a hash code value (an integer number).

The hashCode() method returns the same integer number if two keys (by calling the equals() method) are identical.

However, it is possible that two hash code numbers can have different or the same keys.

If two objects do not produce an equal result by using the equals() method, then the hashcode() method will provide a different integer results for both objects.

Q22. Why do we override the equals() method?

The equals method is used to check whether two objects are the same or not. It needs to be overridden if we want to check the objects based on the property.

Q23. How to synchronize List, Set, and Map elements?

Yes, the Collections class provides methods to make List, Set, or Map elements synchronized:

public static List synchronizedList(List l){}

public static Set synchronizedSet(Set s){}

public static SortedSet synchronizedSortedSet(SortedSet s){}

public static Map synchronizedMap(Map m){}

public static SortedMap synchronizedSortedMap(SortedMap m){}

Q24. What is the advantage of the generic collection?

There are three main advantages of using the generic collection.

If we use the generic class, we don't need typecasting.

It is type-safe and checked at compile time.

Generic confirms the stability of the code by making it bug detectable at compile time.

Q25. What is hash-collision in Hashtable and how it is handled in Java?

Two different keys with the same hash value are known as hash-collision. Two separate entries will be kept in a single hash bucket to avoid collision. There are two ways to avoid hash-collision.

Separate Chaining

Open Addressing

Q26. What is the Dictionary class?

The Dictionary class provides the capability to store key-value pairs.

Q27. What is the default size of load factor in hashing-based collection?

The default size of the load factor is 0.75. The default capacity is computed as the initial capacity * load factor. For example, 16 * 0.75 = 12. So, 12 is the default capacity of Map.

Q28. What do you understand by fail-fast?

The Iterator in java which immediately throws ConcurrentmodificationException, if any structural modification occurs, is called a Fail-fast iterator. Fail-fats iterator does not require any extra space in memory.

Q29. What is the difference between Array and ArrayList?

The main differences between the Array and ArrayList are given below.

ArrayArrayList
The Array is of fixed size, means we cannot resize the array as per need.ArrayList is not of the fixed size we can change the size dynamically.
Arrays are of the static type.ArrayList is of dynamic size.
Arrays can store primitive data types as well as objects.ArrayList cannot store the primitive data types it can only store the objects.


Q30. What is the difference between the length of an Array and the size of an ArrayList?

The length of an array can be obtained using the property of length whereas ArrayList does not support the length property, but we can use the size() method to get the number of objects in the list.

Finding the length of the array

Int [] array = new int[10];  

System.out.println("The size of the array is " + array.length);  

Finding the size of the ArrayList

ArrayList<String> list=new ArrayList<String>();    

list.add("Hello");    

list.add("Java");  

list.add("Program"); 

System.out.println(list.size());

Q31. How to convert ArrayList to Array and Array to ArrayList?

We can convert an Array to an ArrayList by using the asList() method of the Arrays class. asList() method is the static method of the Arrays class and accepts the List object. Consider the following syntax:

Arrays.asList(item)  

We can convert an ArrayList to Array using the toArray() method of the ArrayList class. Consider the following syntax to convert the ArrayList to the List object.

objctt.toArray(new String[List_object.size()])

Q33. How to make Java ArrayList Read-Only?

We can obtain java ArrayList Read-only by calling the Collections.unmodifiableCollection() method. When we define an ArrayList as Read-only then we cannot perform any modification in the collection through add(), remove(), or set() method.

Q34. How to synchronize ArrayList?

We can synchronize ArrayList in two ways.

Using Collections.synchronizedList() method

Using CopyOnWriteArrayList<T>

Q35. When to use ArrayList and LinkedList?

LinkedList are better to use for the update operations whereas ArrayLists are better used for search operations.


Exception Handling Interview Questions

DBMS Interview Questions Set -1

DBMS Interview Questions Set -2

SQL Interview Question Set -1

SQL Interview Question Set -2

JPA Interview Questions Set -1

JPA Interview Question Set -2

Hibernate Interview Questions

Spring Boot Interview Questions Set 1

Spring Boot Interview Questions Set 2

GIT Interview Questions

Redis Interview Questions

Core Java Interview Questions Set -1

Docker interview question Set -1

Docker interview question Set -2

Kubernetes Interview Question Set -1

Kubernetes Interview Question Set -2

Collection interview questions

Kafka Interview Questions

Kubernetes commands

1. Cluster Management commands

A Kubernetes cluster is a collection of nodes that execute containerized applications. 

It lets containers run across several machines and environments: cloud-based, virtual, on-premises, and physical.

Listed below are the kubectl commands that can be utilized to manage a cluster.

1. Display endpoint information regarding the services and master in the cluster

command:  kubectl cluster-info

2. Show the Kubernetes version functioning on the client and server

command: kubectl version

3. Get the configuration of the cluster

kubectl config view

4. Make a list of the available API resources

kubectl api-resources

5. Make a list of the available API versions

kubectl api-versions

6. List everything

kubectl get all –all-namespaces

2. Namespaces commands

Shortcode = ns

1.Create namespace <name>

kubectl create namespace <namespace_name>

2. List one or more namespaces

kubectl get namespace <namespace_name>

3. Show the detailed condition of one or more namespace

kubectl describe namespace <namespace_name>

4. Delete a namespace

kubectl delete namespace <namespace_name>

5. Edit and modify the namespace’s definition

kubectl edit namespace <namespace_name>

6. Display Resource (CPU/Memory/Storage) usage for a namespace

kubectl top namespace <namespace_name>

3. Node operations commands

A Node is a worker machine in Kubernetes and can either be a virtual or a physical machine, which depends on the cluster. Every Node is handled by the control plane. A Node can contain several pods, and the Kubernetes control plane handles scheduling the pods automatically across the Nodes in the cluster. The following commands can be utilized for Node Operations.

1. Revise the taints on one or more nodes

kubectl taint node <node_name>

2. List one or more nodes

kubectl get node

3. Delete a node or multiple nodes

kubectl delete node <node_name>

4. Display Resource usage (CPU/Memory/Storage) for nodes

kubectl top node

5. Resource allocation per node

kubectl describe nodes | grep Allocated -A 5

6. Pods running on a node

kubectl get pods -o wide | grep <node_name>

7. Annotate a node

kubectl annotate node <node_name>

8. Mark a node as unschedulable

kubectl cordon node <node_name>

9. Mark node as schedulable

kubectl uncordon node <node_name>

10. Drain a node in preparation for maintenance

kubectl drain node <node_name>

11. Add the labels of one or more nodes

kubectl label node

4. Listing Resources commands

Kubernetes resources are also regarded as Kubernetes objects related to a certain namespace, you can either utilize individual kubectl get command to jot down every resource one by one, or you can jot down all the resources in a Kubernetes namespace by executing a single command. Mentioned below is the list of commands to get the resource information.

1. Create a plain-text list of all namespaces

kubectl get namespaces

2. Create a plain-text list of all pods

kubectl get pods

3. Create a comprehensive plain-text list of all pods

kubectl get pods -o wide

4. Create a list of all pods functioning on a certain node server

kubectl gets pods–field-selector=spec. nodeName=[server-name]

5. In plain text, make a list of a specific replication controller

kubectl get replicationcontroller [replication-controller-name]

6. Generate a plain-text list of all replication services and controllers

kubectl get replicationcontroller, services

5. Daemonsets commands:

A Daemonset assures that some or all  Nodes run a copy of a Pod. As nodes are incorporated into the cluster, Pods are implemented to them. As nodes are erased from the cluster, those Pods are garbage collected. Erasing a DaemonSet will clean up the Pods it created.

1. List one or more daemonsets 

kubectl get daemonset

2. Edit and modify the definition of one or more daemonset

kubectl edit daemonset <daemonset_name>

3. Delete a daemonset

kubectl delete daemonset <daemonset_name>

4. Create a new daemonset

kubectl create daemonset <daemonset_name>

5. Manage the rollout of a daemonset

kubectl rollout daemonset

6. Show the comprehensive state of daemonsets within a namespace

kubectl describe ds <daemonset_name> -n <namespace_name>

6. Events commands:

Shortcode = ev

Kubernetes events are objects that display what is happening within a cluster, like what decisions were implemented by the scheduler or why some pods were erased from the node. Events are the first thing to look at for application, along with infrastructure operations when something is not functioning as anticipated. Mentioned below are the kubectl commands to get the events.

1. List current events for all resources in the system

kubectl get events

2. List Warnings only

kubectl get events –field-selector type=Warning

3. List events but exclude Pod events

kubectl get events –field-selector involvedObject.kind!=Pod

4. Pull events for a single node with a distinct name

kubectl get events –field-selector involvedObject.kind=Node, involvedObject.name=<node_name>

5. From a list of events, filter out normal events

kubectl get events –field-selector type!=Normal

7. Logs commands

You can use Kubernetes logs commands to monitor, log and debug the pods

1. Print the logs for a pod 

kubectl logs <pod_name>

2. Print the logs for a pod for the last hour

kubectl logs –since=1h <pod_name>

3. Get the current 20 lines of logs

kubectl logs –tail=20 <pod_name>

4. Get logs from a service and choose which container optionally

kubectl logs -f <service_name> [-c <$container>]

5. Adhere to new logs and print the logs for a pod

kubectl logs -f <pod_name>

6. For a container in a pod, Print the logs

kubectl logs -c <container_name> <pod_name>

7. Output the logs for a pod into a ‘pod.log’ file

kubectl logs <pod_name> pod.log

8. View the logs for the last failed pod

kubectl logs –previous <pod_name>

8. Deployments commands:

Shortcode = deploy.

A Kubernetes Deployment is utilized to inform Kubernetes how to design or change instances of the pods that hold a containerized application. Deployments can enhance the number of replica pods, enable the rollout of revised code in a controlled way, or roll back to an earlier deployment version if required.

1. List one or more deployments

kubectl get deployment

2. Show the in-depth state of one or more deployments

kubectl describe deployment <deployment_name>

3. Edit and revise the definition of one or more deployments on the server

kubectl edit deployment <deployment_name>

4.  Generate a new deployment

kubectl create deployment <deployment_name>

5. Delete deployments

kubectl delete deployment <deployment_name>

6. Check the rollout status of a deployment

kubectl rollout status deployment <deployment_name>

9. Replication Controllers commands:

Shortcode = rc

1. Make a list of  the replication controllers

kubectl get rc

2. Make a list of  the replication controllers by namespace

kubectl get rc –namespace=”<namespace_name>” 

10. ReplicaSets commands:

Shortcode = rs

1. List ReplicaSets

kubectl get replicasets

2. Show the detailed state of one or more ReplicaSets

kubectl describe replicasets <replicaset_name>

3. Scale a ReplicaSet

kubectl scale –replicas=[x]

11. Secrets commands:

A Kubernetes Secret is an object that comprises a minor portion of sensitive data like a token, a key, or a password. Such data might otherwise be inserted in an image or in a Pod specification. Users can build Secrets and the system also generates a few Secrets with the help of the following kubectl commands.

1. Create a secret

kubectl create secret

2. List secrets

kubectl get secrets

3. List details about secrets

kubectl describe secrets

4. Delete a secret

kubectldelete secret <secret_name>

12. Services and Service Accounts commands:

A Kubernetes service is a logical abstraction for a deployed group of pods in a cluster (which all perform the same function) and Service accounts are used to provide an identity for pods. Pods that want to interact with the API server will authenticate with a particular service account.

1. Make a list of  one or more services

kubectl get services

2. Show the detailed state of a service

kubectl describe services

3. Reveal a replication controller, service, deployment, or pod as a new Kubernetes service

kubectl expose deployment [deployment_name]

4. Edit and modify the definition of one or more services

kubectl edit services

5. List service accounts

kubectl get serviceaccounts

6. Show the in-depth state of one or more service accounts

kubectl describe serviceaccounts

7. Replace a service account 

kubectl replace serviceaccount

8. Delete a service account

kubectl delete serviceaccount <service_account_name>

9. Kubectl commands adhere to syntax or a common structure, which lets administrators read and verify every kubectl command entered in the terminal window. There are four important parameters to each kubectl call:

Kubectl Syntax

10. The <command> parameter is the operation that should be executed on a resource. Kubectl backs several operations, such as describe, create, get, execute and delete

kubectl <command> <type> <name> <flags>

The <type> parameter specifies the resource type, like pods, bindings, and nodes. Usually, Resource type designations make use of abbreviations to streamline the command line. For instance, the “persistentvolumeclaims” type can be shortened to “pvc.” The <type> parameter is strong since there are several resource types, which also include namespaces, services, jobs, resource quotas, replication controllers, leases, and events. Programmers and Kubernetes administrators should be acquainted with a complete list of resource types.

The <name> parameter defines the name of the resource in the environment. If we omit the name parameter, the details for all resources are returned, similarly to a wildcard argument. Also, administrators can point out multiple resource types and names in the exact command line, as mentioned below.

11. This is effective when the names are all the same resource type, for instance:

kubectl <command> <type> <name1> <name2> … <nameX>

12. Kubectl syntax also backs the combination of several resource types and names on the exact command line in two ways:

kubectl get pod test-pod1 test-pod2

13. Lastly, the <flags> parameter incorporates optional flags to the command line. Flags differ with the command, so not all flags are available for all commands. For instance, the -s, (one dash shorthand notation) or –server (two dashes, longhand notation) flags designate the port and address of the Kubernetes API server.

kubectl <command> <type1/name1> <type2/name2> … <typeX/nameX>

or:

kubectl get pod/test-pod1 replicationcontroller/xyzcorp-rc1

The -o or –output <flag> sends responses to a terminal window in a certain format. For instance, the -o yaml flag will output a YAML-formatted API object, whereas the -o json flag will output a JSON-formatted API object.


Kubernetes Interview Question Set -1

Kubernetes Interview Question Set -2