Classes/Objects  «Prev  Next»


Java Logical Operators - Exercise

Using operators

Objective:Test your familiarity with the operators provided by Java.

Instructions

The following program displays 10 lines of output. Analyze the program and identify each line of output (in order).

public class Operators {
  static int i = 0;
 
  public static boolean sideEffect(boolean b) {
    ++i; 
    return b; 
  }

  public static void main(String[] args) {
    System.out.println(i++);
    System.out.println(++i);
    System.out.println(i--);
    i = ~i >>> 30;
    System.out.println(i);
    System.out.println(i & 7);
    System.out.println(i | 7);
    System.out.println(i ^ 7);
    if (sideEffect(true) || sideEffect(false))
      i *= 2;
    System.out.println(i);
    if (sideEffect(true) && sideEffect(false))
      i %= 3;
    System.out.println(i);
    i = sideEffect(i % 2 == 0) ? i << 2 : --i;
    System.out.println(i);   }  }

Scoring


This exercise is scored automatically and is worth 10 points. When you are finished, click Submit and you will receive full credit for the exercise.