These ten questions update a classic Java quiz for Java SE 25, including modern entry points, interface methods, primitive sizes, and string indexing.
A. public static int main(char args[])
B. public static void main(String args[])
C. public static void MAIN(String args[])
D. public static void main(String args)
E. public static void main(char args[])
Answer: B is the only valid entry point among these choices. Java is case-sensitive, and the classic signature returns void and accepts String[]. Writing String args[] or String[] args declares the same parameter type.
However, B is no longer the only possible form of main in the language. Java SE 25 also supports eligible instance main methods and no-argument main methods, without preview flags. For example:
public class ModernMain {
void main() {
System.out.println("Java 25");
}
}
Compile with javac ModernMain.java and run with java ModernMain using JDK 25. The launcher creates an instance through the implicit no-argument constructor and invokes main. A private main is not eligible; the familiar public static signature remains useful when compatibility with older Java releases matters.
public class Xyz {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
for (int j = 2; j >= 0; j--) {
if (i == j) break;
System.out.println("i=" + i + " j=" + j);
}
}
}
}
Answer: The three output lines are:
i=0 j=2
i=0 j=1
i=1 j=2
An unlabeled break exits only the innermost loop. When i is zero, j reaches zero and triggers break before that pair is printed. The outer loop then advances i to one; j starts again at two, prints once, and triggers break at one. Braces and indentation help expose which statement belongs to the if, but it is the Java syntax that controls execution.
public class Test {
public static void main(String[] args) {
Integer intObj = Integer.valueOf(args[args.length - 1]);
int i = intObj.intValue();
if (args.length > 1)
System.out.println(i);
if (args.length > 0)
System.out.println(i - 1);
else
System.out.println(i - 2);
}
}
Answer: It prints 1. The application receives one argument, the string "2"; the class name Test is not part of args. The first if is false, and the second is true, so only i minus one is printed. The else belongs to the second if.
This example assumes valid input. With no arguments it throws ArrayIndexOutOfBoundsException before reaching either if. An unparseable last argument throws NumberFormatException. Production code should validate the argument count before indexing and handle malformed input. Integer.parseInt would avoid the unnecessary explicit wrapper conversion here.
Answer: Use arr.length, with no parentheses. An array has a final length field established when it is created. The length describes the number of element slots, including slots containing null, rather than a count of populated values.
For a multidimensional array, arr.length gives the outer array's length; individual inner arrays may have different lengths or be null. Access through a null arr reference throws NullPointerException. Contrast array length with text.length() for a String and collection.size() for a Collection.
Answer: Write 0x5 or 0X5. If up to four characters are allowed, 0x05 is also valid. The prefix identifies hexadecimal notation in Java source; the value is the decimal number five.
Literal notation does not attach a permanent display format to an integer. Printing the value with println normally produces decimal 5. Use Integer.toHexString or a hexadecimal formatting conversion when hexadecimal output is required. The numeral five uses the same digit in decimal and hexadecimal, but the Java source prefix still distinguishes the notation.
Answer: The correct choices are b, c, f, and g: synchronized, volatile, transient, and this. The other listed words are not Java keywords.
Synchronized establishes monitor-based mutual exclusion and memory visibility. Volatile supplies visibility and ordering for accesses to a field, but does not make count++ atomic. Transient excludes a field from default Java object serialization; custom serialization can still write its value explicitly. This refers to the current instance and is unavailable in a static context. These meanings matter more than treating keywords as a memorization list.
Answer: True. A constructor declaration has the class's simple name and no return type, not even void. A declaration such as void Widget() is a method named Widget, not a constructor.
Constructors initialize newly created instances and can have parameters and throws clauses. Their access can be public, protected, private, or package access. A private constructor can restrict construction to class-controlled operations, such as a factory method. Constructors are not ordinary methods inherited by subclasses, and return-type rules should not be confused with the type produced by a new expression.
Answer: Eight bytes, choice c. A long is a signed 64-bit integer with values from -263 through 263 - 1. Its value width is specified by Java and is not compiler-dependent. The earlier answer â64 bytesâ confused bits with bytes.
Long.SIZE is 64 and Long.BYTES is 8. These describe the primitive value width, not the total memory footprint of a boxed Long object, an object containing a long field, or an array including its header. Those storage costs include implementation details such as alignment and object metadata.
Answer: It returns "am". String indices start at zero; the beginning index is inclusive and the ending index is exclusive. Positions two and three contain a and m. The original String remains unchanged because String is immutable.
public class StringAndNumbers {
public static void main(String[] args) {
System.out.println("example".substring(2, 4));
System.out.println(Long.SIZE + " bits, " + Long.BYTES + " bytes");
System.out.println(0x05);
}
}
String indexing counts UTF-16 code units, not necessarily whole Unicode characters or user-perceived characters. Incorrect substring boundaries can split a surrogate pair. Invalid index ranges throw IndexOutOfBoundsException.
Answer: Only a is correct. A native method declaration uses the native modifier and has no Java method body; the implementation is supplied outside ordinary Java source, commonly through JNI.
Choice b is false because a package declaration can precede imports. Choice c is false because interfaces can declare default, static, and private methods as well as abstract methods; they cannot declare protected methods. Choice d is false because constructors may also be private or have package access. The legacy instruction to select two answers is therefore inappropriate for Java SE 25.
References: Java 25 main methods, Literals and keywords, Integer types, and Interface methods.