Bird
Raised Fist0
Jenkinsdevops~20 mins

Scripted vs declarative comparison in Jenkins - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Jenkins Pipeline Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Difference in Pipeline Syntax Style

Which statement correctly describes the main difference between scripted and declarative Jenkins pipelines?

ADeclarative pipelines cannot include parallel stages, but scripted pipelines can.
BDeclarative pipelines require writing Groovy code for all logic, whereas scripted pipelines use YAML files for configuration.
CScripted pipelines are only for building Java projects, while declarative pipelines support all languages.
DScripted pipelines use a Groovy-based imperative programming style, while declarative pipelines use a simpler, predefined syntax focused on stages and steps.
Attempts:
2 left
💡 Hint

Think about how each pipeline style expresses the build process: one is more like writing code, the other more like describing steps.

💻 Command Output
intermediate
2:00remaining
Output of Declarative Pipeline with Parallel Stages

Given this declarative Jenkins pipeline snippet, what will be the output in the Jenkins console log?

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
      }
    }
    stage('Test') {
      parallel {
        stage('Unit Tests') {
          steps {
            echo 'Running unit tests'
          }
        }
        stage('Integration Tests') {
          steps {
            echo 'Running integration tests'
          }
        }
      }
    }
  }
}
A
Building...
Running unit tests
Running integration tests
B
Building...
Running unit tests
C
Running unit tests
Running integration tests
Building...
D
Building...
Running integration tests
Attempts:
2 left
💡 Hint

Remember that parallel stages run at the same time, but their output appears sequentially in the log.

❓ Configuration
advanced
2:00remaining
Correct Scripted Pipeline Syntax for Loop

Which scripted Jenkins pipeline snippet correctly loops over a list of environments and echoes each one?

A
def envs = ['dev', 'qa', 'prod']
for (e in envs) {
  echo "Deploying to ${e}"
}
B
def envs = ['dev', 'qa', 'prod']
envs.each() {
  echo 'Deploying to ' + it
}
C
def envs = ['dev', 'qa', 'prod']
for (int i=0; i<envs.size(); i++) {
  echo "Deploying to ${envs[i]}"
}
D
def envs = ['dev', 'qa', 'prod']
for e in envs {
  echo "Deploying to ${e}"
}
Attempts:
2 left
💡 Hint

Check Groovy syntax for loops and list indexing carefully.

❓ Troubleshoot
advanced
2:00remaining
Troubleshooting Declarative Pipeline Syntax Error

Why does this declarative pipeline snippet cause a syntax error?

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
      }
    }
    stage('Test') {
      steps {
        parallel {
          unit: {
            echo 'Unit tests'
          },
          integration: {
            echo 'Integration tests'
          }
        }
      }
    }
  }
}
AThe 'parallel' block is incorrectly placed inside 'steps'; it should be directly inside 'stages'.
BThe syntax for parallel stages requires using 'stage' blocks inside 'parallel', not just labels with closures.
CThe parallel stages must be defined as separate 'stage' blocks, not inside 'steps'.
DThe 'parallel' block keys (unit, integration) must be strings in quotes.
Attempts:
2 left
💡 Hint

Check the correct way to define parallel stages in declarative pipelines.

✅ Best Practice
expert
3:00remaining
Choosing Pipeline Style for Complex Logic

You need to implement a Jenkins pipeline with complex conditional logic, loops, and dynamic stage creation. Which pipeline style is best suited for this task and why?

ADeclarative pipeline, because it is simpler and always preferred regardless of complexity.
BDeclarative pipeline, because it has built-in support for complex loops and dynamic stages.
CScripted pipeline, because it allows full Groovy programming capabilities for complex logic and dynamic behavior.
DScripted pipeline, because it uses YAML syntax which is easier for complex logic.
Attempts:
2 left
💡 Hint

Consider which pipeline style supports full programming features like loops and dynamic stage creation.

Practice

(1/5)
1. Which statement best describes the main difference between scripted and declarative Jenkins pipelines?
easy
A. Scripted pipelines use Groovy code and are flexible; declarative pipelines have a fixed structure and are easier to read.
B. Declarative pipelines use Groovy code and are flexible; scripted pipelines have a fixed structure and are easier to read.
C. Both scripted and declarative pipelines have the same syntax and flexibility.
D. Scripted pipelines are only for building Java projects; declarative pipelines are for all other projects.

Solution

  1. Step 1: Understand scripted pipeline nature

    Scripted pipelines use Groovy code, allowing more flexibility but requiring more coding skill.
  2. Step 2: Understand declarative pipeline nature

    Declarative pipelines have a fixed, simpler structure designed for easier writing and reading.
  3. Final Answer:

    Scripted pipelines use Groovy code and are flexible; declarative pipelines have a fixed structure and are easier to read. -> Option A
  4. Quick Check:

    Scripted = flexible Groovy, Declarative = fixed structure [OK]
Hint: Remember: scripted = flexible code, declarative = fixed format [OK]
Common Mistakes:
  • Confusing which pipeline uses Groovy code
  • Thinking both pipelines have the same syntax
  • Believing scripted pipelines are only for Java
2. Which of the following is the correct way to start a declarative Jenkins pipeline script?
easy
A. node { stage('Build') { echo 'Building...' } }
B. def build() { echo 'Building...' }
C. build { echo 'Building...' }
D. pipeline { agent any; stages { stage('Build') { steps { echo 'Building...' } } } }

Solution

  1. Step 1: Identify declarative pipeline syntax

    Declarative pipelines start with the pipeline {} block and include agent and stages.
  2. Step 2: Compare options

    pipeline { agent any; stages { stage('Build') { steps { echo 'Building...' } } } } correctly uses pipeline { agent any; stages { stage('Build') { steps { echo 'Building...' } } } } which matches declarative syntax.
  3. Final Answer:

    pipeline { agent any; stages { stage('Build') { steps { echo 'Building...' } } } } -> Option D
  4. Quick Check:

    Declarative starts with pipeline block [OK]
Hint: Declarative pipelines always start with 'pipeline { ... }' [OK]
Common Mistakes:
  • Using scripted syntax for declarative pipelines
  • Omitting the 'pipeline' block
  • Missing 'stages' or 'steps' keywords
3. Given this scripted pipeline snippet, what will be the output?
node {
  stage('Test') {
    echo 'Running tests'
  }
}
medium
A. Prints 'Running tests' during the 'Test' stage
B. Syntax error because 'stage' is not allowed in scripted pipelines
C. No output because 'node' block is empty
D. Pipeline fails due to missing 'pipeline' block

Solution

  1. Step 1: Analyze scripted pipeline structure

    The scripted pipeline uses node and stage blocks correctly with an echo step inside.
  2. Step 2: Determine output

    The pipeline will run the 'Test' stage and print 'Running tests' to the console.
  3. Final Answer:

    Prints 'Running tests' during the 'Test' stage -> Option A
  4. Quick Check:

    Scripted pipelines allow stage and echo inside node [OK]
Hint: Scripted pipelines use node and stage blocks freely [OK]
Common Mistakes:
  • Thinking 'stage' is only for declarative pipelines
  • Expecting a syntax error without 'pipeline' block
  • Assuming no output if node block is present
4. Identify the error in this declarative pipeline snippet:
pipeline {
  agent any
  stages {
    stage('Build') {
      echo 'Building...'
    }
  }
}
medium
A. Incorrect use of 'pipeline' keyword
B. Missing 'agent' declaration
C. Missing 'steps' block inside the 'Build' stage
D. Using 'echo' instead of 'sh' command

Solution

  1. Step 1: Review declarative pipeline stage structure

    Each stage must contain a steps block that holds the commands to run.
  2. Step 2: Check the snippet

    The 'Build' stage directly contains echo without wrapping it inside steps {}, which is invalid.
  3. Final Answer:

    Missing 'steps' block inside the 'Build' stage -> Option C
  4. Quick Check:

    Declarative stages require steps block [OK]
Hint: Declarative stages always need a 'steps' block [OK]
Common Mistakes:
  • Omitting the 'steps' block inside stages
  • Confusing scripted and declarative syntax
  • Thinking 'echo' must be replaced by 'sh'
5. You want to create a Jenkins pipeline that runs a complex Groovy script for dynamic branching but also wants to keep some stages simple and readable. Which approach is best?
hard
A. Use a fully scripted pipeline for all stages to keep consistency.
B. Use a declarative pipeline with a scripted block inside for complex parts.
C. Use only declarative pipeline and avoid any Groovy scripting.
D. Write two separate pipelines and run them manually.

Solution

  1. Step 1: Understand pipeline flexibility needs

    Complex dynamic branching requires Groovy scripting flexibility, which scripted pipelines provide.
  2. Step 2: Combine declarative simplicity with scripted flexibility

    Declarative pipelines allow embedding scripted blocks for advanced logic, keeping most stages simple and readable.
  3. Final Answer:

    Use a declarative pipeline with a scripted block inside for complex parts. -> Option B
  4. Quick Check:

    Declarative + scripted block = best of both worlds [OK]
Hint: Embed scripted blocks inside declarative for complex logic [OK]
Common Mistakes:
  • Using only scripted pipeline losing readability
  • Avoiding scripted blocks in declarative pipelines
  • Running separate pipelines manually instead of combining
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