📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    Consider the following C++ code:     int a =

    5;     int b = 10;     if (a > 0 && b < 10) {         a = a + b;     } else if (a == 5 || b == 10) {         a = b - a;     } else {         a = 0;     }     // What is the value of 'a' after the execution of this code?
    B 5 Correct Answer Incorrect Answer
    C 10 Correct Answer Incorrect Answer
    D 15 Correct Answer Incorrect Answer
    E -5 Correct Answer Incorrect Answer

    Solution

    Let's trace the execution with the initial values: a = 5, b = 10.     1.  if (a > 0 && b < 10):         (a > 0) evaluates to (5 > 0), which is true.         (b < 10) evaluates to (10 < 10), which is false.         The entire condition true && false evaluates to false.         Therefore, the code block inside this if statement is skipped.     2.  else if (a == 5 || b == 10):         (a == 5) evaluates to (5 == 5), which is true.         (b == 10) evaluates to (10 == 10), which is true.         The entire condition true || true evaluates to true.         Therefore, the code block inside this else if statement is executed.     3.  Inside the else if block:         a = b - a;         Substitute the current values: a = 10 - 5;         a becomes 5.     Since an else if block was executed, the final else block is skipped.     The final value of a is 5.

    Practice Next
    ask-question