What will be printed out as a result of the following code execution / compilation?

public class Test {
    public static void main(String[] args) {
        ElectricInverter inverter = new ElectricInverter();
        int AC = 220;
        System.out.println(inverter.invert(AC));
    }
}

class ElectricInverter {    
    public static final int AC = ~220;
    public static final int DC = -110;
    public static final int GROUND = 0;
    
    int invert(int type) {
        if (type == AC) {
            return DC;
        }
        else if (type == DC) {
            return AC;
        }
        return GROUND;
    }
}
Explanation
The ~ operator performs a bitwise inversion of integers - it changes all the bits in the binary representation of the number to the opposite ones, therefore 220 != ~ 220.
Since all the comparisons in the invert () method will be false, it will return a GROUND = 0 constant.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes