Core Java Interview Questions- Part 7

Core Java Interview Questions- Part 7
Java is a strong and flexible language that is used to build websites, apps, games, and software. If you’re preparing for a Java interview, you must be prepared with the core Java topics. As employers prefer people who know the basics of Java and advanced topics when hiring for Java positions, it’s important to go over common interview questions and practice your answers.
In this guide, you’ll find questions that commonly come in Java interviews. These questions cover topics like object-oriented programming, exception handling, multithreading, collections, and more. Each question comes with an answer so that you feel confident while answering during your interview.

Whether you are a beginner or have some experience in programming, this page will help you brush up on Java concepts and feel confident in your next interview.

Answer:

Java possesses a built-in garbage collection mechanism that uses GC algorithms to manage the memory automatically. It finds unused objects & deletes them to free up memory.

Answer:

Yes, there can be two main methods in a class. It means that the main method is overloaded, but during execution time, JVM only calls the original main method & not the overloaded main method.

Answer:

No, the private variable cannot be inherited as a private member’s scope is only limited to a class in which it is defined. Only public members are inherited & the subclass cannot inherit the private members of its parent class.

Answer:

It is impossible to increase the size of an array in Java after the declaration, which is a limitation of Java arrays.

Answer:

Java supports eight primitive data types, including short, int, float, byte, long, char, double, & Boolean.

Answer:

There are two different ways for finding ASCII value of character, i.e.:

  • Assign the character in Java to an integer variable
  • Typecast the character to an integer

Answer:

You can use the Scanner class with the InputStream, System.in, to read a string from the console as input in the Java applications.

Answer:

One can use the built-in sorting utility sort() method in Java to sort the elements. Else, you can also write custom functions, but it is prudent to use the built-in function as it is highly optimized.

Answer:

You cannot store a double value in a long type variable without casting as the range of double is greater than that of a long variable, which is why we need to typecast.

Answer:

An Integer is an object that takes 16 bytes or 128 bits to store. However, int is a primitive data type & takes 4 bytes or 32 bits to store.

Answer:

A top-level class must have the same name as a source file’s name, while there is no such requirement for the nested static class. A nested class is inside the top-level class & you need to use the name of a top-level class to refer to the nested static class.

Answer:

The final keyword is useful for declaring the final state of an entity. The value of an entity cannot be modified at a later stage in Java application. It helps to prevent unnecessary modifications in Java applications.

Answer:

Shallow copy in Java, copies all attributes & values of an object to another object, and both objects reference the same memory locations. Deep copy refers to creating an object with the same attributes & values of an object, but both objects reference has different memory locations.

Answer:

Object cloning refers to the process of creating an exact copy of a class’s object. The state of a newly created object is exactly the same as an object used for cloning. The clone() method is useful for cloning objects.

Answer:

Static block helps to initialize static variables. It is executed when the class is loaded in the memory. A class can have several Static blocks that will execute in the same sequence in which they were written in the program.

Answer:

A StringBuilder is mutable in contrast to a String, which is immutable. An immutable object is that object whose content is not changeable after it has been created.

Answer:

An Expanded Name consists of a namespace name (optional) & a nearby or local name. XName represents a name of an XML attribute & does not contain a public constructor.

Answer:

No, the static method cannot access non-static variables & methods. The static method can only access static data members & static methods of another class or the same class.

Answer:

Multiple static blocks get executed in a sequence in which they were written in a top-down manner. Firstly, the top blocks are executed, and subsequently, other blocks are executed.