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

Find your next Integration job at DZone Jobs. See jobs focused on integration, or create your profile and have the employers come to you!

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

  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!

Find your next Integration job at DZone Jobs. See jobs focused on integration, or create your profile and have the employers come to you!

Topics:
java

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}