MCQs on Control Flow and Logic | MATLAB

Conditional Statements (if, else, elseif)

  1. What is the result when the following condition is true?
    if (x > 10)
    a) x is less than 10
    b) x is greater than 10
    c) Syntax error
    d) None of the above
  2. Which of the following is true about the else if statement?
    a) It is only used after an else statement.
    b) It always follows an if statement.
    c) It can be used without an if statement.
    d) It is optional in the flow of control.
  3. What happens if no else block is provided in a conditional structure?
    a) The program stops.
    b) The program runs regardless of the condition.
    c) The program continues to the next condition or block.
    d) None of the above.
  4. How do you check multiple conditions in a nested if-else structure?
    a) Using a switch statement.
    b) Using logical operators like AND (&&) and OR (||).
    c) By adding more else statements.
    d) By nesting for loops.
  5. Which of the following is the correct syntax to implement an if-else statement in most programming languages?
    a) if condition { action } else { action }
    b) if (condition) { action } else { action }
    c) if condition: action else action
    d) if condition then action else action

Loops (for, while)

  1. What is the output of the following loop?
    for i in range(5): print(i)
    a) 1 2 3 4 5
    b) 0 1 2 3 4
    c) 1 2 3 4
    d) 0 2 4
  2. Which loop is guaranteed to run at least once?
    a) for loop
    b) while loop
    c) do-while loop
    d) None of the above
  3. How do you exit a loop early in most programming languages?
    a) break
    b) exit
    c) return
    d) stop
  4. What is the result of the following while loop?
    i = 0; while (i < 3): print(i); i += 1
    a) 0 1 2
    b) 1 2 3
    c) Infinite loop
    d) Error
  5. What type of loop would be used to iterate through all elements in a list or array?
    a) for loop
    b) while loop
    c) Both for and while
    d) do-while loop
  6. Which loop is most appropriate when you don’t know the number of iterations in advance but want to keep looping until a condition is met?
    a) for loop
    b) while loop
    c) do-while loop
    d) switch loop
  7. What is a potential issue when using an infinite while loop?
    a) The program might run indefinitely unless stopped.
    b) The loop will not execute at all.
    c) It will always crash the program.
    d) It slows down the performance unnecessarily.

Logical Operators and Conditions

  1. What is the result of the following expression: True AND False?
    a) True
    b) False
    c) Syntax error
    d) None of the above
  2. Which logical operator is used to combine two conditions so that both must be true?
    a) OR
    b) AND
    c) NOT
    d) XOR
  3. Which logical operator is used to negate a condition?
    a) AND
    b) OR
    c) NOT
    d) XOR
  4. What is the result of False OR False?
    a) True
    b) False
    c) Syntax error
    d) None of the above
  5. How do you check if a value x is between 5 and 10 inclusive using logical operators?
    a) if x >= 5 and x <= 10
    b) if x < 5 and x > 10
    c) if x >= 5 or x <= 10
    d) if x == 5 and x == 10
  6. What is the outcome of the expression x > 5 or x == 10 when x = 7?
    a) True
    b) False
    c) Error
    d) Undefined
  7. What does the logical expression !(a && b) evaluate to when a = true and b = false?
    a) true
    b) false
    c) error
    d) undefined

Error Handling Using try and catch

  1. What is the purpose of using a try block in error handling?
    a) To ignore errors
    b) To test for errors during execution
    c) To define the function
    d) To raise an error manually
  2. What happens if an error occurs inside a try block without a catch block?
    a) The program will terminate immediately.
    b) The error will be handled by the system.
    c) The program will continue executing as normal.
    d) The program will silently ignore the error.
  3. What type of error does a catch block handle?
    a) Syntax errors
    b) Runtime errors
    c) Logical errors
    d) None of the above
  4. Which statement is used to specify the error in a catch block?
    a) catch(Exception e)
    b) catch e as Exception
    c) catch(Error)
    d) try(Error)
  5. What will happen if a catch block does not specify the type of error?
    a) It will catch all errors.
    b) It will not catch any errors.
    c) The program will crash.
    d) Syntax error.
  6. Which of the following statements is used to raise an error manually?
    a) raise
    b) throw
    c) error
    d) exception
  7. What is the purpose of the finally block?
    a) To catch errors
    b) To execute code regardless of error occurrence
    c) To declare variables
    d) To end the program
  8. How can you prevent an error from propagating after catching it?
    a) By returning a default value
    b) By logging the error only
    c) By re-throwing the error
    d) By ignoring it in the catch block
  9. Which of the following is a valid use of a try-catch block?
    a) Handling exceptions that might occur during file I/O
    b) Controlling program flow based on user input
    c) Checking if a file exists
    d) All of the above
  10. What is a common mistake when using error handling mechanisms?
    a) Using too many catch blocks
    b) Failing to include a finally block
    c) Ignoring errors entirely
    d) Using throw inside the catch block
  11. How does a try-catch structure improve code?
    a) It makes code less readable
    b) It ensures errors are logged without interrupting program flow
    c) It prevents errors from being raised
    d) It terminates the program early

Answer Table

QnoAnswer
1b) x is greater than 10
2b) It always follows an if statement.
3c) The program continues to the next condition or block.
4b) Using logical operators like AND (&&) and OR (`
5b) if (condition) { action } else { action }
6b) 0 1 2 3 4
7c) do-while loop
8a) break
9a) 0 1 2
10a) for loop
11b) while loop
12a) The program might run indefinitely unless stopped.
13b) False
14b) AND
15c) NOT
16b) False
17a) if x >= 5 and x <= 10
18a) True
19a) true
20b) To test for errors during execution
21a) The program will terminate immediately.
22b) Runtime errors
23a) catch(Exception e)
24a) It will catch all errors.
25b) throw
26b) To execute code regardless of error occurrence
27a) By returning a default value
28a) Handling exceptions that might occur during file I/O
29c) Ignoring errors entirely
30b) It ensures errors are logged without interrupting program flow

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top