Groovy Quiz 15 | Methods


Reference video | Groovy Playground


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

1.

Guess the output

def sayHello(){
    println "Hello..."
}
 
 
 

2.

//calling function

def sayHello(){
    println "Hello..."
}
sayHello()
 
 
 

3.

//calling function with parameter

def sayHello(String name){
    println "Hello "+name
}
sayHello("Raghav")
 
 
 

4.

//parameterized function OR function with parameter

def sum(int num1, int num2){
    println "Sum is "+(num1+num2)
}
sum(2,5)
 
 
 

5.

//calling a parameterized function without parameters

def sum(int num1, int num2){
    println "Sum is "+(num1+num2)
}
sum()
 
 

6.

//Running with default parameters

def sum(int num1=10, int num2=20){
    println "Sum is "+(num1+num2)
}
sum()
 
 
 

7.

def sum(int num1, int num2=20){
    println "Sum is "+(num1+num2)
}
sum(5)
 
 
 

8.

//method return

def sub(int num1, int num2){
    def result = num1 - num2
    return result
}
print sub(20, 10)
 
 
 

12