Groovy Quiz 9 | For Loop


Reference video | Groovy Playground


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

1.

Traditional for loop – Guess the output

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

for(int num=1; num<5; num++){
    println num
}
 
 
 

2.

for in loop – Guess the output

for (num in 1..5){
    println num
}
 
 
 
 

3.

Guess the output

1.upto(5){
    println "I love Groovy"
}
 
 

4.

Guess the Output

1.upto(5){
    println "$it"
}
 
 

5.

Guess the output

5.times{
    println "I Love Programming"
}
 
 

6.

Guess the output

Run and check here – https://groovy-playground.appspot.com/

5.times{
    println "$it"
}
 
 

7.

Guess the output

1.step(10,2){
    println "$it"
}
 
 
 

8.

Guess the output

x = 0
for ( i in [0, 1, 2, 3, 4] ) {
    x += i
}
println x
 
 
 

9.

Guess the output

def map = ["name":"Raghav", "subject":"Groovy"]

for (entry in map){
    println entry
}
 
 

10.

Guess the output

def map = ["name":"Raghav", "subject":"Groovy"]
for (entry in map){
    println entry.key
}
 
 
 

11.

Guess the output

def map = ["name":"Raghav", "subject":"Groovy"]
for (entry in map){
    println entry.value
}
 
 

11