---Advertisement---

JAVA OOPS Interview Questions and Answers (Level-3)

By Manisha

Published On:

---Advertisement---

15. What are static methods and variables in Java? How are they used in utility classes?

Answer

In Java, the keyword static is used to declare members (variables or methods) that belong to the class rather than instances of the class.

🔹 Static Variable:

  • A static variable is shared across all instances of the class.
  • Memory is allocated only once, regardless of the number of objects created.
  • Changes made by one object will reflect across all others.

java

class Counter {

    static int count = 0;

    Counter() {

        count++;

    }

}

🔹 Static Method:

  • Can be called without creating an object.
  • Can only access other static members of the class directly.
  • Commonly used for utility/helper methods that don’t rely on object state.

java

public class MathUtil {

    public static int add(int a, int b) {

        return a + b;

    }

}

In Selenium Frameworks:

Utility classes like WaitUtils, ExcelUtils, or BrowserFactory use static methods:

java

public class WaitUtils {

    public static void waitForElement(WebDriver driver, By locator, int timeInSec) {

        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeInSec));

        wait.until(ExpectedConditions.visibilityOfElementLocated(locator));

    }

}

  • No need to create an object for WaitUtils — just call WaitUtils.waitForElement(…).

Benefits:

  • Improved memory efficiency.
  • Supports code modularity and reusability in test automation.

16. What is the difference between == and .equals() in Java? Provide examples.

Answer

In Java:

🔹 == Operator:

  • Compares object references (memory locations).
  • Returns true only if both references point to the same object in memory.

🔹 .equals() Method:

  • Compares actual values/content.
  • Defined in Object class, but overridden in most classes like String, List, etc.

🔹 Example:

java

String a = “hello”;

String b = “hello”;

String c = new String(“hello”);

System.out.println(a == b);       // true (string pool)

System.out.println(a == c);       // false (different memory)

System.out.println(a.equals(c));  // true (value comparison)

Real-World Selenium Use:

While validating text retrieved from elements:

java

String actual = driver.findElement(By.id(“greeting”)).getText();

String expected = “Welcome”;

if (actual.equals(expected)) {

    System.out.println(“Test Passed”);

}

Use .equals() for value-based comparisons in test scripts, and avoid == unless comparing object references intentionally.


17. Explain Constructor Overloading in Java with an example.

Answer

Constructor Overloading is when a class has multiple constructors with different parameter lists.

  • Helps initialize objects in different ways.
  • Promotes flexibility and code readability.

🔹 Example:

java

public class User {

    String name;

    int age;

    User() {

        this.name = “Guest”;

        this.age = 18;

    }

    User(String name) {

        this.name = name;

        this.age = 18;

    }

    User(String name, int age) {

        this.name = name;

        this.age = age;

    }

}

Selenium Framework Use:

Constructor overloading is widely used in Page Object Model (POM):

java

public class LoginPage {

    WebDriver driver;

---Advertisement---

Leave a Comment