Bird
0
0

Given List<int> nums = new List<int> {7, 2, 9, 4, 6};

hard🚀 Application Q9 of 15
C Sharp (C#) - Collections
Given List<int> nums = new List<int> {7, 2, 9, 4, 6};
Which code snippet correctly finds the first number greater than 5 and removes it from the list?
Anums.Remove(nums.Find(x => x < 5));
Bnums.RemoveAt(nums.FindIndex(x => x > 5));
Cnums.Remove(nums.FindIndex(x => x > 5));
Dint val = nums.Find(x => x > 5); nums.Remove(val);
Step-by-Step Solution
Solution:
  1. Step 1: Find first number > 5

    nums.Find(x => x > 5) returns the first matching value.
  2. Step 2: Remove that value

    nums.Remove(val) removes the first occurrence of that value.
  3. Step 3: Check other options

    nums.RemoveAt(nums.FindIndex(x => x > 5)); removes by index correctly but is less clear; nums.Remove(nums.FindIndex(x => x > 5)); passes index to Remove which expects a value; nums.Remove(nums.Find(x => x < 5)); removes a number < 5 which is incorrect.
  4. Final Answer:

    int val = nums.Find(x => x > 5); nums.Remove(val); -> Option D
  5. Quick Check:

    Find returns value, Remove removes by value [OK]
Quick Trick: Find returns value; Remove removes by value [OK]
Common Mistakes:
MISTAKES
  • Passing index to Remove instead of RemoveAt
  • Removing wrong condition
  • Confusing Find and FindIndex usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes