Bird
0
0

What is the output of the following MATLAB code?

medium📝 Predict Output Q13 of 15
MATLAB - String Handling
What is the output of the following MATLAB code?
str = 'apple banana apple';
newStr = strrep(str, 'apple', 'orange');
disp(newStr);
Aorange banana apple
Bapple banana orange
Corange banana orange
Dapple banana apple
Step-by-Step Solution
Solution:
  1. Step 1: Identify the original string and replacement

    The original string is 'apple banana apple'. The function replaces all 'apple' with 'orange'.
  2. Step 2: Apply strrep to replace all occurrences

    Both 'apple' words are replaced, resulting in 'orange banana orange'.
  3. Final Answer:

    orange banana orange -> Option C
  4. Quick Check:

    strrep replaces all matches, not just first [OK]
Quick Trick: strrep replaces every match, not just one [OK]
Common Mistakes:
  • Replacing only the first occurrence
  • Confusing strrep with regex functions
  • Expecting original string unchanged

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes