Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q5 of 15
Selenium Java - Handling Windows, Frames, and Alerts
What will be the output of the following code?
Set handles = driver.getWindowHandles();
for (String handle : handles) {
    System.out.println(handle);
}

Assuming there are two windows open with handles "CDwindow-1" and "CDwindow-2".
APrints handles separated by commas in one line
BPrints only the first handle: CDwindow-1
CPrints both handles: CDwindow-1 and CDwindow-2 in any order
DThrows a runtime exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand iteration over Set of window handles

    The for-each loop prints each handle on a new line. The Set contains both handles.
  2. Step 2: Recognize order of Set iteration

    Set does not guarantee order, so handles may print in any order but both will be printed.
  3. Final Answer:

    Prints both handles: CDwindow-1 and CDwindow-2 in any order -> Option C
  4. Quick Check:

    Iterate Set to print all handles [OK]
Quick Trick: For-each loop prints all window handles [OK]
Common Mistakes:
  • Expecting only one handle to print
  • Thinking handles print in insertion order
  • Assuming handles print on same line

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes