Protected: Infosys Interview Questions

This content is password protected. To view it please enter your password below:


if input as 121213131414 i need output as 111111223344 how can u write code?

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AscendingOrderNumbers {
    public static void main(String[] args) {

    private static void ascendingOrder(String numbers){
        List<Character> list = new ArrayList<>();
        for(char c:numbers.toCharArray())
        StringBuilder builder = new StringBuilder();
        for (char c:list){

Method Hiding In Java

Method hiding can be achived by overriding static metthod.

public class MethodHiding extends SuperMethod{
    static void foo(){
        System.out.println("I am sub class method");

    public static void main(String[] args) {;;
        SuperMethod superMethod = new SuperMethod();;
        SuperMethod superSub = new MethodHiding();;
        MethodHiding methodHiding = new MethodHiding();;
        SuperMethod superNull = null;;
        MethodHiding subNull = null;;


class SuperMethod {
    static void foo(){
        System.out.println("I am super class method");

In above code as 2 foo methods(from sub class and super class) are static methods. So we can call those method with class name as well as with references of it. Even reference are null still we didn’t get nullpointer exception.

Binary Search

Binary Search uses Divide and Conquer algorithm to search given element in a sorted array.


  1. Divide array into half
  2. Compare given value with middle element, if matches return index
  3. If the value is greater then the middle element then values is in the left array then repeat the process with the left array.
  4. If the value is less than the middle element then the value is in the right array then repeat the process with the right array.

Time complexity for binary search is O(logn).

Example :

Object class

Object class is present in the java.lang package. It is the super class for all the classes directly or indirectly. If a class is not extended any class then for the class Object class is super class. If a class extends an other class then for this class Object class is indirect super class.

Object class has 12 methods which given below.

  1. private static native void registerNatives(): It is a private method so we no need to consider it. It is implemented in the other language(C/C++).
  2. public final native Class<?> getClass(): It is a native method and implementation in other language(C/C++). Which is used for the getting the class level information. It also provides the meta data of a class. It is a final class and it can’t be overridden.Example:
  3. public native int hashCode(): It is a native method. For every object JVM generates a unique number which is different for different objects. This distinct number is generated by the hashcode. hashCode method returns the unsigned hex decimal integer. This number is formed by hashing the internal address of the object by using an algorithm. The main advantage of hashing is searching becomes easy. This method can be overridden, when it overridden we have to make sure that each object will have different hashcode.
  4. public boolean equals(Object obj): It is used for compare a object with ‘this’ object. By default implementation in Object class of this method is compare the given object reference with ‘this’ object reference (Not contents of the object).  It is recommended that whenever this method is overridden then override hashcode method for make sure same content objects has same hash code.
  5. protected native Object clone(): It is used for the make a copy of the object. If we want to make a copy of an object with using this method then class must implemented with Cloneable interface(Marker Interface) otherwise will get a CloneNotSupportedException.  By using clone method we can do 2 types of object copies. 1. Shallow cloning ( When we use it, it can copy only instance variables but not reference variables. i.e if we try to modify using this reference variables from copied object then actual object of that reference will be modified). 2. Deep cloning (When we use it, it can copy instance variables and also reference variable. i.e if we modify the data with reference variable from the copied object then it won’t modify the actual object.)
  6. public String toString(): It is used for converting the object to String. By default implementation of this method in Object class is to convert the object reference to String. i.e class name + @+ hex integer. Not actual value of object. It is recommended to override to get the content of object to convert String.
  7. public final void wait(): It is used in the multi-threaded programming. It keeps the current thread in sleep and release the lock until another thread enters the same monitor and calls notify.
  8. public final void wait(long timeout): It keeps the current thread in sleep for specified time and release the lock on it. Here time is milliseconds.
  9. public final void wait(long timout, int nanos): It keeps the current thread in sleep for specified milliseconds and nanoseconds and releases the lock on it.
  10. public final native void notify(): It wakes up(notify) one thread which is called wait on same object. notify method just wakes up the thread but not release the lock until its execution finished.
  11. public final native void notifyAll():It wakes up all threads which are called wait on same object. It also doesn’t release the lock until execution is finished.

Note: Above 5 methods (from 7 to 11) used in synchronized block only.

12.protected void finalize(): This method is called just before an object is garbage collected. It is called by the Garbage Collector on an object when garbage collector determines that there are no more references to the object. This method we need to override whenever we need to clean up the resources used in the object and also to avoid the memory leaks.


JVM(Java Virtual Machine): JVM is a part of JRE(Java Runtime Enviroment). Jvm is responsible for call the main method present in java code.

Image Source: Wikipedia.

When we compile .java file it will generate .class(Byte code) with same name. When we run it it will go to several steps, these steps together describes JVM.

Class Loader: Class loader loads the class information in the method area. Class loader responsibilities are below.

  1. Loading
  2. Linking
  3. Initialization

Loading: Class loader reads the .class file and generates the binary data then save it in the method area. It loads the following information in the method area for every .class file.

-> Fully qualified class name and its immediate super class name.

-> Whether .class file related to Class or Interface or Enum.

-> Modifiers, variables and method information etc.

After loading .class file JVM creates the object for the Class which is from java.lang package. This Class object represents the class loaded .class file. Using this object we can get the class level information of the loaded class. We can get Class object from the getClass method of Object class.

Linking: It performs verification, preparation and resolution.

-> It verifies the correctness of the .class file. i.e it checks the file format is proper or not and also is it compiled with proper compiler or not. If verification fails then we will get java.lang.VerifyError runtime exception.

-> It allocates the memory for the instance variables and assign them with their default values.

Initialization: In this phase all the static variables assigned with its values and static blocks if any from the top to button and from the parent class to child class.

Basic class loader are below.

  1. Bootstrap class loader: It is capable of loading trusted classes. It loads core java api classes which are present in the jre/lib directory. This directory popularly know as bootstrap path. Every JVM must implement bootstrap class loader. It is implemented in native languages like C/C++.
  2. Extension class loader: It is a child class loader of bootstrap class. It is responsible for loading the classes which is present in the jre/lib/ext directory or java.ext.dirs property set path. It is implemented by the sun.misc.Launcher$ExtClassLoader class.
  3. Application/System class loader: It is a child class loader of extension class loader which is responsible for load the class present in the application path. It is internally uses the class path(java.class.path). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

JVM follow Delegation-Hierarchy principle to load classes.

Image Source:

JVM Memory:

Method Area: In method area classes information will be stored like class name, immediate super class name, variables(including static variables) and method informations. Only one method area per JVM and it is shared memory.

Heap Memory: Information of all the objects will be present here. Only one heap area per JVM and it is shared memory.

Stack Memory: For every thread JVM creates a stack memory, here local variables of the method will be stored and when thread is terminated the corresponding stack memory will be cleared by the JVM. It is not a shared memory.

PC Registers: Each thread as its own pc registers and it holds the address of current execution of a thread.

Native Method Stack: It stores the native methods information. Every thread has its own native method stack.

Execution Engine: It reads the .class file(byte code) line by line and use the data or information present in different memory areas then executes the instructions.

Execution engine consists of 3 parts.

  1. Interpreter: It interprets the byte code line by line then executes. It needs more time when we call method multiple time for interpretation.
  2. Just In Time(JIT) compiler: It compiles the byte code and converts it into native code, so whenever interpreter requires multiple class then JIT provides native code to the interpreter so re-interpretation is not required. Because of it efficiency of interpreter will be improved.
  3. Garbage Collector: It is used for the clear the memory of object which are no longer used and their reference is referred to null.

Java Native Interface(JNI): It is a interface which interacts with the Native method libraries and it provides this libraries for execution.

Native Method Libraries: These are native libraries developed in C/C++ which are required for the execution engine.