193k views
2 votes
Consider the following program:

class prob1{
int puzzel(int n){

int result;

if (n==1)
return 1;
result = puzzel(n-1) * n;
return result;
}
}

class prob2{

public static void main(String args[])

{

prob1 f = new prob1();

System.out.println(" puzzel of 6 is = " + (6));

}
}
Which of the following will be the output of the above program?
(a) 6
(b) 120
(c) 30
(d) 720
(e) 12.

User Cavachon
by
8.0k points

1 Answer

2 votes

Final answer:

Due to a typo in the print statement, the program will incorrectly output '6' instead of the factorial of 6, which is '720'. The factorial calculation is correct but not actually invoked in the printed statement.

Step-by-step explanation:

The given program is designed to calculate the factorial of a number using a recursive method defined in the prob1 class. The program should call the puzzel method with the input 6 to calculate 6!, which is the factorial of 6, but due to a typo in the prob2 class, it only prints out the number 6. To correct the code, the print statement should call the method and pass the integer 6 to it like so: System.out.println(" puzzel of 6 is = " + f.puzzel(6));. The correct output should be 720, which is the factorial of 6 (6 x 5 x 4 x 3 x 2 x 1).

If the code were corrected and the puzzel method were called correctly, the output would be (d) 720, as that is the product of all positive integers up to 6. However, because of the typo where the method is not called, the displayed output will be (a) 6.

User Swbbl
by
8.2k points