Challenge - 5 Problems
Master of MATLAB Character and String Handling
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of string concatenation with plus operator
What is the output of the following MATLAB code?
MATLAB
str1 = "Hello"; str2 = "World"; result = str1 + " " + str2; disp(result);
Attempts:
2 left
💡 Hint
In MATLAB, the plus operator '+' concatenates string objects with spaces if added explicitly.
✗ Incorrect
In MATLAB, using '+' between string objects concatenates them. Adding " " between them inserts a space, so the output is "Hello World".
❓ Predict Output
intermediateResult of character array concatenation
What is the output of this MATLAB code?
MATLAB
char1 = ['H', 'e', 'l', 'l', 'o']; char2 = [' ', 'M', 'A', 'T', 'L', 'A', 'B']; result = [char1 char2]; disp(result);
Attempts:
2 left
💡 Hint
Character arrays concatenate by joining their elements in order.
✗ Incorrect
Concatenating two character arrays horizontally joins their characters into one array, so the output is the string "Hello MATLAB".
❓ Predict Output
advancedOutput of string length and character array length
What is the output of this MATLAB code?
MATLAB
str = "MATLAB"; charArr = ['M', 'A', 'T', 'L', 'A', 'B']; lenStr = strlength(str); lenChar = length(charArr); fprintf("%d %d\n", lenStr, lenChar);
Attempts:
2 left
💡 Hint
Both string and character array represent 6 characters.
✗ Incorrect
The string "MATLAB" has length 6, and the character array has 6 elements, so both lengths are 6.
❓ Predict Output
advancedResult of modifying a character in a string
What happens when you run this MATLAB code?
MATLAB
str = "hello"; str(1) = 'H'; disp(str);
Attempts:
2 left
💡 Hint
MATLAB strings can be indexed and modified by character.
✗ Incorrect
In MATLAB, strings are mutable and you can change characters by indexing. Changing first character to 'H' results in "Hello".
🧠 Conceptual
expertDifference between character arrays and string arrays
Which statement correctly describes the difference between character arrays and string arrays in MATLAB?
Attempts:
2 left
💡 Hint
Think about how MATLAB stores text data in character arrays vs string arrays.
✗ Incorrect
Character arrays are arrays of characters and have fixed size per array, while string arrays are arrays of string objects that can each have different lengths.
