0% found this document useful (0 votes)
5 views

Java Interview

Uploaded by

pp14221422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Interview

Uploaded by

pp14221422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1. What is Java?

Java is a high-level, object-oriented programming language that is platform-


independent. The syntax of java is based on C and C++ languages.

2. What are the features of Java?

Some features of Java include:

Object-oriented programming

Platform-independence

Memory management

Exception handling

Multithreading

Security

Networking

Garbage collection

3. What is the difference between JDK, JRE, and JVM?

JDK– (Java Development Kit) is a software development environment that includes


the Java compiler, Java Runtime Environment (JRE), and other development tools.

JRE –(Java Runtime Environment) is a runtime environment that allows Java


programs to run on a machine.

JVM –(Java Virtual Machine) is a virtual machine that provides a runtime


environment for Java programs.

4. What are the data types in Java?

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.

6. What is an object in Java?

An object is an instance of a class in Java. Objects have properties (attributes) and


behaviors (methods).

7. What is a constructor in Java?

A constructor is a special method that is used to create an object in Java.


Constructors have the same name as the class and are called when an object is
created using the new keyword.

8. What is inheritance in Java?

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.

9. What is polymorphism in Java?

Polymorphism is a feature in Java that allows objects to take on multiple forms. In


Java, polymorphism is achieved through method overriding and method overloading.

10. What is encapsulation in Java?

Encapsulation is a mechanism in Java that hides the implementation details of a


class from the outside world and exposes a public interface for interacting with the
class. Encapsulation is achieved through the use of access modifiers and
getter/setter methods.

11. What is an interface in Java?

An interface is a collection of abstract methods that define a contract for a class to


implement. Interfaces are used to achieve abstraction and decoupling in Java.
12. What is an abstract class in Java?

An abstract class is a class that cannot be instantiated and is meant to be extended


by other classes. Abstract classes can contain abstract methods, which are meant to
be implemented by the subclasses.

13. What is a package in Java?

A package is a namespace that organizes classes and interfaces in Java. Packages


are used to avoid naming conflicts and to create a hierarchical structure for classes
and interfaces.

14. What is the difference between an array and an ArrayList in Java?

An array is a fixed-size collection of elements of the same data type, while an


ArrayList is a dynamic-size collection of elements of any data type. Arrays can be
multidimensional, while ArrayLists are always one-dimensional.

Java Interview Questions for Experienced Candidates

Here are the Java interview questions for experienced professionals:

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.

16. What is the difference between a checked and an unchecked exception in


Java?
A checked exception is a type of exception that must be declared in a method’s
signature using the throws keyword or handled using a try-catch block.

Examples of checked exceptions include IOException and SQLException. An


unchecked exception, such as NullPointerException or IllegalArgumentException,
does not need to be declared or handled explicitly.
17. What is a JVM and how does it work?

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.

19. What are annotations in Java?

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.

20. What are generics in Java?

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.

21. What is the difference between a stack and a queue in Java?

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.

22. What is the use of the synchronized keyword in Java?

The synchronized keyword is used to make a method or a block of code thread-safe.


It ensures that only one thread can access the synchronized code at a time,
preventing concurrent access and race conditions.

23. What is the purpose of the Java NIO package?


The Java NIO (New Input/Output) package provides a non-blocking I/O API for Java
programs. It allows for efficient I/O operations on large amounts of data by using
channels and buffers instead of streams.

24. What is a lambda expression in Java?

A lambda expression is a concise way of implementing functional interfaces in Java.


It allows you to define a method without a name that can be passed as an argument
to another method. Lambda expressions make it easier to write and use functional
interfaces, which are a key feature of Java 8 and later versions.

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.

Core Java Interview Questions

Here are the Core Java interview questions:

26. What is the difference between a constructor and a method in Java?

A constructor is a special method that is used to initialize objects, while a method is


a regular function that is used to perform a specific task.

27. What is the difference between an interface and an abstract class in Java?

An interface is a collection of abstract methods that can be implemented by any


class, while an abstract class is a class that cannot be instantiated and can contain
both abstract and concrete methods.
28. What is the difference between ArrayList and LinkedList in Java?

ArrayList is an implementation of a resizable array, while LinkedList is an


implementation of a doubly-linked list. ArrayList provides constant-time access to its
elements, while LinkedList provides constant-time insertion and deletion of elements.

29. What is the purpose of the “static” keyword 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.

31. What is the purpose of the “final” keyword in Java?

The “final” keyword is used to denote a variable or method that cannot be modified
once it has been initialized.

32. What is the purpose of the “this” keyword in Java?

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.

33. What is the purpose of the “super” keyword in Java?

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.

Java Collections Interview Questions

Here is the collection of Java interview questions:

34. What are the types of Collections in Java?


The types of Collections in Java are:
List

Set

Queue

Deque

Map

35. What is the difference between List and a Set in Java?

A List is an ordered collection of elements with duplicates allowed while a Set is an


unordered collection of unique elements.

36. What is the difference between HashSet and TreeSet?

HashSet is an unordered Set that does not allow duplicates, while TreeSet is a
sorted Set that does not allow duplicates.

37. What is the difference between ArrayList and LinkedList in Java?

ArrayList is implemented as a resizable array, while LinkedList is implemented as a


doubly linked list. ArrayList provides faster element access, while LinkedList provides
faster element insertion and deletion.

38. What is the difference between HashMap and TreeMap in Java?

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.

39. What is the difference between Iterator and ListIterator in Java?

Iterator is used to iterate over a collection in a forward direction only, while


ListIterator is used to iterate over a List in both forward and backward directions.

40. How does the compareTo() method work in Java?

The compareTo() method is used to compare two objects based on a natural


ordering. It returns a negative integer, zero, or a positive integer as the first object is
less than, equal to, or greater than the second object.
41. What is the difference between fail-fast and fail-safe iterators in Java?

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.

42. What is the difference between Hashtable and HashMap in Java?

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.

Java OOPs Interview Questions

Here are the OOPS Java interview questions:

44. What is the OOP concept in Java?

Object-oriented programming (OOP) is a programming paradigm that focuses on the


concept of objects, which can contain data and code to manipulate that data.

Java is an object-oriented programming language, which means that it supports


OOP concepts such as inheritance, encapsulation, polymorphism, and abstraction.

45. What is inheritance in Java?

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.

46. What is encapsulation in Java?


Encapsulation is the concept of binding data and methods that operate on that data
into a single unit, called a class. Encapsulation is important because it protects the
data from being accessed or modified by code outside of the class, which can help
improve the robustness and maintainability of the code.

47. What is an abstraction in Java?

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.

49. What is a constructor in Java?

A constructor is a special method that is used to initialize objects in Java.


Constructors have the same name as the class and are called automatically when an
object is created. Constructors can be used to set default values for instance
variables and to perform other initialization tasks.

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.

52. What is a package in Java?

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.

Advanced Java Interview Questions

Here are the advanced Java interview questions:

53. What is the difference between composition and inheritance in Java, and
when would you use each?

Composition is when one class contains an instance of another class, while


inheritance is when a subclass extends a superclass. Composition is generally
preferred when you need greater flexibility and modularity, while inheritance is
preferred when you need a more hierarchical structure and code reuse.

54. What is a lambda expression in Java, and how can you use it?

A lambda expression is a concise way to define an anonymous function, which can


be used in place of a single-method interface or class. It is defined using the “->”
operator and can take arguments and return a value. Lambda expressions can make
code more concise and readable and can be used with functional interfaces in Java.

55. What is the difference between a class and an object in Java?

A class is a blueprint or template for creating objects, while an object is an instance


of a class. Classes define the properties and behaviors that objects will have, while
objects have specific values for those properties and can perform specific actions.

56. What is the difference between a synchronized method and a synchronized


block in Java?

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.

59. What is a reflection in Java?

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.

Java String Interview Questions

Here are the string Java interview questions:

60.What is a Java string, and how is it different from other data types?

A Java string is a sequence of characters that represents text. It is different from


other data types in that it is

You might also like