50 multiple-choice questions with answers and detailed explanations. Untimed — choose an answer to check it instantly.
0/50 answered0 correct
Question 1 of 50
Which method serves as the entry point of a Java application?
Correct answer: A) public static void main(String[] args)
Explanation: The Java Virtual Machine begins execution at public static void main(String[] args). It must be public, static, and return void to be recognized as the entry point.
Question 2 of 50
What does JVM stand for?
Correct answer: B) Java Virtual Machine
Explanation: The Java Virtual Machine executes compiled Java bytecode, providing platform independence by translating bytecode into instructions for the host system.
Question 3 of 50
What file extension does compiled Java bytecode use?
Correct answer: C) .class
Explanation: The Java compiler translates .java source files into .class files containing bytecode, which the JVM then executes.
Question 4 of 50
Which of the following is a primitive data type in Java?
Correct answer: D) int
Explanation: int is one of Java's eight primitive types. String, ArrayList, and Integer are reference types (objects), with Integer being the wrapper class for int.
Question 5 of 50
How many bits does a Java int occupy?
Correct answer: A) 32
Explanation: A Java int is a 32-bit signed two's complement integer, holding values from -2,147,483,648 to 2,147,483,647.
Question 6 of 50
Which primitive type would you use for a value requiring decimal precision in financial calculations?
Correct answer: B) BigDecimal is preferred over primitives for exact decimal values
Explanation: Floating-point types like double introduce rounding errors, so BigDecimal is preferred for exact decimal arithmetic such as monetary calculations.
Question 7 of 50
What is the default value of an uninitialized int instance field in Java?
Correct answer: C) 0
Explanation: Instance fields receive default values automatically: numeric types default to 0, boolean to false, and reference types to null.
Question 8 of 50
What is the default value of an uninitialized boolean field?
Correct answer: D) false
Explanation: A boolean instance field defaults to false when not explicitly initialized.
Question 9 of 50
Which keyword declares a value that cannot be reassigned in Java?
Correct answer: A) final
Explanation: The final keyword prevents reassignment of a variable after initialization. Java reserves const but does not use it.
Question 10 of 50
What does the static keyword indicate when applied to a field?
Correct answer: B) The field belongs to the class rather than to any instance
Explanation: A static field is shared by all instances of the class and belongs to the class itself, accessible without creating an object.
Question 11 of 50
Which access modifier makes a member accessible only within its own class?
Correct answer: C) private
Explanation: The private modifier restricts access to within the declaring class, forming the basis of encapsulation.
Question 12 of 50
Which access modifier makes a member accessible from any class?
Correct answer: D) public
Explanation: The public modifier allows access from any class in any package.
Question 13 of 50
What is the scope of a member declared with no access modifier?
Correct answer: A) Accessible within the same package only
Explanation: With no modifier, a member has package-private (default) access, visible only to classes in the same package.
Question 14 of 50
What does the protected access modifier permit?
Correct answer: B) Access within the package and by subclasses in other packages
Explanation: Protected members are accessible within the same package and by subclasses, even when those subclasses reside in a different package.
Question 15 of 50
How do you write a single-line comment in Java?
Correct answer: C) // comment text
Explanation: Java uses // for single-line comments and /* ... */ for multi-line comments. Javadoc comments begin with /**.
Question 16 of 50
Which operator concatenates two Strings in Java?
Correct answer: D) +
Explanation: The + operator concatenates Strings in Java. When either operand is a String, the other is converted to its String representation.
Question 17 of 50
What is the result of the expression 7 / 2 when both operands are int?
Correct answer: A) 3
Explanation: Integer division discards the fractional part, so 7 / 2 evaluates to 3. To get 3.5, at least one operand must be a floating-point type.
Question 18 of 50
What does the expression 7 % 2 evaluate to?
Correct answer: B) 1
Explanation: The modulus operator returns the remainder of division. 7 divided by 2 leaves a remainder of 1.
Question 19 of 50
What does the ++ operator do when used as i++?
Correct answer: C) Returns the current value, then increments i
Explanation: Post-increment (i++) yields the original value of i in the expression and then increases i by one. Pre-increment (++i) increments first, then yields the new value.
Question 20 of 50
Which comparison operator tests whether two primitive values are equal?
Correct answer: D) ==
Explanation: The == operator compares primitive values for equality. A single = performs assignment, and equals() compares object content.
Question 21 of 50
Why should Strings be compared with equals() rather than ==?
Correct answer: A) == compares references, while equals() compares character content
Explanation: For objects, == tests whether two references point to the same object. String.equals() compares actual character content, which is almost always what is intended.
Question 22 of 50
What does the logical && operator do in Java?
Correct answer: B) Returns true only if both operands are true, with short-circuit evaluation
Explanation: The && operator returns true only when both operands are true and short-circuits, skipping evaluation of the right operand when the left is already false.
Question 23 of 50
What is short-circuit evaluation?
Correct answer: C) Skipping evaluation of the second operand when the result is already determined
Explanation: Short-circuit evaluation stops as soon as the outcome is known, which both improves efficiency and allows safe patterns such as checking for null before dereferencing.
Question 24 of 50
What is the purpose of the if-else statement?
Correct answer: D) Executing different code paths based on a boolean condition
Explanation: An if-else statement chooses between alternative blocks of code based on whether a boolean condition evaluates to true or false.
Question 25 of 50
Which loop in Java always executes its body at least once?
Correct answer: A) do-while
Explanation: A do-while loop evaluates its condition after executing the body, so the body always runs at least one time.
Question 26 of 50
What is the enhanced for loop (for-each) used for?
Correct answer: B) Iterating over arrays and collections without an explicit index
Explanation: The enhanced for loop iterates directly over each element of an array or Iterable collection, removing index management and reducing off-by-one errors.
Question 27 of 50
How do you declare an array of 10 integers in Java?
Correct answer: C) int[] numbers = new int[10];
Explanation: The syntax int[] numbers = new int[10]; declares an array reference and allocates space for 10 int elements, each initialized to 0.
Question 28 of 50
What is the index of the last element in a Java array of length n?
Correct answer: D) n - 1
Explanation: Java arrays use zero-based indexing, so valid indices run from 0 to length minus one. Accessing index n throws ArrayIndexOutOfBoundsException.
Question 29 of 50
Which field returns the number of elements in a Java array?
Correct answer: A) length
Explanation: Arrays expose a public final field named length. Collections such as ArrayList use the size() method instead.
Question 30 of 50
What exception is thrown when accessing an invalid array index?
Correct answer: B) ArrayIndexOutOfBoundsException
Explanation: Accessing an index that is negative or greater than or equal to the array length throws ArrayIndexOutOfBoundsException at runtime.
Question 31 of 50
What is a method signature in Java?
Correct answer: C) The method name together with its parameter types
Explanation: A method signature consists of the method name and its parameter list types. The return type is not part of the signature for overloading purposes.
Question 32 of 50
What is method overloading?
Correct answer: D) Defining multiple methods with the same name but different parameter lists
Explanation: Overloading defines several methods sharing a name but differing in parameter number or types. The compiler selects the correct one based on the arguments supplied.
Question 33 of 50
Can two methods be overloaded by differing only in return type?
Correct answer: A) No, the parameter list must differ
Explanation: Return type is not part of the method signature for overload resolution, so methods differing only by return type cause a compile-time error.
Question 34 of 50
What is a class in Java?
Correct answer: B) A blueprint defining the state and behavior of objects
Explanation: A class defines fields (state) and methods (behavior) that its instances will have, serving as a template from which objects are created.
Question 35 of 50
What is an object in Java?
Correct answer: C) An instance of a class created at runtime
Explanation: An object is a concrete instance of a class, occupying memory and holding its own field values while sharing the class's method definitions.
Question 36 of 50
Which keyword creates a new object instance?
Correct answer: D) new
Explanation: The new keyword allocates memory for a new object, invokes the appropriate constructor, and returns a reference to the created instance.
Question 37 of 50
What is a constructor?
Correct answer: A) A special method that initializes a newly created object
Explanation: A constructor has the same name as its class, has no return type, and runs when an object is created to initialize its state.
Question 38 of 50
What happens if a class defines no constructor?
Correct answer: B) The compiler provides a no-argument default constructor
Explanation: When no constructor is declared, the compiler supplies a default no-argument constructor. Declaring any constructor removes this automatic default.
Question 39 of 50
What does the this keyword refer to?
Correct answer: C) The current object instance
Explanation: The this reference points to the current object, commonly used to distinguish fields from parameters with the same name or to invoke another constructor.
Question 40 of 50
What is encapsulation?
Correct answer: D) Restricting direct access to fields and exposing controlled methods
Explanation: Encapsulation hides internal state behind private fields and exposes controlled access through methods, protecting invariants and allowing implementation to change safely.
Question 41 of 50
What is the purpose of getter and setter methods?
Correct answer: A) Providing controlled read and write access to private fields
Explanation: Getters and setters provide managed access to private fields, allowing validation, computed values, or logging without exposing the field directly.
Question 42 of 50
What does the String class's length() method return?
Correct answer: B) The number of characters in the String
Explanation: String.length() returns the count of characters in the String. Note that arrays use a length field while Strings use a length() method.
Question 43 of 50
Why are Java Strings described as immutable?
Correct answer: C) Once created, their character content cannot be changed
Explanation: String objects cannot be modified after creation. Methods that appear to change a String actually return a new String, leaving the original intact.
Question 44 of 50
Which class is preferred for building Strings in a loop?
Correct answer: D) StringBuilder
Explanation: Repeated String concatenation creates many intermediate objects. StringBuilder modifies an internal buffer, making it far more efficient for building Strings iteratively.
Question 45 of 50
What does the substring(2, 5) method return for the String "programming"?
Correct answer: A) "ogr"
Explanation: substring(2, 5) returns characters from index 2 up to but not including index 5. For "programming", that yields "ogr".
Question 46 of 50
What is a wrapper class in Java?
Correct answer: B) A class that represents a primitive value as an object
Explanation: Wrapper classes such as Integer, Double, and Boolean represent primitive values as objects, allowing them to be used in collections and generics.
Question 47 of 50
What is autoboxing?
Correct answer: C) Automatic conversion between a primitive and its wrapper class
Explanation: Autoboxing automatically converts a primitive to its wrapper object, and unboxing performs the reverse, letting primitives be used where objects are required.
Question 48 of 50
What does the final keyword do when applied to a class?
Correct answer: D) Prevents the class from being subclassed
Explanation: A final class cannot be extended. String is a well-known example, declared final to guarantee its immutability and security properties.
Question 49 of 50
What is a package in Java?
Correct answer: A) A namespace grouping related classes and interfaces
Explanation: A package organizes related classes into a namespace, preventing naming conflicts and providing an access control boundary.
Question 50 of 50
What does the import statement accomplish?
Correct answer: B) Makes classes from another package usable by their simple names
Explanation: The import statement lets code refer to classes from other packages by their simple names rather than fully qualified names. It does not copy any code.
Set 1 complete
You scored 0%
0 correct out of 0 answered (50 questions in this set).