Bird
0
0

What will be the output of the following code snippet?

medium📝 Predict Output Q4 of 15
Selenium Java - Handling Form Elements
What will be the output of the following code snippet?
WebElement checkbox = driver.findElement(By.id("subscribe"));
checkbox.click();
System.out.println(checkbox.isSelected());
AAlways false regardless of initial state
BAlways true regardless of initial state
Ctrue if checkbox was unchecked before click, false otherwise
DThrows an exception because isSelected() cannot be called after click()
Step-by-Step Solution
Solution:
  1. Step 1: Understand click() effect on checkbox

    Click toggles the checkbox state: checked becomes unchecked and vice versa.
  2. Step 2: Check isSelected() after click

    After clicking, isSelected() returns the new state, so if it was unchecked, it becomes true.
  3. Final Answer:

    true if checkbox was unchecked before click, false otherwise -> Option C
  4. Quick Check:

    Click toggles checkbox, isSelected() shows new state [OK]
Quick Trick: Click toggles checkbox; isSelected() shows current state [OK]
Common Mistakes:
  • Assuming isSelected() always returns true
  • Thinking isSelected() throws exception after click
  • Ignoring toggle effect of click()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes