0
0
MATLABdata~20 mins

String vs character array in MATLAB - Practice Questions

Choose your learning style9 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.