Java Interview Questions - Keywords

01 Nov 2011 | By Mihai Roman | Tags java keywords

Complete list of Java keywords: abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, enum, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, transient, try, void, volatile, while.

Reserved words for literal use: true, null, false.

  1. Is main/next/delete/exit/null/sizeof/virtual a keyword in Java?
    None of these are Java keywords.
  2. Final, finally, finalize in Java?*
    Final is a modifier that can be applied to classes, methods, fields, variables and parameters. Classes cannot be extended, methods cannot be overridden, fields cannot be assigned multiple times and must be instantiated in the constructors, variables can only be assigned once and parameters cannot be assigned. Finalize is the method called just before the object is garbage collected. Finally indicated the code block to be executed in connection with a try/catch block. The finally code is executed even if the function returns a value in try/catch and can change the return value.
  3. What are the final fields and final methods?
    See above.
  4. What is static in Java?
    The static keyword is used to declare fields, methods or inner classes. Objects hold a single copy of the fields no matter how many instance are there. Static member classes are actually top-level classes and not inner classes.
  5. What if I write static public void instead of public static void?
    Program compiles, no error, the same thing.
  6. How is the transient keyword used?
    Transient modifier applies to members that are not stored during serialization.
  7. How is volatile keyword used?
    Volatile fields can be modified asynchronously by multiple threads.
  8. How are goto and const keywords used in Java?
    Although these are actually keywords, they are not used and don’t serve any purpose.

blog comments powered by Disqus