Bird
0
0

Which of the following is the correct way to declare a BasePage class constructor that accepts a WebDriver instance in Java?

easy📝 Syntax Q12 of 15
Selenium Java - Page Object Model
Which of the following is the correct way to declare a BasePage class constructor that accepts a WebDriver instance in Java?
Apublic BasePage(WebDriver driver) { this.driver = driver; }
Bpublic void BasePage(WebDriver driver) { this.driver = driver; }
CBasePage(WebDriver driver) { driver = this.driver; }
Dpublic BasePage() { WebDriver driver; }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct constructor syntax

    In Java, constructors have no return type and match the class name exactly.
  2. Step 2: Check parameter assignment

    The constructor should assign the passed driver to the class field using this.driver = driver;.
  3. Final Answer:

    public BasePage(WebDriver driver) { this.driver = driver; } -> Option A
  4. Quick Check:

    Constructor no return + this.driver = driver [OK]
Quick Trick: Constructor has no return type and uses this.driver = driver [OK]
Common Mistakes:
MISTAKES
  • Adding void return type to constructor
  • Reversing assignment order
  • Declaring driver inside constructor without class field

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes