0
0
MATLABdata~20 mins

Type checking (class, isa) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MATLAB Type Checking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this MATLAB code using class()?

Consider the following MATLAB code snippet:

var = 3.14;
disp(class(var));

What will be printed to the console?

MATLAB
var = 3.14;
disp(class(var));
Adouble
Bsingle
Cint32
Dchar
Attempts:
2 left
💡 Hint

Check the default numeric type for decimal numbers in MATLAB.

Predict Output
intermediate
2:00remaining
What does isa() return for a cell array?

Given this MATLAB code:

c = {1, 2, 3};
result = isa(c, 'cell');
disp(result);

What is the output?

MATLAB
c = {1, 2, 3};
result = isa(c, 'cell');
disp(result);
A1
B0
C'true'
D'false'
Attempts:
2 left
💡 Hint

isa returns logical true (1) or false (0).

Predict Output
advanced
3:00remaining
What is the output of isa() for a subclass object?

Suppose you have a class Animal and a subclass Dog. Consider this code:

classdef Animal
end

classdef Dog < Animal
end

obj = Dog();
disp(isa(obj, 'Animal'));

What will be displayed?

MATLAB
classdef Animal
end

classdef Dog < Animal
end

obj = Dog();
disp(isa(obj, 'Animal'));
AError: Undefined function or variable 'Dog'
B0
CError: Invalid class definition
D1
Attempts:
2 left
💡 Hint

isa returns true if the object is an instance of the class or its subclass.

Predict Output
advanced
2:00remaining
What error does this code raise when using class() on an undefined variable?

Analyze this MATLAB code:

disp(class(undefinedVar));

What error message will MATLAB produce?

MATLAB
disp(class(undefinedVar));
AError: Invalid class name.
BError: Syntax error near 'class'.
CError: Undefined function or variable 'undefinedVar'.
DNo error, prints 'double'.
Attempts:
2 left
💡 Hint

Check what happens when you use a variable that was never created.

🧠 Conceptual
expert
3:00remaining
Which option correctly checks if a variable is numeric or a subclass of numeric?

You want to check if a variable x is numeric or any subclass of numeric types in MATLAB. Which code snippet correctly does this?

Aisa(x, 'double') || isa(x, 'single')
Bisnumeric(x)
Cclass(x) == 'numeric'
Disa(x, 'numeric')
Attempts:
2 left
💡 Hint

Consider built-in functions designed for type checking of numeric data.