0
0
Jenkinsdevops~20 mins

Groovy methods in pipelines in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Groovy Pipeline Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a Groovy method call in Jenkins pipeline
What is the output of this Groovy method when called in a Jenkins pipeline?
Jenkins
def greet(name) {
  return "Hello, ${name}!"
}

def result = greet('DevOps')
println(result)
AHello, DevOps!
Bgreet(DevOps)
CError: Missing return statement
DHello, ${name}!
Attempts:
2 left
💡 Hint
Look at how the method uses string interpolation with ${}.
🧠 Conceptual
intermediate
2:00remaining
Understanding method scope in Jenkins pipeline Groovy scripts
In a Jenkins pipeline script, where can you define a Groovy method so it can be used in multiple stages?
AOnly inside each stage { } block
BInside the pipeline { } block directly
CInside the script { } block or at the top level outside pipeline { }
DInside environment { } block
Attempts:
2 left
💡 Hint
Think about where Groovy code can be declared for reuse in Jenkins pipelines.
🔀 Workflow
advanced
2:00remaining
Correct order of method execution in Jenkins pipeline
Given these Groovy method calls in a Jenkins pipeline, what is the correct order of execution?
A1,3,2,4
B4,1,2,3
C1,2,3,4
D4
Attempts:
2 left
💡 Hint
The method calls happen in the order they are written inside the last code line.
Troubleshoot
advanced
2:00remaining
Error caused by missing parentheses in method call
What error will this Jenkins pipeline Groovy script produce?
Jenkins
def greet(name) {
  return "Hi, ${name}!"
}

greet 'World'
AMissingMethodException: No signature of method: greet() is applicable
BNo error, prints 'Hi, World!'
CSyntaxError: Unexpected string literal
DNullPointerException
Attempts:
2 left
💡 Hint
Groovy allows calling methods without parentheses in some cases.
Best Practice
expert
2:00remaining
Best practice for defining reusable Groovy methods in Jenkins pipelines
Which option shows the best practice for defining reusable Groovy methods in a Jenkins pipeline shared across multiple jobs?
AUse a shared library with Groovy classes and methods
BDefine methods inside environment variables
CCopy-paste methods into each pipeline script
DDefine methods inside each Jenkinsfile separately
Attempts:
2 left
💡 Hint
Think about maintainability and reuse across many pipelines.