Enhanced For Loop (For-Each Loop) in Java - Quiz

  • A Single values only
  • B Only integers
  • C Elements of an array or a collection
  • D Strings only
  • A It can modify the array elements directly.
  • B It does not allow the use of an index.
  • C It can only be used with integer arrays.
  • D It requires explicit index management.
  • A for (int i : array) {}
  • B for (array : int) {}
  • C for each (int i in array) {}
  • D for (int i in array) {}
  • A Yes, directly.
  • B No, only arrays are supported.
  • C Yes, but it must be done using the keySet() method.
  • D Yes, but it can only access keys, not values.
  • A The program will compile successfully but may throw a runtime exception.
  • B The elements will be modified without any issues.
  • C The loop will stop executing.
  • D You cannot modify elements in a for-each loop.
  • A It throws a NullPointerException.
  • B It skips null values without any issues.
  • C It stops the loop execution.
  • D It prints "null" for null values.
  • A It is faster than traditional for loops.
  • B It simplifies syntax and enhances code readability.
  • C It provides access to element indices.
  • D It can only iterate over arrays.
  • A When you need to modify elements in the collection.
  • B When you need to access elements using their indices.
  • C When the collection contains a large number of elements.
  • D When iterating through a list of strings.