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 dropdown = driver.findElement(By.id("colors"));
Select select = new Select(dropdown);
select.selectByIndex(2);
System.out.println(select.getFirstSelectedOption().getText());

Assuming the dropdown options are: 0: Red, 1: Green, 2: Blue, 3: Yellow
ABlue
BGreen
CYellow
DRed
Step-by-Step Solution
Solution:
  1. Step 1: Understand selectByIndex()

    The method selectByIndex(2) selects the option at index 2 (zero-based).
  2. Step 2: Identify option at index 2

    Given the options, index 2 corresponds to "Blue".
  3. Final Answer:

    Blue -> Option A
  4. Quick Check:

    selectByIndex(2) = Blue [OK]
Quick Trick: Index starts at 0; index 2 means third option [OK]
Common Mistakes:
  • Assuming index starts at 1
  • Confusing option text with value
  • Printing wrong selected option

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes