Bird
Raised Fist0
Jenkinsdevops~20 mins

Script blocks for Groovy in Jenkins - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Groovy Script Blocks Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Groovy script block in Jenkins pipeline
What is the output of this Groovy script block in a Jenkins pipeline?
Jenkins
def result = ''
script {
  result = 'Hello from Groovy script block'
}
println(result)
Anull
BCompilation error
CSyntaxError
DHello from Groovy script block
Attempts:
2 left
💡 Hint
Remember that variables assigned inside script blocks are accessible outside in Jenkins scripted pipelines.
❓ Configuration
intermediate
2:00remaining
Correct syntax for a Groovy script block in Jenkins pipeline
Which of the following is the correct way to write a Groovy script block inside a Jenkins declarative pipeline?
A
steps {
  script echo 'Running Groovy code'
}
B
script {
  echo 'Running Groovy code'
}
C
groovy {
  echo 'Running Groovy code'
}
D
sh {
  echo 'Running Groovy code'
}
Attempts:
2 left
💡 Hint
The keyword for embedding Groovy code in declarative pipelines is 'script'.
❓ Troubleshoot
advanced
2:00remaining
Troubleshooting variable scope in Groovy script block
Given this Jenkins pipeline snippet, what is the cause of the error 'groovy.lang.MissingPropertyException: No such property: count'?
Jenkins
pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        script {
          def count = 5
        }
        echo "Count is ${count}"
      }
    }
  }
}
AVariable 'count' is defined inside script block and not accessible outside it
BSyntax error in the script block
CThe echo step does not support variable interpolation
DMissing 'def' keyword before 'count' in echo step
Attempts:
2 left
💡 Hint
Think about variable scope inside and outside script blocks in Jenkins pipelines.
🔀 Workflow
advanced
2:00remaining
Order of execution in Jenkins pipeline with multiple script blocks
Consider this Jenkins pipeline snippet. What will be the output order of the echo statements?
Jenkins
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        script {
          echo 'First script block'
        }
        echo 'Outside script block'
        script {
          echo 'Second script block'
        }
      }
    }
  }
}
AFirst script block\nSecond script block\nOutside script block
BOutside script block\nFirst script block\nSecond script block
CFirst script block\nOutside script block\nSecond script block
DSecond script block\nFirst script block\nOutside script block
Attempts:
2 left
💡 Hint
Steps in Jenkins pipeline run in the order they are written.
✅ Best Practice
expert
2:00remaining
Best practice for complex Groovy logic in Jenkins pipelines
Which approach is best for managing complex Groovy logic in Jenkins pipelines to keep the pipeline readable and maintainable?
AUse shared libraries to encapsulate complex Groovy logic and call them from the Jenkinsfile
BPlace all Groovy code inside multiple script blocks directly in the Jenkinsfile
CWrite Groovy code inline in the pipeline steps without script blocks
DAvoid Groovy and write all logic in shell scripts called from the pipeline
Attempts:
2 left
💡 Hint
Think about code reuse and separation of concerns in Jenkins pipelines.

Practice

(1/5)
1. What is the main purpose of using script blocks in Jenkins pipelines?
easy
A. To define environment variables
B. To specify the agent where the pipeline runs
C. To run Groovy code for custom logic inside the pipeline
D. To declare pipeline stages

Solution

  1. Step 1: Understand Jenkins pipeline structure

    Jenkins pipelines use declarative syntax but allow Groovy code inside script blocks for flexibility.
  2. Step 2: Identify the role of script blocks

    script blocks let you write Groovy code for loops, conditions, and other logic not directly supported in declarative syntax.
  3. Final Answer:

    To run Groovy code for custom logic inside the pipeline -> Option C
  4. Quick Check:

    Script blocks = Groovy code inside pipeline [OK]
Hint: Script blocks run Groovy code inside steps [OK]
Common Mistakes:
  • Confusing script blocks with environment or agent declarations
  • Thinking script blocks define stages
  • Assuming script blocks are optional for all logic
2. Which of the following is the correct way to start a script block inside a declarative Jenkins pipeline?
easy
A. script { echo 'Hello' }
B. script: { echo 'Hello' }
C. script() { echo 'Hello' }
D. script => { echo 'Hello' }

Solution

  1. Step 1: Recall correct syntax for script blocks

    In declarative pipelines, script is a block keyword followed by curly braces containing Groovy code.
  2. Step 2: Compare options

    Only script { ... } is valid Groovy block syntax. Others use invalid punctuation or keywords.
  3. Final Answer:

    script { echo 'Hello' } -> Option A
  4. Quick Check:

    script block syntax = script { ... } [OK]
Hint: Use script { } with braces, no colons or arrows [OK]
Common Mistakes:
  • Using colons or arrows instead of braces
  • Adding parentheses after script keyword
  • Misplacing quotes or braces
3. What will be the output of this Jenkins pipeline snippet?
pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        script {
          def sum = 0
          for (i in 1..3) {
            sum += i
          }
          echo "Sum is ${sum}"
        }
      }
    }
  }
}
medium
A. Sum is 6
B. Sum is 3
C. Sum is 0
D. Syntax error

Solution

  1. Step 1: Analyze the for loop inside script block

    The loop runs from 1 to 3 inclusive, adding each number to sum: 1 + 2 + 3 = 6.
  2. Step 2: Understand echo output

    The echo prints the string with sum value interpolated, so output is "Sum is 6".
  3. Final Answer:

    Sum is 6 -> Option A
  4. Quick Check:

    1+2+3=6 output = Sum is 6 [OK]
Hint: Sum 1 to 3 = 6; echo prints it [OK]
Common Mistakes:
  • Thinking loop ends at 2
  • Confusing sum initialization
  • Assuming syntax error due to script block
4. Identify the error in this Jenkins pipeline snippet using a script block:
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        script {
          for i in 1..3 {
            echo "Number: ${i}"
          }
        }
      }
    }
  }
}
medium
A. Script block must be outside steps
B. Incorrect use of echo inside script block
C. Agent declaration is invalid
D. Missing parentheses around for loop variable and range

Solution

  1. Step 1: Check for loop syntax in Groovy

    Groovy requires parentheses around the for loop variable and range: for (i in 1..3).
  2. Step 2: Identify the error in given code

    The code uses for i in 1..3 without parentheses, causing syntax error.
  3. Final Answer:

    Missing parentheses around for loop variable and range -> Option D
  4. Quick Check:

    Groovy for loop needs ( ) [OK]
Hint: Always use parentheses in Groovy for loops [OK]
Common Mistakes:
  • Omitting parentheses in for loops
  • Misplacing script block outside steps
  • Confusing echo usage inside script
5. You want to use a script block in Jenkins to create a map of numbers to their squares for numbers 1 to 4, then print each pair. Which code snippet correctly does this inside a script block?
hard
A. def squares = [] for (i in 1..4) { squares.add(i * i) } squares.each { echo "${it} squared" }
B. def squares = [:] for (i in 1..4) { squares[i] = i * i } squares.each { k, v -> echo "${k} squared is ${v}" }
C. def squares = [:] for i in 1..4 { squares[i] = i * i } squares.each { k, v -> echo "${k} squared is ${v}" }
D. def squares = [:] for (i in 1..4) { squares[i] = i ^ 2 } squares.each { k, v -> echo "${k} squared is ${v}" }

Solution

  1. Step 1: Check map creation and loop syntax

    def squares = [:] for (i in 1..4) { squares[i] = i * i } squares.each { k, v -> echo "${k} squared is ${v}" } correctly creates an empty map with [:] and uses parentheses in the for loop.
  2. Step 2: Verify square calculation and iteration

    def squares = [:] for (i in 1..4) { squares[i] = i * i } squares.each { k, v -> echo "${k} squared is ${v}" } uses i * i for squares and iterates map with each { k, v -> ... } to echo key-value pairs.
  3. Final Answer:

    def squares = [:] for (i in 1..4) { squares[i] = i * i } squares.each { k, v -> echo "${k} squared is ${v}" } -> Option B
  4. Quick Check:

    Map with for (i in 1..4) and i*i is correct [OK]
Hint: Use [:] for map, for (i in range), i*i for squares [OK]
Common Mistakes:
  • Using list [] instead of map [:]
  • Omitting parentheses in for loop
  • Using ^ for power instead of *
  • Incorrect map iteration 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