Java Interview
Java Interview
Object-oriented programming
Platform-independence
Memory management
Exception handling
Multithreading
Security
Networking
Garbage collection
Java has two categories of data types: primitive and non-primitive. Primitive data
types include byte, short, int, long, float, double, char, and boolean. Non-primitive
data types include Strings, arrays, and classes.
5. What is a variable in Java?
A variable is a container that holds a value in Java. Variables are declared using a
data type and a name and can be assigned a value using an assignment operator.
Inheritance is a mechanism in Java that allows one class to inherit the properties and
methods of another class. The class that is being inherited from is called the
superclass or parent class, and the class that is inherited is called the subclass or
child class.
15. What is the difference between the final, finally, and finalize keywords in
Java?
The final keyword is used to declare a variable or method that cannot be changed or
overridden. The final keyword is used in a try-catch block to execute a block of code
regardless of whether an exception is thrown or not. The finalize() method is called
by the garbage collector before an object is removed from memory.
The JVM (Java Virtual Machine) is an abstract machine that provides a runtime
environment for Java programs. It interprets compiled Java code and executes it on
the host machine. The JVM provides features such as memory management,
garbage collection, security, and class loading.
18. What are some differences between the Comparable and Comparator
interfaces in Java?
The Comparable interface is used to define a natural ordering for a class, while the
Comparator interface allows you to define a custom order for a class.
The Comparable interface is implemented by the class itself, while the Comparator
interface is implemented by a separate class.
Annotations are a way of adding metadata to Java code. They provide additional
information about a class, method, field, or parameter. Annotations are used for
documentation, code analysis, and code generation.
Generics allow you to create classes, interfaces, and methods that can work with
different data types. They provide type safety and eliminate the need for casting.
Generics are implemented using parameterized types and type variables.
A stack is a last-in, first-out (LIFO) data structure, while a queue is a first-in, first-out
(FIFO) data structure. In Java, a Stack is implemented using the Stack class, while a
queue is implemented using the LinkedList class or the PriorityQueue class.
25. What use do the contracts for hashCode() and equals() serve?
Think about the HashMap object’s scenario. By distinctly identifying the key-value
combination in the map, we can infer that the Key of the HashMap employs the
hashCode() and equals() methods to retrieve the index or the value of a given key.
Two keys might yield the same outcome for these methods if they are not
implemented properly, which could lead to inaccurate results and the updating of the
value of the erroneous key when the method is updated. Therefore, it is crucial to
correctly implement the equals method and hashCode. If we adhere to the
hashCode-equals contract, this can be done correctly. The agreement specifies that:
If two objects are equal, then the hashCode method should produce the same result
for both objects. To ensure this, we have to override the hashCode() method
whenever we override the equals() method.
27. What is the difference between an interface and an abstract class in Java?
The “static” keyword is used to denote a variable or method that belongs to the class
rather than an instance of the class. Static variables and methods can be accessed
without creating an instance of the class.
30. What is the difference between a private and a protected method in Java?
A private method can only be accessed within the same class, while a protected
method can be accessed by any subclass of the class that defines the method.
The “final” keyword is used to denote a variable or method that cannot be modified
once it has been initialized.
The “this” keyword is used to refer to the current object instance. It helps to eliminate
the confusion between class attributes and parameters with the same name.
The “super” keyword is used to refer to the superclass of the current class. It can be
used to call a superclass constructor or method.
Set
Queue
Deque
Map
HashSet is an unordered Set that does not allow duplicates, while TreeSet is a
sorted Set that does not allow duplicates.
HashMap is an unordered Map that does not allow duplicates and allows one null
key, while TreeMap is a sorted Map that does not allow duplicates and does not
allow null keys.
Fail-fast iterators fail as soon as they detect that the collection has been modified
while the iterator is still iterating, while fail-safe iterators make a copy of the collection
before iterating and iterate over the copy instead.
Hashtable is a synchronized Map that does not allow null keys or values, while
HashMap is an unsynchronized Map that allows one null key and any number of null
values.
43. What is the purpose of the Comparable and Comparator interfaces in Java?
The Comparable interface is used to define a natural ordering for a class, while the
Comparator interface is used to define multiple sorting orders for a class.
Inheritance is a feature in Java that allows a class to inherit properties and methods
from another class. The class that inherits the properties and methods is called the
subclass or derived class, and the class that provides the properties and methods is
called the superclass or base class.
Abstraction is the process of hiding implementation details while showing only the
necessary information to the user. In Java, abstraction is achieved through abstract
classes and interfaces. Abstract classes are classes that cannot be instantiated but
can be extended, while interfaces are a collection of abstract methods that can be
implemented by any class.
48. What is the difference between abstract classes and interfaces in Java?
Abstract classes can have both abstract and non-abstract methods, while interfaces
can only have abstract methods. Abstract classes can also have instance variables,
constructors, and static methods, while interfaces cannot. A class can implement
multiple interfaces but can only extend one abstract class.
50. What is the difference between the final, finally, and finalize keywords in
Java?
The final keyword is used to make a variable, method, or class constant and
unmodifiable. The finally keyword is used in a try-catch-finally block to execute a
block of code regardless of whether an exception is thrown or not. The finalize()
method is called by the garbage collector before an object is destroyed and can be
used to perform final cleanup tasks.
51. What is the difference between static and non-static methods in Java?
Static methods are associated with the class, rather than with an instance of the
class. Non-static methods, on the other hand, are associated with an instance of the
class. Static methods can be called directly on the class, while non-static methods
can only be called on an instance of the class.
A package is a way of organizing related classes and interfaces into a single unit,
called a package. Packages help to avoid naming conflicts between classes and also
make it easier to manage and maintain large Java applications.
53. What is the difference between composition and inheritance in Java, and
when would you use each?
54. What is a lambda expression in Java, and how can you use it?
A synchronized method is a method that locks the entire object on which it is called,
while a synchronized block allows you to lock a specific block of code within a
method. Synchronized methods can be simpler to use, but can lead to performance
issues in certain situations, while synchronized blocks provide more fine-grained
control over locking.
57. What is a singleton pattern in Java, and how would you implement it?
A singleton pattern is a design pattern that ensures that only one instance of a class
can be created. This can be useful in situations where you need to ensure that there
is only one instance of a particular resource or service. In Java, you can implement a
singleton pattern using a private constructor, a static instance variable, and a public
static method to return the instance.
58. What is the difference between an abstract class and an interface in Java,
and when would you use each?
Abstract classes and interfaces are both used for abstraction, but they have some
key differences. Abstract classes can have both abstract and non-abstract methods,
while interfaces can only have abstract methods. Additionally, a class can implement
multiple interfaces, but can only extend one abstract class. When deciding which to
use, it depends on the situation.
Reflection is a mechanism in Java that allows code to examine and manipulate the
structure, behavior, and properties of classes at runtime. It allows you to inspect
classes, methods, fields, and annotations at runtime, and even invoke methods
dynamically.
60.What is a Java string, and how is it different from other data types?