Java Interview Questions-Core Faq - 2

How to get the current time in milliseconds?

You can get the current time in milliseconds in Java using System.currentTimeMillis() method. It returns the current time in milliseconds since January 1, 1970 (UNIX epoch time).

Difference between a static and a non-static inner class?

A static inner class is associated with its outer class and does not hold an implicit reference to the outer class instance. A non-static inner class, also known as an inner class, holds an implicit reference to its outer class instance and can access its members.

Difference between a break statement and a continue statement?

The break statement is used to terminate the current loop or switch statement and exit the block. The continue statement, on the other hand, skips the rest of the loop's body for the current iteration and moves to the next iteration.

Difference between a while statement and a do-while statement?

The while statement tests the loop condition before executing the loop body, so it may not execute the loop at all if the condition is initially false. The do-while statement executes the loop body first and then tests the condition, ensuring the loop body is executed at least once.

Difference between an if statement and a switch statement?

The if statement allows you to test multiple conditions using various if, else if, and else blocks, providing more flexibility in handling different cases. The switch statement is used when you have a single expression to test against multiple constant values, making the code more concise and readable.

Difference between Executor.submit() and Executor.execute() method?

Executor.submit() method is used to submit a task for execution and returns a Future representing the task's result. Executor.execute() method is used to submit a task for execution but does not return any result, making it suitable for Runnable tasks.

Difference between the prefix and postfix forms of the ++ operator?

In the prefix form (++i), the value is incremented first, and then the updated value is used in the expression. In the postfix form (i++), the current value is used in the expression first, and then the value is incremented.

Which class is the superclass of all classes in Java?

The Object class is the superclass of all classes in Java. Every class in Java implicitly or explicitly inherits from the Object class.

What is object cloning?

Object cloning in Java is the process of creating a copy of an object with the same state. It is achieved by implementing the Cloneable interface and overriding the clone() method, allowing objects to be duplicated independently.

Do I need to import java.lang package any time? Why?

No, you do not need to import the java.lang package explicitly in Java because it is automatically imported by the compiler for all Java programs. The classes in the java.lang package, such as String and Object, are fundamental and widely used, and the import is handled automatically.

What is a native method?

A native method in Java is a method that is implemented in a language other than Java, such as C or C++. It is used to access platform-specific or low-level operations, and it is declared with the native keyword and defined outside the Java code.

Is there any difference between nested classes and inner classes?

In Java, nested classes are classes defined inside another class, and they can be static or non-static. Inner classes are a specific type of nested class that is non-static and has access to the enclosing class's instance variables.

What is Locale?

Locale in Java is a representation of a specific geographical, political, or cultural region. It is used to customize the behavior of programs to suit the conventions and formats of a particular locale, such as language, date, time, and currency formats.

Can an Interface implement another Interface?

Yes, an interface can extend another interface in Java using the extends keyword. This allows the sub-interface to inherit the abstract methods from the parent interface and add more methods of its own.

Can a Class extend more than one Class?

No, Java does not support multiple inheritance for classes. A class can only extend a single class, but it can implement multiple interfaces to achieve a form of multiple inheritance through interfaces.

Can a class be defined inside an Interface?

Yes, starting from Java 9, a class can be defined inside an interface as a nested class or a static nested class. Prior to Java 9, classes were not allowed inside interfaces.

Can an Interface be defined inside a class?

Yes, an interface can be defined inside a class as a nested interface. Nested interfaces are implicitly static, meaning they are not tied to an instance of the outer class.

Can we define private and protected modifiers for variables in interfaces?

Yes, starting from Java 9, interfaces can have private and private static methods as well as private, private static, and protected variables. These modifiers help to organize the code and hide implementation details within the interface.

When does the compiler supply a default constructor for a class?

The compiler supplies a default constructor for a class if the class does not explicitly define any constructors. It is provided automatically when no constructor is present, and it initializes member variables with default values.

What are the various access specifiers for Java classes?

The various access specifiers for Java classes are: public (accessible from any class), protected (accessible within the same package or by subclasses), default (package-private, accessible within the same package), and private (accessible only within the same class).

How can we make a copy of a Java object?

To make a copy of a Java object, you can use either the clone() method (by implementing Cloneable) or use libraries like Apache Commons BeanUtils or Gson to perform deep or shallow copying of objects.

How are destructors defined in Java?

Java does not have destructors like C++ or other languages. Instead, Java uses garbage collection to automatically reclaim memory when objects are no longer referenced or go out of scope.

Where does class, object, and reference variable get stored in Java?

Class definitions are stored in the method area of the Java Virtual Machine (JVM). Objects are created on the heap, and reference variables (pointers) that hold the memory address of objects are stored on the stack or in the method area, depending on the context.

What is the difference between == and equals() in comparing Java String objects?

The == operator compares the memory addresses of objects, while the equals() method compares the content of String objects for equality. For String comparison, it is generally recommended to use equals().

Can we create abstract classes without any abstract methods?

Yes, we can create abstract classes without any abstract methods. An abstract class can have regular methods (concrete methods) with implementations, along with abstract methods.

Can we have static methods in an interface?

Yes, starting from Java 8, interfaces can have static methods. These methods are associated with the interface and cannot be overridden by implementing classes.

Can you override a private or static method in Java?

No, private and static methods cannot be overridden in Java. Private methods are not accessible outside the class, and static methods are associated with the class itself, not with individual instances.

What is an efficient way to implement a singleton pattern in Java?

An efficient way to implement a singleton pattern in Java is by using the Enum singleton pattern. Enums in Java guarantee that only one instance is created for each enum constant, making it a safe and efficient approach for creating singletons.

What is the best way to determine the size of an object?

In Java, there is no direct way to determine the size of an object precisely. However, you can use tools like Java's Instrumentation API or third-party libraries like SizeOf to get an approximation of an object's size.

How do I test a class that has private methods, fields, or inner classes?

To test a class with private methods, fields, or inner classes, you can use reflection in your test code to access and invoke private members. However, it's generally recommended to test a class's public interface, ensuring that it behaves correctly and covers the private methods and inner classes implicitly.

Can a Byte object be cast to a double value?

Yes, a Byte object can be cast to a double value using the byteValue() method to get the byte value and then directly casting it to a double.

What is the purpose of the Process class in Java?

The Process class in Java is used to execute external processes and interact with them. It allows you to start a new process, obtain its output, and manage its input and error streams.

Last Updated : 28 July 2023