Quizzes
Site Language: English
Українська
English
Русский
Programming Tests
Login
Sign Up
Programming Tests
Theory
Snippets
Papers
Landing
Android
Prices
FAQ
Cosmo Story
Terms and Conditions
Privacy Policy
Cookies Policy
Send Feedback
recursion
:
Content language: English
Русский
What will the following code print out? public class Main { public static void main(String[] args) { recur(99); } public static void recur(int a) { if (a <= 100) { System.out.println("a=" + a); recur(++a); System.out.println("a=" + a); } } }
recursion
What will be the result of compiling and running the following code? public class Test { static String s; static void go() { System.out.println(s); go(); } public static void main(String[] args) { go(); } }
recursion
What will be the result of compilation and execution of the following code: public class Test { static int fact(int x) { if (x == (-2)) return -2; return (fact(x-1) * x); } public static void main(String[] args) { System.out.println(Test.fact(2)); } }
recursion
What will be the result of compilation and execution of the following code? public class Test { enum EnumTest { VALUE1 { public EnumTest getValue() { return VALUE2.getValue().getValue(); } }, VALUE2 { public EnumTest getValue() { return VALUE1; } }; abstract EnumTest getValue(); } public static void main(String... args) { System.out.println(EnumTest.VALUE1); System.out.println(EnumTest.VALUE2); System.out.println(EnumTest.VALUE1.getValue()); System.out.println(EnumTest.VALUE2.getValue()); } }
recursion
Tail recursion optimization
recursion
Which recursive calls are optimized by Scala complier, so they run just as quickly as hand-optimized versions that use while loops?
recursion
What will be the result of following code execution? object Main extends App { def gcd(a: Int, b: Int) = if (b == 0) a else gcd(b, a % b) println(gcd(5, 15)) }
recursion
Try to fix the loop function inside fib so that it returns the correct values for each case in a tail-recursive way. What should the missing expressions for the trivial case and the recursive call be? def fib(n: Int): Int = { @annotation.tailrec def loop(n: Int, prev: Int, cur: Int): Int = if (n <= __ ) prev else loop(n - __, cur, prev + cur) loop(n, 0, 1) }
recursion
Take a detailed look at implementation of isSorted function, what would be the result of applying the following anonymous function with array to it? def isSorted[A](as: Array[A], ordering: (A, A) => Boolean): Boolean = { @annotation.tailrec def go(n: Int): Boolean = if (n >= as.length - 1) true else if (ordering(as(n), as(n + 1))) false else go(n + 1) go(0) } isSorted(Array(1, 3, 5, 7), (x: Int, y: Int) => x > y)
recursion
Take a detailed look at implementation of isSorted function, what would be the result of applying the following anonymous function with array to it? def isSorted[A](as: Array[A], ordering: (A, A) => Boolean): Boolean = { @annotation.tailrec def go(n: Int): Boolean = if (n >= as.length - 1) true else if (ordering(as(n), as(n + 1))) false else go(n + 1) go(0) } isSorted(Array(7, 5, 1, 3), (x: Int, y: Int) => x < y)
recursion
← Prev
1
2
Next →
Sign Up Now
or
Subscribe for future quizzes