Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

MATLAB & GNU Octave Module 4

Logical arrays and control flow interview questions

Interview questions on logical arrays and control flow: warm-ups, then what each harder question tests and how a good answer starts.

9 questions: 3 warm-ups and 6 with model answers · Levels: Fresher to senior

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    What is logical indexing, and what does x(x < 0) = 0 do?

    Answered in the lesson Logical arrays, comparisons and logical indexing

  2. Fresher to mid

    What is the difference between & and && in MATLAB and Octave?

    Answered in the lesson Decisions: if, elseif, else and switch

  3. Fresher

    What does a for loop iterate over when it is given a matrix?

    Answered in the lesson Loops: for, while, break and continue

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Fresher to mid

    A logger stores 10 million readings from 8 channels in a 10,000,000-by-8 matrix. Negative values and the error code 999 must become NaN, and the report must list how many values each channel lost. Write the vectorised code, estimate the memory it needs, and say how readings that were already NaN affect the counts.

    What it tests
    Building masks on a matrix, counting along a dimension with sum, sizing logical and double arrays in memory, and knowing that NaN fails every comparison.
    First step
    Build one mask for both faults, bad = X < 0 | X == 999, which is 10,000,000-by-8 like X, and count per channel with sum(bad), which adds down each column. The data take 640 MB as doubles, and every mask of that size takes 80 MB.
    Follow-ups

    From the lesson Logical arrays, comparisons and logical indexing

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  2. Fresher to mid

    A colleague filters temperatures with T(0 < T < 40), and the result keeps every value, -5 and 120 included, with no error. Explain what the expression computes, fix it, and say which test cases would have caught the bug before it shipped.

    What it tests
    Left-to-right evaluation of comparisons, combining masks with &, and choosing test values below, at, inside and above both limits, and NaN.
    First step
    Work the expression out in order: 0 < T runs first and gives a mask of 0s and 1s, and that mask is then compared with 40. Every 0 and every 1 is below 40, so every element passes.
    Follow-ups

    From the lesson Logical arrays, comparisons and logical indexing

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Fresher to mid

    An input check reads if loads <= 0, error('invalid loads'), end. It let [5 0 7] through, let an empty vector through, and let [5 NaN 7] through. Explain each case, and rewrite the check so that it stops all three.

    What it tests
    How if reads an array condition, any and all, empty input, NaN in comparisons, and writing a validation that states its intent.
    First step
    Work out the condition for [5 0 7]: loads <= 0 is [0 1 0], and if takes its branch only when every element is non-zero, so the error never fires. The check means any, not all.
    Follow-ups

    From the lesson Decisions: if, elseif, else and switch

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  4. Mid to senior

    A script that runs without errors in Octave 11.3 stops in MATLAB at if flags && n > 0, where flags is a 1-by-3 logical array. Explain the difference, give the portable fix, and name two other condition or comparison patterns that behave differently in the two programs.

    What it tests
    Scalar operands of && and ||, how Octave treats array and empty operands, & inside conditions, and character arrays against strings created with double quotes.
    First step
    MathWorks requires each operand of && to evaluate to a scalar logical result, and flags has three elements. Octave 11.3 accepted [1 1] && 1, so the problem never showed there. Decide what the script means, all(flags) or any(flags), and write that.
    Follow-ups

    From the lesson Decisions: if, elseif, else and switch

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  5. Fresher to mid

    A Newton solver reports convergence and prints NaN as the root for some starting points. Its loop is while abs(f) > tol && k < maxit, and it reports success whenever k < maxit. Explain the failure, fix the loop and the report, and say what else a robust loop should check.

    What it tests
    NaN in loop conditions, deciding convergence from the error rather than the count, iteration caps, and reporting failure with an error identifier.
    First step
    Start it at x0 = 0 for f(x) = x^2 - 2: the derivative 2x is 0, so the first step gives Inf and the second NaN. abs(NaN) > tol is false, so the loop ends after 2 passes, and k < maxit calls that a success.
    Follow-ups

    From the lesson Loops: for, while, break and continue

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  6. Fresher to mid

    A deposit model updates a balance every quarter in a for loop, and a colleague asks you to vectorise it. When can you, how do you show that the two versions give the same balances, and which loops should stay loops?

    What it tests
    Recognising recurrences that have an array form, preallocation, checking two implementations against each other with a tolerance, and steps that depend on a decision about the previous value.
    First step
    Write the recurrence: b(q) = b(q - 1) * (1 + r(q)/4). Each balance is P times a running product of factors, so P * cumprod(1 + r/4) gives all of them at once. Compare the two results with a tolerance, not with isequal.
    Follow-ups

    From the lesson Loops: for, while, break and continue

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All MATLAB & GNU Octave interview questions · The MATLAB & GNU Octave track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.