Computer Science, asked by AnilVasyani, 2 months ago

Q1. Study the method and answer the given questions.
public void sampleMethod()
{
for( int i=0; j<3; j++)
{
for( int j=0; j<2; j++)
{
int number = (int)
(Math.random()*10);
System.out.println(number);
}
}
}

(i) How many times does the loop execute?
(ii) What is the range of possible values stored in the variable number?

Answers

Answered by SƬᏗᏒᏇᏗƦƦᎥᎧƦ
195

Required códe:

Correct question.

Study the method and answer the given questions.

public void sampleMethod()

{

for( int i=0; j<3; j++)

{

for( int j=0; j<2; j++)

{

int number = (int)(Math.random()*10);

System.out.println(number);

}

}

}

(i) How many times does the loop execute?

(ii) What is the range of possible values stored in the variable number?

Answer:

(i) The outer loop executes three times, with i=0, 1 and 2.

For such execution of the outer loop, the inner loop executes 2 times, with j=0 and 1.

Therefore, the inner loop executes 3*2 = 6 times.

(ii) Math.random() returns a value r such that

0<=r <10

(int)(Math.random()*10) will return an integer value i such that 0<=i<=9

Thus, the range of possible values stored in the variable number will be 0-9 (both inclusive).

Similar questions