Java Quiz: Introducing Mathematical Tricks
Java Quiz: Introducing Mathematical Tricks
The latest advanced Java quiz from DZone's resident quizmaster!
Join the DZone community and get the full member experience.
Join For Free"I love writing authentication and authorization code." ~ No Developer Ever. Try Okta Instead.
Last Week's Answer
First Method: mc.method(3)
int x = 0;
if(i > 2)
//3 > 2 is true;
i = (1 / 0) + Integer.parseInt("x");
//The first part of this equation is dividing by 0.
//That is why the exception is handled by the “ArithmeticException” block.
x += 4; x = 4;
//Finally and the last statement are always executed:
x = 4+2+1=7;
Second Method: mc.method(2)
//2 > 2 is false
i = Integer.parseInt("x") + (1 / 0);
//The first part of the exception would be handled in the “RuntimeException” block not in the “ArithmeticException” block. See the RuntimeException hirarchy Java library.
X = 7+3 = 10
//finally and the last statement are always executed, that is why the end result is:
10 + 2 + 1 = 13
This Week's Quiz
Purpose
Demonstrating a trick that might distract programmers.
Helps to stay focused by programming.
What is written to the standard output as a result of executing the following code?
public class MyClass {
int x;
int[] arrayInt = {4,3,0 };
int methodA(int i, int i2){
int i3 = i + i2 - 1;
return(i3 - (2 * i) / x - x / 2);
}
int methodB(){
for(int i:arrayInt) {
switch(i) {
case 3:
x ++ ;
case 5:
x += 2;
break;
default:
x += 3;
case 0:
x += 4;
case 1:
x += 5;
}
}
return x / 2;
}
public static void main(String[] args){
MyClass mc = new MyClass();
System.out.println("result: " + mc.methodA(mc.methodB(),2));
}
}
Let us know your thoughts in the comments, and check out Sar's site here!
"I love writing authentication and authorization code." ~ No Developer Ever. Try Okta Instead.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}