Computer Applications
Consider the following program segment in which the statements are jumbled.
Choose the correct order of statements to calculate and return the factorial of 4.
for(k = 1; k <= 4; k++) → 1
return fa; → 2
long fa = 1, k; → 3
fa *= k; → 4
- 1, 2, 3, 4
- 3, 1, 4, 2
- 3, 1, 2, 4
- 1, 3, 2, 4
Answer
3, 1, 4, 2
Reason — To calculate and return the factorial of 4 using a loop, we must follow the proper sequence of statements:
long fa = 1, k;– This is the declaration and initialization of variablesfaandk.for(k = 1; k <= 4; k++)– This sets up a loop to run from 1 to 4.fa *= k;– This multiplies the current value offabykduring each iteration.return fa;– This returns the final calculated factorial value.
So, the correct order is: 3, 1, 4, 2, which correctly calculates and returns the factorial.
Related Questions
The output of the below statement is:
System.out.println("I said, \"It\'s wise to obey elders.\"");- I said, 'It is wise to obey elders.'
- I said, "It's wise to obey elders."
- I said, It's wise to elders.
- "'It's wise to obey elders.'"
What is the output of the statement given below?
"ANGER".compareTo("ANGEL")- 3
- -6
- 6
- 0
Write the Java expression to find the product of square root of P and the square root of Q using the methods of Math class.
Write the output of the following String method:
String x = "talent"; String y = "matrix"; System.out.println(x.substring(3).concat(y.substring(3)));