Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Java Questions

1. ## Java Questions

These questions are part of a captcha. I'm 75% sure I got the first one right, but the second, I have no clue. Can anyone help?
Code:
``` int a = 2;
int b = "JZG".hashCode() % 3000;
int c = "ARA".hashCode() % 3000;
for (int i = 0; i <= c; i++)
a = (a ^ i) % b;
return a;```
Q. 1. What is the returned value?

Q. 2. What is the greatest common divisor of the previous two results?

• Despite the similar sounding names, Java is not the same as Javascript.
Moving from Java forum to Javascript forum.

• Objection ... this code is JAVA and not Javascript ... so the section was correct but the headline isn't

• Moved back to Java.

• My bad, I should have paid attention that you have datatypes here.
The first answer is 1955. I ran this as you have here to get that, but also wrote a quick hashcode creation for it to see how you'd do it by hand if you had to.
The greatest common divisor is more complicated. Since I don't know what this refers to by the "previous two results", I have no choice but to write a function to determine this. To do this, I used recursion, although you may also loop:
Code:
```	public static int getGCD(int a, int b)
{
int iResult = 0;
if (a < 0)
{
a *= -1;
}
if (b < 0)
{
b *= -1;
}

if (a == 0 || b == 1 || a == b)
{
iResult = b;
}
else if (a == 1 || b == 0)
{
iResult = a;
}
else if (a > b)
{
iResult = getGCD(b, a % b);
}
else
{
iResult = getGCD(a, b % a);
}
return iResult;
}```
As for where you have to use it, I have no idea. If its referring to the results of the loop, then its a matter of capturing the last two results using a shadow. Since your code is a function, I somehow doubt that its the last two within the for loop since you would have no way to return the results.

• Originally Posted by devnull69
Objection ... this code is JAVA and not Javascript ... so the section was correct but the headline isn't
My bad

Originally Posted by Fou-Lu
My bad, I should have paid attention that you have datatypes here.
The first answer is 1955. I ran this as you have here to get that, but also wrote a quick hashcode creation for it to see how you'd do it by hand if you had to.
The greatest common divisor is more complicated. Since I don't know what this refers to by the "previous two results", I have no choice but to write a function to determine this. To do this, I used recursion, although you may also loop:
Code:
```	public static int getGCD(int a, int b)
{
int iResult = 0;
if (a < 0)
{
a *= -1;
}
if (b < 0)
{
b *= -1;
}

if (a == 0 || b == 1 || a == b)
{
iResult = b;
}
else if (a == 1 || b == 0)
{
iResult = a;
}
else if (a > b)
{
iResult = getGCD(b, a % b);
}
else
{
iResult = getGCD(a, b % a);
}
return iResult;
}```
As for where you have to use it, I have no idea. If its referring to the results of the loop, then its a matter of capturing the last two results using a shadow. Since your code is a function, I somehow doubt that its the last two within the for loop since you would have no way to return the results.
Thanks mate. Both answers are 1955. I should have mentioned that the "two previous results" meant to also refer to the answer from the very first question, which was what year Albert Einstein was born. That wasn't Java related so I didn't include it.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•