Determine output of the following program.public class Test{ public static void main(String args[]){ System.out.println( Math.floor( Math.random( ) ) ) ; }}1.010.00.00.5
Question
Determine output of the following program.public class Test{ public static void main(String args[]){ System.out.println( Math.floor( Math.random( ) ) ) ; }}1.010.00.00.5
Solution
The output of the program will always be 0.0.
Here's why:
-
Math.random()generates a random double greater than or equal to 0.0 and less than 1.0. -
Math.floor()rounds down to the nearest whole number.
So, no matter what random number is generated by Math.random(), Math.floor() will always round it down to 0.0 because the generated number is less than 1.0.
Similar Questions
Analyse the following Java code carefully. Write the output as produced by each printlnstatement. (In your answer just write the particular output statement or line numberfollowed by the exact output it produces). (11 Marks)1. public class Test {2. public static void main(String[] args) {3. System.out.println((int)(Math.random() * 10) / 10);4. System.out.println(Math.pow(3, 2));5. System.out.println(35 % 7);6. System.out.println(3 + 4.5 * 2 < 2 * 9.5);7. int number = 14;8. if (number % 7 == 0)9. System.out.println(8 * number);10. System.out.println(2 * number);11. int x = 1942;12. System.out.println(x / 100);13. System.out.println(x % 100);14. System.out.println(x + " is " + ((x % 2 == 0) ?"even" : "odd"));15. int y = -5;16. ++y;17. System.out.println(y);18. int value = 453;19. int d1 = value % 10;20. int d2 = (value / 10) % 10;21. int d3 = (value / 100) % 10;22. System.out.println("" + d1 + d2 + d3);23. }24. }
Select the correct answerWhat will be the output of the following Java program?class Output { public static void main(String args[]) { double ct1 = 3.0; double ct2 = 2.0; double ct3 = Math.pow( ct1, ct2 ); System.out.print(ct3); } }Options3.02.09.08.0
e correct answerWhat is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}
Determine output:public class Test{ public static void main(String args[]){ int i, j; for(i=1, j=0;i<10;i++) j += i; System.out.println(i); }}1011920
Determine output:public class Test{ public static void main(String args[]){ int i; for(i = 1; i < 6; i++){ if(i > 3) continue ; } System.out.println(i); }}23466
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.