Common Interview Questions For Java Developers

Welcome to ValueInBrief Blog. We need to know what is the common questions asked in the interview, and the answers. So I fetch all common questions and their answers here for you to practice and know the main ideas of questions.

Common Interview Questions For Java Developers

1. List any five features of Java?

Object Oriented, Platform Independent, Robust, Interpreted,
Multi-threaded

2. Why is Java Architectural Neutral?

It's Compiler generates an architecture-neutral object file format, which makes the compiled Code to be executable on many processors, with the presence of Java runtime system.

3. What is JIT compiler?

It is used to improve the performance. JIT Compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term "compiler" refers to a translator from the instruction Set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

4. What gives Java its 'write once and run anywhere' nature?

The bytecode. Java is compiled to be a byte code which is the intermediate language between Source Code and machine code. This byte code is not platform specific and hence can be fed to any platform.

5. Why Java is considered dynamic?

It is designed to adapt to an evolving environment. Java programs can carry extensive amount of runtime information that can be used to Verify and resolve accesses to objects on run-time.

6. Define class?

A class is a blueprint from which individual objects are created. A class can Contain fields and methods to describe the behavior of an object.

7. What if I write static public void instead of public static Void?

Program compiles and runs properly.

8. What kind of variables a class can consist of?

A class consist of Local variable, instance Variables and class Variables.

9. What is the default value of the local variables?

The local Variables are not initialized to any default value, neither primitives nor object references

10. What is Singleton class?

Singleton class control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes.

11. What is constructor?

Constructor is just like a method that is used to initialize the State of an object. It is invoked at the time of object creation.

12. What is the purpose of default Constructor?

The java compiler creates a default constructor only if there is no Constructor in the class.

13. Does constructor return any value?

yes, that is current instance (You cannot use return type yet it returns a value).

14. ls constructor inherited?

NO, Constructor is not inherited.

15. Can you make a constructor final?

No, Constructor can't be final.

16. What is static variable?

Static Variable is used to refer the Common property of all objects (that is not unique for each object).

17.  What is static block?

ls used to initialize the Static data member. It is executed before main method at the time of classloading.

18. Can We execute a program without main() method?

Yes, one of the Way is static block.

19. What if the Static modifier is removed from the signature of the main method?

Program compiles. But at runtime throws an error "NoSuchMethodError".

20. What is the default value of byte datatype in Java?

Default value of byte datatype is 0.

21. When a byte datatype is used?

This data type is used to save space in large arrays, mainly in place of integers, since a byte is four times Smaller than an int.

22. When parselnt0 method can be used?

This method is used to get the primitive data type of a certain String.

23. What is the difference between StringBuffer and StringBuilder class?

Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects.

24. Which package is used for pattern matching with regular expressions?

java. util.regex package is used for this purpose.

25. java. util.regex Consists of which classes?

java. util.regex consists of three classes - Pattern class, Matcher class and PatternSyntaxException class.

26. Which class is the superclass for every class?

Object class.

27. What is composition?

Holding the reference of the other class Within Some other class is known as composition.

28. Why Java does not support pointers?

Pointer is a Variable that refers to the memory address. They are not used in java because they are unsafe (unsecured) and Complex to understand.

29. What is super in java?

It is a keyword that refers to the immediate parent class object.

30. Can you use this() and Super() both in a constructor?

No. Because Super() or this O must be the first statement.

31. Why method overloading is not possible by changing the return type in java?

Because of ambiguity.

32. Can we overload main() method?

Yes, You can have many main() methods in a class by Overloading the main method.

33. Which are the two Subclasses under Exception class?

The Exception class has two main subclasses : IOException class and RuntimeException Class.

34. When throws keyword is used?

If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a method's signature.

35. How finally used under Exception Handling?

The finally keyword is used to Create a block of Code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

36. What is Abstraction?

It refers to the ability to make a class abstract in OOP. It helps to reduce the complexity and also improves the maintainability of the System.

37. What is Abstract class?

These classes cannot be instantiated and are either partially implemented or not at all implemented. This class Contains One Or more abstract methods which are simply method declarations without a body.

38. What is an Interface?

An interface is a Collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

39. Define Packages in Java?

A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations) providing access protection and name Space management.

40. What are the two ways in which Thread can be Created?

Thread can be created by: implementing Runnable interface, extending the Thread class.

41. Can We Override static method?

No, you can't Override the static method because they are the part of class not object.

42. Can We Override the Overloaded method?

yes.

43. Can you have virtual functions in Java?

Yes, all functions in Java are virtual by default.

44. What is final variable?

If you make any Variable as final, you cannot change the value of final variable(It will be constant).

45. What is blank final variable?

A final Variable, not initialized at the time of declaration, is known as blank final Variable.

46. Can We initialize blank final Variable?

Yes, only in constructor if it is non-static. If it is Static blank final Variable, it can be initialized only in the Static block.

47. Can you declare the main method as final?

Yes, such as, public static final Void main (String[] args){}.

48. What is an applet?

An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal.

49. An applet extend which class?

An applet extends java.applet. Applet class.

50. Explain garbage Collection in Java?

It uses garbage Collection to free the memory. By cleaning those objects that is no longer reference by any of the program.

51. Define immutable object?

An immutable object can't be changed once it is created.

52. Explain the usage of this() With Constructors?

It is used With Variables Or methods and used to call Constructor of same class.

53. Can you achieve Runtime Polymorphism by data members?

No.

54. Can there be any abstract method without abstract class?

No, if there is any abstract method in a class, that class must be abstract.

55. Can you use abstract and final both With a method?

No, because abstract method needs to be overridden whereas you can't Override final method.

56. ls it possible to instantiate the abstract class?

No, abstract class can never be instantiated.

57. What is marker interface?

An interface that have no data member and method is known as a marker interface. For example Serializable, Cloneable etc.

58. Can We define private and protected modifiers for variables in interfaces?

No, they are implicitly public.

59. When can an object reference be cast to an interface reference?

An object reference can be cast to an interface reference When the object implements the referenced interface.

60. What is the difference between object oriented programming language and object based programming language?

Object based programming languages follow all the features of OOPS except inheritance. JavaScript is an example of object based programming languages.

61. What is the purpose of default Constructor?

The java compiler creates a default constructor only if there is no Constructor in the class.

62. What is function overloading?

If a class has multiple functions by same name but different parameters, it is known as Method Overloading.

63. What is function overriding?

If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding.

64. What is an Exception?

An exception is a problem that arises during the execution of a program. Exceptions are Caught by handlers positioned along the thread's method invocation Stack.

65. What do you mean by Checked Exceptions?

It is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.

66. What is NullPointerException?

A NullPointerException is thrown When calling the instance method of a null object, accessing or modifying the field of a null object etc.

67. Explain Runtime Exceptions?

It is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of Compilation.

68. Do I need to import java.lang package any time? Why?

No. It is by default loaded internally by the JVM.

69. What is the base class for Error and Exception?

Throwable.

70. Which are the two Subclasses under Exception class?


The Exception class has two main Subclasses : IOException class and RuntimeException Class.

71. Can finally block be used Without catch?

Yes, by try block. finally must be followed by either try or catch.

72. When throws keyword is used?

If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a method's signature.

73. When throw keyword is used?

An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using throw keyword.

74. Can an exception be rethrown?

Yes.

75. What is exception propagation?

Forwarding the exception object to the invoking method is known as exception propagation.

76. What will happen to the Exception object after exception handling?

The Exception object will be garbage Collected in the next garbage Collection.

77. What is the meaning of immutable in terms of String?

The simple meaning of immutable is unmodifiable or unchangeable. Once string object has been created, its Value can't be changed

78. How many Ways We can Create the string object?

There are two Ways to create the String object, by String literal and by new keyword.

79. Why java uses the concept of string literal?

To make Java more memory efficient (because no new objects are created if it exists already in String constant pool).

80. What is the basic difference between string and stringbuffer object?

String is an immutable object. StringBuffer is a mutable object.

81. What is the difference between StringBuffer and StringBuilder?

String Buffer is synchronized whereas String Builder is not synchronized.

82. What is nested class?

A class Which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, anonymous inner class and Static nested class.

83. ls there any difference between nested classes and inner classes?

Yes, inner classes are non-static nested classes i.e. inner classes are the part of nested classes.

84. Can We access the non-final local variable, inside the local inner class?

No, local variable must be constant if you want to access it in local inner class.

85. What is nested interface?

Any interface i.e. declared inside the interface or class, is known as nested interface. It is static by default.

86. Can a class have an interface?

Yes, it is known as nested interface.

87. Can an Interface have a class?

Yes, they are static implicitly.

88. Define inheritance?

It is the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical Order.

89. When super keyword is used?

If the method Overrides One of its Superclass's methods, Overridden method can be invoked through the use of the keyword Super. It can be also used to refer to a hidden field.

90. What is Polymorphism?

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

91. What is Abstraction?

It refers to the ability to make a class abstract in OOP. It helps to reduce the complexity and also improves the maintainability of the system.

92. What is Abstract class?

These classes cannot be instantiated and are either partially implemented or not at all implemented. This class Contains One or more abstract methods which are simply method declarations without a body.

93. When Abstract methods are used?

If you want a class to contain a particular method but you Want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.

94. What is Encapsulation?

It is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. Therefore encapsulation is also referred to as data hiding.

95. What is an Interface?

An interface is a Collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

96. Define Packages in Java?

A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations) providing access protection and name space management.

97. Why Packages are used?

Packages are used in Java in-Order to prevent naming conflicts, to control access, to make searching/ locating and usage of classes, interfaces, enumerations and annotations, etc., easier.

98. What is the difference between the Reader / Writer class hierarchy and the InputStream / OutputStream class hierarchy?

The Reader / Writer class hierarchy is character-Oriented, and the InputStream / OutputStream class hierarchy is byte-oriented.

99. What an I/O filter?

An I/O filter is an object that reads from One stream and Writes to another, usually altering the data in Some Way as it is passed from One Stream to another.

Finally!

I hope you enjoyed reading the questions and answers above! Please SUBSCRIBE to receive all future articles and posts directly into your Email Inbox.

Follow me to read our future blog posts by Click on Subscribe Above, or by follow ValueInBrief page on Facebook.
Comment via Facebook
0 Comment via Google

0 تعليقات:

Post a Comment