Question: Consider the Scala definitions: class A { def f ( n: Int ) : Int = if ( n < = 1 ) 1 else

Consider the Scala definitions:
class A {
def f(n: Int): Int = if (n <=1)1 else n * f(n -1)
}
class B extends A {
override def f(n: Int): Int ={ println(n); super.f(n)}
}
val b: B = new B()
println(b.f(3))
What happens when this program is compiled and executed?
Options:
The program fails to compile because the recursive invocation of f inside the definition of method f in class A cannot refer to the other class's f method.
The program compiles and runs successfully and prints 3,2,1,6 on separate lines.
The program compiles and runs successfully and prints 6.
The program compiles and runs successfully and prints 3,6 on separate lines.
The program compiles, but fails at runtime when making a recursive call to f.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!