Matlab Input & Output Quiz

Matlab
0 Passed
0% acceptance

35 practical questions on interacting with users via the command window, including input, disp, and formatted output with fprintf.

35 Questions
~70 minutes
1

Question 1

Which command prompts the user to enter a number and stores it in variable 'x'?

A
x = input('Enter a number: ');
B
x = get('Enter a number: ');
C
x = read('Enter a number: ');
D
input(x, 'Enter a number: ');
2

Question 2

How do you display the text 'Hello World' in the Command Window without the variable name?

A
disp('Hello World')
B
print('Hello World')
C
show('Hello World')
D
'Hello World'
3

Question 3

Which `fprintf` specifier is used to print a floating-point number?

A
%f
B
%d
C
%s
D
%x
4

Question 4

How do you request a *string* input from the user?

A
name = input('Enter name: ', 's');
B
name = input('Enter name: ');
C
name = inputString('Enter name: ');
D
name = raw_input('Enter name: ');
5

Question 5

What does the escape character '\n' do in `fprintf`?

A
Inserts a new line
B
Inserts a null character
C
Inserts a number
D
Nothing, it prints \n
6

Question 6

What is the output of `fprintf('Value: %d', 5.7)`?

A
Value: 5.700000
B
Value: 5.7e+00
C
Value: 5
D
Value: 5.7
7

Question 7

How do you combine the string 'Age: ' and the number 25 into a single displayed string?

A
disp(['Age: ' num2str(25)])
B
disp('Age: ' + 25)
C
disp('Age: ', 25)
D
disp('Age: ' & 25)
8

Question 8

What happens if the user presses Enter without typing anything for `x = input('Val: ')`?

A
x becomes an empty matrix []
B
x becomes 0
C
x becomes NaN
D
MATLAB waits indefinitely
9

Question 9

Which format specifier limits a floating point number to 2 decimal places?

A
%.2f
B
%2f
C
%f.2
D
%.2d
10

Question 10

What is the difference between `disp` and `fprintf`?

A
`disp` adds a newline automatically; `fprintf` does not
B
`fprintf` adds a newline automatically; `disp` does not
C
`disp` can format numbers; `fprintf` cannot
D
They are identical
11

Question 11

How do you print a literal percent sign '%' using `fprintf`?

A
%%
B
%
C
%
D
pct
12

Question 12

If `name = 'Alice'`, what does `fprintf('Hello %s', name)` output?

A
Hello Alice
B
Hello 'Alice'
C
Hello name
D
Error
13

Question 13

Which function converts a number to a string for use in `disp`?

A
num2str
B
str2num
C
to_string
D
char
14

Question 14

What does the command `pause` do?

A
Stops execution until the user presses a key
B
Stops execution forever
C
Clears the screen
D
Skips the next line
15

Question 15

How do you display a matrix `A` nicely in the Command Window?

A
disp(A)
B
fprintf(A)
C
show(A)
D
echo(A)
16

Question 16

What is the result of `input('Prompt')` if the user types `pi`?

A
The numeric value 3.1416...
B
The string 'pi'
C
Error
D
NaN
17

Question 17

Which specifier prints a number in exponential notation (e.g., 1.2e+05)?

A
%e
B
%f
C
%g
D
%x
18

Question 18

How do you print a backslash character `` using `fprintf`?

A
\\
B
\
C
/\
D
bs
19

Question 19

What does `int2str(10)` return?

A
The string '10'
B
The number 10
C
The integer 10
D
Error
20

Question 20

Can `fprintf` write to a file instead of the screen?

A
Yes, if you provide a file ID as the first argument
B
No, it is only for the console
C
Yes, using `fprintf('filename', ...)`
D
Only using `save`
21

Question 21

What happens if you use `disp` with multiple arguments, like `disp(a, b)`?

A
Error: Too many input arguments
B
It displays both
C
It displays the first one
D
It concatenates them
22

Question 22

Which command clears the Command Window text but keeps variables in the workspace?

A
clc
B
clear
C
clf
D
home
23

Question 23

What is the result of `sprintf('Val: %d', 10)`?

A
It returns the string 'Val: 10' but does not print it
B
It prints 'Val: 10' to the screen
C
It saves to a file
D
Error
24

Question 24

How do you align text in columns using `fprintf`?

A
Use width specifiers like `%10d`
B
Use tabs `\t` only
C
Use `align` function
D
It is automatic
25

Question 25

What does `input('Prompt', 's')` return if the user types `123`?

A
The string '123'
B
The number 123
C
A 1x3 double array
D
Error
26

Question 26

Which function allows you to display a warning message in orange text?

A
warning('Message')
B
error('Message')
C
disp('Message', 'orange')
D
alert('Message')
27

Question 27

How do you print a single quote `'` inside a `disp` string?

A
disp('It''s me')
B
disp('It's me')
C
disp("It's me")
D
Both A and C
28

Question 28

What does `mat2str` do?

A
Converts a matrix to a string representation that can be evaluated
B
Converts a matrix to a structure
C
Converts a .mat file to a string
D
Nothing
29

Question 29

If `x = [1, 2, 3]`, what does `fprintf('%d ', x)` output?

A
1 2 3
B
1
C
[1 2 3]
D
Error
30

Question 30

What is the purpose of the `error` function?

A
Displays a message in red and stops script execution
B
Displays a message and continues
C
Logs an error to a file
D
Checks for errors
31

Question 31

How do you read a single character from the keyboard without waiting for Enter?

A
k = getkey() (requires external tool) or pause (wait for key)
B
input('Prompt', 'char')
C
readchar()
D
Standard MATLAB requires Enter for `input`
32

Question 32

Which output function is best for creating tabular data in a text file?

A
fprintf
B
disp
C
save
D
textwrite
33

Question 33

What does `newline` return in modern MATLAB?

A
A character representing a new line
B
It prints a new line
C
It clears the line
D
Error
34

Question 34

How do you suppress the output of an assignment `x = 5`?

A
Add a semicolon: x = 5;
B
Use `silent(x = 5)`
C
Use `quiet` mode
D
You cannot
35

Question 35

What is the correct way to print 'Value is 10%'?

A
fprintf('Value is 10%%')
B
fprintf('Value is 10%')
C
fprintf('Value is 10%')
D
disp('Value is 10%')

QUIZZES IN Matlab