Bird
Raised Fist0
FreeRTOSprogramming~20 mins

xQueueSend() and xQueueReceive() in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this FreeRTOS queue send and receive example?

Consider the following FreeRTOS code snippet that sends and receives an integer via a queue:

QueueHandle_t queue = xQueueCreate(1, sizeof(int));
int valueToSend = 42;
int valueReceived = 0;

xQueueSend(queue, &valueToSend, 0);
xQueueReceive(queue, &valueReceived, 0);
printf("%d", valueReceived);

What will be printed?

FreeRTOS
QueueHandle_t queue = xQueueCreate(1, sizeof(int));
int valueToSend = 42;
int valueReceived = 0;

xQueueSend(queue, &valueToSend, 0);
xQueueReceive(queue, &valueReceived, 0);
printf("%d", valueReceived);
A42
B0
CGarbage value (uninitialized)
DProgram crashes
Attempts:
2 left
💡 Hint

Remember that xQueueSend copies the data into the queue, and xQueueReceive copies it out.

🧠 Conceptual
intermediate
1:30remaining
What happens if xQueueSend is called with a full queue and zero block time?

In FreeRTOS, if you call xQueueSend() on a queue that is already full and specify a block time of zero, what will happen?

AThe function overwrites the oldest item in the queue
BThe function blocks until space is available in the queue
CThe function causes a system crash
DThe function returns immediately with an error indicating the queue is full
Attempts:
2 left
💡 Hint

Think about what zero block time means for waiting.

🔧 Debug
advanced
2:00remaining
Why does this xQueueReceive call block indefinitely?

Examine this code snippet:

QueueHandle_t queue = xQueueCreate(1, sizeof(int));
int receivedValue;

xQueueReceive(queue, &receivedValue, portMAX_DELAY);
printf("%d", receivedValue);

The program blocks forever at xQueueReceive. Why?

FreeRTOS
QueueHandle_t queue = xQueueCreate(1, sizeof(int));
int receivedValue;

xQueueReceive(queue, &receivedValue, portMAX_DELAY);
printf("%d", receivedValue);
AThe queue size is zero, so receive cannot get data
BThe queue was deleted before receive was called
CNo item was ever sent to the queue, so receive waits forever
DThe receive buffer pointer is NULL
Attempts:
2 left
💡 Hint

Think about what portMAX_DELAY means and the queue state.

📝 Syntax
advanced
1:30remaining
Which option correctly sends a struct to a FreeRTOS queue?

Given the struct:

typedef struct {
  int id;
  float value;
} SensorData;

Which code snippet correctly sends a SensorData variable to a queue?

FreeRTOS
typedef struct {
  int id;
  float value;
} SensorData;

SensorData data = {1, 23.5};
QueueHandle_t queue = xQueueCreate(5, sizeof(SensorData));
AxQueueSend(queue, data, 0);
BxQueueSend(queue, &data, 0);
CxQueueSend(queue, *data, 0);
DxQueueSend(queue, &data.id, 0);
Attempts:
2 left
💡 Hint

Remember that xQueueSend expects a pointer to the data to copy.

🚀 Application
expert
1:30remaining
How many items can this queue hold and why?

Consider this queue creation:

QueueHandle_t queue = xQueueCreate(3, sizeof(char));

How many characters can this queue hold at once, and why?

FreeRTOS
QueueHandle_t queue = xQueueCreate(3, sizeof(char));
A3 characters, because the queue length is 3 and each item is one char
B1 character, because the queue size is the size of one char only
CDepends on the memory available, not fixed by parameters
D0 characters, because the queue is not initialized properly
Attempts:
2 left
💡 Hint

Think about the parameters of xQueueCreate: length and item size.

Practice

(1/5)
1. What is the primary purpose of the xQueueSend() function in FreeRTOS?
easy
A. To remove an item from a queue
B. To add an item to a queue for other tasks to receive
C. To create a new queue
D. To delete an existing queue

Solution

  1. Step 1: Understand the role of xQueueSend()

    xQueueSend() is used to place data into a queue so that other tasks can retrieve it later.
  2. Step 2: Differentiate from other queue functions

    Functions like xQueueReceive() remove data, while xQueueCreate() and xQueueDelete() manage queue lifecycle, not data transfer.
  3. Final Answer:

    To add an item to a queue for other tasks to receive -> Option B
  4. Quick Check:

    xQueueSend() adds data [OK]
Hint: Remember: Send puts data in, Receive takes data out [OK]
Common Mistakes:
  • Confusing sending with receiving
  • Thinking it creates or deletes queues
  • Ignoring the function's data transfer role
2. Which of the following is the correct syntax to send data to a queue using xQueueSend()?
easy
A. xQueueSend(queueHandle, data, 0);
B. xQueueSend(&queueHandle, data, portMAX_DELAY);
C. xQueueSend(queueHandle, &data, portMAX_DELAY);
D. xQueueSend(queueHandle, &data);

Solution

  1. Step 1: Check parameter types for xQueueSend()

    The function expects the queue handle, a pointer to the data, and a timeout value.
  2. Step 2: Match the correct syntax

    xQueueSend(queueHandle, &data, portMAX_DELAY); correctly passes the queue handle, address of data, and a timeout constant portMAX_DELAY.
  3. Final Answer:

    xQueueSend(queueHandle, &data, portMAX_DELAY); -> Option C
  4. Quick Check:

    Queue handle + &data + timeout = correct syntax [OK]
Hint: Always pass data by address and include timeout [OK]
Common Mistakes:
  • Passing data by value instead of pointer
  • Omitting the timeout parameter
  • Passing queue handle by address
3. Consider the following code snippet:
int value = 10;
if (xQueueSend(queue, &value, 0) == pdPASS) {
    int receivedValue = 0;
    if (xQueueReceive(queue, &receivedValue, 0) == pdPASS) {
        printf("%d", receivedValue);
    }
}

What will be printed when this code runs?
medium
A. 10
B. 0
C. No output (queue empty)
D. Compilation error

Solution

  1. Step 1: Analyze xQueueSend() call

    The value 10 is sent to the queue with zero wait time. If the queue is not full, it returns pdPASS.
  2. Step 2: Analyze xQueueReceive() call

    The code tries to receive from the queue immediately. Since the value was just sent, it will receive 10 and print it.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Send 10, receive 10, print 10 [OK]
Hint: Send then receive immediately prints sent value [OK]
Common Mistakes:
  • Assuming queue is empty at receive
  • Confusing return values with data
  • Ignoring zero timeout behavior
4. What is wrong with this code snippet?
int data = 5;
if (xQueueSend(queue, data, 10) != pdPASS) {
    // handle error
}
medium
A. Data should be passed by pointer, not by value
B. Timeout value 10 is invalid
C. Return value should not be compared to pdPASS
D. Queue handle is missing

Solution

  1. Step 1: Check parameter types in xQueueSend()

    The second parameter must be a pointer to the data, but here data is passed by value.
  2. Step 2: Identify the error cause

    Passing data by value causes a type mismatch and likely a compilation error or incorrect behavior.
  3. Final Answer:

    Data should be passed by pointer, not by value -> Option A
  4. Quick Check:

    Pass &data, not data [OK]
Hint: Always pass address of data to queue functions [OK]
Common Mistakes:
  • Passing data directly instead of pointer
  • Misunderstanding timeout parameter
  • Ignoring return value check
5. You want to send multiple integers to a queue and then receive them in order. Which approach correctly uses xQueueSend() and xQueueReceive() to achieve this without losing data?
hard
A. Use xQueueReceive() to send data and xQueueSend() to receive data
B. Send all integers at once by passing an array pointer to xQueueSend(), then receive with one xQueueReceive()
C. Send integers without checking return values, then receive once expecting all data
D. Send each integer with xQueueSend() using a timeout, then receive each with xQueueReceive() using a timeout, checking return values each time

Solution

  1. Step 1: Understand queue data handling

    Queues handle one item at a time, so each integer must be sent individually with xQueueSend().
  2. Step 2: Properly receive data

    Each item must be received individually with xQueueReceive(), checking return values to avoid errors or data loss.
  3. Step 3: Eliminate incorrect options

    Send all integers at once by passing an array pointer to xQueueSend(), then receive with one xQueueReceive() is wrong because queues do not send arrays as single items. Send integers without checking return values, then receive once expecting all data risks data loss by ignoring return values. Use xQueueReceive() to send data and xQueueSend() to receive data reverses function roles.
  4. Final Answer:

    Send each integer with xQueueSend() using a timeout, then receive each with xQueueReceive() using a timeout, checking return values each time -> Option D
  5. Quick Check:

    Send and receive items one by one, check returns [OK]
Hint: Send and receive items individually, always check returns [OK]
Common Mistakes:
  • Trying to send multiple items at once
  • Ignoring return values causing lost data
  • Mixing up send and receive functions
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