DevLabs Alliance - WhatsApp
DevLabs Alliance Logo

Home / Interview /Top 20 Basic Java I...

Top 20 Basic Java Interview Questions for SDET – 2024

Admin 1

2023-09-07

DevLabs Alliance Interview

0 mins read

DevLabs Alliance Interview

Similar Blogs

How can a QA or Test engineer become SDET?

0 mins read

2023-09-12


Eclipse Cheat Sheet

0 mins read

2023-09-13


How to write a Good Test Case in Software Testing

0 mins read

2023-09-14


How to Prepare for SDET Interview

0 mins read

2024-03-14


Selenium Career Opportunities Why should you master Selenium WebDriver

0 mins read

2023-09-14


How to Handle Alerts and Pop-ups in Selenium?

0 mins read

2023-09-13


How To Use DataProvider in TestNG – Parameterization

0 mins read

2023-09-13


What Is TestNG Annotations-Benefits, Hierarchy

0 mins read

2023-09-13


How to Download & Install Selenium WebDriver in Eclipse (Step-By-Step)

0 mins read

2023-09-13


What Are The Skills Required for SDET?

0 mins read

2023-09-13


Q1. What are the various access specifiers for Java classes?

There are 4 types of access specifiers in java for SDET:


  • Public: Public class, public methods, or public variables can be accessed from anywhere.


  • Protected: Protected methods or fields can be accessed from the same class, or subclass or from the class within the same package.


  • Private: Private methods or fields can be accessed only from the same class to which they belong.


  • Default: Default methods or Default fields or Default class can be accessed only from the same class or from the same package.


Q2. Explain OOPS principles in Java?

The OOPS principles in Java are as follows:


  • Abstraction: Abstraction is used to hide internal details and showing only the functionality. In Java, we use abstract class and interface to achieve abstraction.


  • Encapsulation: Encapsulation means to bind or wrap code and data together into a single unit. A Java class is an example of Encapsulation.


  • Polymorphism: Polymorphism means that one task is performed in different ways. There are 2 types of Polymorphism: Run-time Polymorphism and Compile-Type polymorphism.


  • Inheritance: Inheritance means acquiring all the properties and behavior of an object. It provides code re-usability.


Q3. What is the difference between an Inner class and a sub-class?

An inner class is defined as a class which is nested within another class. An inner class can access all methods and variables that are defined in the outer class.


Sub-class is defined as a class which is inherited from some another class which is called a superclass. A subclass can access only public and protected method and fields of its superclass.


Q4. Can we overload the main() method?

Yes, we can overload main() method like other methods.


But when we run the program, the program doesn’t execute the overloaded main method, so we need to call the overloaded main method from the actual main method only.


Q5. What is Final keyword in Java?

In Java for SDET, Final keyword can be used with variable, method and class.


If a variable is declared using Final keyword, then its value can be assigned only once and after assignment, it can’t be changed.


If a method is declared using Final keyword, then this method cannot be overridden by the subclasses.

If a class is declared using Final keyword, then this class cannot be inherited.


Q6. What is the difference between an Abstract Class and Interface?

Abstract ClassInterfaceIt can have abstract and non-abstract methodsIt can have abstract methods only.


Abstract keyword is used to declare an abstract class Interface keyword is used to declare an interface.It can extend another Java class and implement multiple Java interfaces.It can extend another Java interface onlyIt can have class members like private, protected, etc.Members of Interface are public by defaultIt can have final, non-final, static and non-static variablesIt has only static and final variables.It can provide the implementation of interfaceIt can’t provide the implementation of abstract class


Q7. What is the difference between Local variables and Instance variables?

Local Variables: Local Variables are defined within a block or method or constructor.

These variables are created when the block is entered and destroyed after exiting from the block. We can access these variables only within the block where they are declared. We cannot use any access specifier with local variables.


Instance Variables: Instance variables are declared in a class outside any method, block or constructor.

These variables are created when an object of the class is created and destroyed when the object is destroyed. We can access these variables only by creating objects.


We can use access specifiers for instance variables. If no access specifier is specified then the default access specifier is used.


Java Tutorials

Q8. Can we declare the main method of our class as private?

The main method must always be public static in order to run any application correctly in Java. If the main method is declared as private, then it will not give any compilation error but the program will not get executed and will give a run-time error.


Q9. What do you mean by Constructor and what is the need of Constructor?

A constructor is defined as a member function in a class which has the same name as of class.

It is automatically called at the run-time whenever an object class is created. It doesn’t have any return type.


Need of Constructor:There might be a situation where exposing the class variable to the main program is not secured. At that time the class variables can be declared as private because the private variables are not accessible from the other class. In such a situation, if constructors are defined, then the main method need not to access the class variables directly.


Q10. How many types of constructors are used in Java?

There are 2 types of constructors in Java:


  • Default Constructor: In Java, the Default constructor is the one which does not accept any input. It is used to initialize the instance variable with the default values and is majorly used for object creation. A default constructor is invoked implicitly by the compiler if no constructor is defined by the user.


  • Parameterized Constructor: In Java, the Parameterized constructor is the one which is capable of initializing the instance variables with the given values. In other words, those constructors that accept the arguments are called parameterized constructors.


Q11. What is the difference between this() and super() in Java?


this() and super() both are used to call the constructor.


this()super()It represents the current instance of a class.It represents the current instance of parent class.It is used to call the default constructor of the same class.It is used to call the default constructor of the parent class.It is used for pointing the instance of a current class.It is used to point the instance of parent class.It is used to access methods of the current class.It is used to access the methods of parent class.


Q12. Is it necessary for a try block to be followed by a catch block in Java for Exception handling?


Try block always needs to be followed by either catch block or finally block or both.


If any exception is thrown from try block, then it should either be caught in catch block or if any specific tasks needs to be performed before abortion, then they are put in a finally block.


Q13. What is a Thread?


Thread is a piece of code which can be executed independently.


In Java, all program has at least one thread called as the main thread that is created by JVM. The main thread is used to invoke the main() method of program. Threads are executed concurrently.


Q14. What are the various ways to create a Thread?

The user can create their own threads by following 2 ways:


  • By extending Thread class:

public class Test extends Thread{public void test2() {}}


  • By Implementing Runnable Interface:

publicclass Test implements Runnable{ publicvoid test2() { }}


Q15. Explain Thread life cycle in Java?

New: In this state, the thread has been created but start() method is not invoked yet.


Runnable: The thread is in the runnable state after start() method is invoked but before the invocation of run() method.


Running: The thread is in running state after the invocation of run() method.


Blocked: The thread is alive but it is not eligible to run.


Terminated: The thread is in the terminated state when the run() method is completed


Q16. What is Method Overloading?

Method Overloading is a feature that allows a class to have more than one method with the same name and different parameters.


We can overload the method by either changing number of arguments or changing the data


public class Test {
public static int add(int a, int b) {return a+b};
public static int add(int a, int b, int c) {return a+b+c};
}
public class TestOverloadingMethod {
public static void main(String[] args)
{ System.out.println(Test.add(10,11));
System.out.println(Test.add(10,11,12)); }


Q17. What is Method Overriding?

Method Overriding is a feature that allows a sub-class(child class) to have the same method (with the same name and same parameters) as that in the superclass(parent class).

Method Overriding is used for run time polymorphism


public class Vehicle
{
void run()
{
System.out.println(“Vehicle is running”);
}
}
//Creating a child class
class Bike extends Vehicle{
public static void main(String args[]){
//creating an instance of child class
Bike obj = new Bike();
//calling the method with child class instance
obj.run();
}
}


Q18. What is meant by Collections in Java?

A collection is a group of objects represented as a single unit. It is a framework that provides an architecture to store the objects and manipulate a group of objects.

Collections are used to perform the following operations:


• Searching

• Sorting

• Manipulation

• Insertion

• Deletion


Q19. What is meant by Exception?


Exception is a problem that occurs when the normal flow of program is disrupted and therefore program or application terminates abnormally.


Exceptions are a subclass of java.lang.Exception


To maintain the normal flow of application we can handle the Exception through Exception handling.

Exception handling is a process to handle run-time errors such as IOException, SQLException, etc.


Q20. What are the types of Exception?

There are two types of Exception:


  • Checked Exceptions: Checked Exceptions are the exceptions that are checked at the time of compilation by compiler. These type of exceptions should be handled by the programmer.


  • Eg.: FileNotFoundException, SQLException,etc.


  • Unchecked Exceptions: Unchecked Exceptions are the exceptions that are not checked at the time of compilation but they occur at the time of execution. They are also called as Runtime Exceptions.


  • Eg.: ArithmeticException, NullPointerException.


👉Certified SDET Professional Training

👉Certified SDET Python Training

Meet The Author

DevLabs Alliance Author

Admin 1


HOD Neoload


DevLabs Alliance TwitterDevLabs Alliance LinkedInDevLabs Alliance Instagram

Author Bio

DevLabs Alliance conducts career transformation workshops & training in Artificial Intelligence, Machine Learning, Deep Learning, Agile, DevOps, Big Data, Blockchain, Software Test Automation, Robotics Process Automation, and other cutting-edge technologies.

INQUIRY

Want To Know More


Email is valid



Phone


By tapping continuing, you agree to our Privacy Policy and Terms & Conditions

“ The hands-on projects helped our team put theory into practice. Thanks to this training, we've achieved seamless collaboration, faster releases, and a more resilient infrastructure. ”
DevLabs Alliance Blogs Page Review
Vijay Saxena

SkillAhead Solutions

Lets get started today!

and get that DREAM JOB

DevLabs Alliance Footer section
DevLabs Alliance LinkedIn ProfileDevLabs Alliance Twitter ProfileDevLabs Alliance Facebook ProfileDevLabs Alliance Facebook Profile
DevLabs Alliance Logo

Gurgaon

USA

1603, Capitol Avenue, Suite 413A, 2659, Cheyenne, WY 82001, USA

DevLabs Alliance ISO 9001

DevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer Section

`Copyright © DevLabs Alliance. All rights Reserved`

|

Refund & Reschedule Policy

Privacy Policy

Terms of Use