Bird
Raised Fist0
MATLABdata~20 mins

Character arrays and strings in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
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
intermediate
2:00remaining
Output 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);
AHello World
BHelloWorld
CError: Undefined operator '+' for input arguments of type 'string'.
DHello+ World
Attempts:
2 left
💡 Hint
In MATLAB, the plus operator '+' concatenates string objects with spaces if added explicitly.
❓ Predict Output
intermediate
2:00remaining
Result 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);
AError: Dimensions of arrays being concatenated are not consistent.
BHelloMATLAB
CHello MATLAB
D['H', 'e', 'l', 'l', 'o', ' ', 'M', 'A', 'T', 'L', 'A', 'B']
Attempts:
2 left
💡 Hint
Character arrays concatenate by joining their elements in order.
❓ Predict Output
advanced
2:00remaining
Output 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);
AError: strlength function not defined.
B6 6
C6 1
D1 6
Attempts:
2 left
💡 Hint
Both string and character array represent 6 characters.
❓ Predict Output
advanced
2:00remaining
Result of modifying a character in a string
What happens when you run this MATLAB code?
MATLAB
str = "hello";
str(1) = 'H';
disp(str);
AHello
Bhello
CError: Strings are immutable and cannot be modified this way.
DolleH
Attempts:
2 left
💡 Hint
MATLAB strings can be indexed and modified by character.
🧠 Conceptual
expert
3:00remaining
Difference between character arrays and string arrays
Which statement correctly describes the difference between character arrays and string arrays in MATLAB?
ACharacter arrays can hold multiple strings of varying lengths, but string arrays can only hold one string at a time.
BCharacter arrays support Unicode characters, but string arrays only support ASCII.
CString arrays are deprecated and replaced by character arrays in modern MATLAB versions.
DCharacter arrays are arrays of characters with fixed size, while string arrays are arrays of string objects that can hold different lengths.
Attempts:
2 left
💡 Hint
Think about how MATLAB stores text data in character arrays vs string arrays.

Practice

(1/5)
1. Which of the following correctly creates a character array in MATLAB?
easy
A. name = [Hello];
B. name = "Hello";
C. name = 'Hello';
D. name = (Hello);

Solution

  1. Step 1: Understand character array syntax

    Character arrays in MATLAB use single quotes to enclose text.
  2. Step 2: Check each option

    name = 'Hello'; uses single quotes correctly. name = "Hello"; uses double quotes which create string objects, not character arrays. Options A and D are invalid syntax.
  3. Final Answer:

    name = 'Hello'; -> Option C
  4. Quick Check:

    Single quotes create character arrays [OK]
Hint: Use single quotes for character arrays, double for strings [OK]
Common Mistakes:
  • Using double quotes for character arrays
  • Missing quotes around text
  • Using brackets or parentheses incorrectly
2. Which of the following is the correct way to create a string in MATLAB?
easy
A. str = "MATLAB";
B. str = 'MATLAB';
C. str = {"MATLAB"};
D. str = ("MATLAB");

Solution

  1. Step 1: Recall string creation syntax

    Strings in MATLAB are created using double quotes.
  2. Step 2: Evaluate options

    str = "MATLAB"; uses double quotes correctly. str = 'MATLAB'; creates a character array, not a string. Options C and D use invalid or different data types.
  3. Final Answer:

    str = "MATLAB"; -> Option A
  4. Quick Check:

    Double quotes create strings [OK]
Hint: Double quotes create strings, single quotes create char arrays [OK]
Common Mistakes:
  • Confusing single and double quotes
  • Using curly braces which create cell arrays
  • Using parentheses incorrectly
3. What is the output of the following MATLAB code?
charArray = 'abc';
str = "def";
disp([charArray str])
medium
A. abcdef
B. [abc def]
C. Error: Cannot concatenate char array and string
D. abc def

Solution

  1. Step 1: Understand concatenation of char arrays and strings

    In MATLAB, concatenating a character array and a string with [] converts the string to a char array and joins them.
  2. Step 2: Evaluate the concatenation result

    Concatenating 'abc' and "def" results in 'abcdef' as a character array, displayed without spaces.
  3. Final Answer:

    abcdef -> Option A
  4. Quick Check:

    Concatenation joins text without spaces [OK]
Hint: Concatenate char arrays and strings with [] to join text [OK]
Common Mistakes:
  • Expecting spaces between concatenated strings
  • Thinking concatenation causes error
  • Confusing display output with variable type
4. Identify the error in the following MATLAB code:
str1 = 'Hello';
str2 = "World";
result = strcat(str1, str2);
medium
A. strcat cannot concatenate character arrays and strings
B. No error, code runs correctly
C. Missing semicolon after str2 assignment
D. str1 and str2 must both be strings or both char arrays

Solution

  1. Step 1: Understand strcat behavior

    strcat can concatenate character arrays and strings by automatically converting strings to character arrays.
  2. Step 2: Verify the code

    str1 is a character array, str2 is a string, but strcat(str1, str2) returns 'HelloWorld' as a character array. No error occurs.
  3. Final Answer:

    No error, code runs correctly -> Option B
  4. Quick Check:

    strcat handles mixed types [OK]
Hint: strcat automatically handles char and string inputs [OK]
Common Mistakes:
  • Assuming strcat requires matching types
  • Confusing with + operator which doesn't work for mixed types
  • Looking for syntax errors where none exist
5. You have a character array chars = 'MATLAB' and want to convert it to a string and append " is fun". Which code correctly does this?
hard
A. newStr = chars + " is fun";
B. newStr = chars + ' is fun';
C. newStr = strcat(chars, " is fun");
D. newStr = string(chars) + " is fun";

Solution

  1. Step 1: Convert character array to string

    Use string(chars) to convert the character array to a string type.
  2. Step 2: Append text using + operator

    Strings can be concatenated with + and another string, so adding " is fun" works.
  3. Step 3: Check other options

    newStr = chars + ' is fun'; tries to add char array and char array with + which is invalid. newStr = strcat(chars, " is fun"); mixes types but produces a character array, not a string. newStr = chars + " is fun"; adds char array and string with + which errors.
  4. Final Answer:

    newStr = string(chars) + " is fun"; -> Option D
  5. Quick Check:

    Convert char to string, then use + to join [OK]
Hint: Convert char array to string first, then use + to append [OK]
Common Mistakes:
  • Using + to add char arrays
  • Mixing char arrays and strings in strcat
  • Not converting char array before concatenation
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