Question

What is returned by the call: ben(51) ? public static String ben(int x) { if(x /...

What is returned by the call: ben(51) ?

public static String ben(int x)

{

if(x / 5 <= 0)

return x % 5;

else return (x % 5) + ben(x / 5);

}

Homework Answers

Answer #1

Here, the output will be an error because the function ben(int x) is suppossed to return 'String' datatype but it returns 'int' datatype.

The String in the declaration line : public static String ben(int x), says that the function is returning String value, but the return statements in the function is returning integer values.

To solve this error, we can replace the String in public static String ben(int x) with int which will return 3 as the return value for ben(51);

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
public class Mystery { public static String mystery(String str, int input) { String result = "";...
public class Mystery { public static String mystery(String str, int input) { String result = ""; for (int i = 0; i < str.length() - 1; i++) { if (input == 0) { str = ""; result = str; } if (input == -2) { result = str.substring(2, 4); } if (input == 1) { result = str.substring(0, 1); } if (input == 2) { result = str.substring(0, 2); } if (input == 3) { result = str.substring(2, 3); }...
Write a method with the following header: public static int getValidInput(int low, int high, String message)...
Write a method with the following header: public static int getValidInput(int low, int high, String message) This method will return a user entered number between high and low inclusive. If a number is entered that is not between high and low, string message will be printed to the screen. If high is less than low, a -1 is returned. For example, given this call: int input = getValidInput(0, 100, “Not a valid grade, Please re- enter”); The following could occur:...
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
Given the recursive method definition:    public static int recurMethod (int num) {         if (num...
Given the recursive method definition:    public static int recurMethod (int num) {         if (num > 10)             return num;         else             return num + recurMethod(num / 4);     } And the initial call: int answer = recurMethod(320); list the values that will be returned from each call to the previous call of recursMethod. Start with the last recursive call, and work backwards, with one space between each returned value, ending with the value returned to the original...
What is the output of the following code segment? public class Exception { ... static int...
What is the output of the following code segment? public class Exception { ... static int divider(int x, int y) { try { return x/y; } catch (ArrayIndexOutOfBoundsException a) { System.out.print("A"); return 1; } } static int computer(int x, int y) { try { return divider(x,y); } catch (NullPointerException b) { System.out.print("B"); } return 2; } public static void main(String args[]){ try { int i = computer (100, 0); } catch (ArithmeticException c) { System.out.print("C"); } } } ABC A...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
Consider the recursive function pingPong described below. static String pingPong(int x) { if (x % 2...
Consider the recursive function pingPong described below. static String pingPong(int x) { if (x % 2 == 0 && x % 3 == 0) return "ping-pong"; else if (x % 2 == 0) return "ping," + pingPong(x - 1); else if (x % 3 == 0) return "pong," + pingPong(x - 1); else return "?," + pingPong(x - 1); } a) what's the base-case of pingPong? b) what's the result of pingPong(10)?
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)          ...
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)           return true;        else           return false; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length)        return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the following parameters: numbers = {1, 2, 2, 3, 3,...
using System; public static class Lab5 { public static void Main() { // declare variables int...
using System; public static class Lab5 { public static void Main() { // declare variables int inpMark; string lastName; char grade = ' '; // enter the student's last name Console.Write("Enter the last name of the student => "); lastName = Console.ReadLine(); // enter (and validate) the mark do { Console.Write("Enter a mark between 0 and 100 => "); inpMark = Convert.ToInt32(Console.ReadLine()); } while (inpMark < 0 || inpMark > 100); // Use the method to convert the mark into...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX;...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX; y= initialY; } public boolean equals (Object o){ if (o instanceof Point){ Point other = (Point)o; return (x == other.x && y == other.y); }else{ return false; } } } We haev defined "equals" method for our class using "instanceof". We define and use instances (or objects) of this class in the following scenarios. In each case, specify what is the output. (hint: there...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT