In this post, Java Interview Questions and Answers are discussed, which are helpful for Java beginners and Experienced persons. When you go for Selenium Test Automation Interview, you can expect similar kind of questions.
41. Can we define package statement after import statement in java?
We can’t define package statement after import statement in java. package statement must be the first statement in source file. We can have comments before the package statement.
42. What are identifiers in java?
Identifiers are names in java program. Identifiers can be class name, method name or variable name. Rules for defining identifiers in java:
- Identifiers must start with letter,Underscore or dollar($) sign.
- Identifiers can’t start with numbers .
- There is no limit on number of characters in identifier but not recommended to have more than 15 characters
- Java identifiers are case sensitive.
- First letter can be alphabet ,or underscore and dollar sign. From second letter we can have numbers
- We shouldn’t use reserve words for identifiers in java.
43. What are access modifiers in java?
The important feature of encapsulation is access control. By preventing access control we can misuse of class, methods and members.
A class, method or variable can be accessed is determined by the access modifier. There are three types of access modifiers in java. public,private,protected. If no access modifier is specified then it has a default access.
44. What is the difference between access specifiers and access modifiers in java?
In C++ we have access specifiers as public,private,protected and default and access modifiers as static, final. But there is no such division of access specifiers and access modifiers in java. In Java we have access modifiers and non access modifiers.
Access Modifiers : public, private, protected, default Non Access Modifiers : abstract, final
45. What access modifiers can be used for class ?
We can use only two access modifiers for class public and default. public: A class with public modifier can be visible
- In the same class
- In the same package subclass
- In the same package nonsubclass
- In the different package subclass
- In the different package non subclass.
default : A class with default modifier can be accessed
- In the same class
- In the same package subclass
- In the same package nonsubclass
- In the different package subclass
- In the different package non subclass.
46. Explain what access modifiers can be used for methods?
We can use all access modifiers public, private,protected and default for methods. public : When a method is declared as public it can be accessed
- In the same class
- In the same package subclass
- In the same package nonsubclass
- In the different package subclass
- In the different package non subclass.
default : When a method is declared as default, we can access that method in
- In the same class
- In the same package subclass
- In the same package non subclass
We cannot access default access method in
- Different package subclass
- Different package non subclass.
protected : When a method is declared as protected it can be accessed
- With in the same class
- With in the same package subclass
- With in the same package non subclass
- With in different package subclass
It cannot be accessed non subclass in different package.
private : When a method is declared as private it can be accessed only in that class. It cannot be accessed in
- Same package subclass
- Same package non subclass
- Different package subclass
- Different package non subclass.
47. Explain what access modifiers can be used for variables?
We can use all access modifiers public, private,protected and default for variables. public : When a variables is declared as public it can be accessed
- In the same class
- In the same package subclass
- In the same package non subclass
- In the different package subclass
- In the different package non subclass.
default : When a variables is declared as default, we can access that method in
- In the same class
- In the same package subclass
- In the same package non subclass
We cannot access default access variables in
- Different package subclass
- Different package non subclass.
protected : When a variables is declared as protected it can be accessed
- With in the same class
- With in the same package subclass
- With in the same package non subclass
- With in different package subclass
It cannot be accessed non subclass in different package.
private : When a variables is declared as private it can be accessed only in that class. It cannot be accessed in
- Same package subclass
- Same package non subclass
- Different package subclass
- Different package non subclass.
48. What is final access modifier in java?
final access modifier can be used for class, method and variables. The main advantage of final access modifier is security no one can modify our classes, variables and methods. The main disadvantage of final access modifier is we cannot implement oops concepts in java. Ex : Inheritance, polymorphism.
final class : A final class cannot be extended or subclassed. We are preventing inheritance by marking a class as final. But we can still access the methods of this class by composition. Ex: String class
final methods: Method overriding is one of the important features in java. But there are situations where we may not want to use this feature. Then we declared method as final which will print overriding. To allow a method from being overridden we use final access modifier for methods.
final variables : If a variable is declared as final ,it behaves like a constant . We cannot modify the value of final variable. Any attempt to modify the final variable results in compilation error. The error is as follows
“final variable cannot be assigned.”
49. Explain about abstract classes in java?
Sometimes we may come across a situation where we cannot provide implementation to all the methods in a class. We want to leave the implementation to a class that extends it. In such case we declare a class as abstract.To make a class abstract we use key word abstract. Any class that contains one or more abstract methods is declared as abstract. If we don’t declare class as abstract which contains abstract methods we get compile time error. We get the following error.
“The type <class-name> must be an abstract class to define abstract methods.”
Signature ; abstract class <class-name>
{
}
For example if we take a vehicle class we cannot provide implementation to it because there may be two wheeler , four wheeler etc. At that moment we make vehicle class abstract. All the common features of vehicles are declared as abstract methods in vehicle class. Any class which extends vehicle will provide its method implementation. It’s the responsibility of subclass to provide implementation.
The important features of abstract classes are :
- Abstract classes cannot be instantiated.
- An abstract classes contains abstract methods, concrete methods or both.
- Any class which extends abstract class must override all methods of abstract class.
- An abstract class can contain either 0 or more abstract methods.
Though we cannot instantiate abstract classes we can create object references . Through super class references we can point to subclass.
50. Can we create constructor in abstract class ?
We can create constructor in abstract class , it doesn’t give any compilation error. But when we cannot instantiate class there is no use in creating a constructor for abstract class.
51. What are abstract methods in java?
An abstract method is the method which doesn’t have any body. Abstract method is declared with keyword abstract and semicolon in place of method body.
Signature : public abstract void <method name>(); Ex : public abstract void getDetails();
It is the responsibility of subclass to provide implementation to abstract method defined in abstract class.
52. What is an exception in java?
In java exception is an object. Exceptions are created when an abnormal situations arises in our program. Exceptions can be created by JVM or by our application code. All Exception classes are defined in java.lang. In other words we can say Exception as run time error.
53. State some situations where exceptions may arise in java?
- Accessing an element that does not exist in array.
- Invalid conversion of number to string and string to number. (NumberFormatException)
- Invalid casting of class (Class cast Exception)
- Trying to create object for interface or abstract class (Instantiation Exception)
54. What is Exception handling in java?
Exception handling is a mechanism what to do when some abnormal situation arises in program. When an exception is raised in program it leads to termination of program when it is not handled properly. The significance of exception handling comes here in order not to terminate a program abruptly and to continue with the rest of program normally. This can be done with help of Exception handling.
55. What is an error in Java?
Error is the subclass of Throwable class in java. When errors are caused by our program we call that as Exception, but some times exceptions are caused due to some environment issues such as running out of memory. In such cases we can’t handle the exceptions. Exceptions which cannot be recovered are called as errors in java.
Ex : Out of memory issues.
56. What are advantages of Exception handling in java?
- Separating normal code from exception handling code to avoid abnormal termination of program.
- Categorizing in to different types of Exceptions so that rather than handling all exceptions with Exception root class we can handle with specific exceptions. It is recommended to handle exceptions with specific Exception instead of handling with Exception root class.
- Call stack mechanism : If a method throws an exception and it is not handled immediately, then that exception is propagated or thrown to the caller of that method. This propagation continues till it finds an appropriate exception handler ,if it finds handler it would be handled otherwise program terminates abruptly.
57. In how many ways we can do exception handling in java?
We can handle exceptions in either of the two ways :
- By specifying try catch block where we can catch the exception.
- Declaring a method with throws clause .
58. List out five keywords related to Exception handling ?
- Try
- Catch
- throw
- throws
- finally
59. Explain try and catch keywords in java?
In try block we define all exception causing code. In java try and catch forms a unit. A catch block catches the exception thrown by preceding try block. Catch block cannot catch an exception thrown by another try block. If there is no exception causing code in our program or exception is not raised in our code JVM ignores the try catch block.
Syntax :
try { // code lines here } Catch(Exception e) { // exception handling code here }
60. Can we have try block without catch block?
Each try block requires at-least one catch block or finally block. A try block without catch or finally will result in compiler error. We can skip either of catch or finally block but not both.