Question 36

Consider the following code segment: if ( Y<0 )

Consider the following code segment: if ( Y<0 ) {X = −X; Y = − Y;} Z = 0; while (Y>0) {Z = Z + X; Y = Y −1;} Assume that X, Y and Z are inter variables, and that X and Y have be initialized. Which of the following best describes what this code segment does?
  1. ASets Z to be the product X* Y
  2. BSets Z to be the sum X+Y
  3. CSets Z to be the absolute value of X
  4. DSets Z to be the value of Y
Show answerHide answer

Correct answer: (A) Sets Z to be the product X* Y

Explanation

If Y is negative, both signs are flipped. The loop then adds X to Z Y times, so Z = X × Y.

From DSSSB TGT Computer Science Practice Set 2

This is question 36 of DSSSB TGT Computer Science Practice Set 2. See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.