Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

# Java Quiz: Introducing Mathematical Tricks

DZone 's Guide to

# Java Quiz: Introducing Mathematical Tricks

### The latest advanced Java quiz from DZone's resident quizmaster!

· Java Zone ·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

### 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

1. Demonstrating a trick that might distract programmers.

2. 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!

Topics:
java

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Opinions expressed by DZone contributors are their own.