61. Does Java allow us to declare private and protected modifiers for variables in an interface?
🅰 No, all the variables in an interface are by default public.
62. What is polymorphism in Java?
🅰 Polymorphism is a concept by which we can perform a single task in different ways. There are two types of polymorphism in java. They are: Static polymorphism and Dynamic polymorphism.
63. Explain static and dynamic polymorphism with example.
🅰 A polymorphism that exhibited during compilation is called static polymorphism in java. In the static polymorphism, the behavior of a method is decided at compile-time. Therefore, this type of polymorphism is also called compile-time polymorphism.
Compile-time polymorphism can be achieved/implemented by method overloading in java. Another example of static polymorphism is constructor overloading and method hiding.
A polymorphism that is exhibited at runtime is called dynamic polymorphism in java. In dynamic polymorphism, the behavior of a method is decided at runtime, therefore, it is called runtime polymorphism.
Dynamic polymorphism can be achieved/implemented in java using method overriding. An example of runtime polymorphism is method overriding.
64. What is binding in Java?
🅰 The connecting (linking) between a method call and method body/definition is called binding in java.
65. What is the difference between static binding and dynamic binding?
🅰 The difference between static and dynamic binding are as follows:
- Static binding occurs at compile-time while dynamic binding occurs at runtime.
- In static binding, actual object is not used whereas, actual object is used in the dynamic binding.
- Static binding is resolved at compile time whereas, dynamic binding is resolved at runtime.
- Static binding is also called late binding because it happens during compilation whereas, dynamic binding is called early binding because it happens during runtime.
- An example of static binding is method overloading whereas, example of dynamic binding is method overriding.
- Private, static, and final methods show static binding because they cannot be overridden whereas, except private, static, and final methods, other methods show dynamic binding because they can be overridden.
Core Java Interview Questions based on Final Keyword
66. What is final in Java?
🅰 Final is a keyword that is used to restrict the user in Java programming. It is a non-access modifier that can be applied to a variable, method, or class.
67. What is the final keyword in Java?
🅰 Java Final keyword has three different uses:
- To create a constant.
- To prevent inheritance.
- To prevent the method from being overridden.
68. Is it allow to change the value of final variable in Java?
🅰 No, Java does not allow to change the value of final variable. Once the value is defined, it cannot be changed.
69. Can a class be declared as final in Java?
🅰 Yes, a class can be declared as final in Java. Once a class is declared final, it cannot be extended.
70. What is blank final variable? Can we initialize the blank final variable?
🅰 A variable that is declared as final and not initialized at a time of declaration is known as a blank final variable.
Yes, if it is non-static then we can declare in the constructor. If it is static blank final variable, it can be initialized only in the static block.
71. Can we change state of object to which a final reference is pointing?
🅰 Yes, we can change the state of an object to which a final reference variable is pointing but we cannot re-assign a new object to this final reference variable.
72. Can we declare a method as final in Java?
🅰 Yes, we can declare a method as final but a final method cannot be overridden by a child class.
73. How can inheritance be prohibited in Java?
🅰 If a class is declared as final, it cannot be extended. This will prevent the inheritance of that class in Java.
74. Is it allowed to mark main method as final?
🅰 Yes, the main method can be marked as final.
75. What is the use of final class in Java?
🅰 Three important uses of a final class in Java are as follows:
- To prevent inheritance because the final class cannot be extended.
- To create an immutable class like the predefined String class. We cannot make a class immutable without making it final.
- A final class is very useful when we want high security in any application.
76. Which class is the most common predefined final class object in Java?
🅰 String class
Core Java Interview Questions based on Static
77. What is static in Java?
🅰 Static is a keyword that is used for memory management mainly. Static means single copy storage for variables or methods.
78. Can a class be declared as static?
🅰 No, a class cannot be marked as static but an inner class can be static.
79. What is the use of static keyword in Java?
🅰 The purpose of using static keyword in Java programming is that we can access the data, method, or block of the class without any object creation.
80. Is it allowed to change the value of static variable in Java?
🅰 Yes, we can change the value of the static variable by using a constructor, or static block but not inside a static method.
81. What is the difference between static variable and instance variable?
🅰 The difference between static variable (class variable) and instance variable are as follows:
- A static variable is also known as class variable whereas, instance variable is also known as non-static variable.
- Class variables can be accessed inside the static block, instance block, static method, instance method, and method of inner class whereas instance variable can be accessed inside the instance block, instance method, and method of inner class.
- Class variable is always resolved during compile time whereas, instance variable is resolved during the runtime.
- They are not serialized in Java whereas, instance variables are serialized in Java.
82. Can we use this or super keyword in static method in Java?
🅰 In entire core java, this and super keyword is not allowed inside the static method or static area.
83. Can static method be overloaded in Java?
🅰 Yes, a static method can be overloaded in Java but not override it.
84. In Java, why do we use static variable?
🅰 If we have a common property for all objects of a class, we use a class-level variable i.e. a static variable. This variable is loaded in memory only once at the time of class loading. So, it saves memory.
85. What is the difference between static method and instance method?
🅰 The difference between static method (class method) and instance method are as follows:
- A static method is also known as class method whereas instance method is also known as non-static method.
- The only static variable can be accessed inside static method whereas, in the instance method, both static and instance variables can be accessed.
- We do not need to create the object of the class for accessing static method whereas, in the case of an instance method, we need to create the object for access.
- Class method cannot be overridden whereas, an instance method can be overridden.
86. Why a static method cannot be overridden in Java?
🅰 No, we cannot override static method because a static method is resolved at compile time by Java compiler whereas method overriding is resolved at runtime by JVM because objects are only available at runtime.
You can declare static methods with the same signature in subclass, but it is not considered as overriding. It is considered a method hiding.
87. What is the use of static block in Java?
🅰 The purpose of using a static block is to write that logic inside static block that is executed during the class loading. It is also used for changing the default value of static variable.
Core Java Interview Questions based on Constructor
88. What is the use of constructor in Java?
🅰 The constructor is mainly used in a program to assign the default value of instance variables.
89. What is the use private constructor in Java?
🅰 Private constructor is used to preventing other classes from accessing objects of a class. It can be also used in single tone classes where the object of the class cannot be created outside the class.
90. What is the difference between constructor and method?
🅰 The difference between constructor and method is as follows:
- Constructor is used to initialize the state of an object whereas, method is used to expose the behavior of an object.
- Constructor has no return type even void also where method has both void and return type.
- If we don’t define any constructor in the class, Java Compiler provides a default constructor for that class. Whereas, method is not provided by the compiler in any case.
- Constructor name must be same as name of the class whereas, method name may or may not be the same name as the class name.
- The purpose of a constructor is to create an object of a class whereas, the purpose of a method is to execute the functionality of the application.
- Constructor can not be inherited by subclass whereas method can be inherited in subclass.
91. Can a constructor be final?
🅰 Not, a constructor cannot be final.
92. What is constructor overloading in Java?
🅰 Constructor overloading is a technique in Java in which a class can have more than one constructor that differ in the parameters list.
93. What is constructor chaining in Java?
🅰 Constructor chaining is a technique of calling one constructor from another constructor using this and super keyword. The keyword “this” is used to call a constructor from another constructor within the same class whereas, keyword “super” is used to call parent (super) class constructor from child (subclass) class constructor. It occurs through inheritance.
94. Why do we need default constructor in a class?
🅰 Default constructor is a no-argument constructor that is automatically generated by JVM if we do not define any constructor in a class. We need at least one constructor to create an object. Therefore, Java provides a default constructor.
95. Can we inherit a constructor?
🅰 No, Java does not support the inheritance of constructor.
Core Java Interview Questions based on Method
96. Why do we need or use method in Java?
🅰 The purpose of using methods in the Java program is to write the logic of the application which performs some specific task.
97. What is main method in Java?
🅰 A main() method is an entry point to start the execution of a program. Every Java application has at least one class and at least one main method.
98. Why do we need to declare main method as static in Java?
🅰 The main method is declared as static. It is called by JVM when we run a class. JVM does not know how to create an object of a class. It needs a standard way to start the execution of a program.
Therefore, the main method is declared as static so that JVM can call it using the class name which is passed on the command line.
99. Can we have more than one main() method in class?
🅰 Yes, a class can have any number of main() methods but the execution always starts from public static void main(String[ ] args) only.
100. What is the difference between argument and parameter in Java?
🅰 A parameter is a variable in the definition of a method whereas an argument is an actual value of this variable that is passed to the method’s parameter.
Core Java Interview Questions based on Access Modifiers
101. What is the use of access modifiers in Java?
🅰 Access modifiers is used to restrict the visibility/accessibility of classes, fields, methods, or constructors.
102. Which access modifiers can be used with a class?
🅰 Public and default access modifiers.
103. What is the difference between public and private in Java?
🅰 The difference between public and private access modifiers is as follows:
- Public can be applied to a class but private cannot be applied to class.
- Public members can be accessed from anywhere outside class whereas, private members can be accessed only within the class.
- Public members of a class can be inherited to any subclass whereas, private members cannot be inherited to subclass.
104. Can we reduce the visibility of inherited or overridden method?
🅰 No, we can reduce the visibility of overridden method.
105. What will happen if we make a constructor of class private?
🅰 If we make any constructor as a private, we cannot create the object of that class from another class and also cannot create the subclass of that class.
106. Can we create an object of class if we make a constructor protected?
🅰 Yes, if we make constructor as protected then we can create the subclass of that class within the same package but not outside the package.
107. Which modifiers are applicable to the outer class?
🅰 Public, default, final, abstract, and strictfp are five modifiers that can be applied to outer class.
108. Which modifiers cannot be applied to the method?
🅰 Transient and volatile are two modifiers that cannot be applied to the method.
109. Which modifiers are not applicable to variables?
🅰 Abstract, synchronized, native, and strictfp are not applicable to variables.
110. Which modifier is applicable to local variable?
🅰 The only applicable modifier with local variable is final.
111. Which modifiers are applicable to the constructor?
🅰 Only access modifiers are applicable to the constructor. Non-access modifiers cannot be applied.
112. What are modifiers applicable to the outer interface?
🅰 Public, default, abstract, and strictfp are such modifiers that are applicable for outer interface.
113. What are non-access modifiers in Java?
🅰 Abstract, final, native, static, strictfp, synchronized, transient, and volatile are non-access modifiers in Java.
Core Java Interview Questions based on Class, Object, Data types, and Variables
114. What are the types of primitive data types in Java?
🅰 Primitive data types are those data types whose variables can store only one value at a time. Java defines eight primitive data types: boolean, char, byte, short, int, long, char, float, and double.
115. What are the non-primitive data types in Java?
🅰 Non-primitive data types are created by programmers. These data types are used to store a group of values or several values. Class, object, string, array, and interface are five non-primitive data types in Java.
116. What is the default value of the local variable?
🅰 Java does not initialize local variable with any default value. Therefore, local variable will be null by default.
117. What is the difference between an object and object reference?
🅰 An object is an instance of a class whereas object reference is a pointer to the object. There can be many references to the same object.
118. What are the default values of primitive data types?
🅰 For boolean data type, it is false. For byte, short, int, and long, it is 0. For float and double, it is 0.0. For char, the default value is ‘u000’.
Hope that 100++ Category wise most important frequently asked Core Java interview questions for Selenium automation testing will be helpful to tackle interview. These questions can be asked for fresher and experienced candidates by the interviewer.
All the best!!!