Bird
Raised Fist0
MATLABdata~20 mins

String vs character array in MATLAB - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String vs Character Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of string concatenation vs character array concatenation

What is the output of the following MATLAB code?

MATLAB
str1 = "Hello";
str2 = "World";
char1 = ['Hello'];
char2 = ['World'];

result1 = str1 + " " + str2;
result2 = [char1 ' ' char2];
disp(result1)
disp(result2)
A
HelloWorld
Hello World
B
Hello World
HelloWorld
C
Hello World
Hello World
D
Error
Hello World
Attempts:
2 left
💡 Hint

Remember that strings use + for concatenation, while character arrays use square brackets [].

❓ Predict Output
intermediate
2:00remaining
Length difference between string and character array

What will be the output of the following MATLAB code?

MATLAB
str = "MATLAB";
charArr = ['M' 'A' 'T' 'L' 'A' 'B'];
lenStr = strlength(str);
lenChar = length(charArr);
disp(lenStr)
disp(lenChar)
A
7
6
B
6
6
C
6
7
D
Error
6
Attempts:
2 left
💡 Hint

Check how strlength and length functions behave on strings and character arrays.

🔧 Debug
advanced
2:00remaining
Identify the error when mixing string and character array

What error does the following MATLAB code produce?

MATLAB
str = "Data";
charArr = ['S' 'c' 'i' 'e' 'n' 'c' 'e'];
result = str + charArr;
AError: Undefined operator '+' for input arguments of type 'string' and 'char'.
BDataScience
CData Science
DError: Matrix dimensions must agree.
Attempts:
2 left
💡 Hint

Consider the types of variables and what + operator supports.

🧠 Conceptual
advanced
2:00remaining
Difference in indexing between string and character array

Consider the following MATLAB code:

str = "Hello";
charArr = ['H' 'e' 'l' 'l' 'o'];

char1 = str(2);
char2 = charArr(2);

What are the values of char1 and char2?

Achar1 is a string "e"; char2 is a character 'e'
Bchar1 is a character 'e'; char2 is a string "e"
Cchar1 is a string "el"; char2 is a character 'e'
Dchar1 is a character 'e'; char2 is a character 'l'
Attempts:
2 left
💡 Hint

Remember that indexing a string returns a string scalar, indexing a character array returns a char.

❓ Predict Output
expert
2:00remaining
Behavior of strcmp with string and character array

What is the output of this MATLAB code?

MATLAB
str = "Test";
charArr = ['T' 'e' 's' 't'];
result = strcmp(str, charArr);
disp(result)
AError: Inputs must be character arrays or cell arrays of character vectors.
B0
CError: Undefined function 'strcmp' for input arguments of type 'string' and 'char'.
D1
Attempts:
2 left
💡 Hint

Check if strcmp supports comparing string and character array.

Practice

(1/5)
1. Which of the following correctly represents a string in MATLAB?
easy
A. 'Hello World'
B. ('Hello World')
C. {'Hello World'}
D. "Hello World"

Solution

  1. Step 1: Understand string syntax in MATLAB

    Strings in MATLAB are enclosed in single quotes, like 'Hello World'.
  2. Step 2: Identify string and character array syntax

    In MATLAB, single quotes denote character arrays (which are often used as strings). Double quotes denote string arrays introduced in newer MATLAB versions, but the classic string representation is single quotes.
  3. Final Answer:

    'Hello World' -> Option A
  4. Quick Check:

    Single quotes denote character arrays (strings) = B [OK]
Hint: Strings use single quotes in MATLAB [OK]
Common Mistakes:
  • Confusing single quotes for strings
  • Using curly braces which create cell arrays
  • Using parentheses which are invalid for strings
2. Which of the following is the correct way to create a character array in MATLAB?
easy
A. charArray = "abc";
B. charArray = ["a", "b", "c"];
C. charArray = { 'a', 'b', 'c' };
D. charArray = 'abc';

Solution

  1. Step 1: Recognize character array syntax

    Character arrays are created using single quotes around text, like 'abc'.
  2. Step 2: Differentiate from string and cell array

    Double quotes create string arrays, and curly braces create cell arrays, so only single quotes create character arrays.
  3. Final Answer:

    charArray = 'abc'; -> Option D
  4. Quick Check:

    Character arrays use single quotes = C [OK]
Hint: Use single quotes for character arrays, double for strings [OK]
Common Mistakes:
  • Using double quotes for character arrays
  • Using curly braces which create cell arrays
  • Using square brackets with commas incorrectly
3. What is the output of the following MATLAB code?
str = "MATLAB";
charArr = 'MATLAB';
disp(class(str));
disp(class(charArr));
medium
A. string char
B. char string
C. string string
D. char char

Solution

  1. Step 1: Identify variable types

    Variable str is created with double quotes, so it is a string type. Variable charArr uses single quotes, so it is a character array (char).
  2. Step 2: Understand class() output

    The class() function returns the type name as a string. So class(str) returns 'string' and class(charArr) returns 'char'.
  3. Final Answer:

    string char -> Option A
  4. Quick Check:

    Double quotes = string, single quotes = char [OK]
Hint: class() shows 'string' for double quotes, 'char' for single quotes [OK]
Common Mistakes:
  • Assuming both are 'char' type
  • Confusing output order
  • Thinking class() returns variable content
4. Consider this MATLAB code snippet:
str = 'Hello';
str(1) = "h";

What is the error in this code?
medium
A. Indexing starts at 0, so str(1) is invalid
B. Cannot assign double quotes to a character array element
C. Strings cannot be indexed like arrays
D. No error, code runs fine

Solution

  1. Step 1: Identify variable type

    Variable str is created with single quotes, so it is a character array.
  2. Step 2: Understand assignment rules for character arrays

    Character array elements must be assigned single characters using single quotes. Assigning a double-quoted string "h" causes a type mismatch error.
  3. Final Answer:

    Cannot assign double quotes to a character array element -> Option B
  4. Quick Check:

    Character arrays need single quotes for elements [OK]
Hint: Assign single characters with single quotes to char arrays [OK]
Common Mistakes:
  • Using double quotes to assign char array elements
  • Thinking MATLAB indexing starts at 0
  • Assuming strings can't be indexed
5. You want to concatenate the words "Data" and 'Science' in MATLAB to form a string. Which code correctly does this and results in a string type?
hard
A. result = "Data" + 'Science';
B. result = "Data" + "Science";
C. result = "Data" + string('Science');
D. result = "Data" + char('Science');

Solution

  1. Step 1: Understand types of inputs

    "Data" is a string. 'Science' is a character array. To concatenate strings, both must be string type.
  2. Step 2: Convert character array to string before concatenation

    Using string('Science') converts the char array to string, allowing + operator to concatenate strings properly.
  3. Final Answer:

    result = "Data" + string('Science'); -> Option C
  4. Quick Check:

    Convert char to string before + concatenation [OK]
Hint: Convert char arrays to strings before using + for concatenation [OK]
Common Mistakes:
  • Trying to add string and char directly
  • Using + with char arrays (causes error)
  • Not converting char to string 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