This is Part 1 in the series of SDET Interview questions and answers in Java

1. What is the difference between JDK, JRE, and JVM?

JDK is Java Development Kit it is used for Developing Java Application

JRE is Java Runtime Environment that is used for running java programs/application

JVM is Java Virtual Machine that provides a Platform Independent execution environment for running your code.

2. What are the different types of constructors in Java? And How to overload Constructor?

Constructor is a block of code, which will have a name as the Class name. The constructor will be executed when a new instance of the class is created.

There are two types of Constructor

Default Constructor

Default constructor will not have any parameter, If you don’t define it then compiles creates one automatically. Default constructor will be called by compiler if there is no other constructor.

Example Code Snippet

class Academy {

    // Constructor

    Academy() {

          System.out.println("Constructor called");

    }

}

public class SampleClass {

    public static void main(String args[]) {

            // this invokes default constructor.

          Academy academy = new Academy();

}  

}

           

Output:

Constructor called

Parameterized Constructor

The parameterized constructor will have parameters, this is mainly used for initialization purposes. You can pass different values to a parameterized constructor at the time of object creation, but values should match the constructor signature

Example Code Snippet

    class Academy {

    int a=0;

    int b=0;

    // Parameterized Constructor

    Academy(int a, int b) {

          System.out.println("Constructor called");

          System.out.println("Addition of "+a+" and "+b+" = "+(a+b));

    }

}

public class SampleClass {

          public static void main(String args[]) {

                      // this invokes default constructor.

                Academy academy = new Academy(10,20);

          }

}

Output:

Constructor called

Addition of 10 and 20 = 30

3. What is the static keyword? How it can be used?

The static indicates that the particular member belongs to a type itself, rather than to an instance of that type. That means a static member of the class can be accessed before creating any object for that class.

Static keywords can be used for

  • Variable, also called static fields or Class Variables
  • Method
  • Block
  • Nested Class

Static Variable or Static Fields:

When you prefix fields or variables with static which is called a static variable. When you create the static variable single copy of that variable is created and shared among all instances of an object.

  • The static variable associated with the class can be accessed directly using the class name. The object reference is not needed.
  • Static variables are created at the class level.

Example Code Snippet

public class SampleClass{

    //Example of static variable

    public static int exampleVariable =10;

}

Static method

When you create a method and prefix with a static keyword it becomes static methods, static methods belong to the class, not to any object.

  • The static method can be accessed using class name no need for any object reference. The main method is the example of a static method which is commonly used
  • A static method cannot be overridden
  • You cannot create abstract method using static keyword
  • this or super keyword cannot be used for static methods

Example Code Snippet

public class SampleClass{

    public static void sampleMethod(String name) {

          System.out.println(name);

    }

}

Static Block

A block of code which is used with static keyword is known as static block. Static block is mainly used for initializing static variables. You can have multiple static block inside single class. Static block will be executed before main method

Example Code Snippet

public class SampleClass{

    public static List<String> fruits = new LinkedList<>();

    static {

          fruits.add("Apple");

          fruits.add("Banana");

          fruits.add("Kiwi");

    }

   

    public static void main(String args[]) {

          System.out.println(SampleClass.fruits.get(0));

    }

}

Output: Apple

Static Class

When you create a class inside another class it is called a nested class, static class only be created as a nested class. Members of the static class can be directly accessed using the outer class.

  • A static class cannot access non-static members of the Outer class
  • Static class members can be called with outer class reference
  • Static classes are always nested or inner class
  • You can use access modifier for static class

Example Code Snippet

public class SampleClass{

    //static class declared here

    static class SampleStaticClass{

          int a = 10;

    }

   

    public static void main(String args[]) {

          SampleClass.SampleStaticClass staticClass = new SampleClass.SampleStaticClass();

          System.out.println(staticClass.a);

    }

}

4. What is this final keyword? how I can be used?

The final keyword is used for restricting the behavior variable, method, class, etc.

The final keyword can be used with

  • Variable
  • Methods
  • Class

Final Variable:

  • If you prefix any variable with a final keyword then it is called the final variable.
  • Once the value is assigned to the final variable you cannot change it.
  • If you don’t initialize the final variable during declaration it is called blank final variable, it can be initialized through constructor.
  • A  final variable can be declared as a static
  • You can assign the final static variable during the declaration
  • If you have created a blank static final variable, it can be initialized inside the static block.

Example Code Snippet

public class FinalTest{

    public static void main(String args[]) {

        final int i = 10;

    }

}

Final Methods

If you prefix the final keyword to methods it becomes the final method

Final methods cannot be overridden

Static final method is completely valid in java, and it can be declared

Example Code Snippet

final void print() {

    int a = 10;

    System.out.println(a);

}

Final Class

If you prefix final to class, then it becomes the final class

Final classes cannot be extended or inherited

You can create the inner class as a static final

Example Code Snippet

public final class

{

   //Some Code

}

5. What is the difference between method overloading and method overriding?

Method overloading is class has more than one method of the same name but having different parameter. Method overriding Subclass provides the implementation of the method that has been declared by the Parent class is called method overriding.

Method overloading is an example of compile-time polymorphism. Method overriding is an example of run time polymorphism.

You cannot achieve overloading only by changing the return type. Method overriding will have usually the same return type.

Method overloading can be achieved without inheritance. Method overriding requires inheritance

Example Code Snippet for method overloading

//Method overloading

public class SampleClass{

    public void add(int a, int b, int c) {

          //some code

    }

    public void add(int a, int b) {

          //some code

    }

}

Example Code Snippet for Method overriding

//Method overriding

public class SampleClass{

    public void someMethod() {

          System.out.println("Super Class");

    }

}

class MyClass extends SampleClass{

    public void someMethod() {

          System.out.println("Sub Class");

    }

}

6. Could you explain IS-A and HAS-A relationship?

IS -A relationship

IS-A relationship is formed in inheritance, The class which inherit is known as subclass or child class. That means that child class is the type of parent class.

For example, Orange is a Fruit, you need to extend fruit class to make IS-A relationship 

Example Code Snippet  : IS-A Relationship

class Fruit{

    //some code

}

//IS-A relationship

class Orange extends Fruit {

//some code

}

HAS -A relationship

HAS-A relationship is composition, which means you are creating the object of a class inside another class. Let’s take the example of Room. The room has a chair. The room should contain chair class reference, i.e you need to create an instance of Chair particular class inside the Room class.

Example Code Snippet : HAS-A Relationship

class Room {

    Chair chair = new Chair();

}

7. Explain this and super keyword

this keyword is used to refer current class instance/object

this keyword can be used to

  • Current class instance variable
  • Invoke current class methods
  • Invoke current class constructor
  • Can be used to pass the argument to a method
  • Can be used to pass the argument to a constructor
  • Return the current class instance

Super Keyword

  • The super keyword is a reference variable that is used to refer to parent class objects
  • super can be used to refer to immediate parent class instance variable
  • super can be used to invoke the parent class method
  • super can be used to invoke parent class Constructor

Example Code Snippet : this and super keywords

class First {

    void print() {

          System.out.println("Printing First...");

    }

}

class Second extends First {

    void printOneMore() {

          System.out.println("Printing Second...");

    }

    void myMethod() {

          super.print();

          this.printOneMore();

    }

}

public class KeywordTest {

    public static void main(String args[]) {

          Second d = new Second();

          d.myMethod();

    }

}

8. How to check instance of Specified type? Explain with example

The Java provides instanceof operater which can be used to check instance of Specified type.

  • The instanceof operator returns either true or false
  • The instanceof operators are used for down casting
  • The instanceof operator works on the principle of the is-a relationship
  • If you instanceof Operator for null object it will return false.

Example Code Snippet

public class Apple {

    public static void main(String args[]) {

          Apple apl = new Apple();

          if (apl instanceof Apple) {

            System.out.println("apl is instance of Apple");

          } else {

             System.out.println("apl is not an instance of Apple");

          }

    }

}

9. What is a package in Java? What are different types of packages? Can we create a custom package?

  • A package is a group of similar interfaces, classes, and subclasses
  • Custom package can be created with the package keyword
  • Access modified can be used for packages
  • Import keyword is used for importing already created packages.
  • The package is a good example of java encapsulation
  • Packages that are inside another package are the sub-packages
  • Java provides many built-in packages and it allows you to create custom ones as well

Some of the built-in packages are

java.lang : This package contains the classes and interfaces that are fundamental to the core Java

java.util : This library provides users with generic java utilities like collections framework, formatted printing and scanning, array manipulation utilities, event model, date and time facilities, internationalization, and miscellaneous utility classes

java.io This library is very useful and contains the classes that handle fundamental input and output operations in Java

java.net : This package contains classes and interfaces that provide a powerful infrastructure for networking in Java.

java.sql : This package contains all the SQL-related classes and interfaces. This package provides the API for accessing and processing data stored in a data source.

We can create custom packages in Java

Example Code Snippet

package packageName; // Use package keyword for creation

public class ExampleClass{ 

 // Some Code Here

}

10. What is run time polymorphism in Java?

Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time.

  • The overridden method is called through the reference variable of a superclass
  • The object is determined by the object being referred to by the reference variable
  • The reference variable of Parent class refers to the object of Child class is called upcasting

class Car {

    void print() {

          System.out.println("Inside Car Class");

    }

}

class Honda extends Car {

    void print() {

          System.out.println("Inside Honda Class");

    }

}

public class MainClass {

    public static void main(String args[]) {

          Car car = new Honda();// This is Upcasting

          car.print();

    }

}

11. What is the difference between static and dynamic binding in java?

  • The binding which can be resolved by the compile-time is static binding. The binding which will be resolved at the run time is known as dynamic binding.
  • Type information is used for resolving static binding. Object information is used for resolving dynamic binding
  • Method overloading is an example of static binding. Method overriding is an example of dynamic binding
  • Static binding is also known as early binding. Dynamic binding is also known as late binding

Example Code Snippet: Static Binding

//Static binding

public class MainClass {

    void print() {

          System.out.println("Printing");

    }

    public static void main(String args[]) {

          MainClass mc = new MainClass();

          mc.print();

    }

}

Example Code Snippet: Dynamic binding

//Dynamic Binding

class Fruit {

    void print() {

          System.out.println("This is fruit class");

    }

}

public class Apple extends Fruit {

    void print() {

          System.out.println("This is Apple");

    }

    public static void main(String args[]) {

          Fruit f = new Apple();

          f.print();

    }

}

Course Alert

If you are interested in landing a SDET or Sr. QA Automation Engineer role, check out our popular SDET course to learn the fundamental skills required an SDET needs to perform at work. Also, I have another popular course which covers Top 150+ Interview questions most commonly asked in SDET / Automation interviews.

A Quick Note

Please remember all these courses comes with 100% Lifetime access and free updates for Lifetime!!

Please click on the link Sr.QA and SDET Interview Questions in Java – Part 2 to continue to part-2 series of this article.


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}