Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Java Tutor

Java Tutor is my platform for teaching about Java 7+ and JavaFX 2.0+, mainly via programming projects.


Year End Java Quiz

 

How well do you know the Java language? Now that 2012 is drawing to a close and a new year is about to dawn, perhaps you might resolve to learn more about Java. A perfect way to gain this knowledge is to take a quiz and rate your understanding.

The following quiz challenges you on many Java language features (and a few language-support APIs), and is organized into true/false and multiple choice questions. At the end of the quiz, I present a link to an archive that contains the answers. Good luck!

  1. True or false: Multiline comments (comments beginning with /* and ending with */) can be nested (i.e., declared within other multiline comments).
  2. Which one of the following is not a valid Javadoc tag?

    a) @author
    b) {@link}
    c) @serialField
    d) @arg

  3. True or false: The javadoc tool defaults to generating HTML-based documentation for public classes and public/protected members of these classes.
  4. Which one of the following statements about an identifier is incorrect?

    a) An identifier can include letters and the first character can be a letter.
    b) An identifier can include digits, but the first character cannot be a digit.
    c) An identifier's length can exceed the length of the current line.
    d) The symbol for PI (3.14159...) is a valid identifier.

  5. True or false: Java is a case-sensitive language (e.g., age and Age are distinct identifiers).
  6. Which one of the following identifiers is not a keyword?

    a) null
    b) transient
    c) assert
    d) const

  7. True or false: Java is not a strongly typed language (in which every expression, variable, and so on has a type).
  8. Which one of the following primitive types is intended for use with Unicode?

    a) Double precision floating-point (double)
    b) Character (char)
    c) Byte integer (byte)
    d) Boolean (boolean)

  9. True or false: An integer type represents equal numbers of negative and positive values in the range denoted by the type.
  10. Which one of the following type-oriented statements is incorrect?

    a) User-defined types are also known as reference types.
    b) User-defined types are only defined via classes and interfaces.
    c) An array type is signified by an identifier and one or more pairs of square brackets.
    d) The declaration int[] x; introduces an array type.

  11. True or false: When declaring an array variable, you can specify the square brackets ([]) after the type (e.g., int[] x;) or after the variable name (e.g., int x[];).
  12. Which one of the following syntactic constructs is not a simple expression?

    a) null
    b) 43
    c) x+2
    d) print()

  13. True or false: A string literal can be surrounded by a pair of single quotes (e.g., 'abc') or by a pair of double quotes (e.g., "abc").
  14. Which one of the following escape sequences cannot appear in a string literal?

    a) \f
    b) \u
    c) \"
    d) \\

  15. True or false: Java 7 lets you place underscores before, in the middle of, and after an integer literal's digits.
  16. Which one of the following is not a widening conversion rule?

    a) Short integer to character
    b) Integer to long integer
    c) Floating-point to double precision floating-point
    d) Character to floating-point

  17. True or false: Java copies a smaller integer's sign bit into a larger integer's extra bits when widening a shorter integer to a longer integer.
  18. The ?: operator is an example of which kind of operator?

    a) Binary
    b) Prefix
    c) Postfix
    d) Infix

  19. True or false: All infix operators are also binary operators.
  20. Which one of the following operators offers the fastest way to divide a negative integer by 2 and keep the negative result (e.g., -4/2 equals -2)?

    a) /
    b) %
    c) >>
    d) >>>

  21. True or false: The compiler reports a possible loss of precision error when it encounters byte b = 127;, because 127 is of 32-bit integer type.
  22. Which one of the following operators is right-associative?

    a) *
    b) /
    c) =
    d) &

  23. True or false: The instanceof operator has higher precedence than the conditional operator (?:).
  24. Which one of the following statements about statements is false?

    a) A statement is either simple or compound.
    b) Every statement must end with a semicolon.
    c) A compound statement is also known as a block.
    d) A block begins with an open brace ({) and ends with a close brace (}).

  25. True or false: There are two forms of the assignment statement (involving the = operator).
  26. Which one of the following decision statements is a synonym for the conditional operator (?:)?

    a) if statement
    b) if-else statement
    c) switch statement
    d) none of the above

  27. True or false: The dangling-else problem involves use of the if and if-else statements along with poor indentation.
  28. Which one of the following primitive/reference types cannot be used in a switch statement context?

    a) 32-bit integer
    b) string literal
    c) Boolean value
    d) enum constant

  29. True or false: The default keyword can be used only in a switch statement context.
  30. Which one of the following for loop statement components is compulsory?

    a) initialize section
    b) test section
    c) update section
    d) statement being executed

  31. True or false: The while statement tests its Boolean expression at the bottom of the loop and the do-while statement tests its Boolean expression at the top of the loop.
  32. Which one of the following statements lets you exit nested loops and still continue to execute one of these loops?

    a) break
    b) labeled break
    c) continue
    d) labeled continue

  33. True or false: Classes are templates for manufacturing objects.
  34. What kind of programming language is Java?

    a) Object-based programming language
    b) Object-oriented programming language
    c) Structured programming language
    d) None of the above

  35. True or false: A constructor initializes an object and can have a return type.
  36. Which one of the following statements is incorrect?

    a) Objects are stored in the heap.
    b) The new operator invokes a constructor to initialize the object.
    c) The new operator returns the address of a newly created object.
    d) Instance fields not explicitly initialized are initialized to default values.

  37. True or false: A parameter is an expression value passed to a constructor method.
  38. A local variable has which one of the following properties?

    a) scope
    b) scope and lifetime
    c) lifetime
    d) scope, lifetime, and arity

  39. True or false: The compiler reports an error when it attempts to compile the following array creation and initialization expression: new String[4] { "North", "South", "East", "West" }.
  40. Which one of the following statements is incorrect?

    a) Encapsulation is equivalent to information hiding.
    b) Class fields store class state.
    c) Instance fields store object state.
    d) The new operator initializes instance fields to default values (e.g., false for boolean fields).

  41. True or false: Each instance has its own copy of a class field.
  42. Which one of the following statements is incorrect?

    a) To access a class field from inside its class, specify the class field's name only (or prefix this name with the class name and member access operator [.] when a local variable or parameter shadows this name).
    b) To access a class field from outside its class, you must specify the name of the class followed by the member access operator, followed by the class field name.
    c) To access an instance field from inside any instance method or constructor, just specify the instance field's name (with a this. prefix when the name of a parameter is the same as the name of the instance field).
    d) You cannot access an instance field from a class method that's located in the same class as the instance field.

  43. True or false: A blank final is a single read-only value that's shared by all class instances.
  44. Which one of the following statements is incorrect?

    a) Instance methods are passed a this reference as a hidden argument.
    b) Class methods cannot access instance fields.
    c) A method's name, parameter types, and return type constitute its signature.
    d) Void methods cannot return values.

  45. True or false: Method calls can be chained together provided that the methods are declared with the name of their class as the return type and that they return this.
  46. Which one of the following statements is incorrect?

    a) Java uses pass-by-value to pass primitive type-based arguments and pass-by-reference to pass reference type-based arguments when calling a method.
    b) The compiler always reports an error when it encounters code to execute after an unconditional return statement.
    c) A recursive method must end in a base problem to prevent stack space from being exhausted.
    d) Same-named methods with different parameter lists are said to be overloaded.

  47. True or false: Methods can be overloaded by changing only their return types.
  48. Which one of the following statements is incorrect?

    a) To invoke a class method from inside its class, just specify the class method's name and argument list.
    b) To invoke a class method from outside its class, you must specify the name of the class followed by the member access operator (.), followed by the class method name and an argument list.
    c) To call an instance method from inside any instance method or constructor, just specify the instance method's name and an argument list.
    d) You cannot invoke an instance method from a class method that's located in the same class as the instance method.

  49. True or false: Exposing a class's interface to the outside world while hiding its implementation is known as encapsulation.
  50. Which one of the following access control levels is not associated with a reserved word?

    a) public access
    b) private access
    c) package access
    d) protected access

  51. True or false: Only one top-level public class can be declared in a source file.
  52. Which one of the following statements is incorrect?

    a) A class initializer's bytecode is stored in a <clinit>() method.
    b) An instance initializer's bytecode is stored in an <init>() method.
    c) Field Initializers can refer to subsequently declared field initializers.
    d) Constructors correspond to <init>() methods

  53. True or false: Java supports two forms of multiple inheritance in an interface context.
  54. Which one of the following statements is incorrect?

    a) The compiler reports an error when a superclass doesn't declare a constructor and a subclass constructor executes super();.
    b) You cannot place any code before a super() call to the superclass constructor.
    c) A super() call can only appear in a constructor.
    d) You can use the super keyword to invoke superclass methods.

  55. True or false: You can prevent a class from being extended or a method from being overridden by declaring that class or method final.
  56. Which one of the following statements is incorrect?

    a) A subclass constructor always calls its superclass constructor as its first task.
    b) The ultimate superclass is named Class.
    c) The superclass layer always initializes before the subclass layer.
    d) You cannot combine a super() call with a this() call in a constructor.

  57. True or false: The default equals() method returns true when two objects are logically the same.
  58. Which one of the following statements is incorrect?

    a) A class must implement the java.lang.Cloneable interface so that its instances can be shallowly cloned.
    b) The clone() method throws java.lang.CloneNotSupportedException when a class doesn't implement Cloneable.
    c) The class containing an overriding clone() method must implement Cloneable.
    d) Deep cloning involves cloning object fields.

  59. True or false: You don't have to use equals() to compare strings in certain circumstances.
  60. What property is demonstrated by x.equals(x) returning true?

    a) reflexivity
    b) symmetry
    c) transitivity
    d) consistency

  61. True or false: A resurrected object's finalizer can be called again.
  62. A field name that can associate with different types is an example of what kind of polymorphism?

    a) coercion
    b) overloading
    c) subtype
    d) parametric

  63. True or false: Covariance involves a type with a wider range of values being converted to a type with a narrower range of values.
  64. The compiler performs late binding on which kind of method?

    a) non-final instance method
    b) final instance method
    c) non-final class method
    d) final class method

  65. True or false: A class containing an abstract method must be declared abstract.
  66. Which one of the following forms of runtime type identification is associated with contravariance?

    a) cast operator
    b) instanceof operator
    c) reflection
    d) none of the above

  67. True or false: An interface's fields are implicitly declared public and static.
  68. Which kind of nested class doesn't have a name?

    a) static member class
    b) nonstatic member class
    c) anonymous class
    d) local class

  69. True or false: When the compiler compiles a class that contains a static member class, the compiler generates a classfile whose name consists of the enclosing class name followed by an underscore (_) followed by the name of the static member class.
  70. Which one of the following statements is false?

    a) Local classes have the same scope as local variables and parameters.
    b) You cannot declare a class within an interface.
    c) Anonymous classes cannot have constructors, but can have instance initializers.
    d) Instantiating a nonstatic member class can involve prefixing new with an instance of the enclosing class.

  71. True or false: Only top-level public types can be accessed from outside of their packages.
  72. Which one of the following statements is false?

    a) Only one package statement can appear in a source file.
    b) The package statement must appear before any types are declared.
    c) A source file's types belong to the unnamed package when there is no package statement.
    d) Reserved words can appear in package names (e.g., package y.class.z;).

  73. True or false: Use of the wildcard (*) in import statements can lead to name conflicts.
  74. Which one of the following statements is false?

    a) Multiple import statements can appear in a source file.
    b) An import statement must appear before any types are declared.
    c) The compiler automatically imports types from the java.lang package.
    d) You cannot import the same type multiple times.

  75. True or false: When looking for a type, the compiler first searches Java platform packages (e.g., the packages stored in rt.jar), then searches extension packages, and finally searches the user classpath.
  76. Which one of the following statements is false?

    a) A JAR file's manifest is stored in a MANIFEST.MF file in the JAR file's MANIFEST directory.
    b) A package is sealed when all classes belonging to a package are archived in the same JAR file.
    c) The java.lang.Package class's getName() method returns the name of a package in standard dot notation.
    d) In order for Package's isSealed() method to return true, you must specify Sealed: true in the manifest and, if this is the final line, press Return/Enter to insert a blank line after this line.

  77. True or false: The following command line creates a JAR file from a game package and inserts the contents of the specified manifest file into the JAR file's MANIFEST.MF file: jar cf game.jar manifest game/*.class.
  78. Which one of the following statements is false?

    a) You should avoid using constant interfaces (i.e., interfaces that declare only public static final fields) because they are an implementation detail that leaks into a class's exported interface.
    b) You can import static member names from a class via the import static statement.
    c) You cannot import multiple static members from a class in a single import static statement.
    d) To import MAX_VALUE from java.lang.Integer, you would specify import static java.lang.Integer.MAX_VALUE;.

  79. True or false: Exceptions can be represented by error codes or objects.
  80. Which one of the following statements is false?

    a) The hierarchy of exception classes is ultimately rooted in the java.lang.Exception class.
    b) A cause is a wrapped exception.
    c) The try-with-resources statement suppresses exceptions originating from a finally block when an exception has been thrown from the try block.
    d) An exception is either checked or unchecked.

  81. True or false: All exception class names must have an Exception suffix.
  82. Which one of the following statements is false?

    a) An object is not created when an exception is thrown from a constructor.
    b) The virtual machine terminates an application and calls an exception's printStackTrace() method when an application's main() method throws an exception that isn't handled.
    c) A checked exception class name must always appear in a throws clause.
    d) It is an error for a method to throw a checked exception and not handle that exception or declare it in a method's throws clause.

  83. True or false: You shouldn't handle unchecked exceptions because they represent coding mistakes that should be fixed.
  84. Which one of the following statements is false?

    a) When specified, a catch block must always follow a try block or another catch block.
    b) When multiple catch blocks are specified, they can appear in any order.
    c) You cannot specify multiple catch blocks with the same type parameter (e.g., catch (IOException ioe) {} catch (IOException ioe) {}).
    d) You can specify multiple exception types in a catch block provided that you don't specify types where one type is a supertype of another type, and provided that you use a vertical bar to separate adjacent types.

  85. True or false: You cannot throw an exception from a catch block.
  86. Which one of the following statements is false?

    a) Final rethrow involves throwing the actual type of a caught exception instead of throwing the catch block parameter type.
    b) A finally block follows either a catch block or a try block.
    c) The try-with-resources statement automatically closes resources when execution leaves the try block normally or through a thrown exception.
    d) You cannot specify a catch block but can specify a finally block after the try block of a try-with-resources statement.

  87. True or false: Implementations of java.lang.AutoCloseable's close() method are not required to be idempotent (i.e., calling close() more than once may have some visible side effect).
  88. Which one of the following statements is false?

    a) An assertion is a statement for expressing an assumption of program correctness.
    b) An internal invariant is expression-oriented behavior that shouldn't change.
    c) Design-by-Contract is based on preconditions and postconditions.
    d) To enable assertions in all classes except for system classes, specify -ea with no argument.

  89. True or false: The transient reserved word is an example of an ad hoc annotation mechanism.
  90. Which one of the following annotation types is located in the java.lang.annotation package?

    a) Documented
    b) Deprecated
    c) SafeVarargs
    d) SuppressWarnings

  91. True or false: The Target annotation type is an example of a meta-annotation type for identifying the kinds of program elements (e.g., classes and methods) to which an annotation type applies.
  92. Which one of the following Retention annotation type members causes the compiler to discard annotations after using them?

    a) CLASS
    b) SOURCE
    c) RUNTIME
    d) none of the above

  93. True or false: Generic types are classes that introduce parameterized type families via formal type parameter lists.
  94. Which one of the following kinds of actual type arguments indicates an unknown actual type argument?

    a) concrete type
    b) wildcard
    c) type parameter
    d) concrete parameterized type

  95. True or false: List<?> is an example of a raw type.
  96. Set<E>'s E type parameter is an example of what?

    a) an unbounded type parameter
    b) a type parameter with an upper bound
    c) a type parameter with a lower bound
    d) a recursive type bound

  97. True or false: A type parameter's scope is the entire generic type.
  98. Which one of the following statements is false?

    a) A generic method is a class or instance method with a type-generalized implementation.
    b) Java arrays are reified.
    c) An enumerated type is either a named sequence of integer/string constants or a type expressed via reserved word enum.
    d) The switch statement doesn't support enum constants.

  99. True or false: The java.lang.Enum class's toString() method defaults to returning the same value as this class's name() method.
  100. Which one of the following ways of obtaining a java.lang.Class object can lead to a runtime failure?

    a) Class's forName() method
    b) a class literal
    c) java.lang.Object's getClass() method
    d) none of the above

  101. True or false: By default, java.lang.String objects that are represented as literal strings (e.g., "XYZ") or as string-valued constant expressions (e.g., "X"+"YZ") are interned in an internal table of String objects.
  102. Which one of the following java.lang.Thread methods clears the interrupt status flag?

    a) interrupt()
    b) isInterrupted()
    c) interrupted()
    d) sleep()

  103. True or false: A thread that's holding a lock releases the lock when it calls any one of Thread's sleep() methods.
  104. Which one of the following statements is false?

    a) Variables of type long or double are subject to data corruption when written to in a multithreaded context on 32-bit virtual machines.
    b) The synchronized keyword forces a thread to acquire a lock before entering a critical section.
    c) The volatile keyword causes a thread to access a shared copy of a field and not a cached copy when the thread executes on a multiprocessor/multicore machine.
    d) You can invoke Object's wait() methods from anywhere in your program.

  105. True or false: The enhanced for statement can be used in an array context.
  106. Which one of the following statements is false?

    a) Autoboxing wraps a primitive type value in an object of the appropriate primitive type wrapper class (e.g., Integer).
    b) The Integer i1 = 40000; Integer i2 = 40000; System.out.println(i1 == i2); code sequence outputs true, whereas the Integer i1 = 40; Integer i2 = 40; System.out.println(i1 == i2); code sequence outputs false.
    c) Unboxing unwraps a primitive type value from its wrapper object.
    d) The Integer i1 = 10; Integer i2 = 20; System.out.println(i1+i2); code sequence outputs 30.

  107. True or false: Fields that are marked transient are not serialized.
  108. Which one of the following statements is false?

    a) A native method is declared with the native keyword.
    b) An instance of the java.lang.UnsatisfiedLinkError class is thrown when a native library doesn't exist.
    c) The java.lang.System class's void loadLibrary(String libname) method loads the system library specified by the libname argument, which must be a complete pathname.
    d) When using loadLibrary(), you must not specify a lib prefix or a .dll or .so extension.

  109. True or false: Java 8 will support lambda expressions.
  110. Which one of the following additional new language features isn't expected to be included in Java 8?

    a) collection literals (e.g., words["red"] = 1; instead of words.put("red", 1);)
    b) extended set of annotatable locations
    c) repeating annotations
    d) generalized target-type inference

Code

You can download this post's code and answers here. Code was developed and tested with JDK 7u6 on a Windows 7 platform.

* * *

I welcome your input to this blog, and will write about relevant topics that you suggest. While waiting for the next blog post, check out my TutorTutor website to learn more about Java and other computer technologies (and that's just the beginning).

Learn more about the Java 7 language and many of its APIs by reading my book Beginning Java 7. You can obtain information about this book here and here.