মেধাবী
Toggle menu

Let x be an integer which can take a value of 0 or 1. The statement if (x == 0) x =1 ; else x =0 ; is equivalent to which one of the following ?

সঠিক উত্তর
x =1 -x ;

বিস্তারিত ব্যাখ্যা

এই প্রশ্নের বিশেষজ্ঞ বিশ্লেষণ

Let x be an integer which can take a value of 0 or 1. The statement <code>if (x == 0) x =1 ; else x =0 ;</code> is equivalent to which one of the following?

  1. x = 1 + x;

  2. x = x - 1;

  3. x = 1 - x;

  4. x = 1 % x;

The correct answer is: x = 1 - x;

To understand why the statement <code>x = 1 - x;</code> is equivalent to <code>if (x == 0) x = 1; else x = 0;</code>, consider the logical flow of the given if-else statement:

  • When <code>x == 0</code>: The statement <code>x = 1;</code> is executed.

  • When <code>x == 1</code>: The statement <code>x = 0;</code> is executed.

Now, let's analyze each choice to see which one replicates this behavior:

  1. <code>x = 1 + x;</code> <br>This does the following:

    • If <code>x = 0</code>, then <code>x = 1 + 0 = 1</code>.

    • If <code>x = 1</code>, then <code>x = 1 + 1 = 2</code>.

    Therefore, it does not correctly replicate the behavior of the given if-else statement.

  2. <code>x = x - 1;</code> <br>This does the following:

    • If <code>x = 0</code>, then <code>x = 0 - 1 = -1</code>.

    • If <code>x = 1</code>, then <code>x = 1 - 1 = 0</code>.

    Again, this does not correctly replicate the behavior because <code>x</code> can become -1, which is not a valid outcome based on the original problem constraints (i.e., <code>x</code> can only be 0 or 1).

  3. <code>x = 1 - x;</code> <br>This does the following:

    • If <code>x = 0</code>, then <code>x = 1 - 0 = 1</code>.

    • If <code>x = 1</code>, then <code>x = 1 - 1 = 0</code>.

    Hence, this choice perfectly replicates the behavior of the given if-else statement.

  4. <code>x = 1 % x;</code> <br>This choice is problematic because it attempts to divide by zero when <code>x</code> is 0, which will result in a runtime error. Therefore, it is not a valid transformation of the given problem.

Therefore, upon examination of each choice, we conclude that the correct answer is: x = 1 - x;

সকল অপশন

রেফারেন্স মাত্র

x = 1 +x ;
x =x -1 ;
x =1 -x ; সঠিক
x = 1 % x;

প্রশ্ন তথ্য

বিষয়
আই সি টি
শ্রেণী
চাকুরী প্রস্তুতি - ব্যাংক
মার্ক
1.00

অ্যাপে আরও ১ লক্ষ+ প্রশ্ন অনুশীলন করুন

বিনামূল্যে • ৪.৯★ রেটিং • ৫০K+ ডাউনলোড

ডাউনলোড করুন

মেধাবী অ্যাপ

বিনামূল্যে • ৪.৯★

ডাউনলোড