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?
def result = [] for (int i = 1; i <= 3; i++) { result.add(i * 2) } println(result)
Think about what the loop does for each value of i.
The loop runs from 1 to 3. For each i, it multiplies by 2 and adds to the list. So the list becomes [2, 4, 6].
Choose the correct Groovy syntax to define a Jenkins pipeline stage named Build that runs a shell command echo Hello.
Remember the correct block names for steps and shell commands in declarative pipelines.
The correct syntax uses steps block and sh command to run shell scripts.
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?
def x = 5 if (x = 10) { println('x is 10') } else { println('x is not 10') }
Check the difference between assignment = and comparison ==.
In Groovy, assignment = is allowed inside an if condition and returns the assigned value (10), which is truthy, so it prints 'x is 10'. Use == for comparison to avoid accidental assignment.
In Jenkins pipelines, you want to pause and ask for user approval before proceeding. Which snippet does this correctly?
Look for the Jenkins pipeline step that pauses for input.
The input step pauses the pipeline and waits for user approval with a message and button label.
Choose the snippet that guarantees the cleanup() function runs even if build() fails.
Think about how try-catch-finally works in Groovy.
The finally block always runs whether an error occurs or not, ensuring cleanup is done.