JavaRush/Java Blog/Random EN/Coffee break #145. 90 Frequently Asked Java Interview Que...

Coffee break #145. 90 Frequently Asked Java Interview Questions and Answers

Published in the Random EN group
members
Source: Usemynotes This publication contains a list of questions that are often encountered in interviews with entry-level and intermediate-level Java developers. Coffee break #145.  90 Frequently Asked Java Interview Questions and Answers - 1

1. What makes Java a platform independent language?

Java is called a platform independent language because the byte code generated by the JDK (Java Development Kit) can be executed by the JVM (Java Virtual Machine) on any platform, regardless of the operating system.

2. Why is Java not a fully object-oriented language?

Java is not a fully object-oriented language because it uses eight primitive data types that are not objects: boolean , byte , char , int , float , double , long , and short .

3. Why is the main() method made static in Java?

The main() method is declared static so that it can be directly called from the JVM without creating an object or instance of the class in which it is declared.

4. What are wrapper classes in Java?

In Java, every primitive data type such as int , float has a corresponding class known as wrapper classes. They are also called wrappers because they wrap or convert a primitive data type into objects.

5. What is a constructor in Java?

Constructors in Java are special methods that are automatically called when an object or instance is created. The constructor and class names must match.

6. What are the differences between arrays and ArrayList in Java?

Arrays ArrayList
It is a static data structure, which means its size is fixed and cannot be changed. It is a variable length or dynamically sized array in a collection structure, which means its size can be changed as per requirement.
They can store both objects and primitive types. It can only store or contain objects, but not primitive types.
We can access an element by specifying its index in square brackets [ ]. We can access an element by specifying its index number inside the get() method .

7. Why don't Java use pointers?

Java does not use pointers because they are unsafe and also add complexity to the program. Additionally, Java avoids pointers to avoid providing direct memory access to the user.

8. What types of access modifiers are there in Java?

In Java, access modifiers are keywords used to indicate access to a class and methods outside the class or in another class. There are four different types of access modifiers in Java:
  • Default
  • Public
  • Private
  • Protected

9. What is JIT in Java?

JIT is an acronym for Just-In-Time Compiler in Java. At runtime, it converts the bytecode into native machine code. The JIT compiler helps improve the performance of Java programs.

10. What is final keyword in Java?

The final keyword in Java is used to represent constants. It can be used with variables, classes and methods.
  • If the final keyword is used with variables, you cannot change its value.
  • If the final keyword is used with methods, you cannot override that method.
  • If you declare a class final , then that class cannot be extended or inherited by a subclass.

11. What are the basic concepts of OOP or Object Oriented Programming in Java?

Object-oriented programming or OOP in Java uses 4 main concepts:
  • Encapsulation.
  • Abstraction.
  • Inheritance.
  • Polymorphism.

12. What is a singleton class in Java and how to make a class a singleton?

In Java, a singleton class is a class that has only one instance or object. By creating a private constructor, we can make the class a singleton.

13. What is the difference between local variables and instance variables in Java?

A local variable is defined within a block or method. The scope of a local variable is exclusively within the block. A class instance variable is a variable that is defined within the class but outside the method. It is accessed by creating an object of the class and is terminated when the object is deleted.

14. What is OOP or Object Oriented Programming?

Object-oriented programming or OOP is a programming paradigm used to develop programs or applications using the concept of objects.

15. What makes Java strings immutable?

Strings in Java are immutable, which means that once a string object is created, it cannot be updated, but a new string object is created. This is because string objects are cached in the string pool.

16. Describe the differences between interface and abstract classes in Java

Interface Abstract class
Can only have abstract methods. Allows both abstract and non-abstract methods.
The subclass implements the interface using the Implements keyword . A subclass or derived class inherits the abstraction by using keywords to extend the implementation (extends).
An interface can only have public static methods with no implementation. It can have methods without implementation or with implementation.
All methods of an interface must be implemented by the class that implements it. A class that inherits from an abstract class is not required to implement all the methods of its superclass.

17. What do you mean by polymorphism and what are its types?

Polymorphism in Java is the ability of an object to take more than one form. It can also be defined as a single task or activity that can be accomplished in different ways. Polymorphism is divided into two types:
  1. Compile-time polymorphism.
  2. Runtime polymorphism.

18. How does Java achieve compile-time and run-time polymorphism?

In Java, compile-time polymorphism is achieved through method overloading, while run-time polymorphism is achieved through method overriding.

19. What do you mean by abstraction and how is it achieved in Java?

Abstraction is the act of representing important information without showing internal or implementation details. In Java, this is achieved through the use of abstract classes and interfaces.

20. What are interfaces in Java?

An interface in Java is a set of abstract methods with only function declarations, but no implementation. We cannot create objects or instances of an interface. An interface cannot have constructors. A class can implement multiple interfaces using the Implements keyword in Java.

21. What types of inheritance are there in Java?

In Java, we use the extends keywords to inherit the attributes and methods of a super class or base class. There are five types of inheritance:
  1. Single Inheritance.
  2. Multilevel Inheritance.
  3. Hierarchical Inheritance.
  4. Multiple Inheritance through Interface.
  5. Hybrid Inheritance.

22. Why doesn't Java support multiple inheritance?

Java does not support multiple inheritance to prevent ambiguity. For example, suppose there are two superclasses A and B, and a subclass C that inherits both A and B. Both classes A and B have a show() method . If we call the show() method by creating an object of class C, then the compiler throws an error because it is not clear which class of the show() method should be executed.

23. What is the difference between method overloading and method overriding?

Method overloading Method overriding
Compile-time polymorphism. Runtime polymorphism.
Two or more methods in the same class with the same function or method name but different signatures. Two or more methods that have the same function or method name and signature in different classes.

24. Is it possible to override a static or private method in Java?

A private method is not accessible outside the class, so it will not be overridden by a subclass. Therefore, it cannot be overridden. Similarly, a static method cannot be overridden because if we create static methods with the same method name and signature in both the base and derived class, the derived class will hide the methods in the base class. This is known as method hiding.

25. What is constructor overloading in Java?

In Java, a constructor is considered to be overloaded if a class has more than one constructor, but with a different list of parameters.

26. Can we define an Abstract class even if it has no abstract methods?

Yes, we can create an abstract class even if it doesn't have an abstract method. However, if a class contains at least one abstract method, it must be defined as abstract, otherwise an error will occur.

27. What is the use of static variables and methods?

We use static members (methods and variables) when we want to use a common method or variable for all objects of a class instead of creating multiple copies of each object. For example, in the Employee class, the company name can be made static because all objects will have the same company name.

28. How many different types of constructors are there in Java?

There are 2 types of constructors in Java:
  1. The default constructor does not accept any parameters.
  2. Parameterized constructor - takes at least one parameter.

29. Does Java use a copy constructor?

No, Java does not have a copy constructor like C++.

30. State the differences between constructor and methods in Java

Constructor Method
It is mandatory that the constructor name and the class name match. The method name may or may not be the same as the class name.
It cannot have a return type. Required to have a return type.
Constructors cannot be static. The method may or may not be static.
It is invoked or called automatically when an object or instance of a class is created. It is invoked or applied using the name of the object along with a period (.).
It is used to initialize the state of an object. It is used to define the behavior of an object.

31. Is it possible to execute a program without using the main() method?

No, a program cannot be executed without main() method in Java because program execution starts from main() method .

32. What does the this keyword mean in Java?

The this keyword in Java is a reference variable used to refer to the current object calling a method or variable.

33. Is it possible to use the this keyword in Java to refer to static members?

Yes, since it is a reference variable for the current object, we can use the this keyword to refer to static members.

34. Can we make the main() method in Java private instead of public?

We can make the main() method private, but then we will get a runtime error and hence the program will not execute.

35. What does the super keyword mean in Java?

In Java, the super keyword is a reference variable that is used to refer to an object of the immediate parent class.

36. State the differences between a vector and an array.

Vector Array
This is a dynamically sized array. It is a fixed size data structure.
Vector is synchronized. The array is not synchronized.
It is slower compared to an array. It is faster compared to vector.
It can only contain or store Java objects. It is used to store both primitive types and Java objects.
We can use the size() method to find out the length of the vector. We can use the length property to find out the length of the array.

37. What is multithreading in Java?

Multithreading is the process of executing multiple tasks simultaneously within a single program. This is done to achieve maximum processor load.

38. What are the ways to implement multithreading in Java?

Multithreading in Java can be implemented in two ways:
  1. By implementing the Runnable interface in java.lang.Runnable .
  2. By extending the Thread class in java.lang.Thread .

39. State a few differences between super and this keywords in Java.

Super This
Used to refer to objects of the parent class. Used to refer to the current class object.
Used to access methods of a parent or base class. Used to access a method of the current class.
Calls the base class's default constructor. Calls the default constructor of the current class.

40. What are the differences between a subclass and an inner class?

Subclass Inner class
This is a class that inherits the properties and methods of a base or parent class. This is a class nested within another class.
It can access all public and protected members of a superclass or base class. It has access to all members of the outer class.

41. ​​What is a class loader in Java?

The Java class loader is a component of the Java Runtime Environment or JRE that is used to load Java classes into the Java Virtual Machine (JVM) at runtime.

42. Is it necessary for a Try block to be followed by Java exception handling in a Catch block?

Yes, it is mandatory to use a catch block because any exception thrown by a try block must be caught in the catch block .

43. Name the types of exceptions in Java

Exceptions in Java are of two types:
  1. Unchecked exception
  2. Checked exception

44. What are the thread states?

There are 4 thread states in Java:
  1. New stream.
  2. Runnable.
  3. Non-Runnable.
  4. Dead or Terminated.

45. What is a NullPointerException?

When a user tries to access or change the values ​​of a null object, a NullPointerException is thrown .

46. ​​What types of keywords are used in Java exception handling?

  1. try

  2. catch

  3. finally

  4. throw

  5. throws

47. Is it possible to overload the main() method in Java?

Yes, the main() method can be overloaded, but the JVM or Java Virtual Machine only calls the original main() method .

48. Is it possible to override the main() method in Java?

No, because the main() method is a static method and static methods cannot be overridden.

49. What are the differences between equals() and == ?

In Java, equals() is a method used to compare the values ​​of two objects or variables, while == is a comparison operator used to compare the addresses or memory locations of two more objects or variables.

50. Can a class constructor return any value?

No, the constructor does not have a return type, so it cannot return any value.

51. Define aggregation

Aggregation is a form of association that is defined as a has-a relationship between two classes. This is done for code reuse. This is a one-way or unidirectional relationship between two classes.

52. What is an association?

An association defines a relationship between two classes using objects. The relationship or association can be one-to-one, one-to-many, many-to-many, or many-to-one. Aggregation and composition are two forms of association.

53. What is composition?

Composition is a form of association in which two classes depend on each other.

54. What is the difference between dynamic and static binding?

With dynamic binding, the decision to bind a method to an object is made at run time. Late binding is another name for dynamic binding. With static binding, the decision to bind a method to an object is made at compile time. Static binding is another name for early binding.

55. State the difference between encapsulation and abstraction

In encapsulation, data and methods are packaged or bundled into a single unit known as a class. Abstraction displays only important information and hides internal or implementation details.

56. What is an abstract class in Java?

An abstract class is a class that cannot be instantiated, but we can create a reference variable. A class is declared abstract using the abstract keyword. It includes both abstract and non-abstract methods.

57. Can we use both final and abstract keywords in a method?

No, we cannot use final and static methods along with abstract because an abstract method must be overridden by a subclass and a final method cannot be overridden.

58. Can we declare a method as final in an interface?

No, we cannot declare a method as final because all methods in an interface are abstract, so it must be implemented by a subclass and a final method cannot be overridden.

59. Name the base class of all Java exception classes

The base class for all exceptions is java.lang.Throwable .

60. Is it possible to create multiple catch blocks in one try block?

Yes, we can create multiple catch blocks for one try block .

61. Define two environment variables that need to be specified to run any Java program

  1. Path variable.
  2. Classpath variable.

62. Is it possible to use a destructor in Java?

No, we cannot use destructors in Java.

63. What do you mean by anonymous class?

An anonymous inner class in Java has no name and creates only one object.

64. Name a Java method that must be implemented for all threads

Run() method .

65. What is the difference between break and continue in Java?

Break Continue
Used to terminate or terminate a loop if a certain condition is met. Used to skip execution of the loop for the current iteration based on some condition.
It can be used inside a loop and switch. Only used within a loop.

66. What are string pools in Java?

A string pool in Java is a collection of strings stored in heap memory.

67. What is the difference between throw and throws keywords in Java?

Throw Throws
Used to explicitly throw an exception. Used to declare an exception that can be thrown by a method at runtime.
Used inside the method body. Using the throws keywords , we can declare multiple exceptions.
Throw is followed by an instance of the Exception class . It is followed by the name of the Exception class that is to be thrown.

68. Is it possible to call a constructor inside another constructor of the same class?

Yes, we can call a constructor of the same class inside another constructor using this() as the first line.

69. Is it possible to compare String Buffer and String class in Java?

No, we cannot compare String Buffer and String objects . Because we will get an error.

70. In what sequence are constructors called during inheritance?

The constructor of the parent or base class is called first, and then the constructor of the derived class is called.

71. Can we name a constructor other than the class name?

No, the constructor and class name must be the same.

72. What are Java packages?

A package is a collection of grouped classes and interfaces.

73. What is the difference between the String and StringBuffer classes?

String StringBuffer
The String class is immutable. The StringBuffer class is mutable.
Slower than StringBuffer . Faster than String .
It uses more memory when concatenating many strings. It uses less memory when concatenating many strings.

74. Is it possible to overload static methods in Java?

Yes, we can overload static methods in Java by passing different function signatures.

75.Which component of memory, the stack or the heap, is cleaned up during garbage collection?

Heap.

76.Which of the following string classes should be used when data needs to be updated frequently?

The StringBuffer class should be used in case of frequent updates as it is mutable and hence the string pool will not be overloaded.

77. What happens if the static keyword is not used with the main() method in Java?

There will be no compilation error, but at runtime we will get a NoSuchMethodError error .

78. Will a finally block be executed if a return statement is placed at the end of try and catch blocks?

Yes, the finally block will always execute regardless of whether there is an exception.

79. Is it possible to have many public classes in a Java source file?

No, we can only have one public class in a Java source file.

80. Can an interface extend another interface?

Yes, one interface can extend another interface using the extends keyword in Java.

81. Name the different types of class loaders in Java

  1. Bootstrap ClassLoader

  2. Extensions ClassLoader

  3. System ClassLoader

82. What does the ternary operator do in Java?

The ternary operator is used as a replacement for the if-else operator in Java. This is the only conditional operator that contains three operands.

83. What does garbage collection do in Java?

Java garbage collection is a method by which Java applications automatically conserve their memory by removing or cleaning up unused objects and packages in memory.

84. How is the default constructor used in Java?

In Java, the default constructor is used to initialize the data members or member variables of an object with their default values. It is automatically generated by the compiler when there are no other constructors in the program.

85. What is the difference between path and classpath in Java

  1. The classpath defines the location of the bytecode files, that is, .class files.
  2. The path specifies the location of the .exe files.

86. What are the data types in Java?

There are 8 data types in Java:
  1. short

  2. byte

  3. char

  4. int

  5. float

  6. double

  7. long

  8. boolean

87. What are the four concepts of OOP in Java?

  1. Encapsulation.
  2. Abstraction.
  3. Polymorphism.
  4. Inheritance.

88. Can we assign null value to the this keyword in Java?

No.

89. What do you mean by static block in Java?

Static block in Java is used to initialize static variables of a class. It is executed only once: when the class is first loaded into memory.

90. What is an exception? Which package contains all the exception class definitions in Java?

Exceptions are abnormal situations that occur during program execution. They can occur as a result of incorrect user input or incorrect encoder logic. The java.lang.Exception package contains all the exception class definitions in Java.
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet