Core Java Interview Questions and Answers – Part 3

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:

  1. Identifiers must start with letter,Underscore or dollar($) sign.
  2. Identifiers can’t start with numbers .
  3. There is no limit on number of characters in identifier but not recommended to have more than 15 characters
  4. Java identifiers are case sensitive.
  5. First letter can be alphabet ,or underscore and dollar sign. From second letter we can have numbers
  6. 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

  1. In the same class
  2. In the same package subclass
  3. In the same package nonsubclass
  4. In the different package subclass
  5. In the different package non subclass.

default : A class with default modifier can be accessed

  1. In the same class
  2. In the same package subclass
  3. In the same package nonsubclass
  4. In the different package subclass
  5. 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

  1. In the same class
  2. In the same package subclass
  3. In the same package nonsubclass
  4. In the different package subclass
  5. In the different package non subclass.

default : When a method is declared as default, we can access that method in

  1. In the same class
  2. In the same package subclass
  3. In the same package non subclass

We cannot access default access method in

  1. Different package subclass
  2. Different package non subclass.

protected : When a method is declared as protected it can be accessed

  1. With in the same class
  2. With in the same package subclass
  3. With in the same package non subclass
  4. 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

  1. Same package subclass
  2. Same package non subclass
  3. Different package subclass
  4. 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

  1. In the same class
  2. In the same package subclass
  3. In the same package non subclass
  4. In the different package subclass
  5. In the different package non subclass.

default : When a variables is declared as default, we can access that method in

  1. In the same class
  2. In the same package subclass
  3. In the same package non subclass

We cannot access default access variables in

  1. Different package subclass
  2. Different package non subclass.

protected : When a variables is declared as protected it can be accessed

  1. With in the same class
  2. With in the same package subclass
  3. With in the same package non subclass
  4. 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

  1. Same package subclass
  2. Same package non subclass
  3. Different package subclass
  4. 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 :

  1. Abstract classes cannot be instantiated.
  2. An abstract classes contains abstract methods, concrete methods or both.
  3. Any class which extends abstract class must override all methods of abstract class.
  4. 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?

  1. Accessing an element that does not exist in array.
  2. Invalid conversion of number to string and string to number. (NumberFormatException)
  3. Invalid casting of class (Class cast Exception)
  4. 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?

  1. Separating normal code from exception handling code to avoid abnormal termination of program.
  2. 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.
  3. 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 :

  1. By specifying try catch block where we can catch the exception.
  2. Declaring a method with throws clause .

58. List out five keywords related to Exception handling ?

  1. Try
  2. Catch
  3. throw
  4. throws
  5. 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.

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories