Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
Java - Packages and Access Control
What will be the output of the following code?
import java.util.HashSet;
public class Demo {
  public static void main(String[] args) {
    HashSet set = new HashSet<>();
    set.add("Code");
    System.out.println(set.contains("Code"));
  }
}
Afalse
Btrue
CCode
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand HashSet behavior

    The contains method checks if the element exists in the set.
  2. Step 2: Analyze code

    "Code" is added, so set.contains("Code") returns true.
  3. Final Answer:

    true -> Option B
  4. Quick Check:

    Contains returns true if element exists [OK]
Quick Trick: contains() returns true if element is present [OK]
Common Mistakes:
  • Confusing contains() with get()
  • Expecting the element itself as output
  • Assuming compilation error due to import

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes