Skip to main content

Java Day 1

 

DAY 1

  1. Choose the correct output among the following options.
  2. class Example {
        static String s = "-";

        public static void main(String[] args) {
            new Example().s1();
            System.out.println(s);
        }

        void s1() {
            try {
                s2();
            } catch (Exception e) {
                s += "c";
            }
        }

        void s2() throws Exception {
            s3();
            s += "2";
            s3();
            s += "2b";
        }

        void s3() throws Exception {
            throw new Exception();
        }
    }
    1. -
    2. -c
    3. -c2
    4. -c22b
    5. -2c2b
    6. -2c2bc
    7. Compilation Error
  3. Which would be used to create an appropriate catch block? (Choose all that apply)
  4. try{int x = Integer.parseInt("two");}
    1. ClassCastException
    2. IllegalStateException
    3. NumberFormatException
    4. ArrayIndexOutOfBoundException
    5. ExceptionInInitializerException
    6. IllegalArgumentException
  5. Which of the following can be written in place of INSERT CODE HERE? (Choose all that apply)
  6. class Example {
        public static void main(String[] args) {
            int[] x = { 7, 6, 5, 4, 3, 2, 1 };
            // INSERT CODE HERE
                System.out.print(y + " ");
            }
        }
    }
    1. for(int y : x) {
    2. for(x : int y) {
    3. int y = 0; for(y : x) {
    4. for(int y = 0, z = 0; z < x.length; z++) {y = x[z]
    5. for(int y = 0, int z = 0; z < x.length; z++) {y = x[z]
    6. int y = 0; for(int z = 0; z < x.length; z++) {y = x[z]
  7. Choose the correct output among the following options.
  8. class Example {
        static int x = 7;

        public static void main(String[] args) {
            String s = "";
            for (int y = 0; y < 3; y++) {
                x++;
                switch (x) {
                    case 8:
                        s += "8 ";
                    case 9:
                        s += "9 ";
                    case 10: {
                        s += "10 ";
                        break;
                    }
                    default:
                        s += "d ";
                    case 13:
                        s += "13 ";
                }
            }
            System.out.println(s);
        }

        static {
            x++;
        }
    }
    1. 9 10 d
    2. 8 9 10 d
    3. 9 10 10 d
    4. 9 10 10 d 13
    5. 8 9 10 10 d 13
    6. 8 9 10 9 10 10 d 13
    7. Compilation Error
  9. Choose the correct output among the following options.
  10. class Example {
        static String s = "-";

        public static void main(String[] args) {
            try {
                throw new Exception();
            } catch (Exception e) {
                try {
                    try {
                        throw new Exception();
                    } catch (Exception ex) {
                        s += "ic ";
                    }
                    throw new Exception();
                } catch (Exception x) {
                    s += "mc ";
                } finally {
                    s += "mf ";
                }
            } finally {
                s += "of ";
            }
            System.out.println(s);
        }
    }
    1. -ic of
    2. -mf of
    3. -mc mf
    4. -ic mf of
    5. -ic mc mf of
    6. -ic mc of mf
    7. Compilation Error
Prev Next

Comments

Post a Comment