Recursion

What will be the result when you run this class?

For clarification, the language used is Java.

It will have a stack overflow error. It can have different outputs. It will not have output. It will print "Brilliant".

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

L S
Feb 1, 2015

In Java, division with integers will automatically round down to a int, so 1/a will be 0 if a>1. Math.random() returns a double that is greater than or equal to 0 and less than 1. In this class the base case is always true, so it will leave the recursive method before it can output anything, therefore there is no output.

Feels so great to see a Java question among all the Python ones !!

A Former Brilliant Member - 6 years, 4 months ago

Great solution. :)

Ellen Shang - 6 years, 4 months ago
Rahadian Putra
Feb 22, 2015

Been working with C++, never played with Java. But since "Math.random()" (i suspected) has a value greater than 0 and division 1/a for integer always equal to 0, so it has no output. (The condition will never be met)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...