In which all cases does an exception gets generated. Select the two correct answers. int i = 0, j = 1;

  1. if((i == 0) || (j/i == 1))

  2. if((i == 0) | (j/i == 1))

  3. if((i != 0) && (j/i == 1))

  4. if((i != 0) & (j/i == 1))


Correct Option: B,D

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) if((i == 0) || (j/i == 1)) - This option is incorrect because it uses the logical OR operator (||) which short-circuits and does not evaluate the second part of the condition if the first part is true. Therefore, an exception will not be generated in this case.

Option B) if((i == 0) | (j/i == 1)) - This option is correct because it uses the bitwise OR operator (|) which evaluates both parts of the condition. If i is 0, the second part of the condition (j/i == 1) will be evaluated, and since dividing by 0 is undefined, an exception will be generated.

Option C) if((i != 0) && (j/i == 1)) - This option is incorrect because it uses the logical AND operator (&&) which short-circuits and does not evaluate the second part of the condition if the first part is false. Therefore, an exception will not be generated in this case.

Option D) if((i != 0) & (j/i == 1)) - This option is correct because it uses the bitwise AND operator (&) which evaluates both parts of the condition. If i is not 0, the second part of the condition (j/i == 1) will be evaluated, and since dividing by 0 is undefined, an exception will be generated.

The correct answers are B) if((i == 0) | (j/i == 1)) and D) if((i != 0) & (j/i == 1)). These options are correct because they use the bitwise OR and bitwise AND operators, respectively, which evaluate both parts of the condition and can result in an exception if dividing by 0.

Find more quizzes: