What is the difference between JDK, JRE, and JVM?
Explain OOP concepts and how they are applied in Java.
extends
keyword.What is polymorphism? Can you provide an example?
class Animal {
void sound() { System.out.println("Animal makes sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Dog barks"); }
}
Animal obj = new Dog();
obj.sound(); // Output: Dog barks
What is the difference between an interface and an abstract class in Java?
What are the different types of exceptions in Java? How do you handle exceptions?
IOException
, SQLException
).NullPointerException
, ArithmeticException
).try
, catch
, finally
, and throw
keywords.Explain the concept of collections in Java. What is the difference between ArrayList
and LinkedList
?
ArrayList
: Resizable array. Provides constant-time access but slower insertion and deletion at random positions.LinkedList
: Doubly linked list. Provides efficient insertion and deletion but slower access by index.What is the difference between Set
, List
, and Map
in Java?
HashSet
, TreeSet
).ArrayList
, LinkedList
).HashMap
, TreeMap
).What is synchronization, and why is it important in Java?
Explain the concept of immutability in Java with examples (e.g., String class).
String
is immutable, meaning once a string is created, it cannot be modified. Any modification creates a new String
object.What is the purpose of the final
, finally
, and finalize
keywords?
final
: Used to declare constants, prevent inheritance, or prevent method overriding.finally
: Block in exception handling that always executes, regardless of whether an exception is handled or not.finalize
: A method called by the garbage collector before an object is destroyed.Can you explain the purpose of the static
keyword in Java?
static
keyword is used for memory management. It makes fields and methods belong to the class rather than any instance of the class.What are Java generics? How are they used in automation frameworks?
What is multithreading in Java, and how is it useful in automation testing?
How does garbage collection work in Java?
Explain the concept of this
and super
keywords in Java.
this
: Refers to the current instance of the class.super
: Refers to the superclass instance. Used to access superclass methods, fields, or constructors.What is Selenium WebDriver, and how is it used with Java for automation?
How would you handle web elements that are dynamically loaded in a webpage using Selenium?
WebDriverWait
) to wait for elements to load dynamically.Explain how you can handle multiple windows or pop-ups in Selenium WebDriver.
getWindowHandles()
to switch between windows, and switchTo().window()
to control them. Example:
String mainWindow = driver.getWindowHandle();
Set allWindows = driver.getWindowHandles();
for (String window : allWindows) {
driver.switchTo().window(window);
}
What is TestNG, and why is it commonly used in automation testing with Java?
@Test
, @BeforeMethod
), and better test configuration and reporting.How do you handle synchronization issues in Selenium (explicit vs. implicit waits)?
Can you explain the Page Object Model (POM) design pattern and its advantages?
What is a WebElement
, and how do you locate elements on a webpage using Java?
WebElement
represents an element in the DOM. Elements are located using locators like id
, name
, xpath
, cssSelector
,
WebElement element = driver.findElement(By.id("example"));
How would you capture a screenshot in Selenium WebDriver?
TakesScreenshot
interface to capture screenshots:
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
What are the best practices for writing maintainable automation tests using Java?