Bird
Raised Fist0
Jenkinsdevops~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In a Jenkins pipeline script using Groovy, what does the stage block represent?
easy
A. A loop for repeating tasks
B. A logical phase or step in the pipeline process
C. A function definition
D. A variable declaration

Solution

  1. Step 1: Understand the purpose of stage in Jenkins pipelines

    The stage block is used to define a distinct phase or step in the pipeline, such as build, test, or deploy.
  2. Step 2: Differentiate from other Groovy constructs

    It is not a variable, function, or loop but a way to organize pipeline tasks clearly.
  3. Final Answer:

    A logical phase or step in the pipeline process -> Option B
  4. Quick Check:

    stage = logical step [OK]
Hint: Remember: stage means a step or phase in pipeline [OK]
Common Mistakes:
  • Confusing stage with variable or function
  • Thinking stage is a loop
  • Assuming stage is a Groovy keyword unrelated to Jenkins
2. Which of the following is the correct syntax to define a variable name with value Jenkins in a Groovy pipeline script?
easy
A. name := "Jenkins"
B. var name = 'Jenkins'
C. def name = "Jenkins"
D. let name = 'Jenkins'

Solution

  1. Step 1: Recall Groovy variable declaration syntax

    Groovy uses def to declare variables, so def name = "Jenkins" is correct.
  2. Step 2: Identify incorrect syntax

    var and let are JavaScript keywords, and := is not Groovy syntax.
  3. Final Answer:

    def name = "Jenkins" -> Option C
  4. Quick Check:

    def declares variables in Groovy [OK]
Hint: Use def to declare variables in Groovy [OK]
Common Mistakes:
  • Using JavaScript keywords like var or let
  • Using := which is not Groovy syntax
  • Forgetting quotes around string values
3. What will be the output of this Groovy pipeline snippet?
def result = []
for (i in 1..3) {
  result << i * 2
}
return result
medium
A. [2, 4, 6]
B. [1, 2, 3]
C. [3, 6, 9]
D. SyntaxError

Solution

  1. Step 1: Understand the loop and range

    The loop runs from 1 to 3 inclusive, so i takes values 1, 2, 3.
  2. Step 2: Calculate each iteration's result

    Each i is multiplied by 2 and added to the list: 2, 4, 6.
  3. Final Answer:

    [2, 4, 6] -> Option A
  4. Quick Check:

    Loop 1..3 * 2 = [2,4,6] [OK]
Hint: Multiply each loop value by 2 and collect in list [OK]
Common Mistakes:
  • Confusing range syntax with exclusive end
  • Forgetting to append to list
  • Expecting syntax error due to unfamiliar Groovy syntax
4. Identify the error in this Jenkins pipeline Groovy snippet:
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
      }
    }
  }
  post {
    always {
      echo 'Cleaning up'
    }
  }
}
medium
A. No error, syntax is correct
B. Missing script block inside steps
C. Missing environment block
D. Incorrect use of post block outside stages

Solution

  1. Step 1: Review pipeline structure

    The pipeline has agent, stages with one stage and post block correctly placed.
  2. Step 2: Check syntax correctness

    Using echo inside steps is valid without needing script. The post block is correctly outside stages.
  3. Final Answer:

    No error, syntax is correct -> Option A
  4. Quick Check:

    Pipeline syntax valid as given [OK]
Hint: Remember post block is outside stages, steps can have echo directly [OK]
Common Mistakes:
  • Thinking echo needs script block
  • Assuming post must be inside stages
  • Expecting environment block is mandatory
5. How can you write a Groovy pipeline snippet to run a shell command ls -l only if a variable runList is true?
hard
A. stage('List') { if runList sh 'ls -l' }
B. when (runList) { sh 'ls -l' }
C. sh 'ls -l' if runList
D. if (runList) { sh 'ls -l' }

Solution

  1. Step 1: Use Groovy if statement syntax

    Groovy uses if (condition) { ... } to run code conditionally.
  2. Step 2: Apply condition to run shell command

    Inside the steps block, if (runList) { sh 'ls -l' } runs the shell command only if runList is true.
  3. Final Answer:

    if (runList) { sh 'ls -l' } -> Option D
  4. Quick Check:

    Groovy if syntax with braces [OK]
Hint: Use if(condition) { ... } to run commands conditionally [OK]
Common Mistakes:
  • Using when instead of if for condition
  • Omitting parentheses in if statement
  • Trying inline if without proper Groovy syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard