Groovy Quiz 8 | Conditional Statements


Reference video | Groovy Playground


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

1.

Guess the output

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

def age = 17
if (age >= 10){
    print "You are eligible to drive"
}
else{
    print "You are not eligible to drive"
}
 
 
 

2.

Guess the output

def num = 0
if (num > 0){
    println "num is positive"
}
else if(num < 0){
    println "num is negative"
}
else{
    println "num is zero"
}
 
 
 

3.

Guess the output

def num = -1
switch(num){
    case 0: 
        println "num is zero"
        break
    case {num>0}:
        println "num is positive"
        break
    case {num<0}:
        println "num is negative"
        break
}
 
 
 
 

4.

Guess the output.

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

def num = 'A'
switch(num){
    case 0: 
        println "num is zero"
        break
    case {num>0}:
        println "num is positive"
        break
    case {num<0}:
        println "num is negative"
        break
    default:
        println "invalid input"
}
 
 
 
 

5.

Guess the output:

def num = 'abc'
switch(num){
    case 0: 
        println "num is zero"
        break
    case {num>0}:
        println "num is positive"
        break
    case {num<0}:
        println "num is negative"
        break
    default:
        println "invalid input"
}
 
 

11