Android Interview Question: Part-2

1.What is JIT?

The Just-In-Time (JIT) compiler is a key component of the OpenJ9 VM that improves the performance of Java applications by compiling platform-neutral Java bytecode into native machine code at run time. Without the JIT, the VM has to interpret the bytecodes itself — a process that requires extra CPU and memory.

The JIT compiler doesn’t compile every method that gets called because thousands of methods can be called at startup. Instead, OpenJ9 records the number of times a method is called. When the count reaches a pre-defined invocation threshold, JIT compilation is triggered. Once a method has been compiled by the JIT, the VM can call the compiled method rather than interpreting it.

Java JIT Compiler — Overview

The Just-In-Time compiler is one of the integral parts of the Java Runtime Environment. It is mainly responsible for performance optimization of Java-based applications at run time or execution time. In general, the main motto of the compiler is increasing the performance of an application for the end user and the application developer.

Deep Dive into JIT in Java

  • Byte code is the chief potential of Java’s WORA (Write once, run anywhere) environment. Speed of the Java application depends on the way the byte code gets converted to the native machine code. The bytecode can either be interpreted or compiled to native code or directly executed on the processor. But, if the bytecode is interpreted, it directly affects the speed of the application.
  • In order to speed up the performance, JIT compiler communicates with JVM at the execution time in order to compile byte code sequences into native machine code. Basically, when using JIT Compiler, the native code is easily executed by the hardware when compared to JVM Interpreter. By doing so, there will be a huge gain in execution speed.
  • When the JIT Compiler compiles the series of byte code, it also performs certain optimizations such as data analysis, translation from stack operations to register operations, eliminating subexpressions, etc. This makes Java very efficient when it comes to execution and performance.

Working of JIT Compiler in Java

The JIT Compiler speeds up the performance of the Java applications at run time. As Java is an object-oriented approach, it comprises of classes and objects. Basically, it constitutes a byte code that is platform independent and executed by the JVM across diversified architectures.

Work Flow:

Below diagram depicts how the actual working of compilation takes place in Java Runtime Environment.

  1. When you code the Java Program, JRE uses javac compiler to compile the high-level Source code to byte code. After this, JVM loads the byte code at run time and converts into machine level binary code for further execution using Interpreter.
  2. As I have already mentioned above, interpretation of Java byte code reduces the performance when compared to a native application. That’s where JIT Compiler aids to boost up the performance by compiling the byte code into native machine code “just-in-time” to run.
  3. The JIT Compiler is activated and enabled by default when a method is invoked in Java. When a method is compiled, Java Virtual Machine invokes the compiled code of the method directly without interpreting it. Hence, it does not require much memory usage and processor time. That basically speeds up the performance of the Java Native Application.

2. What is StringPool?

String Pool is a storage area in Java heap.

String allocation, like all object allocation, proves to be a costly affair in both the cases of time and memory. The JVM performs some steps while initializing string literals to increase performance and decrease memory overhead. To decrease the number of String objects created in the JVM, the String class keeps a pool of strings.

Each time a string literal is created, the JVM checks the string literal pool first. If the string already exists in the string pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object initializes and is placed in the pool.

3. What is the linear data structure and How it differs from a non-linear data structure?

Linear Data Structures

A Linear data structure has data elements arranged in a sequential manner and each member element is connected to its previous and next element. This connection helps to traverse a linear data structure in a single level and in a single run. Such data structures are easy to implement as computer memory is also sequential. Examples of linear data structures are List, Queue, Stack, Array, etc.

Non-linear Data Structures

A non-linear data structure has no set sequence of connecting all its elements and each element can have multiple paths to connect to other elements. Such data structures support multi-level storage and often cannot be traversed in a single run. Such data structures are not easy to implement but are more efficient in utilizing computer memory. Examples of non-linear data structures are Tree, BST, Graphs, etc.

4. Why java does not support pointer?

Most studies agree that pointers are one of the primary features that enable developers to inject bugs into their code. When Java was created, the intention was to create a language that is easy to learn and not prone to the bugs that C++ is prone to. It’s not like c/c++ where we have to manage the memory management by destructors. In java, automatic Garbage Collector works for memory management. Actually, Java references are pointers so everything in Java is accessed only through pointers.

Some reasons for Java does not support Pointers:

1. Memory access via pointer arithmetic: this is fundamentally unsafe. Java has a robust security model and disallows pointer arithmetic for the same reason. It would be impossible for the Virtual Machine to ensure that code containing pointer arithmetic is safe without expensive runtime checks.

2. Security: By not allowing pointers, Java effectively provides another level of abstraction to the developer. No pointer support make Java more secure because they point to a memory location or used for memory management that loses the security as we use them directly.

3. Passing argument by reference: Passing a reference that allows you to change the value of a variable in the caller’s scope. Java doesn’t have this, but it’s a pretty rare use case and can easily be done in other ways. This is in general equivalent to changing a field in an object scope that both the caller and callee can see.

4. Manual memory management: you can use pointers to manually control and allocate blocks of memory. This is useful for some bigger applications like games, device drivers, etc. but for general-purpose Object Oriented programming it is simply not worth the effort. Java instead provides very good automatic Garbage Collection (GC) which takes care of memory management.

5. How abstract any method in java or how to achieve Abstraction in java?

An abstract class is declared by using the abstract keyword. These classes cannot be instantiated, but they can be extended into subclasses or derived classes.

An abstract class cannot be directly instantiated using new operator, because an abstract class is not defined.

Abstract classes are those classes which contain at least one abstract method. It means if any class contains abstract function then it should be declared as abstract class. That is an abstract class can contain both abstract and non-abstract methods.

Properties of Abstract class

  1. Abstract class contains abstract methods.
  2. Abstract class cannot be instantiated.
  3. Abstract class can contain a mixture of abstract and nonabstract methods.
  4. To use abstract class one has to inherit it from another class.
  5. If the program contains an abstract method then it must implement all the abstract method of abstract class

6. Private Constructors and Singleton Classes in Java?

As you can easily guess, like any method we can provide access specifier to the constructor. If it’s made private, then it can only be accessed inside the class.

Do we need such ‘private constructors ‘?

There are various scenarios where we can use private constructors. The major ones are

  1. Internal Constructor chaining
  2. Singleton class design pattern

What is a Singleton class?

As the name implies, a class is said to be singleton if it limits the number of objects of that class to one.

We can’t have more than a single object for such classes.

Singleton classes are employed extensively in concepts like Networking and Database Connectivity.

Design Pattern of Singleton classes:

The constructor of the singleton class would be private so there must be another way to get the instance of that class. This problem is resolved using a class member instance and a factory method to return the class member.

7. What is clonable to and how to clone object?

The object cloning is a way to create an exact copy of an object. The clone() method of Object class is used to clone an object.

The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don’t implement Cloneable interface, clone() method generates CloneNotSupportedException.

The clone() method is defined in the Object class. Syntax of the clone() method is as follows:

  1. protected Object clone() throws CloneNotSupportedException

Why use clone() method ?

The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing time to be performed that is why we use object cloning.

Advantage of Object cloning

Although Object.clone() has some design issues it is still a popular and easy way of copying objects. Following is a list of advantages of using clone() method:

  • You don’t need to write lengthy and repetitive codes. Just use an abstract class with a 4- or 5-line long clone() method.
  • It is the easiest and most efficient way of copying objects, especially if we are applying it to an already developed or an old project. Just define a parent class, implement Cloneable in it, provide the definition of the clone() method, and the task will be done.
  • Clone() is the fastest way to copy the array.

Disadvantage of Object cloning

Following is a list of some disadvantages of clone() method:

  • To use the Object.clone() method, we have to change a lot of syntaxes to our code, like implementing a Cloneable interface, defining the clone() method and handling CloneNotSupportedException, and finally, calling Object.clone(), etc.
  • We have to implement cloneable interface while it doesn’t have any methods in it. We just have to use it to tell the JVM that we can perform clone() on our object.
  • Object.clone() is protected, so we have to provide our own clone() and indirectly call Object.clone() from it.
  • Object.clone() doesn’t invoke any constructor so we don’t have any control over object construction.
  • If you want to write a clone method in a child class then all of its superclasses should define the clone() method in them or inherit it from another parent class. Otherwise, the super.clone() chain will fail.
  • Object.clone() supports only shallow copying but we will need to override it if we need deep cloning.

Does the clone object and the original object point to the same location in memory

The answer is no. The cloned object has its own space in the memory where it copies the content of the original object. That’s why when we change the content of the original object after cloning, the changes do not reflect in the clone object.

8. Diff between Comparator and Comparable?

What is Comparable?

A comparable object has the capability of comparing itself with another object. The class should implement the java.lang.Comparable interface in order to compare its instances.

It also helps you to sort the list of custom objects.

Array of objects implementing a comparable interface is sorted automatically by, Arrays.sort and Collections.sort method.

What is Comparator?

Comparator interface is used to arrange the objects of user-defined classes. It is capable of comparing two objects of two different classes. It includes two important methods known as compare (Object obj1, Object obj2) and equals (Object element).


  • Comparable provides compareTo() method to sort elements in Java whereas Comparator provides compare() method to sort elements in Java.
  • Comparable interface is present in java.lang package whereas Comparator interface is present in java.util package.
  • Comparable provides single sorting sequences while Comparator provides multiple sorting sequences.
  • Comparable affects the original class whereas comparator doesn’t affect the original class.

Method used in Comparable:

Following is an important method used in the Comparable interface:


CompareTo() method is used to perform natural sorting on string. The meaning of natural sorting is the sort order which applies on the object, e.g., numeric order for sorting integers, alphabetical order for String, etc.

The syntax of CompareTo() method is as follows:

int compareTo(T obj)

In the above syntax, T signifies the type of objects you are going to compare.

CompareTo() method compares the object with T obj.


  • It returns 0 if the values are equal.
  • In case, if the object has a lesser value, then this method returns a negative value.
  • If the object has a higher value, it returns a positive value.

Method used in Comparator:

Following are the important methods used in comparator interface:


Compare() enables you to order objects. To do this, you have to create a class that implements comparator interface. After this, you need to override it’s compare method.

The syntax of compare() method is as follows:

compare(Object obj1, Object obj2)

In the above syntax, obj1 and obj2 are two objects that you have to compare using compare() method.


  • It returns a negative integer if the first argument is less than the second one.
  • Returns zero if the first argument and second argument is equal.
  • This method can return a positive integer, in case the first argument is greater than the second.

You have to ensure that the relation is transitive. For example, ((compare(a, b)>0) && (compare(b, c)>0)) which implies compare(a, c)>0.

9. How to encapsulate data in java class?

Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates.Other way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield.

  • Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared.
  • As in encapsulation, the data in a class is hidden from other classes using the data hiding concept which is achieved by making the members or methods of class as private and the class is exposed to the end user or the world without providing any details behind implementation using the abstraction concept, so it is also known as combination of data-hiding and abstraction..
  • Encapsulation can be achieved by: Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.

Advantages of Encapsulation:

  • Data Hiding: The user will have no idea about the inner implementation of the class. It will not be visible to the user that how the class is storing values in the variables. He only knows that we are passing the values to a setter method and variables are getting initialized with that value.
  • Increased Flexibility: We can make the variables of the class as read-only or write-only depending on our requirement. If we wish to make the variables as read-only then we have to omit the setter methods like setName(), setAge() etc. from the above program or if we wish to make the variables as write-only then we have to omit the get methods like getName(), getAge() etc. from the above program
  • Reusability: Encapsulation also improves the re-usability and easy to change with new requirements.
  • Testing code is easy: Encapsulated code is easy to test for unit testing.

10. How abstraction achieve in java?

Data abstraction is a method where essential elements are displayed to the user and trivial elements are kept hidden.

In Java, abstraction is achieved by using the abstract keyword for classes and interfaces. In abstract classes, we can have abstract methods as well as concrete methods.

The abstract keyword is a non-access modifier, used for classes and methods:

  • Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
  • Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

An abstract class can have both abstract and regular methods:

11. How to set final keyword value to null or without assign?

A final variable in Java can be assigned a value only once, we can assign a value either in declaration or later.

final int i = 10;
i = 30; // Error because i is final.

A blank final variable in Java is a final variable that is not initialized during declaration. Below is a simple example of blank final.

// A simple blank final example 
final int i;
i = 30;

There are 3 ways to initialize a Java final variable:

  • You can initialize a final variable when it’s declared. This approach is the most common. A final variable is named blank final variable if it’s not initialized whereas declaration.
  • A blank final variable is initialized within instance-initializer block or within a constructor. If you have got one constructor in your class then it should initialize in all of them, otherwise, a compile-time error is thrown.
  • A blank final static variable is initialized within a static block.




Senior Software Engineer | Android | Java | Kotlin|Xamarin Native Android|Flutter

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Srivastava

Abhishek Srivastava

Senior Software Engineer | Android | Java | Kotlin|Xamarin Native Android|Flutter

More from Medium

Intent Android

Android DataStore with my POV for beginners’ help

ViewModel: What, Why & How?(Part-2)

[DevCTF-2022] Android WebSkills