Skip to main content

--> Test Your Knowledge

 

  1. Given:
    class CardBoard {
        Short story = 200;

        CardBoard go(CardBoard cb) {
            cb = null;
            return cb;
        }

        public static void main(String[] args) {
            CardBoard c1 = new CardBoard();
            CardBoard c2 = new CardBoard();
            CardBoard c3 = c1.go(c2);
            c1 = null;
            // do Stuff
        }
    }
    When // doStuff is reached, how many objects are eligible for GC?
  2. Given:
    class Alien {
        String invade(short ships) {
            return "a few";
        }

        String invade(short... ships) {
            return "many";
        }
    }

    class Defender {
        public static void main(String[] args) {
            System.out.println(new Alien().invade(7));
        }
    }
    What is the result?
  3. Given:
    1.  class Dims {
    2.      public static void main(String[] args) {
    3.          int[][] a = { { 1, 2, }, { 3, 4 } };
    4.          int[] b = (int[]) a[1];
    5.          Object o1 = a;
    6.          int[][] a2 = (int[][]) o1;
    7.          int[] b2 = (int[]) o1;
    8.          System.out.println(b[1]);
    9.      }
    10. }
    What is the result?
  4. Given:
    class Mixer {
        Mixer() {
        }

        Mixer(Mixer m) {
            m1 = m;
        }

        Mixer m1;

        public static void main(String[] args) {
            Mixer m2 = new Mixer();
            Mixer m3 = new Mixer(m2);
            m3.go();
            Mixer m4 = m3.m1;
            m4.go();
            Mixer m5 = m2.m1;
            m5.go();
        }

        void go() {
            System.out.print("hi ");
        }
    }
    What is the result?
  5. Given:
    class Fizz {
        int x = 5;

        public static void main(String[] args) {
            final Fizz f1 = new Fizz();
            Fizz f2 = new Fizz();
            Fizz f3 = FizzSwitch(f1, f2);
            System.out.println((f1 == f3) + " " + (f1.x == f3.x));
        }

        static Fizz FizzSwitch(Fizz x, Fizz y) {
            final Fizz z = x;
            z.x = 6;
            return z;
        }
    }
    What is the result?
  6. Given:
    class Bird {
        {
            System.out.print("b1 ");
        }

        public Bird() {
            System.out.print("b2 ");
        }
    }

    class Raptor extends Bird {
        static {
            System.out.print("r1 ");
        }

        public Raptor() {
            System.out.print("r2 ");
        }

        {
            System.out.print("r3 ");
        }
        static {
            System.out.print("r4 ");
        }
    }

    class Hawk extends Raptor {
        public static void main(String[] args) {
            System.out.print("pre ");
            new Hawk();
            System.out.println("hawk ");
        }
    }
    What is the result?
  7. Given:
    1.  public class Bridge {
    2.      public enum Suits {
    3.          CLUBS(20), DIAMONDS(20), HEARTS(30), SPADES(30),
    4.          NOTRUMP(40) {
    5.              public int getValue(int bid) {
    6.                  return ((bid - 1) * 30) + 40;
    7.              }
    8.          };
     
    9.          Suits(int points) {
    10.             this.points = points;
    11.         }
     
    12.         private int points;
     
    13.        public int getValue(int bid) {
    14.             return points * bid;
    15.        }
    16.     }
     
    17.     public static void main(String[] args) {
    18.         System.out.println(Suits.NOTRUMP.getValue(3));
    19.         System.out.println(Suits.SPADES + " " + Suits.SPADES.points);
    20.         System.out.println(Suits.values());
    21.     }
    22. }
    Which are true? (Choose all that apply)
  8. Given:
    public class Ouch {
        static int ouch = 7;

        public static void main(String[] args) {
            new Ouch().go(ouch);
            System.out.print(" " + ouch);
        }

        void go(int ouch) {
            ouch++;
            for (int ouch = 3; ouch < 6; ouch++)
                ;
            System.out.print(" " + ouch);
        }
    }
    What is the result?
  9. Given:
    public class Bertha {
        static String s = "";

        public static void main(String[] args) {
            int x = 4;
            Boolean y = true;
            short[] sa = { 1, 2, 3 };
            doStuff(x, y);
            doStuff(x);
            doStuff(sa, sa);
            System.out.println(s);
        }

        static void doStuff(Object o) {
            s += "1";
        }

        static void doStuff(Object... o) {
            s += "2";
        }

        static void doStuff(Integer... i) {
            s += "3";
        }

        static void doStuff(Long L) {
            s += "4";
        }
    }
    What is the result?
  10. Given:
    1.  class Dozens {
    2.      int[] dz = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    3.  }
     
    4.  public class Eggs {
    5.      public static void main(String[] args) {
    6.          Dozens[] da = new Dozens[3];
    7.          da[0] = new Dozens();
    8.          Dozens d = new Dozens();
    9.          da[1] = d;
    10.         d = null;
    11.         da[1] = null;
    12.         // do stuff
    13.     }
    14. }
    Which two are true about the objects created within main(), and eligible for garbage collection when line 12 is reached?
  11. Given:
    1.  class Beta {
    2.  }
     
    3.  class Alpha {
    4.      static Beta b1;
    5.      Beta b2;
    6.  }
     
    7.  public class Tester {
    8.      public static void main(String[] args) {
    9.          Beta b1 = new Beta();
    10.         Beta b2 = new Beta();
    11.         Alpha a1 = new Alpha();
    12.         Alpha a2 = new Alpha();
    13.         a1.b1 = b1;
    14.         a1.b2 = b1;
    15.         a2.b2 = b2;
    16.         a1 = null;
    17.         b1 = null;
    18.         b2 = null;
    19.         // do stuff
    20.     }
    21. }
    When line 19 is reached, how many objects will be eligible for garbage collection?
  12. Given:
    class Box {
        int size;

        Box(int s) {
            size = s;
        }
    }

    public class Laser {
        public static void main(String[] args) {
            Box b1 = new Box(5);
            Box[] ba = go(b1, new Box(6));
            ba[0] = b1;
            for (Box b : ba)
                System.out.print(b.size + " ");
        }

        static Box[] go(Box b1, Box b2) {
            b1.size = 4;
            Box[] ma = { b2, b1 };
            return ma;
        }
    }
    What is the result?
  13. Given:
    public class Dark {
        int x = 3;

        public static void main(String[] args) {
            new Dark().go1();
        }

        void go1() {
            int x;
            go2(++x);
        }

        void go2(int y) {
            int x = ++y;
            System.out.println(x);
        }
    }
    What is the result?

Prev Next

Comments