What is the value of ‘p’ in the following C++ code snippet? #include…

2023

What is the value of ‘p’ in the following C++ code snippet? #include <iostream> using namespace std; int main() { int p; bool a = true; bool b = false; int x = 10; int y = 5; p = (x || y) + (a + b); cout << p; return 0; }

  1. A.

    12

  2. B.

    16

  3. C.

    2

  4. D.

    More than one of the above

  5. E.

    None of the above

Attempted by 181 students.

Show answer & explanation

Correct answer: C

Step 1: Evaluate (x || y). Since x = 10 and y = 5, both are non-zero, so (x || y) evaluates to true, which is 1 in integer context.

Step 2: Evaluate (a + b). a = true (1), b = false (0), so (a + b) = 1 + 0 = 1.

Step 3: Add the results: 1 + 1 = 2. Thus, p = 2.

हिन्दी उत्तर:

चरण 1: (x || y) का मान ज्ञात करें। x = 10 और y = 5 दोनों गैर-शून्य हैं, इसलिए (x || y) सत्य होता है, जो पूर्णांक संदर्भ में 1 है।

चरण 2: (a + b) का मान ज्ञात करें। a = true (1), b = false (0), इसलिए (a + b) = 1 + 0 = 1।

चरण 3: परिणामों को जोड़ें: 1 + 1 = 2। इसलिए, p = 2।

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs