Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Java Tutor is my platform for teaching about Java 7+ and JavaFX 2.0+, mainly via programming projects.
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!
/* and ending with */) can be nested (i.e., declared within other multiline comments).
a) @author
b) {@link}
c) @serialField
d) @arg
javadoc tool defaults to generating HTML-based documentation for public classes and public/protected members of these classes.
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.
age and Age are distinct
identifiers).
a) null
b) transient
c) assert
d) const
a) Double precision floating-point (double)
b) Character (char)
c) Byte integer (byte)
d) Boolean (boolean)
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.
[]) after the type (e.g., int[] x;) or after the variable name (e.g., int x[];).
a) null
b) 43
c) x+2
d) print()
'abc') or by a pair of double quotes (e.g., "abc").
a) \f
b) \u
c) \"
d) \\
a) Short integer to character
b) Integer to long integer
c) Floating-point to double precision floating-point
d) Character to floating-point
?: operator is an example of which kind of operator?
a) Binary
b) Prefix
c) Postfix
d) Infix
-4/2 equals -2)?
a) /
b) %
c) >>
d) >>>
byte b = 127;, because 127 is of 32-bit integer type.
a) *
b) /
c) =
d) &
instanceof operator has higher precedence than the conditional operator (?:).
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 (}).
= operator).
?:)?
a) if statement
b) if-else statement
c) switch statement
d) none of the above
a) 32-bit integer
b) string literal
c) Boolean value
d) enum constant
a) initialize section
b) test section
c) update section
d) statement being executed
a) break
b) labeled break
c) continue
d) labeled continue
a) Object-based programming language
b) Object-oriented programming language
c) Structured programming language
d) None of the above
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.
a) scope
b) scope and lifetime
c) lifetime
d) scope, lifetime, and arity
new String[4] { "North", "South", "East", "West" }.
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).
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.
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.
this.
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.
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.
a) public access
b) private access
c) package access
d) protected access
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
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.
final.
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.
equals() method returns true when two objects are logically the same.
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.
equals() to compare strings in certain circumstances.
x.equals(x) returning true?
a) reflexivity
b) symmetry
c) transitivity
d) consistency
a) coercion
b) overloading
c) subtype
d) parametric
a) non-final instance method
b) final instance method
c) non-final class method
d) final class method
abstract.
a) cast operator
b) instanceof operator
c) reflection
d) none of the above
public and static.
a) static member class
b) nonstatic member class
c) anonymous class
d) local class
_) followed by the name of the static member class.
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.
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;).
*) in import statements can lead to name conflicts.
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.
rt.jar), then searches extension packages, and finally searches the user classpath.
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.
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.
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;.
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.
Exception suffix.
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.
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.
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.
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).
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.
transient reserved word is an example of an ad hoc annotation mechanism.
java.lang.annotation package?
a) Documented
b) Deprecated
c) SafeVarargs
d) SuppressWarnings
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.
Retention annotation type members causes the compiler to discard annotations after using them?
a) CLASS
b) SOURCE
c) RUNTIME
d) none of the above
a) concrete type
b) wildcard
c) type parameter
d) concrete parameterized type
List<?> is an example of a raw type.
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
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.
java.lang.Enum class's toString() method defaults to returning the same value as this class's name() method.
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
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.
java.lang.Thread methods clears the interrupt status flag?
a) interrupt()
b) isInterrupted()
c) interrupted()
d) sleep()
Thread's sleep() methods.
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.
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.
transient are not serialized.
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.
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
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. |