Advance Java Interview Questions
1) What is Thread in Java?
A: The most basic question asked in a java interview is that what is thread? Well the simple answer to this is that a thread is basically a program in execution which is performing some specific task. There may be multiple threads running altogether. A single thread is independent in nature and does one specific task. The other thread is also independent and does their specific task. Both the threads run parallel and this is called multi-threading.
2) How do you convert string to int in Java?
A: Well converting string to int in java is very easy. one can use Integer.pardeInt() to convert a string to int.
3) How do you set a java Path?
A: We can set the path in java by using the following two ways
- Temporary – for setting a temporary path, firstly one need to open the command prompt then secondly he needs to copy the path of the JDK/bin directory. After that, he needs to write “set path=copied” path in command prompt.
- Permanent for setting the permanent path of JDK, go to my computer properties, then click on advanced tab, then click on environmental variables, then new tab for user variable, then write path in variable name, then write path of bin folder in variable value then click ok three times.
4) How to declare an array in java?
A: By following below three steps one can declare array in java- First, one must declare a variable of the desired type which will hold the array. Second, allocate the memory which will hold the array, create a new array and assign it to the array variable. Now you can store things in that array.
5) What is a constructor in Java?
A: When an instance of object is created, a special type of method is used to initialize the class. These are called constructors.
6) What is serialization in Java?
A: When we convert the state of an object into a byte stream then this mechanism is called serialization. Deserialization is just the opposite of this. i.e. when we use the byte stream to recreate the actual java object in memory then this mechanism is called deserialization.
7) What is synchronization in java?
A: The dictionary meaning of synchronization is the operation or activity of two or more things at the same time or rate. Similarly accessing and controlling multiple threads from any shared resources in java is called synchronization in java.
8) What is the garbage collector in java?
A: From the word garbage, it is pretty much clear that it is related to something which is now a waste and not in use. The garbage collector in java virtual machine helps in getting rid of the objects which are not being used by Java anymore.
9) What is an applet in java?
A: An applet is a small internet based programming language for the web written in java and can be downloaded by any computer. It runs in a web browser and usually embedded in an HTML page on the website.
10) How can you create immutable data in java?
By following the below steps one can easily create an immutable class in java:
- First, you need to declare the class as final so that no other class can extend it.
- Second, you need to make all the fields private to prevent allowing direct access.
- Third, don’t expose setter methods for variables.
- To prevent the assigning of value of mutable field more than once, mark the mutable field final.
- To prevent it from getting modified by clients afterwards, use a clone copy of the passed argument and avoid setting your mutable field to the real instance passed through the constructor.
- Perform a clone copy of the object in the getter method and never return the real object instance.
11. What is meant by Platform?
A: A Platform is defined as the software or hardware environment where a program is run. There are hardware-based platforms and software-based platforms, and JAVA gives a software-based platform.
12. Why is JAVA called platform independent?
A: JAVA provides software-based platform, but is itself platform independent. This means that JAVA programs can be run in any operating system irrespective of where it is written.
13. How are JVM, JRE, and JDK different?
A: JVM stands for JAVA Virtual Machine and provides the environment where JAVA codes are executed. Different JVMs exist for different platforms. JRE or Java Runtime Environment is the implementation of Java Virtual Machine. JDK or JAVA Development Kit is the actual software to create and run JAVA programs containing JRE and development tools.
14. What is meant by JIT Compiler?
A: The Just in Time Compiler or JIT Compiler is used by JAVA to improve execution performance by compiling parts of the program with similar functionality together.
15. What is meant by classloader?
A: Classloader is a JVM subsystem that loads classes and interfaces.
16. What is meant by class?
A: A class in JAVA is the blueprint for the creation of individual objects. It may contain methods and fields that define the object behaviour.
17. What is meant by Object?
A: A JAVA program entity with a state and behaviour is called an Object. It is the instance or result of a class. The state of an object is saved in fields, and its behaviour is expressed through methods.
18. What is meant by Local Variable?
A: Variables that are defined within methods, constructors, or blocks are Local Variables. They are declared and initialized in the method and exist as long as the method is not completed.
19. What is meant by Instance Variable?
A: Variables declared in a class but outside a method are called Instance Variables.
20. What is meant by a Class Variable?
A: Class variables or Static Variables are also declared in a class and outside a method, but they are declared with the keyword static.
21. What is meant by Constructor?
A: Constructor, like method, initializes the state of an object. They are invoked upon creation of a new object. Each class must have a constructor, and the compiler creates one if it is not defined by the programmer.
22. What is meant by access modifiers?
A: Access modifiers are parts of the code that specify the level of access for classes, methods, variables, and constructors. If access modifiers are not specified, the member has a default or package access.
23. What does the default constructor do?
A: The default constructor, created by the compiler in absence of any defined constructor supplies default values to objects.
24. What is meant by composition?
A: Keeping reference to a class in another class is called composition.
25. What is meant by exception?
A: Exceptions are any problems that may arise when a program is being executed.
26. What is meant by runtime exception?
A: Runtime exception is an exception that occurs due to a programmer’s error and could have been avoided, typing mistakes in the code, for example. These exceptions are ignored during compilation.
27. What is meant by Checked Exception?
A: Checked exception occurs due to a problem beyond the programmer’s control. For example, a file which needs to be opened could not be found. Such exceptions are not ignored during compilation.
28. What is meant by overloading?
A: Multiple methods in the same class nut with differing arguments is called overloading.
29. What is meant by overriding?
A: Overriding is an inheritance concept where two methods are defined with the same signature in both parent and child classes.
30. What is meant by JAVA Package?
A: Packaging is the method of organizing classes in JAVA by grouping them according to modules or specific logic of functionality.
31. What does the ‘finally’ keyword do?
A: The keyword ‘finally’ is applied to create a code block to follow a try block. The ‘finally’ code block executes always, with or without an exception occurring.
32. When is the keyword ‘super’ used?
A: When a method overrides another of its superclass, the method overridden can be called using ‘super’. It may also indicate a hidden field.
33. What is meant by Polymorphism in JAVA?
A: Polymorphism is defined as the ability of objects to exist in multiple forms as required. It is an integral property of Object Oriented Programming.
34. What is meant by Abstract Class?
A: Abstract Classes are classes without an instance and are implemented either partially or not at all. They contain methods declared without a body, called abstract methods.
35. What is meant by Interface?
A: An Interface is a simple group of abstract methods. When an interface is implemented by a class, the abstract methods are inherited by it.
36) How hashmap works initially in Java?
A: We all know that a Hashmap is a hash table based implementation of the map interface. A Hashmap produces an integer by performing a “hashing” calculation on the keys. It then uses that produced integer to find a bucket to get the value. It also determines the mapping of the bucket with the key/value pair by using the key’s hashcode.
37) What is hashcode in Java?
A: A hashcode is a number generated from the java object. This is the work of hashcode to allow objects to be stored or retrieved quickly in a Hashtable. The Hashcode is a 32-bit signed integer which is generally used for comparing objects and are not necessarily unique. Two same type of objects is said to be equal if they have the same type of hashcode.