0
0
MATLABdata~20 mins

String concatenation in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MATLAB String Concatenation 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 MATLAB code?
Consider the following MATLAB code that concatenates strings using square brackets. What will be printed?
MATLAB
str1 = 'Hello';
str2 = 'World';
result = [str1, ' ', str2];
disp(result);
AHelloWorld
BError: Invalid string concatenation
CHello World
DHello World
Attempts:
2 left
💡 Hint
Remember that square brackets concatenate strings and spaces are literal characters.
Predict Output
intermediate
2:00remaining
What does this code output when concatenating numeric and string data?
Look at this MATLAB code that tries to concatenate a number and a string. What will it display?
MATLAB
num = 123;
str = ' apples';
result = [num, str];
disp(result);
AError: Cannot concatenate numeric and character arrays directly.
B123 apples
CError: Dimensions of arrays being concatenated are not consistent.
D123apples
Attempts:
2 left
💡 Hint
In MATLAB, numeric and character arrays cannot be concatenated directly using square brackets.
🧠 Conceptual
advanced
2:00remaining
How to concatenate strings with newline characters in MATLAB?
Which option correctly concatenates two strings with a newline between them so that the output shows on two lines?
Astr = ['Hello', '\n', 'World']; disp(str);
Bstr = ['Hello', newline, 'World']; disp(str);
Cstr = strcat('Hello', '\n', 'World'); disp(str);
Dstr = ['Hello', char(10), 'World']; disp(str);
Attempts:
2 left
💡 Hint
MATLAB has a built-in function for newline characters.
🔧 Debug
advanced
2:00remaining
Identify the error in this string concatenation code
This MATLAB code tries to concatenate strings but causes an error. What is the cause?
MATLAB
str1 = 'Data';
str2 = "Science";
result = [str1, str2];
disp(result);
AError because single quotes and double quotes create different string types that cannot be concatenated with [].
BError because missing comma between str1 and str2 inside brackets.
CNo error; output is 'DataScience'.
DError because disp cannot display concatenated strings.
Attempts:
2 left
💡 Hint
MATLAB treats single-quoted and double-quoted strings differently.
Predict Output
expert
2:00remaining
What is the output of this complex string concatenation with cell arrays?
Given the following MATLAB code, what will be the output displayed?
MATLAB
C = {'Hello', 'World'};
result = [C{1}, ' ', C{2}];
disp(result);
AHello World
B{'Hello', 'World'}
CError: Cell contents reference from a non-cell array object.
DHelloWorld
Attempts:
2 left
💡 Hint
Use curly braces to extract content from cell arrays before concatenation.