Core Java Interview Questions

Q. Can we override static methods of a class?

Answer – No, we cannot override static method in java. Static methods are those which can be called without creating object of class, they are class level methods.

On other hand, If subclass is having same method signature as base class then it is known as method overriding. Its execution decided at run time.

Below are the reasons why we can’t override static method in java:-

Static methods are those which belong to the class. They do not belong to the object and in overriding, object decides which method is to be called.

Method overriding occurs dynamically(run time) that means which method is to be executed decided at run time according to object used for calling while static methods are looked up statically(compile time).

Q. Can a class have multiple constructors?

Answer – A class can have multiple constructors, as long as their signature (the parameters they take) are not the same. You can define as many constructors as you need. When a Java class contains multiple constructors, we say that the constructor is overloaded (comes in multiple versions). This is what constructor overloading means, that a Java class contains multiple constructors.

Q. What is the use of static variable?

Answer – A static variable is common to all the instances (or objects) of the class because it is a class level variable. In other words you can say that only a single copy of static variable is created and shared among all the instances of the class. Memory allocation for such variables only happens once when the class is loaded in the memory.

If you declare any variable as static, it is known as a static variable.

  • The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc.
  • The static variable gets memory only once in the class area at the time of class loading.
Advantages of static variable

It makes your program memory efficient (i.e., it saves memory).

Q. Difference between string Buffer and string Builder?

Answer –

1) Synchronization: StringBuffer methods are synchronized while StringBuilder methods are non-synchronized, it means that for thread-safe operations you must choose StringBuffer class instead of StringBuilder.

2) Performance: In a synchronized environment a single thread can perform a certain operation rather than distributing the work among multiple threads, which makes StringBuffer low performer as it is synchronized. StringBuilder performance is better than StringBuffer because it is not synchronized.

3) Which one to use: Operations (without considering the performance) are almost same in both the classes which means there is nothing in StringBuffer which cannot be done using StringBuilder. As discussed above the main thing which you need to consider while making a choice is thread-safety, if you think that the operation should be thread-safe then use StringBuffer, in all other cases StringBuilder is a better choice as it offers you the same functionality with better performance.

Similarities:
Unlike String, both StringBuffer and StringBuilder are mutable (can be modified).

Q. Can we use multiple catch blocks with try block?

Answer –

  • Highest priority would be always given to first catch block. If the first catch block cannot handle the identified exception object then it considers the immediate next catch block.
  • A single try block can have any number of catch blocks.
  • A generic catch block can handle all the exceptions. Whether it is ArrayIndexOutOfBoundsException or ArithmeticException or NullPointerException or any other type of exception, this handles all of them. To see the examples of NullPointerException and ArrayIndexOutOfBoundsException
catch(Exception e){
  //This catch block catches all the exceptions
}
  • If no exception occurs in try block then the catch blocks are completely ignored.

  • Corresponding catch blocks execute for that specific type of exception:
    catch(ArithmeticException e) is a catch block that can hanlde ArithmeticException
    catch(NullPointerException e) is a catch block that can handle NullPointerException
Q. What OOPS Concept?

Answer – Four features are the main OOPs Concepts that you must learn to understand the Object Oriented Programming in Java

Abstraction

As we aware abstraction is a process of hiding the implementation details from the user and showing only relevant details to the user. It also helps to reduce programming complexity and efforts.

In our automation framework whenever we use POM design pattern we write all locators in page class and we use this locators in our test methods.

Encapsulation

Encapsulation is a mechanism of wrapping data and code together in a single unit. In our framework we use customize webdriver action class where we put all the browser action in a single class.

Encapsulation is one of the best Java OOPs concepts of wrapping the data and code. In this OOPs concept, the variables of a class are always hidden from other classes. It can only be accessed using the methods of their current class. For example – in school, a student cannot exist without a class.

Inheritance

The process by which one class acquires the properties and functionalities of another class is called inheritance. Inheritance provides the idea of reusability of code and each sub class defines only those features that are unique to it, rest of the features can be inherited from the parent class.

Inheritance is a process of defining a new class based on an existing class by extending its common data members and methods.

Inheritance allows us to reuse of code, it improves reusability in your java application.

The parent class is called the base class or super class. The child class that extends the base class is called the derived class or sub class or child class.

Note: The biggest advantage of Inheritance is that the code in base class need not be rewritten in the child class.
The variables and methods of the base class can be used in the child class as well.

In our framework we create base class to initialize web driver interface, waits, reports etc. and we extend this base class to other class.

Polymorphism

Polymorphism is an object oriented programming feature that allows us to perform a single action in different ways.

Types of Polymorphism
1) Static Polymorphism
2) Dynamic Polymorphism

Static Polymorphism:

Polymorphism that is resolved during compiler time is known as static polymorphism. Method overloading can be considered as static polymorphism example.
Method Overloading: This allows us to have more than one methods with same name in a class that differs in signature.

Dynamic Polymorphism

It is also known as Dynamic Method Dispatch. Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime rather, that’s why it is called runtime polymorphism.

Q. What is interface and Abstract class?

Answer –

Sr. No.KeyAbstract ClassInterface
1Supported MethodsAbstract class can have both an abstract as well as concrete methods.Interface can have only abstract methods. Java 8 onwards, it can have default as well as static methods.
2Multiple InheritanceMultiple Inheritance is not supported.Interface supports Multiple Inheritance.
3Supported Variablesfinal, non-final, static and non-static variables supported.Only static and final variables are permitted.
4ImplementationAbstract class can implement an interface.Interface can not implement an interface, it can extend an interface.
5KeywordAbstract class declared using abstract keyword.Interface is declared using interface keyword.
6InheritanceAbstract class can inherit another class using extends keyword and implement an interface.Interface can inherit only an interface.
7InheritanceAbstract class can be inherited using extends keyword.Interface can only be implemented using implements keyword.
8AccessAbstract class can have any type of members like private, public.Interface can only have public members.
Q. What are access modifiers?

Answer – There are two types of modifiers in Java: access modifiers and non-access modifiers.

The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it.

There are four types of Java access modifiers:

  1. Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
  2. Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
  3. Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
  4. Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.

There are many non-access modifiers, such as static, abstract, synchronized, native, volatile, transient, etc.

Leave a comment

Design a site like this with WordPress.com
Get started