Limited access

Upgrade to access all content for this subject

A palindrome is defined as a string that is exactly the same forward or backward.

Which of the following segments of code will return true if the given number is a palindrome or return false otherwise?

For example, 543345 and 757 are both palindromes (returns true), but 1234 and 43223 are not palindromes (returns false).
I.

public static boolean isPalindrome(int n)
{
    String s = ""+n;
    String p = "";

    for (int i=0; i<s.length(); i++)
    {
        p = s.substring(i,i+1) + p;
    }
    if (s.equals(p))
        return true;
    return false;
}

II.

public static boolean isPalindrome(int n)
{
    int num = n;
    String p = "";

    while(num>0)
    {
        p += num/10;
        num = num%10;
    }
    if (p.equals(n+""))
        return true;
    return false;
}

III.

public static boolean isPalindrome(int n)
{
    int num = n;
    String p = "";

    while(num>0)
    {
        p += num%10;
        num = num/10;
    }
    if (p.equals(n+""))
        return true;
    return false;
}
A

I and III only.

B

I and II only.

C

III only.

D

II only.

E

I only.

Select an assignment template