Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What is hash-collision in HashTable and how to handle that in Java?

Answer»

What is HASH collision in HashTable and how to HANDLE that in Java?
In Java HashTable we have number of different KEYS and hash values. But when we two different keys with same has values then it will leads to hash-collision. And to handle this a bucket of TYPE linked list is used to hold the different keys of the same hash value.

2.

What do you mean by blank final variable in Java?

Answer»

What do you mean by blank FINAL variable in JAVA?
Whenever we will declare any of final variable with any value or we can SAY without initial value. Then it is called blank final variable.

3.

Which take more memory int or Integer?

Answer»

Which take more memory INT or INTEGER?
FIRST we will pick Integer as Integer is an object which will take more memory and it will also stores metadata overhead about the object. And on other hand int is a PRIMITIVE type and this will take less space.

4.

What do you mean by JavaFX?

Answer»

What do you MEAN by JavaFX?
JavaFX is one of the Java library which is used for both Desktop and Rich Internet Application(RIA). The application we will create in JavaFX will run on multiple platforms like Web, Mobile and Desktops. JavaFX is mainly used to replace swing in Java applications as a GUI framework. JavaFX have more flexible and have more functionalities than swing. JavaFX ALSO provides its own components and doesn't depend upon the OPERATING system like we have Swing. JavaFX is lightweight and hardware accelerated. And this will supports various operating systems including Windows, Linux and Mac OS.

5.

Write code to read a text file in Java as string?

Answer»

Write code to read a text FILE in JAVA as string?
Below is the code to read a file as string in Java here we use readAllBytes method here to read file. Below is the code for the same


Download Code
// Java Program to READING data from text file
package IO;
import java.nio.file.*;;
public CLASS ReadTextAsString
{
public static String readFileAsString(String fileName)throws Exception
{
String data = "";
data = new String(Files.readAllBytes(Paths.get(fileName)));
return data;
}

public static void main(String[] args) throws Exception
{
String data = readFileAsString("C: Users pankaj Desktop test.java");
System.out.println(data);
}
}


Read File Java

6.

What is output of increment Java Code?

Answer»

What is output of increment Java Code?
class incrementTest {
public static VOID main(String args[])
{
INT a=4;
System.out.print(++a * 5);
}
CHOOSE the correct option from below list
(1)20
(2)25
(3)30
(4)35

Answer:-(2)25
Note:-operator ++ has done before multiplication

7.

Can you define something about Variable Handles in Java with example?

Answer»

Can you define something about Variable HANDLES in JAVA with example?
Variable Handles is nothing but a variable or we can say its a reference to a SET of variables. And variable handle is just a typed reference to a variable. The variable can be an instance, array element or static field of the class. And this will include other components of a non-static fields, static field and outer array elements in the heap data structure. This will also means that Variable Handle is similar to the existing Method Handle. And to represent this we use java.lang.invoke.VarHandle class. And we will use java.lang.invoke.MethodHandles.Lookup static FACTORY method to create Variable Handle objects. And we can also used single element in the array, and byte[] array
to access. The VarHandle class provides write and read access to variables under specific conditions. VarHandles are immutable and have no VISIBLE state.


import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.Arrays;

public class VarHandleTest
{
public static void main(String args[])
{
VarHandle varTestHandle = MethodHandles.arrayElementVarHandle(int[].class);
int[] arrayTest = new int[5];
printArray(arrayTest);
varTestHandle.set(arrayTest, 2, 5);
printArray(arrayTest);
System.out.println(varTestHandle.get(arrayTest, 2));
}
private static void printArray(int[] arrayTest)
{
System.out.println(Arrays.toString(arrayTest));
}
}

8.

Can you define variable shadowing in Java with example?

Answer»

Can you define VARIABLE shadowing in JAVA with example?
If any of instance variable and the local VARIABLES have the common same name. And when we access this variable then it will print the local variable which will shadowing the instance variable. Below are the some reasons for considering a variable shadowing.
(i)When method is overridden in the child class.
(ii)When subclass declares a variable with same name of the parent class variable.
(III)There are some situation where a variable in local scope with same name as the name of a variable defined in and instance scope.
Below is the example for variable shadowing

public class testShadowing
{
string name="interviewqsn";
int subject=12;
public void displaylocal()
{
string name="crackyourinterview";
int subject=20;
System.out.println("Name:-" + name);
System.out.println("subject:-" + subject);
}
public static void main(String args[])
{
new testShadowing().displaylocal();
}
}


Output:-
Name:- crackyourinterview
subject:- 20

9.

Can you define something about the Final keyword in Java?

Answer»

Can you define something about the Final keyword in Java?
When we USED to restrict a class, VARIABLE and method we generally used Final keyword. We can also say final keyword as non-access specifier. Whenever we INITIALIZE any variable with final keyword we cannot modify its values. And one more thing is that when we declare a method as final it cannot be overridden by any of subclasses.

10.

Types of inner classes in Java Programming language?

Answer»

Types of inner classes in Java Programming language?
There are 3 MAIN types in inner classes in Java.
(1)Member Inner Class:-Member Inner class specifies a class within the class by using OUTSIDE method.
(2)ANONYMOUS Inner Class:-Here we use this for extending the class or specifying the implementation of interface.
(3)Local Inner Class:-Here we use this to create a class within the method.

11.

Can you just provide example of Garbage Collection in Java with code?

Answer»

Can you just provide example of Garbage Collection in JAVA with code?
To understand Garbage Collection in Java we used frrememory METHOD. First we print FREE memory in Java available and after that we use garbage collection by using gc method of RunTime class. And freeMemory method return the amount of memory free in JVM. Here in below example we use getRunTime method is used to get the REFERENCE of current RunTime object.

import java.util.*;
class GCClass
{
public static void main(String s[]) throws Exception
{
Runtime RS = Runtime.getRuntime();
System.out.println("Freememory in JVM before GC = "+rs.freeMemory());
rs.gc();
System.out.println("Freememory in JVM after GC = "+rs.freeMemory());
}
}

Output:-

Freememory in JVM before GC=1234
Freememory in JVM after GC=1567

12.

What do you know about Serializable Interface in Java?

Answer»

What do you know about Serializable Interface in Java?
Serializable Interface in Java present in java.io.package. And it is a marker interface. And we does not have methods and fields in Marker Interface. It provides run-time type INFORMATION about objects so compiler and JVM have additional information about the object. As no method is there classes that implementing it do not have to implement any methods. If we need Serialized and Deserialized of instance then we do this by implement this in classes. As we allready aware serialization is one of the mechanism of CONVERTING the state of an object into byte stream. And Serialization is done by using ObjectOutputStrem. And on other hand deserialization is just opposite PROCESS where byte stream is used to recreatse the actual java object in memory. Mehcnism is used to PERSIST the object. And deseialization is done by using ObjectInputStream.

13.

Do Java support global variable if yes or no give reasons?

Answer»

Do Java support global variable if yes or no give reasons?
My answer is No as Java doesnot support global VARIABLES. Below are the two main reasons why this is not supporting global variables.
(1)Global variables will CREATE a collisions in the namespace.
(2)And we use global variable then this will BREAK the referential TRANSPARENCY.

14.

What do you mean by checked and unchecked exception in Java?

Answer»

What do you mean by CHECKED and UNCHECKED exception in Java?
Below is the main DIFFERENCE between checked and unchecked exception in Java.
(1)Checked exception:-Checked exception is checked by compiler at compile time. And it is mandatory for any method to either handle checked exception or we can DECLARE them in throws clause. These are the ones which are a subclass of Exception but doesn t descend from RuntimeException.
(2)unchecked exception:-When we say about unchecked exception it is DESCENDANT of RuntimeException and not checked by the compiler at compile time.

15.

Difference between throw and throws in Java?

Answer»

Difference between throw and throws in Java?
Throw is mainly used to actually throw an instance of CLASS java.lang.Throwable. Which means we can throw ERROR and Exception both by using throw keyword. Below is the example to throw error:-

throw new IllegalArgumentException("Incorrect input values")

And when we talks about throws it is used as part of method declaration and signals which kind of exceptions are THROWN by this method so that its caller can handle them. And in throws we must declare any unhandled checked exception in throws CLAUSE in Java.

16.

Describe term Fail-Fast and Fail-Safe Iterators in Java?

Answer»

Describe term Fail-Fast and Fail-Safe ITERATORS in Java?
FIRST we should calrify that Iterators in Java are used to iterate over the collection of objects.
(1)Fail-Fast Iterators:- When there is any modification happen in STRUCTURAL of collection Fail-fast iterators immediately raise ConcurrentModificationException. Here structural modification means adding, removing any of the elemnt from collection while a thread is iterating over that collection. And for example we have ArrayList, HashMap classes are example of Fail-Fast Iterator.
(2)Fail-Safe Iterators:-On the other hand Fail-Safe iterators not throw any exceptions if any of collection is STRUCTURALLY modified while iterating over it. The reason behind is that it operate on the clone of collection not on the original collection. And that is the reason it is called Fail-Safe iterators. And for example we have CopyOnWriteArrayList, ConcurrentHashMap classes are EXAMPLES of fail-safe Iterator

17.

Six place where we can use this keyword in Java?

Answer»

Six place where we can use this keyword in Java?
Below are the six places where we can use this keyword in Java.
(1)this keyword can be USED which can RETURN the current class instance from the METHOD.
(2)we can use this keyword to refer to the current class instance variable.
(3)We can use this keyword to invoke current class method (implicitly)
(4)We can use this() keyword to invoke the current class constructor.
(5)We can use this keyword which can be passed as an ARGUMENT in the method call.
(6)We can use this keyword which can be passed as an argument in the constructor call.

18.

Main difference between Iterator and Enumeration

Answer»

Main difference between Iterator and Enumeration
Below are the FOUR main difference between Iterator and Enumeration
(1)Iterator:-Iterator in java is an interface which is found in java.util package.
(1)Enumeration:-On the other hand Enumeration is an object that will creates elements but one at a time.

(2)Iterator:-Here we uses three methods to interface
(i)hasNext()
(ii)next()
(III)remove()
(2)Enumeration:-Here we uses two methods
(i)hasMoreElements()
(ii)nextElement()

(3)Iterator:-Iterators allow removing elements from the GIVEN collection during the iteration with well-defined semantics.
(3)Enumeration:-It is USED for passing through a collection, usually of unknown size.

(4)Iterator:-Iterator method NAMES have been improved.
(4)Enumeration:-Here traversing of elements can only be done once per creation.

19.

What do you understand by loadfactor in HashMap in Java?

Answer»

What do you understand by loadfactor in HASHMAP in Java?
First we will say something about the HashMap then will go to loadfactor in HashMap. HashMap in Java has default initial CAPACITY and it is 16 and load factor here is 0.75 which means it is 12 out of 16. And Load factor of HashMap is level at which CAPCITY should be doubled. To understand this we will TAKE a EXAMPLE HashMap of capacity 16 and loadfactor is 0.75 so once 12th key value is used it will increase capacity to 32.

20.

Can we restrict inheritance for a class?

Answer»

Can we restrict inheritance for a class?
There are four things after that we can restrict inheritance for a class
(1)First is by USING the final keyword we can restrict inheritance.
(2)And another thing is that we can make all methods as final then we cannot OVERRIDE that
(3)Third is by using private constructors
(4)Fourth is by using the javadoc comments and to do that we USE("//")

21.

Which of the following is not a valid JavaScript variable name?

Answer»

Which of the FOLLOWING is not a valid JavaScript variable name?
Choose the correct OPTION from below list
(1)2names
(2)_first_and_last_names
(3)FirstAndLast
(4)None of the above

Answer:-(1)2names

22.

What is volatile variable and there purpose?

Answer»

What is volatile VARIABLE and there PURPOSE?
Volatile variables are those variables that always READ from the main memory and not from THREAD cache memory. And these are mainly used during syncronization.

23.

Six main step to create a RMI object in Java?

Answer»

Six main step to create a RMI object in JAVA?
Below are the Six main step to create RMI object in Java
(1)First we need to DEFINE a interface
(2)IN seconds step we need to implement the interface
(3)In third step we will compile the interface and their implementations with the java COMPILER
(4)Now in fourth step we will Compile server IMPLEMENTATION with RMI compiler
(5)Now in fifth step we run RMI registry
(6)In sixth and last step we will Run application

24.

Define Map and their types in Java?

Answer»

Define Map and their types in Java?
In Java Map is an OBJECT that helps to maps keys with values. And this will not contain duplicate keys and EVERY key is map to only ONE value. And to check the TWO keys are same or distinct we use a equals() method. There are four types of map in Java as given below:-
(1)HashMap:-HashMap is an unordered and UNSORTED map so it is a good choice when there is no emphasis on the order. A HashMap allows one null key and multiple null values and doesn't maintain any insertion order.
(2)HashTable:-This will not allow anything null and has methods that are synchro/nized. And it allows for thread safety and performance is very slow.
(3)LinkedhashMap:-It is much slower then HaspMap but it maintains inseration order and has a faster iteration.
(4)TreeMap:-A sorted Map providing support for constructing a sort order using a constructor.

25.

Create daemon thread and some instruction in Java?

Answer»

Create daemon thread and some instruction in Java?
To create daemon thread we use one of the class that setDaemon. Below are the 3 most usefull THINGS abou the daemon thread in Java.
(1)To create daemon thread in java we need to use class setDaemon. And we need to set setDaemon(TRUE) to create a thread.
(2)And this METHOD should be call before the start() method.
(3)If we didnot call this before start we will get illegalThreadStateException ERROR.

26.

Name the five most Memory Allocations available in Java?

Answer»

Name the five most Memory Allocations available in Java?
Below are the five most memory allocations vailable in Java
(1)Class Memory
(2)Heap Memory
(3)STACK Memory
(4)Program Counter-Memory
(5)NATIVE Method Stack Memory

27.

Difference between Java Platform and Other Platforms?

Answer»

Difference between Java Platform and Other Platforms?
Before going to difference between Java platform and other platforms we will first define what is PLATEFORM. Platform is Software or Hardware environment where our software or piece of software is executed. Now comes to difference between these TWO.
(1)Java is software BASED platform on the other hand other plateform may hardware or software based platforms.
(2)Java is executed on the TOP of other hardware platforms whereas other platforms can only have the hardware components.

28.

Difference between Object and Class

Answer»

Difference between Object and Class
Below are the 7 main differences between Object and classes
Object:-(1)Object is nothing but a instance of a class.
Class:-(1)We can say class as blueprint which HELPS to create objects.

Object:-(2)Example of objects are real world ENTITY such as chair, pen, tables and laptops etc.
Class:-(2)In class there is a group of similar objects.

Object:-(3)Object have the physical entity.
Class:-(3)On the other hand class have logical entity.

Object:-(4)We can create objects any times as per requirement.
Class:-(4)On the other hand Class is declared only once.

Object:-(5)We got allocation of memory once we create the objects.
Class:-(5)On the other hand allocation of memory is not done at the creation of class.

Object:-(6)To create object we use new keyword and example of this is
STUDENT stobj=new Student();
Class:-(6)To declare class we use keyword class for example
class Student{}

Object:-(7)There are so many ways to create object in java as given below:-
(i)New Keyword
(ii)newinstance() method
(III)clone() method
(iv)deserialiation
Class:-(7)To create class there is only one way to define that by using class keyword

29.

What are the main difference between Array and Arraylist

Answer»

What are the main difference between Array and Arraylist
Below are the five main difference between Array and Arraylist
(1)In array we need to define the size at the TIME of declaration and on another hand we can change the size DYNAMICALLY.
(2)Arrays can contains both primitive data types as WELL as objects on the other hand Arraylist can have only objects and not allowed any primitive data types.
(3)Arrays are not type parameterized on the other hand Arraylists are type
(4)When we need to add data in array we must specifiy the index on ther hand we donot need and index in arraylist
(5)In array we cannot insert values of different data types on another hand we can store different data types in arraylist.

30.

What is classloader in Java and name of some classloader

Answer»

What is CLASSLOADER in JAVA and name of some classloader
We can say Java ClassLoader is subset of JVM which is RESPONSIBLE for loading class files. When we execute a Java program classloader will load file FIRST. There are below 3 built-in classloaders:-
(1)Bootstrap ClassLoader
(2)Extension ClassLoader
(3)SYstem or APPLICATION ClassLoader

31.

Different modules of the Spring framework

Answer»

Different modules of the Spring FRAMEWORK
Below are the list of some important Spring Framework modules
(1)Spring Context It is basicaly USED for dependency INJECTION.
(2)Spring AOP We use AOP for aspect ORIENTED programming.
(3)Spring DAO We basicaly used this for database operations using DAO pattern
(4)Spring JDBC This is for JDBC and DataSource support.
(5)Spring ORM ORM tools support such as Hibernate
(6)Spring Web Module We use Web module for creating web applications.
(7)Spring MVC We use Model-View-Controller implementation for creating web applications, web services etc.

32.

Tell something about WORA nature of Java?

Answer»

Tell SOMETHING about WORA nature of JAVA?
First we will know WORA full form it is "Write Once Run Anywhere". As we knows java is compiled to bytecode which is not platform specific. And this bytecode is the intermediate language between the SOURCE code and MACHINE code. As we knows that bytecode is not platform specific so it can be used to any platform. So its said that Java is of WORA nature.

33.

Most important 15 aspects that Java Developer should learn in year 2020

Answer»

Most important 15 aspects that Java Developer should learn in year 2020
Below are list of 15 most important aspects for Java Developer
(1)DevOps
(2)Git
(3)Java 9-14
(4)Spring Framework 5
(5)Unit testing
(6)RESTful Web Service
(7)Spring Security 5.0
(8)Spring Boot 2
(9)Angular 2+ or React JS
(10)Android
(11)Apache Spark and Kafka
(12)Docker and KUBERNETES
(13)MICROSERVICES
(14)CLOUD (AWS)
(15)Concurrency

34.

In Java how can we create immutable class?

Answer»

In Java how can we create immutable class?
Below are the some important FACTS about how to create immutable class
(1)FIRST we declare class as final so that it cannot be extended.
(2)Second we create all fields as private so that direct access is not allowed.
(3)Third we will not provide setter methods for variables.
(4)Fourth create all mutable fields final so that its value can be assigned only once.
(5)Fifth we will initialize all fields via a constructor performing the DEEP copy.
(6)Sixth we will Perform cloning of objects in the getter methods to return a copy rather than returning the ACTUAL object reference.

35.

How to Get size of Array and ArrayList in Java Programming?

Answer»

How to Get size of Array and ArrayList in Java Programming?
There are certain property from which we can get the size for both array and for arraylist.
(1)Array:- To get the LENGTH of an array we can use length keyword to get the size.

int[] array=new int[4]
System.out.println("Size of Array is" +array.legth);


(2)ArrayList:-To get the size of Array LIST we use keyword size to get the size. And below list the syntax

ArrayList alist=new ArrayList();
alist.add("crackyourinterview");
alist.add("DOTNET");
alist.add("sqlserver");
System.out.Println("Size of ArrayList" +alist.size());