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
- Java Interview Questions-Core Faq - 1
- Java Interview Questions-Core Faq - 3
- Features of Java Programming Language (2024)
- Difference between Java and JavaScript?
- What is the difference between JDK and JRE?
- What gives Java its 'write once and run anywhere' nature?
- What is JVM and is it platform independent?
- What is Just-In-Time (JIT) compiler?
- What is the garbage collector in Java?
- What is NullPointerException in Java
- Difference between Stack and Heap memory in Java
- How to set the maximum memory usage for JVM?
- What is numeric promotion?
- Generics in Java
- Static keyword in Java
- What are final variables in Java?
- How Do Annotations Work in Java?
- How do I use the ternary operator in Java?
- What is instanceof keyword in Java?
- How ClassLoader Works in Java?
- What are fail-safe and fail-fast Iterators in Java
- What are method references in Java?
- "Cannot Find Symbol" compile error
- Difference between system.gc() and runtime.gc()
- How to convert TimeStamp to Date in Java?
- Does garbage collection guarantee that a program will not run out of memory?
- How setting an Object to null help Garbage Collection?
- How do objects become eligible for garbage collection?
- How to calculate date difference in Java
- Difference between Path and Classpath in Java
- Is Java "pass-by-reference" or "pass-by-value"?
- Difference between static and nonstatic methods java
- Why Java does not support pointers?
- What is a package in Java?
- What are wrapper classes in Java?
- What is singleton class in Java?
- Difference between Java Local Variable, Instance Variable and a Class Variable?
- Can a top level class be private or protected in Java
- Are Polymorphism , Overloading and Overriding similar concepts?
- Locking Mechanism in Java
- Why Multiple Inheritance is Not Supported in Java
- Why Java is not a pure Object Oriented language?
- Static class in Java
- Difference between Abstract class and Interface in Java
- Why do I need to override the equals and hashCode methods in Java?
- Why does Java not support operator overloading?
- Anonymous Classes in Java
- Static Vs Dynamic class loading in Java
- Why am I getting a NoClassDefFoundError in Java?
- How to Generate Random Number in Java
- What's the meaning of System.out.println in Java?
- What is the purpose of Runtime and System class in Java?
- The finally Block in Java
- Difference between final, finally and finalize
- What is try-with-resources in java?
- What is a stacktrace?
- Why String is immutable in Java ?
- What are different ways to create a string object in Java?
- Difference between String and StringBuffer/StringBuilder in Java
- Difference between creating String as new() and literal | Java
- How do I convert String to Date object in Java?
- How do I create a Java string from the contents of a file?
- What actually causes a StackOverflow error in Java?
- Why is char[] preferred over String for storage of password in Java
- What is I/O Filter and how do I use it in Java?
- Serialization and Deserialization in Java
- Understanding transient variables in Java
- What is Externalizable in Java?
- What is the purpose of serialization/deserialization in Java?
- What is the Difference between byte stream and Character streams
- How to append text to an existing file in Java
- How to convert InputStream object to a String in Java
- What is the difference between Reader and InputStream in Java
- Introduction to Java threads
- Synchronization in Java
- Static synchronization Vs non static synchronization in Java
- Deadlock in Java with Examples
- What is Daemon thread in Java
- Implement Runnable vs Extend Thread in Java
- What is the volatile keyword in Java
- What are the basic interfaces of Java Collections Framework
- Difference between ArrayList and Vector | Java
- What is the difference between ArrayList and LinkedList?
- What is the difference between List and Set in Java
- Difference between HashSet and HashMap in Java
- Difference between HashMap and Hashtable in Java?
- How does the hashCode() method of java works?
- Difference between capacity() and size() of Vector in Java
- What is a Java ClassNotFoundException?
- How to fix java.lang.UnsupportedClassVersionError