Java interview Questions And Answers | Software Testing Material

Most Frequently asked Java Interview Questions

In this post, we see Core Java Interview Questions for Freshers and Experienced. This post covers Core Java Interview Questions for Selenium Automation Testers too. This post on Java Interview Questions is prepared with basic and important concepts of Java with examples for your easy understanding.

Java Interview Questions

Don’t miss Java Quiz

Related Posts:

Java Quiz

Most Popular Java Interview Questions

Let’s move on and see this comprehensive list of most important and commonly asked basic and advanced Java programming Interview Questions with answers.

1. What is Java?

Java is a programming language and computing platform first released by Sun Microsystems in 1995. There are lots of applications and websites that will not work unless you have Java installed, and more are created every day. Java is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!

2. Mention some features of Java?

Some of the features which play important role in the popularity of java are as follows:

  • Simple: Java is easy to learn. Eventhough Java is based on C++ , it was developed by eliminating poor programming practices of C++.
  • Object-Oriented: Java is a object oriented programming language. Everything in Java is an Object.

Learn more here.

  • Portable: Java run time environment uses a bytecode verification process to make sure that code loaded over the network doesn’t violate Java security constraints.
  • Platform independent: Java is platform independent. Java is a write once, run anywhere language. Without any modifications, we can use a program in different platforms.
  • Secured: Java is well known for its security. It delivers virus free systems.
  • High Performance: Java enables high performance with the use of JIT (Just-In-Time) compilers
  • Multithreaded: Java Multithreaded features allows us to write programs that can perform many tasks simulatenously. Multithreading concept of Java shares a common memory area. It doesn’t occupy memory for each thread.

3. What is the difference between Declaration and Definition in Java?

Declaration: If you just declare a class or method/function or variable without mentioning anything about what that class or method/function or variable looks like is called as declaration in Java.

Definition: If you define how a class or method/function or variable is implemented then it is called definition in Java.

When we create an interface or abstract class, we simply declare a method/function but not define it.

For clear understanding, check the below image

Declaration Vs Definition

4. What is an Object in Java?

An object is an instance of a class. Objects have state (variables) and behavior (methods).

Example: A dog is an object of Animal class. The dog has its states such as color, name, breed, and behaviors such as barking, eating, wagging her tail.

5. What is a Class in Java?

A class can be defined as a collection of objects. It is the blueprint or template that describes the state and behavior of an object.

6. What is Constructor in Java?

Constructor in Java is used in the creation of an Object that is an instance of a Class. Constructor name should be same as class name. It looks like a method but its not a method. It wont return any value. We have seen that methods may return a value. If there is no constructor in a class, then compiler automatically creates a default constructor.

7. What is Local Variable and Instance Variable?

Local Variable:

Local variable is a variable which we declare inside a Method. A method will often store its temporary state in local variables.

Instance Variable (Non-static):

Instance variable is a variable which is declared inside a Class but outside a Method. We don’t declare this variable as Static because these variables are non-static variables.

Read more about Variables in Java here

8. What are the OOPs concepts?

OOPS Stands for Object Oriented Programming System. It includes Abstraction, Encapsulation, Inheritance, Polymorphism, Interface etc.,

Read more on OOPs concept in Java

9. What is Inheritance in Java?

Inheritance is a process where one class inherits the properties of another class. Read more here

Inheritance In Java

10. What is Polymorphism?

Polymorphism allows us to perform a task in multiple ways. Let’s break the word Polymorphism and see it, ‘Poly’ means ‘Many’ and ‘Morphos’ means ‘Shapes’. Read more here

Polymorphism In Java

Assume we have four students and we asked them to draw a shape. All the four may draw different shapes like Circle, Triangle, and Rectangle.

11. What are the types of Polymorphism?

There are two types of Polymorphism in Java

1. Compile time polymorphism (Static binding) – Method overloading
2. Runtime polymorphism (Dynamic binding) – Method overriding

We can perform polymorphism by ‘Method Overloading’ and ‘Method Overriding’

12. What is Method Overloading?

A class having multiple methods with same name but different parameters is called Method Overloading

There are three ways to overload a method.

  • Parameters with different data types
  • Parameters with different sequence of a data types
  • Different number of parameters

Read more on Method Overloading in Java

13. What is Method Overriding?

Declaring a method in child class which is already present in the parent class is called Method Overriding.

In simple words, overriding means to override the functionality of an existing method.

In this case, if we call the method with child class object, then the child class method is called. To call the parent class method we have to use super keyword.

Read more on Method Overriding

14. What is Abstraction in Java?

Abstraction is the methodology of hiding the implementation of internal details and showing the functionality to the users.

Abstraction In Java

Example: Mobile Phone.

A layman who is using mobile phone doesn’t know how it works internally but he can make phone calls.

15. What is Abstract Class in Java?

We can easily identify whether a class is an abstract class or not. A class which contains abstract keyword in its declaration then it is an Abstract Class.

Syntax:

Points to remember:

  • Abstract classes may or may not include abstract methods
  • If a class is declared abstract then it cannot be instantiated.
  • If a class has abstract method then we have to declare the class as abstract class
  • When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

16. What is Abstract Method?

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

In order to use an abstract method, you need to override that method in sub class.

17. What is Interface in Java?

An interface in Java looks similar to a class but both the interface and class are two different concepts. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract. We can achieve 100% abstraction and multiple inheritance in Java with Interface. Read more on Interface in Java.

18. What is Encapsulation in Java?

Encapsulation is a mechanism of binding code and data together in a single unit. Let’s take an example of Capsule. Different powdered or liquid medicines are encapsulated inside a capsule. Likewise in encapsulation, all the methods and variables are wrapped together in a single class. Read more on Encapsulation in Java

Encapsulation In Java

19. Write a program to print the pattern given below

Here is the program to print the pattern mentioned above

20. Write a program to print Fibonacci Series up to count 10.

21. How to reverse a String in Java?

Another method:

22. How To Find The Largest Value From The Given Array.

23. How to display all the prime numbers between 1 and 100

The number which is only divisible by 1 and itself is known as a prime number. For example 2, 3, 5, 7, 11… are prime numbers.

24. How to display all the prime numbers between 1 and n (n is the number, get the input from user)

25. How to find the given number is a prime number or not by getting input from the user

26. Write a program to print Fibonacci Series

Method 1:

Method 2:

27. Difference between Array and ArrayList?

ArrayArrayList
Array is staticArrayList is dynamic
Size of the array should be given at the time of array declaration. We cannot change the size of array after creating itSize of the array may not be required. It changest the size dynamically. Capacity of ArrayList increases automatically whenever we add elements to an ArrayList
Array can contain both primitive data types as well as objectsArrayList cannot contain primitive data types. It contains only objects
Arrays are multidimensionalArrayList is always single dimension

28. Difference between ArrayList and HashSet in Java?

ArrayListHashSet
ArrayList implements List interfaceHashSet implements Set interface
ArrayList allows duplicatesHashSet doesn’t allow duplicates
ArrayList is an ordered collection and maintains insertion order of elementsHashSet is an unordered collection and doesn’t maintain insertion order
ArrayList is backed by an ArrayHashSet is backed by an HashMap instance
ArrayList is an index basedHashSet is object based
In ArrayList, we can retrive object by calling get() method or remove object by calling remove() methodIn HashSet, we can’t achieve get() method

Learn more on Array and ArrayList with sample programs

29. What are the different access modifiers available in Java?

Access modifiers are subdivided into four types such as Default, Public, Private, Protected

Access Modifiers In Java

default: The scope of default access modifier is limited to the package only. If we do not mention any access modifier, then it acts like a default access modifier.

private: The scope of private access modifier is only within the classes.

Note: Class or Interface cannot be declared as private

protected: The scope of protected access modifier is within a package and also outside the package through inheritance only.

Note: Class cannot be declared as protected

public: The scope of public access modifier is everywhere. It has no restrictions. Data members, methods and classes that declared public can be accessed from anywhere.

See some sample programs of access modifiers

30. Difference between static binding and dynamic binding?

1. Static binding is also known as early binding whereas dynamic binding is also known as late binding.
2. Determining the type of an object at compile time is Static binding whereas determining the type of an object at run time is dynamic binding
3. Java uses static binding for overloaded methods and dynamic binding for overridden methods.

To know more about this you have to go through Method Overloading and Method Overriding.

31. Difference between Abstract Class and Interface?

ABSTRACT CLASSINTERFACE
To declare Abstract class we have to use abstract keywordTo declare Interface we have to use interface keyword
In an Abstract class keyword abstract is mandatory to declare a method as an abstractIn an Interface keyword abstract is optional to declare a method as an abstract. Compiler treats all the methods as abstract by default
An abstract class contains both abstract methods and concrete methods(method with body)An interface can have only abstract methods
An abstract class provides partial abstractionAn interface provides fully abstraction
An abstract class can have public and protected abstract methodsAn interface can have only public abstract methods
An abstract class can have static, final or static final variables with any access modifiersAn interface can have only public static final variables
An abstract class can extend one class or one abstract classAn interface can extend any number of interfaces
Abstract class doesn't support multiple inheritanceInterface supports multiple inheritance

32. What is Multiple Inheritance?

If a class implements multiple interfaces, or an interface extends multiple interfaces then it is known as multiple inheritance.

We will update this post “Java Interview Questions For Selenium Testers” ASAP. Keep visiting.

If you like this post, share it with your friends.

Learn Java with Sample Programs

Related Posts:

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

3 thoughts on “Java interview Questions And Answers | Software Testing Material”

  1. hi,
    Rajkumar Sir i want to know more about related to selenium and more questions on java related to selenium will you plz provide more questions on java

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap