Skip to main content

Java Day 3 (Enum)

 

Java Day 3

  1. Choose the correct output among the following options.
  2. class Frode extends Hobbit {
        public static void main(String[] args) {
            Short myGold = 7;
            System.out.println(countGold(myGold, 6));
        }
    }

    class Hobbit {
        int countGold(int x, int y) {
            return x + y;
        }
    }
    1. 13
    2. Compilation fails due to multiple errors.
    3. Compilation fails due to an error on line 7.
    4. Compilation fails due to an error on line 4.
    5. Compilation fails due to an error on line 9.
  3. Can a static variable be transient?
  4. Choose the correct output among the following options (Choose all that apply).
  5. class ShellClass {
        public static void main(String[] args) {
            for(int __x = 0; __x < 3; __x++);
            int #lb = 7;
            long []x[5];
            Boolean []ba[];
            enum Traffic{RED, YELLOW, GREEN};
        }
    }
    1. Compilation succeeds
    2. Compilation fails due to an error on line 3.
    3. Compilation fails due to errors on line 4.
    4. Compilation fails due to an error on line 5.
    5. Compilation fails due to an error on line 6.
    6. Compilation fails due to an error on line 7.

Enum

  • An enum class is a class declared with abbreviation syntax that defines a small set of named class instances.
  • Enum actually internally converted to a java final class.
class ShellClass {
    public static void main(String[] args) {
        int enum = 10;
    }
}
enum Color {
    RED, GREEN, BLACK;
}
enum Color {
    RED, GREEN, BLUE
}

class ShellClass {
    public static void main(String[] args) {
        Color BLACK = new Color();
    }
}

Note: Shell Class is a class that contains the main() method.

Prev Next

Comments