Tuesday, December 13, 2022

java interview questions

 ava explain runtime vs compile time polymorphism

Polymorphism is a concept in object-oriented programming that refers to the ability of different objects to respond to the same message in different ways. There are two types of polymorphism: compile-time polymorphism and runtime polymorphism.

Compile-time polymorphism is also known as static binding or early binding. This type of polymorphism is achieved by using function overloading or operator overloading in a program. Function overloading is when a program has multiple functions with the same name, but different numbers or types of arguments. Operator overloading is when a program has operators that have different meanings depending on the context in which they are used.

In compile-time polymorphism, the type of object that is going to respond to a message is determined at compile time, based on the type of the object reference variable. This means that the compiler knows which method to call at compile time, based on the type of the object reference variable.

On the other hand, runtime polymorphism is also known as dynamic binding or late binding. This type of polymorphism is achieved by using inheritance and virtual functions in a program. Inheritance is when one class, known as the derived class, inherits the properties and methods of another class, known as the base class. A virtual function is a function that is declared in the base class and is re-defined (overridden) in the derived class.

In runtime polymorphism, the type of object that is going to respond to a message is determined at runtime, based on the type of the object itself. This means that the correct method to call is determined at runtime, based on the type of the object.

To summarize, compile-time polymorphism is achieved using function overloading and operator overloading, and the type of object that is going to respond to a message is determined at compile time. On the other hand, runtime polymorphism is achieved using inheritance and virtual functions, and the type of object that is going to respond to a message is determined at runtime.


A HashMap is a data structure in Java that maps keys to values. It uses a hash table to store its data, which allows it to store and retrieve data very quickly. Here are some key points about HashMaps:

  • A HashMap is a generic class that has two type parameters: the type of the keys and the type of the values. For example, HashMap<String, Integer> is a HashMap that maps strings to integers.
  • A HashMap stores key-value pairs in a hash table. When you add a key-value pair to a HashMap, the key is hashed, and the value is stored in the hash table at the index determined by the hash of the key. When you look up a value in a HashMap, the key is hashed again and the value is retrieved from the hash table at the index determined by the hash of the key.
  • A HashMap does not maintain the order of its elements. The order of the elements in a HashMap may change over time, depending on the implementation of the hash function and the number of elements in the HashMap.
  • A HashMap allows null keys and null values. You can add a key-value pair with a null key or a null value to a HashMap. However, you cannot have more than one null key in a HashMap, because null keys are considered equal, and a HashMap cannot have duplicate keys.
  • A HashMap is not thread-safe. This means that if multiple threads try to access and modify a HashMap concurrently, the results may be unpredictable and may lead to data corruption or other issues. To use a HashMap in a multithreaded environment, you need to synchronize the access to the HashMap using a synchronized block or a synchronized method.


In Java, a thread goes through several different states in its lifecycle. These states are:

  1. New: A new thread begins its lifecycle in the new state. It remains in this state until the program starts the thread.

  2. Runnable: After a thread has been started, it enters the runnable state. In this state, the thread is eligible to run, but it may not be actually running at any given moment. This is because the thread scheduler, which is responsible for deciding which threads should run at any given time, may choose to schedule another thread instead.

  3. Running: A thread in the runnable state is said to be in the running state when it is currently executing.

  4. Blocked: A thread can enter the blocked state for a number of reasons. For example, a thread may be blocked if it is waiting for a resource to become available, or if it is waiting for another thread to perform a specific action.

  5. Terminated: A thread enters the terminated state when it has completed execution.




Polymorphism is a concept in object-oriented programming that refers to the ability of different objects to respond to the same message in different ways. There are two types of polymorphism: compile-time polymorphism and runtime polymorphism.

Compile-time polymorphism is also known as static binding or early binding. This type of polymorphism is achieved by using function overloading or operator overloading in a program. Function overloading is when a program has multiple functions with the same name, but different numbers or types of arguments. Operator overloading is when a program has operators that have different meanings depending on the context in which they are used.

In compile-time polymorphism, the type of object that is going to respond to a message is determined at compile time, based on the type of the object reference variable. This means that the compiler knows which method to call at compile time, based on the type of the object reference variable.

On the other hand, runtime polymorphism is also known as dynamic binding or late binding. This type of polymorphism is achieved by using inheritance and virtual functions in a program. Inheritance is when one class, known as the derived class, inherits the properties and methods of another class, known as the base class. A virtual function is a function that is declared in the base class and is re-defined (overridden) in the derived class.

In runtime polymorphism, the type of object that is going to respond to a message is determined at runtime, based on the type of the object itself. This means that the correct method to call is determined at runtime, based on the type of the object.

To summarize, compile-time polymorphism is achieved using function overloading and operator overloading, and the type of object that is going to respond to a message is determined at compile time. On the other hand, runtime polymorphism is achieved using inheritance and virtual functions, and the type of object that is going to respond to a message is determined at runtime.

Christopher Fox
java what are the key points of a hashmap

A HashMap is a data structure in Java that maps keys to values. It uses a hash table to store its data, which allows it to store and retrieve data very quickly. Here are some key points about HashMaps:

  • A HashMap is a generic class that has two type parameters: the type of the keys and the type of the values. For example, HashMap<String, Integer> is a HashMap that maps strings to integers.
  • A HashMap stores key-value pairs in a hash table. When you add a key-value pair to a HashMap, the key is hashed, and the value is stored in the hash table at the index determined by the hash of the key. When you look up a value in a HashMap, the key is hashed again and the value is retrieved from the hash table at the index determined by the hash of the key.
  • A HashMap does not maintain the order of its elements. The order of the elements in a HashMap may change over time, depending on the implementation of the hash function and the number of elements in the HashMap.
  • A HashMap allows null keys and null values. You can add a key-value pair with a null key or a null value to a HashMap. However, you cannot have more than one null key in a HashMap, because null keys are considered equal, and a HashMap cannot have duplicate keys.
  • A HashMap is not thread-safe. This means that if multiple threads try to access and modify a HashMap concurrently, the results may be unpredictable and may lead to data corruption or other issues. To use a HashMap in a multithreaded environment, you need to synchronize the access to the HashMap using a synchronized block or a synchronized method.

These are the main points about HashMaps in Java.

Christopher Fox

No comments:

Post a Comment