Groovy Quiz 21 | Ranges


Reference video | Groovy Playground


1 Point for every correct answer | Let’s Get Started

1.

Is this statement True

Range is used to create a list of sequential values e.g 1..10

 
 

2.

Is the following statement true

There are 2 types of ranges

1. Inclusive e.g. 1..10 (will go from 1 to 10)

2. Exclusive e.g. 1..<10 (will go from 1 to 9)

 

 
 

3.

Guess the output

(Can run and check code here – https://groovy-playground.appspot.com/)

def myRange = 1..10
print myRange
 
 
 

4.

def myRange = 1..10
println myRange.size()
println myRange.getFrom()
println myRange.getTo()
 
 

5.

def myRange = 1..10
println myRange.from ==1
println myRange.to == 10
 
 
 
 

6.

def myRange = 1..10
println myRange.get(3)
println myRange[3]
 
 
 
 

7.

def myRange = 1..10
println myRange.contains(5)
println myRange.isReverse()
 
 
 

8.

def range1 = 10..1
def range2 = range1.subList(1,5)
println range2
 
 
 

9.

for(int num in 1..10){
    println num
}
 
 

10.

def range1 = 1..10
range1.each{ i ->
    println "$i"
}
 
 

7