Java Programming Quiz Questions
Java Programming Quiz Questions
In Java, the default values for array elements are as follows: int -> 0, String -> null, Dog -> null, char -> '\u0000', float -> 0.0f, boolean -> false. The correct answer from the options provided is B: int, Dog, char, and float .
In Java, the garbage collection mechanism identifies unreachable objects for memory cleanup. An object becomes eligible for garbage collection when there are no reachable references to it. In certain scenarios, such as within a method where objects are only referenced locally, it can be difficult to determine if and when garbage collection occurs without explicit reference analysis. Consequently, declarations beyond point nullifying are often uncertain until execution completes .
Valid constructors for the Thread class, such as 'Thread(Runnable r, String name)' and 'Thread()', facilitate creating Thread instances with specific run behavior ('Runnable r') or identification ('name'). Other variations like 'Thread(int priority)' are invalid as constructors don't set priorities directly, showing the need for additional methods like 'setPriority' .
If a Java class does not explicitly define any constructor, the compiler provides a default no-argument constructor with no operations. This implicit constructor is used during object creation unless overridden, ensuring that objects can still be instantiated even without explicit constructor definitions, which promotes flexibility but might limit precise control over object state initialization .
In Java, assertions are intended for internal invalid assumptions with an optional error message. A statement like 'assert expression;' is supported, with failures resulting in an AssertionError. Including a colon for messages provides clarity during assertion failures. In scenarios lacking direct message expressions, assertion failure simply throws 'AssertionError' without message details, impacting debugging ease and message clarity .
The expression 'Math.round(2.5 + Math.random())' evaluates the sum of 2.5 and a random number between 0.0 and 1.0, rounding the result to the nearest integer. Possible outcomes will always round to 3, as Math.random() returns values in the range [0.0, 1.0), so 2.5 + 0.0 rounds to 3, and closer to any closer integer above will still round to 3 .
Java's inheritance model allows classes like StringBuffer to inherit default implementations of equals() and hashCode() from the Object class. Without overriding, StringBuffer uses reference equality rather than content equality for equals() comparison, leading to distinct instances being unequal even if their contents are identical, impacting hashtable use and consistency .
In Java, if a specific exception is not caught by an earlier catch block due to its sequence or specificity, the program will show a compilation error if a general Exception precedes a specific ArithmeticException or similar. Compilation fails because catch blocks that catch a superclass exception precede subclasses, causing ambiguity without specific handling .
Correct array declarations in Java follow the form 'type[] arrayName', with variations allowing modifiers like 'public', 'private', 'static', etc. Faulty declarations like 'private int a[3]', implying fixed-size allocation as part of the declaration, contradict Java's dynamic array allocation principles; thus, are invalid .
Implementing a Map that maintains the same iteration order as an existing instance relies on using a LinkedHashMap, as it maintains insertion order. Alternatives like HashMap or TreeMap do not guarantee iteration order consistent with another Map, as HashMap does not maintain order and TreeMap sorts according to natural ordering or a custom comparator .