Bird
Raised Fist0
Azurecloud~20 mins

Scripting with variables and loops in Azure - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Azure Scripting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
Azure CLI Loop Variable Behavior
Consider this Azure CLI script snippet that loops to create resource groups with variable names. What will be the name of the last resource group created?
Azure
for i in {1..3}; do az group create --name myResourceGroup$i --location eastus; done
AmyResourceGroup3
BmyResourceGroup1
CmyResourceGroup4
DmyResourceGroup0
Attempts:
2 left
💡 Hint
The loop runs from 1 to 3 inclusive, appending the loop variable to the name.
❓ Configuration
intermediate
2:00remaining
Azure ARM Template Loop Variable Usage
In an Azure ARM template, you want to create 3 storage accounts with names 'storageacct1', 'storageacct2', and 'storageacct3' using a loop. Which of the following 'copy' loop configurations correctly sets the storage account names?
A"copy": { "name": "storageLoop", "count": 3 }, "name": "storageacct${copyIndex()}"
B"copy": { "name": "storageLoop", "count": 3 }, "name": "storageacct[copyIndex()+1]"
C"copy": { "name": "storageLoop", "count": 3 }, "name": "storageacct[copyIndex()]"
D"copy": { "name": "storageLoop", "count": 3 }, "name": "storageacct${copyIndex()+1}"
Attempts:
2 left
💡 Hint
copyIndex() starts at 0, so add 1 to start names at 1.
❓ Architecture
advanced
2:00remaining
Automating VM Creation with Azure CLI Loops
You want to create 5 Azure virtual machines named vm1 to vm5 in a loop using Azure CLI. Which script snippet correctly uses a loop and variable to create these VMs with the name pattern?
Afor i in {1..5}; do az vm create --resource-group myRG --name vm$i --image UbuntuLTS --admin-username azureuser; done
Bfor i in $(seq 1 5); do az vm create --resource-group myRG --name vm$i --image UbuntuLTS --admin-username azureuser; done
Cfor i in 1 2 3 4 5; do az vm create --resource-group myRG --name vm$i --image UbuntuLTS --admin-username azureuser; done
Dfor i in 5 4 3 2 1; do az vm create --resource-group myRG --name vm$i --image UbuntuLTS --admin-username azureuser; done
Attempts:
2 left
💡 Hint
Use brace expansion {1..5} for a simple loop from 1 to 5.
❓ security
advanced
2:00remaining
Looping Over Secrets in Azure Key Vault
You have a script that loops over a list of secret names to retrieve their values from Azure Key Vault. Which approach correctly uses variables and loops to fetch and print each secret's value securely?
Afor secret in secret1 secret2 secret3; do az keyvault secret show --vault-name myVault --name $secret --query value -o table; done
Bfor secret in secret1 secret2 secret3; do az keyvault secret show --vault-name myVault --name "$secret" --query value -o json; done
Cfor secret in secret1 secret2 secret3; do az keyvault secret show --vault-name myVault --name $secret --query value -o tsv; done
Dfor secret in secret1 secret2 secret3; do az keyvault secret show --vault-name myVault --name $secret --query value -o json; done
Attempts:
2 left
💡 Hint
Use -o tsv to get raw secret value without extra formatting.
✅ Best Practice
expert
3:00remaining
Efficient Variable Use in Azure CLI Loops for Resource Tagging
You want to tag multiple Azure resources in a loop with a variable tag value that changes each iteration. Which script snippet correctly updates the tag value variable inside the loop and applies it to each resource?
AtagValue="env"; for i in {1..3}; do az resource tag --tags environment=$tagValue$i --ids /subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/virtualMachines/vm$i; done
Bfor i in {1..3}; do az resource tag --tags environment=env$i --ids /subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/virtualMachines/vm$i; done
Cfor i in {1..3}; do tagValue=env$i; az resource tag --tags environment=$tagValue --ids /subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/virtualMachines/vm$i; done
Dfor i in {1..3}; do tagValue="env$i"; az resource tag --tags environment=$tagValue --ids /subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Compute/virtualMachines/vm$i; done
Attempts:
2 left
💡 Hint
Assign the variable inside the loop with quotes to handle the string correctly.

Practice

(1/5)
1. What is the main purpose of using variables in Azure scripting?
easy
A. To store values that can be reused throughout the script
B. To create loops that repeat commands
C. To delete resources automatically
D. To connect to the internet

Solution

  1. Step 1: Understand what variables do

    Variables hold information like names or numbers that you want to use again without typing them multiple times.
  2. Step 2: Identify the correct purpose in scripting

    Variables help scripts remember values so you can use them repeatedly and easily change them if needed.
  3. Final Answer:

    To store values that can be reused throughout the script -> Option A
  4. Quick Check:

    Variables = reusable values [OK]
Hint: Variables hold values to reuse in scripts [OK]
Common Mistakes:
  • Confusing variables with loops
  • Thinking variables delete resources
  • Believing variables connect to internet
2. Which of the following is the correct way to declare a variable named count with value 5 in an Azure CLI script?
easy
A. var count = 5
B. let count = 5
C. set count=5
D. count=5

Solution

  1. Step 1: Recall Azure CLI variable syntax

    In Azure CLI scripts (bash style), variables are assigned without keywords using name=value format.
  2. Step 2: Match the correct syntax

    count=5 uses count=5, which is the correct bash style assignment.
  3. Final Answer:

    count=5 -> Option D
  4. Quick Check:

    Azure CLI variable = name=value [OK]
Hint: Use name=value to assign variables in Azure CLI [OK]
Common Mistakes:
  • Using 'var' or 'let' which are not bash syntax
  • Using 'set' which is Windows CMD syntax
  • Adding spaces around '=' which breaks assignment
3. What will be the output of this Azure CLI script snippet?
for i in 1 2 3; do
  echo $((i * 2))
done
medium
A. 2 4 6
B. 0 1 2
C. 1 2 3
D. Syntax error

Solution

  1. Step 1: Understand the loop iterations

    The loop runs with i values 1, then 2, then 3.
  2. Step 2: Calculate the expression inside echo

    Each iteration prints i multiplied by 2: 1*2=2, 2*2=4, 3*2=6.
  3. Final Answer:

    2 4 6 -> Option A
  4. Quick Check:

    Loop * 2 = 2 4 6 [OK]
Hint: Multiply loop variable inside $(( )) for math [OK]
Common Mistakes:
  • Confusing echo $i with echo $((i * 2))
  • Expecting space-separated output on one line
  • Thinking syntax error due to missing semicolons
4. Identify the error in this Azure CLI script snippet:
for i in 1 2 3 do
  echo $i
done
medium
A. echo command is incorrect
B. Variable $i is not defined
C. Missing semicolon or newline after for statement
D. done keyword is missing

Solution

  1. Step 1: Check for correct for loop syntax

    In bash scripts, the for loop header must end with a semicolon or a newline before the do keyword.
  2. Step 2: Identify the missing semicolon or newline

    The snippet has no semicolon or newline after for i in 1 2 3, causing syntax error.
  3. Final Answer:

    Missing semicolon or newline after for statement -> Option C
  4. Quick Check:

    For loop header needs semicolon/newline before do [OK]
Hint: Add semicolon or newline before do in for loops [OK]
Common Mistakes:
  • Forgetting semicolon after for statement
  • Assuming $i is undefined
  • Thinking echo syntax is wrong
5. You want to create 3 Azure resource groups named rg1, rg2, and rg3 using a script with a loop and a variable prefix rg. Which script snippet correctly achieves this?
hard
A. prefix=rg for i in 1 2 3; do az group create --name $prefix + $i --location eastus done
B. prefix=rg for i in 1 2 3; do az group create --name "$prefix$i" --location eastus done
C. prefix=rg for i in 1 2 3; do az group create --name prefix$i --location eastus done
D. for i in 1 2 3; do az group create --name "rg" + i --location eastus done

Solution

  1. Step 1: Understand variable concatenation in bash

    To combine a variable and a number, use double quotes and place variables side by side like "$prefix$i".
  2. Step 2: Check each option for correct syntax

    prefix=rg for i in 1 2 3; do az group create --name "$prefix$i" --location eastus done correctly uses "$prefix$i" to form names rg1, rg2, rg3. Others use invalid concatenation or miss quotes.
  3. Final Answer:

    prefix=rg for i in 1 2 3; do az group create --name "$prefix$i" --location eastus done -> Option B
  4. Quick Check:

    Use "$prefix$i" to join strings in bash [OK]
Hint: Use "$prefix$i" to join variable and number in bash [OK]
Common Mistakes:
  • Using + for string concat in bash
  • Not quoting variable expansions
  • Writing variable names without $ sign
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