Java Interview Questions asked in Citibank for freshers

Java Interview Questions asked in Citibank
Download Java Interview Questions asked in Citibank PDF

Below are the list of Best Java Interview Questions asked in Citibank and Answers

    

It is a process in java through which multiple threads can be executed simultaneously. A thread can be explained as a lightweight sub-process, it is the smallest unit of processing. Both multiprocessing and multithreading are done to achieve multitasking in java. The reason why most of the developers use multithreading over multiprocessing is that the threads use a shared memory area. Because they don’t allocate a separate area in the memory and they save a lot of space in the memory because of that.

In Java, A HashMap is a map used to store mappings or links of key-value pairs that works on the hashing principle. It is considered as a data structure that allows us to store objects and retrieve it in constant time O(1).

Differences between overloading and overriding

OverloadingOverriding
Overloading is performed at compilation time.Overriding is performed at runtime
Overloading is also called static binding.Overriding is also called dynamic binding.
Overloading is done within the class.Overriding occurs in between two classes that have an IS-A (inheritance) relationship.
It is an example of compile-time polymorphism.It is an example of run time polymorphism.

Overloading Example:

class ExampleClass{  
static int sum(int a,int b){return a+b;}  
static int sum(int a,int b,int c){return a+b+c;}  
}  

Overriding Example:

class Animal{  
	void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
	  void eat(){System.out.println("eating bread...");}  
}   

The difference between equals and == are as follows:

S. N.==equals() method
1== is an operator.equals() is a method of Object class.
2== should be used during reference comparison. == checks if both references point to the same location or not.equals() method should be used for content comparison. equals() method evaluates the content to check the equality.
3== operator can not be overridden.equals() method if not present and Object. equals() method is utilized, otherwise it can be overridden.

Java StringBuilder class is designed to be used as a drop-in replacement for StringBuffer. It is also used in places where the string buffer was being used by a single thread.

The Transient variable is a modifier that is used in the process of serialization. If the developer doesn’t want to store the value of a particular variable in a file at the time of serialization then they use the Transient keyword. JVM ignores the original value of the variable and saves the default value of that variable data type when it comes across the Transient keyword. It also plays an important role to meet security constraints.

Executor Design pattern can be defined as a particular type of design pattern that serves the purpose of decoupling the execution of a task from the real task taken by the user with the help of executors. Executors are specially designed to provide factory methods. These methods can be further used in the creation of ThreadPools of worker threads. It decouples the submission of a command from the execution of that very command.

The SQL, to compare the top two records is as follows:

SELECT DISTINCT 
    A.id
FROM
    newtable A
INNER JOIN 
    newtable B ON A.id = B.id
WHERE
    A.valB = B.valB AND
    A.valC = B.valC 

The thread pool in Java is actually a reusable pool of Threads. You can say that it contains a group of worker threads that are waiting for the new job to be granted. Thread pool is preferably used because active threads consume system resources, when JVM creates too many threads at the same time, the traffic exists.

Thread Safety refers to the way in which different threads can access the same resources without exposing erroneous behavior or producing unpredictable results.

Some different ways to achieve thread-safety are as follows.

  • Stateless Implementations
  • Immutable Implementations
  • Thread-Local Fields
  • Synchronized Collections
  • Concurrent Collections
  • Atomic Objects
  • Synchronized Methods
  • Synchronized Statements
  • Other Objects as a Lock
  • Volatile Fields
  • Reentrant Locks
  • Caveats
  • Read/Write Locks

Difference between HashSet and HashMap are given below:

HashSetHashMap
The set interface is implemented by the HashSet class.The map interface is implemented by the HashMap class.
Objects like elements or values are stored inside the HashSet. For example, if there is HashSet of string elements then it can portray a set of HashSet elements.Key and value pairs are stored in HashMap. In other words, the mapping of Key and value is maintained by HashMap.
Duplicate elements are not allowed in HashSet, i.e., no duplicate values can be stored in it.Duplicate Key is not allowed in HashMap, but duplicate values can be stored.

The Callable interface defines a task that returns a result and may throw an exception. It is declared in java.util concurrent package. The Future interface represents the value returned from an asynchronous computation.

The externalizable interface is a special type of interface that enables the user to define personalized rules and mechanisms for the purpose of serialization. Users can create their own contracts as well as protocols for serialization. This gives the power of deciding what to store and what not to store in the stream. Here, the user has to mention what they can put in the program as fields and variables explicitly.

Design a Least recently used cache implementation in java should have the below properties.

Bounded size: It should have a bounded size to take care of memory limits.

Fast access: It should be able to fetch or update entries faster.

Evict least recently used entry: Cache should evict least recently used entry if capacity is reached.

The enhancement of HashMap in Java is known as ConcurrentHashMap. Both of the Java classes have lots of similarities but ConcurrentHashMap works more effectively during runtime when you are trying to modify. One can also say that it is an alternative to HashMap in Java 1.5. It provides more safety and security in a multithreaded environment along with better performance. Unlike HashMap it doesn't lock the whole map instead it goes with a portion of the map. You can operate multiple threads without any complication on a single object. The Default level of concurrency in it is 16 and as per the concurrency level, there is a division of object into segments.

                 

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

Here is the simplest example of autoboxing:

Character ch = 'a';

There are mainly three types of database indexes, they are:

  • Clustered Indexing
  • Non-Clustered or Secondary Indexing
  • Multilevel Indexing

The services that are provided to EJB components by the EJB container are caching, concurrency, environment, memory replication, transaction, naming, persistence support, and clustering for the entity objects that live in the container.

The object of HttpSession used to view & manipulate information about the session - Servlets.

ErrorPage Attribute process specifies a JSP page that should process any exceptions thrown but not caught in the current page.

JavaFX is a platform upon which different desktop applications or software are created and delivered. It is also a rich Internet application in short RIAs running over many types of devices. JavaFX is designed to be a perfect replacement for Swing as the standard Graphics User Interface library for Java SE. Nevertheless, the two software platforms are part of the future. JavaFX supports Linux, Web browsers, macOS, desktop computers and Microsoft Windows. After the launching of JDK 11 in the year, 2018, this software platform is part of OpenJDK. The OpenJDK is open-source under the OpenJFX project.