MCQs on Collections and Data Structures | Groovy

Groovy, a dynamic language for the Java platform, offers powerful tools for working with collections and data structures, making it easier to manage and manipulate data. Key topics include Lists and Arrays, Maps, Iteration over Collections, and Data Manipulation. This guide will help you dive deep into Groovy’s versatile collection handling features.


1. Lists and Arrays

  1. Which of the following is a valid way to declare a list in Groovy?
    • a) def list = []
    • b) list = List()
    • c) def list = [1, 2, 3]
    • d) list = {}
  2. How do you add an element to a Groovy list?
    • a) list.add(5)
    • b) list.append(5)
    • c) list.insert(5)
    • d) list.push(5)
  3. Which method would you use to check if an element exists in a Groovy list?
    • a) list.contains()
    • b) list.exists()
    • c) list.search()
    • d) list.find()
  4. What is the default size of a Groovy list when initialized with no elements?
    • a) 1
    • b) 0
    • c) undefined
    • d) -1
  5. How would you access the second element of a list in Groovy?
    • a) list[1]
    • b) list[2]
    • c) list.get(1)
    • d) list.get(2)
  6. How do you remove the last element from a Groovy list?
    • a) list.removeLast()
    • b) list.delete()
    • c) list.pop()
    • d) list.remove()
  7. Which of the following is true about Groovy arrays?
    • a) Arrays are immutable in Groovy
    • b) Arrays in Groovy can only store integers
    • c) Arrays are mutable and can store any type of object
    • d) Arrays are fixed in size and cannot be resized
  8. What would the following code return in Groovy: def arr = [1, 2, 3]; arr[1..2]?
    • a) [2, 3]
    • b) [1, 2, 3]
    • c) [1, 2]
    • d) null
  9. Which of the following is used to iterate through a Groovy list?
    • a) forEach()
    • b) list.each()
    • c) iterate()
    • d) for()
  10. What method do you use to find the index of an element in a Groovy list?
    • a) indexOf()
    • b) findIndex()
    • c) getIndex()
    • d) position()

2. Maps

  1. Which of the following is the correct way to declare a map in Groovy?
    • a) def map = [:]
    • b) def map = {}
    • c) map = Map()
    • d) map = {}[]
  2. How do you add a new key-value pair to a map in Groovy?
    • a) map.add('key', 'value')
    • b) map['key'] = 'value'
    • c) map.insert('key', 'value')
    • d) map.put('key', 'value')
  3. What does the map.get() method do in Groovy?
    • a) Returns the value associated with the key
    • b) Removes the key-value pair from the map
    • c) Adds a new key-value pair to the map
    • d) Updates the value of the key in the map
  4. How do you remove a key-value pair from a map in Groovy?
    • a) map.delete('key')
    • b) map.remove('key')
    • c) map.pop('key')
    • d) map.removeAt('key')
  5. How can you check if a map contains a specific key in Groovy?
    • a) map.hasKey('key')
    • b) map.containsKey('key')
    • c) map.findKey('key')
    • d) map.keyExists('key')
  6. What will map.keySet() return in Groovy?
    • a) The values of the map
    • b) The keys of the map
    • c) The key-value pairs
    • d) The map size
  7. Which of the following will return all the values from a map in Groovy?
    • a) map.values()
    • b) map.getValues()
    • c) map.all()
    • d) map.each()
  8. What will map.each { key, value -> println "$key: $value" } do in Groovy?
    • a) It iterates over the map and prints each key-value pair
    • b) It removes all the elements from the map
    • c) It sorts the map
    • d) It updates all the values in the map
  9. How do you clear all elements from a Groovy map?
    • a) map.clear()
    • b) map.removeAll()
    • c) map.deleteAll()
    • d) map.clearAll()
  10. How do you iterate through the keys of a Groovy map?
    • a) map.keySet().each { println it }
    • b) map.eachKey { println it }
    • c) map.each { it.key }
    • d) map.keys().each { println it }

3. Iterating Over Collections

  1. Which Groovy method is commonly used to iterate over a list?
    • a) forEach()
    • b) each()
    • c) iterate()
    • d) do()
  2. How can you iterate over a range in Groovy?
    • a) 1..5.each { println it }
    • b) for 1..5 { println it }
    • c) iterate(1..5) { println it }
    • d) for i in 1..5 { println i }
  3. What does the eachWithIndex() method do in Groovy?
    • a) Iterates over each element and its index in a collection
    • b) Iterates over each collection in a list
    • c) Iterates only over the indexes of a collection
    • d) Iterates over each collection in reverse order
  4. How do you loop through both keys and values in a map using Groovy?
    • a) map.each { key, value -> println "$key: $value" }
    • b) map.keys().each { println key, value }
    • c) map.eachKey { println key }
    • d) map.eachValue { println value }
  5. How can you iterate over a collection in reverse order in Groovy?
    • a) collection.reverseEach()
    • b) collection.eachReverse()
    • c) collection.reverse()
    • d) collection.each { println it.reverse() }
  6. What is the outcome of list.each { println it } in Groovy?
    • a) It will print each element in the list
    • b) It will add new elements to the list
    • c) It will sort the list in ascending order
    • d) It will reverse the list
  7. How do you filter elements from a collection in Groovy?
    • a) collection.filter()
    • b) collection.select()
    • c) collection.each()
    • d) collection.findAll()
  8. What method do you use to transform each element in a list in Groovy?
    • a) each()
    • b) collect()
    • c) map()
    • d) transform()
  9. What does the find() method do on a Groovy collection?
    • a) Returns the first element that matches the condition
    • b) Returns the total number of elements
    • c) Filters out matching elements
    • d) Finds all elements that meet a condition
  10. How do you loop through elements in a list and apply a condition in Groovy?
    • a) list.each { if (it > 5) println it }
    • b) list.foreach { if (it > 5) println it }
    • c) list.eachIf { it > 5 println it }
    • d) list.eachIf { println it > 5 }

Answers

QnoAnswer
1a) def list = []
2a) list.add(5)
3a) list.contains()
4b) 0
5a) list[1]
6c) list.pop()
7c) Arrays are mutable and can store any type of object
8a) [2, 3]
9b) list.each()
10a) indexOf()
11a) def map = [:]
12b) map['key'] = 'value'
13a) Returns the value associated with the key
14b) map.remove('key')
15b) map.containsKey('key')
16b) The keys of the map
17a) map.values()
18a) It iterates over the map and prints each key-value pair
19a) map.clear()
20a) map.keySet().each { println it }
21b) each()
22a) 1..5.each { println it }
23a) Iterates over each element and its index in a collection
24a) map.each { key, value -> println "$key: $value" }
25a) collection.reverseEach()
26a) It will print each element in the list
27d) collection.findAll()
28b) collect()
29a) Returns the first element that matches the condition
30a) list.each { if (it > 5) println it }

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top