Bird
Raised Fist0
MATLABdata~20 mins

Numeric types (double, single, integer) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Numeric Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of mixed numeric type operations
What is the output of this MATLAB code?
a = single(5.5); b = int32(2); c = a + b; disp(class(c)); disp(c);
MATLAB
a = single(5.5);
b = int32(2);
c = a + b;
disp(class(c));
disp(c);
A
int32
7
B
double
7.5
C
single
7.5
D
single
7
Attempts:
2 left
💡 Hint
Remember MATLAB converts integer types to single or double when mixed with floating types.
❓ Predict Output
intermediate
2:00remaining
Integer division result type
What is the output of this MATLAB code?
x = int16(7); y = int16(2); z = x / y; disp(class(z)); disp(z);
MATLAB
x = int16(7);
y = int16(2);
z = x / y;
disp(class(z));
disp(z);
A
int32
3
B
double
3.5
C
int16
3
D
single
3.5
Attempts:
2 left
💡 Hint
Division of integer types returns double in MATLAB.
🔧 Debug
advanced
2:00remaining
Identify the error in numeric type conversion
What error does this MATLAB code produce?
a = int8(130); disp(a);
MATLAB
a = int8(130); disp(a);
ANo output, infinite loop
BError: Value exceeds int8 range
CWarning: Data truncated
DNo error, displays -126
Attempts:
2 left
💡 Hint
int8 can only store values from -128 to 127.
🧠 Conceptual
advanced
1:30remaining
Memory size of numeric types
Which numeric type uses the least memory in MATLAB?
Aint8
Bdouble
Csingle
Dint32
Attempts:
2 left
💡 Hint
Check the number of bytes each type uses.
❓ Predict Output
expert
2:30remaining
Result of mixed numeric array operations
What is the output of this MATLAB code?
A = single([1.5, 2.5]); B = int16([2, 3]); C = A .* B; disp(class(C)); disp(C);
MATLAB
A = single([1.5, 2.5]);
B = int16([2, 3]);
C = A .* B;
disp(class(C));
disp(C);
A
single
3.0 7.5
B
double
3.0 7.5
C
int16
3 7
D
single
3 7
Attempts:
2 left
💡 Hint
Element-wise multiplication with mixed types converts integers to single.

Practice

(1/5)
1. Which numeric type is the default in MATLAB when you create a number like 5 without specifying its type?
easy
A. single
B. uint8
C. int32
D. double

Solution

  1. Step 1: Understand MATLAB default numeric type

    By default, MATLAB stores numbers as double precision floating-point values.
  2. Step 2: Check the given number type

    The number 5 without any type specification is stored as double.
  3. Final Answer:

    double -> Option D
  4. Quick Check:

    Default numeric type = double [OK]
Hint: Remember: MATLAB defaults to double unless specified [OK]
Common Mistakes:
  • Thinking single is default
  • Confusing integer types as default
  • Assuming uint8 is default
2. Which of the following is the correct way to create a single precision number with value 3.14 in MATLAB?
easy
A. x = single(3.14);
B. x = double(3.14);
C. x = int32(3.14);
D. x = single '3.14';

Solution

  1. Step 1: Identify correct syntax for single precision

    Use the function single() with a numeric argument to create a single precision number.
  2. Step 2: Check each option

    x = single(3.14); uses single(3.14) which is correct syntax. x = double(3.14); creates double, not single. x = int32(3.14); creates integer, which truncates decimals. x = single '3.14'; is invalid syntax.
  3. Final Answer:

    x = single(3.14); -> Option A
  4. Quick Check:

    Use single() function for single precision [OK]
Hint: Use single() function to convert numbers to single [OK]
Common Mistakes:
  • Using double() instead of single()
  • Trying to assign string to single
  • Using int32 for decimals
3. What will be the output of the following MATLAB code?
x = int8(127); y = x + 1; disp(y);
medium
A. -128
B. 128
C. Error: Overflow
D. 127

Solution

  1. Step 1: Understand int8 range

    int8 stores integers from -128 to 127. Adding 1 to 127 causes overflow.
  2. Step 2: Check overflow behavior

    In MATLAB, int8 overflow wraps around to the minimum value, so 127 + 1 becomes -128.
  3. Final Answer:

    -128 -> Option A
  4. Quick Check:

    int8 overflow wraps to -128 [OK]
Hint: int8 max is 127; adding 1 wraps to -128 [OK]
Common Mistakes:
  • Expecting 128 (out of range)
  • Expecting error on overflow
  • Thinking value stays at 127
4. Consider this MATLAB code:
a = single(1.5); b = int16(a); disp(b);

What is the output and why?
medium
A. 1.5
B. 1
C. 2
D. Error: Cannot convert single to int16

Solution

  1. Step 1: Convert single to int16 behavior

    When converting from single to int16, MATLAB truncates the decimal part (does not round).
  2. Step 2: Apply truncation to 1.5

    Truncating 1.5 results in 1, so b becomes 1.
  3. Final Answer:

    1 -> Option B
  4. Quick Check:

    Conversion truncates decimals, so 1.5 -> 1 [OK]
Hint: Conversion truncates decimals, no rounding [OK]
Common Mistakes:
  • Expecting rounding to 2
  • Expecting error on conversion
  • Expecting output as 1.5
5. You want to store a large array of numbers between 0 and 255 efficiently in MATLAB. Which numeric type should you choose to minimize memory while preserving all values exactly?
hard
A. double
B. single
C. uint8
D. int16

Solution

  1. Step 1: Identify value range and memory needs

    Values are between 0 and 255, so an unsigned 8-bit integer (uint8) fits exactly.
  2. Step 2: Compare types for memory and precision

    double and single use more memory and are floating-point. int16 uses 16 bits and allows negative values, wasting space.
  3. Step 3: Choose the best type

    uint8 uses 8 bits, stores 0-255 exactly, and saves memory.
  4. Final Answer:

    uint8 -> Option C
  5. Quick Check:

    Use uint8 for 0-255 integer range to save memory [OK]
Hint: Use uint8 for 0-255 integers to save memory [OK]
Common Mistakes:
  • Choosing double wastes memory
  • Using int16 wastes space and allows negatives
  • Using single loses integer precision
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