0
0
Jenkinsdevops~20 mins

Groovy syntax 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
What is the output of this Groovy pipeline snippet?

Consider this Jenkins pipeline Groovy code snippet:

def result = []
for (int i = 1; i <= 3; i++) {
  result.add(i * 2)
}
println(result)

What will be printed in the Jenkins console?

Jenkins
def result = []
for (int i = 1; i <= 3; i++) {
  result.add(i * 2)
}
println(result)
A[1, 2, 3]
BSyntaxError
C[3, 6, 9]
D[2, 4, 6]
Attempts:
2 left
💡 Hint

Think about what the loop does for each value of i.

Configuration
intermediate
2:00remaining
Which Groovy syntax correctly defines a Jenkins pipeline stage with a shell step?

Choose the correct Groovy syntax to define a Jenkins pipeline stage named Build that runs a shell command echo Hello.

A
stage('Build') {
  steps {
    shell 'echo Hello'
  }
}
B
stage('Build') {
  shell {
    echo 'Hello'
  }
}
C
stage('Build') {
  steps {
    sh 'echo Hello'
  }
}
D
stage('Build') {
  commands {
    sh 'echo Hello'
  }
}
Attempts:
2 left
💡 Hint

Remember the correct block names for steps and shell commands in declarative pipelines.

Troubleshoot
advanced
2:00remaining
What error does this Groovy pipeline code produce?

Analyze this snippet:

def x = 5
if (x = 10) {
  println('x is 10')
} else {
  println('x is not 10')
}

What error or output will Jenkins show?

Jenkins
def x = 5
if (x = 10) {
  println('x is 10')
} else {
  println('x is not 10')
}
ASyntaxError: Cannot assign in if condition
BPrints 'x is 10'
CPrints 'x is not 10'
DRuntimeException: Variable x not defined
Attempts:
2 left
💡 Hint

Check the difference between assignment = and comparison ==.

🔀 Workflow
advanced
2:00remaining
Which Groovy pipeline snippet correctly waits for a user input before continuing?

In Jenkins pipelines, you want to pause and ask for user approval before proceeding. Which snippet does this correctly?

Ainput message: 'Approve deployment?', ok: 'Yes'
Bpause('Approve deployment?')
CwaitForInput('Approve deployment?')
Dconfirm('Approve deployment?')
Attempts:
2 left
💡 Hint

Look for the Jenkins pipeline step that pauses for input.

Best Practice
expert
3:00remaining
Which Groovy pipeline snippet best handles errors to ensure cleanup always runs?

Choose the snippet that guarantees the cleanup() function runs even if build() fails.

A
try {
  build()
} catch (e) {
  echo 'Build failed'
} finally {
  cleanup()
}
B
build()
cleanup()
C
try {
  build()
  cleanup()
} catch (e) {
  echo 'Build failed'
}
D
catch (e) {
  echo 'Build failed'
}
build()
cleanup()
Attempts:
2 left
💡 Hint

Think about how try-catch-finally works in Groovy.