FahmidasClassroom

Learn by easy steps

Java

1. What is Java?

Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is designed to have minimal dependencies, allowing it to run on various platforms.

2. What are the main features of Java?

Java features include platform independence, object-oriented paradigm, strong memory management, multithreading, and built-in libraries.

3. Explain the difference between JDK, JRE, and JVM.

– JDK (Java Development Kit): It includes JRE and development tools necessary to compile and debug Java applications.

– JRE (Java Runtime Environment): It includes JVM and libraries necessary to run Java applications.

– JVM (Java Virtual Machine): It is an abstract machine that provides the runtime environment in which Java bytecode can be executed.

4. What is the difference between `==` and `.equals()` in Java?

– `==` is used for reference comparison (address comparison) for objects.

– `.equals()` method is used to compare the contents of objects.

5. What is the difference between `ArrayList` and `LinkedList`?

– `ArrayList` uses a dynamic array to store elements and provides fast iteration but slower insertion/deletion of elements.

– `LinkedList` uses a doubly linked list to store elements and provides fast insertion/deletion but slower iteration.

6. What is the difference between `HashMap` and `HashTable`?

– `HashMap` is not synchronized and allows null keys/values, whereas `HashTable` is synchronized and does not allow null keys/values.

– `HashMap` is preferred for non-thread-safe applications, while `HashTable` is thread-safe but slower due to synchronization.

7. What is a constructor in Java?

A constructor is a special method used to initialize objects. It is called when an object of a class is created.

8. Can a constructor be private in Java?

Yes, a constructor can be private in Java. Private constructors are used in singleton design patterns to restrict instantiation of a class to one object.

9. What is method overloading?

Method overloading is a feature that allows a class to have more than one method having the same name if their parameter lists are different. It is used to provide different ways to access or use the same method.

10. What is method overriding?

Method overriding occurs when a subclass provides a specific implementation of a method that is already provided by its parent class. It is used for runtime polymorphism.

11. What is the `final` keyword used for?

The `final` keyword can be applied to variables, methods, and classes:

– Variables: If a variable is declared `final`, its value cannot be changed.

– Methods: If a method is declared `final`, it cannot be overridden in subclasses.

– Classes: If a class is declared `final`, it cannot be subclassed.

12. What is a static method?

A static method belongs to the class rather than to any instance of the class. It can be called without creating an instance of the class.

13. What is the difference between `static` and `final` keywords in Java?

– `static`: Used to create class-level variables and methods.

– `final`: Used to declare constants, prevent method overriding, and prevent inheritance of classes.

14. What are access modifiers in Java?

Access modifiers control the visibility of classes, methods, and variables in Java. The main access modifiers are `public`, `protected`, default (package-private), and `private`.

15. What is method hiding in Java?

Method hiding occurs when a subclass defines a static method with the same signature as a static method in its superclass. It does not override the superclass method but hides it.

16. What is the `super` keyword used for?

The `super` keyword in Java is used to refer to the superclass of the current object instance. It can be used to access superclass methods and constructors.

17. What is an abstract class?

An abstract class is a class that cannot be instantiated on its own and is used as a base for creating other classes. It may contain abstract methods that subclasses must implement.

18. What is an interface in Java?

An interface in Java is a reference type that can contain only constants, method signatures, default methods, static methods, and nested types. It cannot contain instance fields.

19. Can an interface have fields?

In Java 8 and later, interfaces can have static final fields (constants). They cannot have instance fields.

20. What is the difference between abstract class and interface?

– Abstract class can have abstract and non-abstract methods. Interface can only have abstract methods (Java 7 and earlier) or can have default and static methods (Java 8+).

– A class can implement multiple interfaces but can extend only one abstract class.

21. What are anonymous classes in Java?

Anonymous classes are inner classes without a name. They are declared and instantiated in a single statement.

22. What is a lambda expression?

A lambda expression is a concise way to represent an anonymous function (a function without a name). It allows you to pass functionality as an argument to a method.

23. What is the `this` keyword used for?

The `this` keyword in Java refers to the current instance of the class. It can be used to refer to instance variables and invoke current class constructors and methods.

24. What is method reference in Java?

Method reference is a shorthand syntax for lambda expressions to refer to methods or constructors by their names. It provides a way to pass existing methods or constructors as lambda expressions.

25. What is a package in Java?

A package in Java is a mechanism to group related classes and interfaces. It helps in organizing the classes and avoids name conflicts.

26. What are checked and unchecked exceptions in Java?

– Checked exceptions: These are exceptions that are checked at compile-time. They need to be handled using try-catch block or declared using `throws` keyword.

– Unchecked exceptions: These are exceptions that are not checked at compile-time. They are subclasses of `RuntimeException` and `Error`.

27. What is the difference between `throw` and `throws` in Java?

– `throw` is used to explicitly throw an exception.

– `throws` is used in method signature to declare that a method may throw one or more exceptions.

28. What is the difference between `ArrayList` and `Vector`?

– `ArrayList` is not synchronized and is faster than `Vector`.

– `Vector` is synchronized and thus thread-safe.

29. What is the `transient` keyword used for?

The `transient` keyword in Java is used to indicate that a variable should not be serialized when the object containing that variable is serialized.

30. What is serialization in Java?

Serialization is the process of converting an object into a stream of bytes to store it in memory, send it over a network, or save it to a file.

31. What is the `volatile` keyword used for?

The `volatile` keyword in Java is used to indicate that a variable’s value may be changed by multiple threads simultaneously. It ensures that the variable is always read from and written to main memory, not from thread cache.

32. What are the different types of inner classes in Java?

– Nested inner class

– Method-local inner class

– Anonymous inner class

– Static nested class

33. What is the diamond problem in Java?

The diamond problem occurs in multiple inheritance when two superclasses have a common method name and a subclass tries to override it without specifying which superclass method to use.

34. What is the `default` keyword used for in Java 8?

In Java 8 and later, the `default` keyword is used to define default methods in interfaces. Default methods have a body and can be overridden by implementing classes.

35. What is type erasure in Java generics?

Type erasure is the process by which Java generics are implemented by the compiler. Generic type information is removed at compile-time and replaced with casts or bridge methods.

36. What is the difference between `Comparator` and `Comparable` in Java?

– `Comparable` interface is used to define the natural ordering of objects. The `compareTo()` method is implemented in the class of the objects being compared.

– `Comparator` interface is used to define custom ordering of objects. A separate comparator class or lambda expression is used to implement comparison logic.

37. What is the difference between `continue` and `break` statements?

– `continue`: It is used to skip the current iteration of a loop and proceed to the next iteration.

– `break`: It is used to terminate the loop or switch statement and transfer control to the next statement after the loop or switch.

38. What is the difference between `StringBuilder` and `StringBuffer`?

– `StringBuilder` is not synchronized and is faster than `StringBuffer`.

– `StringBuffer` is synchronized and thus thread-safe.

39. How does garbage collection work in Java?

Garbage collection in Java is the process of automatically reclaiming memory used by objects that are no longer reachable. It runs in the background and removes unreferenced objects.

40. What is the purpose of the `finalize` method in Java?

The `finalize` method is called by the garbage collector before reclaiming an object’s memory. It can be overridden to perform cleanup operations or resource deallocation.

41. What is the difference between `==` and `.compareTo()` method for strings?

– `==` operator checks for reference equality (whether they point to the same memory location).

– `.compareTo()` method compares the lexicographical difference between two strings.

42. What is the difference between `wait()` and `sleep()` methods?

– `wait()`: It is a method in `Object` class that causes the current thread to wait until another thread invokes the `notify()` or `notifyAll()` method for this object.

– `sleep()`: It is a method in `Thread` class that causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.

43. What are annotations in Java?

Annotations are a form of metadata that provide data about a program but do not directly affect the program’s execution. They can be used for compile-time checks, code generation, and runtime processing.

44. What is the difference between `assert` and `throw` in Java?

– `assert`: It is used to perform assertion testing (testing assumptions about the state of the program). It throws `AssertionError` if the assertion fails.

– `throw`: It is used to explicitly throw an exception to indicate exceptional situations during program execution.

45. What is a static initializer block?

A static initializer block in Java is used to initialize static variables of a class. It runs only once when the class is loaded into memory.

46. What is the `instanceof` operator used for?

The `instanceof` operator in Java is used to test whether an object is an instance of a specific class or implements an interface. It returns `true` if the object is an instance; otherwise, `false`.

47. How does Java handle multiple inheritance?

Java does not support multiple inheritance of classes. However, it supports multiple inheritance of interfaces, allowing a class to implement multiple interfaces.

48. What is the purpose of the `strictfp` keyword in Java?

The `strictfp` keyword in Java is used to restrict floating-point calculations to ensure portability by making them behave consistently across different platforms.

49. What are the different types of references in Java?

– Strong reference

– Weak reference

– Soft reference

– Phantom reference

50. What is the purpose of the `@Override` annotation?

The `@Override` annotation in Java is used to indicate that a method is overriding a method from its superclass. It helps to avoid accidental method signature changes when overriding methods.

These questions cover a wide range of topics and should help you prepare for various Java interviews.