Frequently Asked Java Interview Questions

Software Development | Design, Digital, Technology | Oct 23,2021 | By Nandini S

Java is an object-oriented programming language. It is the most extensively used programming language for Android development. It may also be used to develop desktop applications, web applications, and web services. As a consequence, we've developed a list of the most important Java Interview Questions you'll need to get your dream job in the IT industry.

1.How can I determine the length of a string?

ANS:The length() method in Java may be used to determine the length of strings.

2.In Java, how can we sort a list of elements?

ANS:To sort the items, use the built-in sorting function sort(). Although we may create our own custom functions, it is recommended that we utilise the built-in function because it is highly efficient.

3.Is it possible to make a Java array volatile?

ANS:Yes, you may make a reference to an array volatile in Java, but only the reference, not the entire array. What I mean is that if one thread changes the reference variable to point to another array, the volatile guarantee will be supplied, but if several threads modify individual array items, the volatile guarantee will not be provided.

4.Is it possible to store a double value in a long variable without having to cast it?

ANS:No, you can't put a double value in a long variable without casting since double's range is too short, therefore we have to type cast. It's not difficult to answer this issue, but many developers get it incorrect because they're unsure which is larger in Java: double or long.

5.Which takes up more memory: an int or an Integer?

ANS:Because an integer is an object that holds metadata about the object, it will take up more memory, whereas int is a primitive type that takes up less space.

6.What's the difference between a top-level static class and a nested static class?

ANS:A public top-level class must have the same name as the source file; however, a nested static class has no such requirement. A nested class is always contained within a top-level class, and you must refer to the nested static class using the top-level class's name, such as HashMap. Where HashMap is a top-level class and Entry is a nested, static class, Entry is a nested, static class.

7.What is the final keyword's purpose?

ANS:In Java, the final keyword is used to declare an entity's final state. The entity's value cannot be changed at a later point in the program. A variable, class, object, or other thing can be used as the entity. It's used to keep a Java program from being modified unnecessarily.

8.What is the distinction between deep and shallow copy?

ANS:In Java, a shallow copy transfers all of an object's values and attributes to another object, with both objects referencing the same memory locations Deep copy is the generation of an object having the same values and characteristics as the copied object, but with different memory locations.

9.What is the point of using the default function Object() { [native code] }?

ANS:The default function Object() { [native code] } is a function Object() { [native code] } that is invoked when a class's object is defined. The default function Object() { [native code] } does not have any parameters. The initialization of class variables is a common usage of default constructors.

10.What is the definition of object cloning?

ANS:The technique of generating an exact replica of a class's object is known as object cloning. The newly generated object has the same state as the object that was cloned. Cloning objects is done with the clone() function. A deep copy is an example of cloning done with the clone technique.

11.What is the purpose of static blocks?

ANS:They are primarily responsible for initialising the static variables. If there are several static blocks, they are performed in the order in which they were written, in a top-down fashion.

12.In Java, what is the purpose of this keyword?

ANS:In Java, this term is used to refer to an entity with the current object. It's a multi-purpose term that may be used for a variety of purposes.

13.In Java, what's the difference between String and String Builder?

ANS:The string Builder class is changeable, whereas strings are immutable. Also synchronised is the string builder class.

14.How can you figure out how big something is?

ANS:The size of an object may be determined by adding the sizes of the variables in the class from which it was created. If a class has an integer or a double variable, the size of the class's object equals size(int)+size(double) (double). If there is an array, the object's size is equal to the length of the array*the size of the array's data type.

15.I'm not sure what the distinction is between == and.equals().

ANS:equals() is a function, whereas "==" is an operator. "==" determines whether the references are in the same location, whereas.equals() determines whether the object values are the same on evaluation.

16.In Java, what is serialisation?

ANS:Object serialisation is the process of converting an object's state into a byte stream that may be saved to disk/file or delivered over the network to any other Java virtual machine that is operating. Deserialization is the process of generating an object from a byte stream in reverse.

17.In Java, what is spring?

ANS:The Spring Framework is a Java platform application framework and inversion of control container. Any Java programme may utilise the framework's fundamental capabilities, however there are enhancements for creating web applications on top of the Java EE (Enterprise Edition) platform.

18.In Java, how do you make an immutable class?

ANS:Declare the class final to prevent it from being extended. Make all fields private so that no one may access them directly. Variables should not have setter methods. Make all changeable fields final, allowing just one value to be applied to them. Create a function Object() { [native code] } that does a deep copy and initialises all of the fields. In the getter methods, perform object cloning to return a clone rather than the real object reference.

19.In Java, what is the xname class?

ANS:A (discretionary) namespace name and a neighbouring name combine to form an Expanded Name. Examples of XName are timeless and can be shared.

20.Can non-static variables be referenced by static methods?

ANS:Non-static variables can be referenced via static methods. It is possible to accomplish so by constructing an object of the variable's class.

A2N Academy is one of the world's most prominent IT colleges, providing the best training to all students. Mock interviews, placement assistance, and free opportunities are all provided. Enroll today in their most popular Java course!

Interested in working with IT companies?

Speak with us today

Do you have career gap?


Are you planning to shift your career?


captcha