Bird
Raised Fist0
ROSframework~20 mins

Service definition (.srv files) in ROS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ROS Service Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Identify the correct syntax for a ROS service definition
Which of the following .srv file snippets correctly defines a service with a request containing an integer 'a' and a response containing a string 'result'?
A
int32 a
---
int32 result
B
int32 a
string result
---
C
---
int32 a
string result
D
int32 a
---
string result
Attempts:
2 left
💡 Hint
Remember that the request fields come before the '---' separator and the response fields come after.
❓ component_behavior
intermediate
2:00remaining
What happens when a ROS service client sends a request with missing fields?
Given a service defined with request fields 'int32 x' and 'int32 y', what will happen if the client sends a request with only 'x' set and 'y' missing?
AThe missing field 'y' will default to zero and the service will process the request.
BThe service call will fail with a serialization error.
CThe service will ignore the missing field and process only 'x'.
DThe service will crash due to incomplete request data.
Attempts:
2 left
💡 Hint
Think about how ROS messages handle default values for primitive types.
🔧 Debug
advanced
2:00remaining
Find the error in this .srv file definition
What error will occur when trying to build a ROS package with this service definition? Request: float64 value string name Response: float64 result string Note: The response has a line with only 'string' without a field name.
ROS
float64 value
string name
---
float64 result
string
ARuntime error when calling the service due to invalid response type.
BBuild succeeds with 'string' treated as a field named 'string'.
CSyntaxError: Missing field name for 'string' in response.
DNo error; the service works as expected.
Attempts:
2 left
💡 Hint
Every field in a .srv file must have a type and a name.
❓ state_output
advanced
2:00remaining
What is the output of this service call with nested message types?
Given a service definition where the request contains a custom message 'Point' with fields 'float64 x' and 'float64 y', and the response returns 'float64 distance', what will be the value of 'distance' if the client sends Point(3.0, 4.0) and the service calculates Euclidean distance from origin?
A7.0
B5.0
C25.0
DError: Cannot calculate distance
Attempts:
2 left
💡 Hint
Recall the Pythagorean theorem for distance calculation.
🧠 Conceptual
expert
2:00remaining
Why must the '---' separator be present in every .srv file?
What is the main reason ROS requires the '---' line in every .srv file?
ATo separate the request message fields from the response message fields.
BTo mark the end of the file for the ROS build system.
CTo indicate comments in the service definition.
DTo separate multiple service definitions in one file.
Attempts:
2 left
💡 Hint
Think about how ROS distinguishes request and response parts.

Practice

(1/5)
1. What is the main purpose of a .srv file in ROS?
easy
A. To define the structure of a service with request and response parts
B. To list all the topics a node subscribes to
C. To specify the launch configuration for nodes
D. To store parameters for the robot

Solution

  1. Step 1: Understand what a .srv file represents

    A .srv file defines a service interface in ROS, which includes a request and a response part.
  2. Step 2: Match the purpose to the options

    Only To define the structure of a service with request and response parts correctly states that .srv files define the structure of a service with request and response parts.
  3. Final Answer:

    To define the structure of a service with request and response parts -> Option A
  4. Quick Check:

    .srv files define service structure = C [OK]
Hint: Remember .srv files split request and response with --- [OK]
Common Mistakes:
  • Confusing .srv files with launch files
  • Thinking .srv files list topics
  • Assuming .srv files store parameters
2. Which of the following is the correct syntax to separate request and response fields in a .srv file?
easy
A. Using a colon ':' between request and response
B. Using a comma ',' between request and response
C. Using a semicolon ';' between request and response
D. Using three dashes '---' between request and response

Solution

  1. Step 1: Recall the .srv file format

    The .srv file separates request and response fields with three dashes '---'.
  2. Step 2: Identify the correct separator

    Only Using three dashes '---' between request and response correctly uses '---' to separate the two parts.
  3. Final Answer:

    Using three dashes '---' between request and response -> Option D
  4. Quick Check:

    Request and response separated by --- = A [OK]
Hint: Look for '---' as the separator in .srv files [OK]
Common Mistakes:
  • Using commas or semicolons instead of ---
  • Confusing with YAML or JSON syntax
  • Missing the separator line entirely
3. Given this .srv file content:
int32 a
int32 b
---
int32 sum

What will the response contain when the service is called with request values a=3 and b=4 if the service adds them?
medium
A. sum = 12
B. sum = 34
C. sum = 7
D. sum = 0

Solution

  1. Step 1: Understand the request and response fields

    The request has two integers a and b; the response has one integer sum.
  2. Step 2: Apply the service logic

    The service adds a and b, so sum = 3 + 4 = 7.
  3. Final Answer:

    sum = 7 -> Option C
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Add request fields to get response sum [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Multiplying instead of adding
  • Returning zero by mistake
4. Identify the error in this .srv file:
float64 x
float64 y
float64 z
---
float64 result
medium
A. No error, this is a valid .srv file
B. Response field should be on the same line as request fields
C. Missing separator '---' between request and response
D. Request fields must be integers, not float64

Solution

  1. Step 1: Check the separator

    The separator '---' is present between request and response fields.
  2. Step 2: Validate field types and format

    Using float64 for request and response fields is valid in .srv files.
  3. Final Answer:

    No error, this is a valid .srv file -> Option A
  4. Quick Check:

    Correct separator and valid types = A [OK]
Hint: Look for '---' and valid types to confirm syntax [OK]
Common Mistakes:
  • Thinking separator is missing when it is present
  • Believing response must be on same line
  • Assuming only integers allowed
5. You want to define a service that takes a string command and an integer timeout as request, and returns a boolean success and a string message as response. Which of these .srv files correctly defines this?
hard
A. string command int32 timeout --- bool success, string message
B. string command int32 timeout --- bool success string message
C. string command int32 timeout bool success string message
D. string command int32 timeout --- string message bool success

Solution

  1. Step 1: Check for correct separator and field order

    The request fields are before '---' and response fields after. string command int32 timeout --- bool success string message correctly uses '---' and lists request then response fields.
  2. Step 2: Validate field types and syntax

    string command int32 timeout --- bool success string message lists request fields (string, int32) and response fields (bool, string) correctly without syntax errors.
  3. Final Answer:

    string command int32 timeout --- bool success string message -> Option B
  4. Quick Check:

    Request before --- and response after = D [OK]
Hint: Request fields above ---; response fields below --- [OK]
Common Mistakes:
  • Placing response fields before separator
  • Missing separator line
  • Using commas inside .srv files
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