What is the output of: int x = 5; System.out.println(x++);
Correct answer:
A) 5
Explanation: Post-increment prints the current value 5 first, then increments x to 6. The increment happens after the value is used in the expression.
What is the output of: int x = 5; System.out.println(x++);
Correct answer:
A) 5
Explanation: Post-increment prints the current value 5 first, then increments x to 6. The increment happens after the value is used in the expression.
What is the value of result after: int result = 10 + 5 * 2;
Correct answer:
C) 20
Explanation: Multiplication has higher precedence than addition, so 5 * 2 evaluates to 10 first, then 10 + 10 gives 20.
What does the expression (3 > 2) && (2 > 5) evaluate to?
Correct answer:
D) false
Explanation: The first operand is true but the second is false. Logical AND requires both to be true, so the result is false.
What is the value of s after: String s = "Java"; s.toUpperCase();
Correct answer:
A) "Java"
Explanation: Strings are immutable, so toUpperCase() returns a new String without modifying s. Since the result is not assigned, s still holds "Java".
Which loop prints the numbers 1 through 5?
Correct answer:
B) for (int i = 1; i <= 5; i++)
Explanation: Starting at 1 and continuing while i is less than or equal to 5 produces exactly 1, 2, 3, 4, 5.
How many times does this loop execute: for (int i = 0; i < 10; i += 2)?
Correct answer:
C) 5
Explanation: The counter takes the values 0, 2, 4, 6, and 8 before reaching 10 and failing the condition, giving 5 iterations.
What does the expression "5" + 5 produce in Java?
Correct answer:
D) "55"
Explanation: When one operand of + is a String, the operator performs concatenation, converting the int 5 to "5" and producing "55".
What does 5 + 5 + "5" produce?
Correct answer:
A) "105"
Explanation: Evaluation proceeds left to right: 5 + 5 gives 10, then 10 + "5" concatenates to produce "105".
Which statement correctly declares and initializes a two-dimensional array?
Correct answer:
B) int[][] grid = new int[3][4];
Explanation: The syntax int[][] grid = new int[3][4]; creates a 3-row by 4-column array of integers, all initialized to 0.
What is the result of comparing two String objects created with new using ==?
Correct answer:
C) false, because they are different objects in memory
Explanation: The == operator compares references. Two Strings created with new are distinct objects, so == returns false even when the text is identical.
What does the String method equalsIgnoreCase() do?
Correct answer:
D) Compares content while ignoring capitalization differences
Explanation: equalsIgnoreCase() compares two Strings character by character while treating uppercase and lowercase letters as equivalent.
What does the trim() method on a String accomplish?
Correct answer:
A) Removes leading and trailing whitespace
Explanation: trim() returns a new String with leading and trailing whitespace removed, leaving internal spaces intact.
What does the split() method return?
Correct answer:
B) An array of Strings divided by a delimiter
Explanation: split() divides a String at each occurrence of the given delimiter or regular expression and returns the resulting pieces as a String array.
What does Integer.parseInt("42") return?
Correct answer:
C) The int value 42
Explanation: Integer.parseInt() converts a numeric String into its primitive int equivalent, throwing NumberFormatException if the String is not a valid integer.
What does the charAt(0) method return for the String "Hello"?
Correct answer:
D) 'H'
Explanation: charAt(0) returns the primitive char at index 0, which is 'H' for the String "Hello".
What is the purpose of the indexOf() method on a String?
Correct answer:
A) Returning the position of the first occurrence of a character or substring
Explanation: indexOf() returns the index of the first occurrence of the specified character or substring, or -1 if it is not present.
What does the contains() method on a String return?
Correct answer:
B) A boolean indicating whether the substring is present
Explanation: contains() returns true when the specified character sequence appears within the String, and false otherwise.
Which loop construct is most appropriate for iterating over an ArrayList when the index is not needed?
Correct answer:
C) The enhanced for-each loop
Explanation: The enhanced for loop iterates directly over elements, producing clearer code and eliminating index errors when position is not required.
Which ArrayList method adds an element to the end of the list?
Correct answer:
D) add()
Explanation: ArrayList.add(element) appends the element to the end. An overloaded add(index, element) inserts at a specified position instead.
Which ArrayList method returns the number of elements?
Correct answer:
A) size()
Explanation: ArrayList uses the size() method. Arrays use the length field, and Strings use the length() method, a common source of confusion.
What does the ArrayList get(index) method do?
Correct answer:
B) Returns the element at the specified position
Explanation: get(index) retrieves the element stored at the given zero-based index without modifying the list.
What does HashMap.put(key, value) do when the key already exists?
Correct answer:
C) Replaces the existing value and returns the old one
Explanation: Map keys are unique, so put() with an existing key replaces the associated value and returns the previous value.
What does HashMap.get(key) return when the key is absent?
Correct answer:
D) null
Explanation: HashMap.get() returns null when the key is not present. getOrDefault() can supply an alternative value instead.
Which collection would best store a list of unique usernames?
Correct answer:
A) HashSet
Explanation: A HashSet automatically rejects duplicates and provides fast membership testing, making it well suited to storing unique values.
What is the output of: System.out.println(10 / 3);
Correct answer:
B) 3
Explanation: Both operands are integers, so integer division truncates the fractional part, yielding 3.
What is the output of: System.out.println(10.0 / 3);
Correct answer:
C) 3.3333333333333335
Explanation: Because 10.0 is a double, the division is performed in floating point, producing a decimal result rather than truncating.
What does casting a double to an int accomplish, as in (int) 9.99?
Correct answer:
D) Truncates the decimal portion, giving 9
Explanation: Casting a floating-point value to int truncates toward zero rather than rounding, so (int) 9.99 evaluates to 9.
Which method rounds a double to the nearest whole number?
Correct answer:
A) Math.round()
Explanation: Math.round() rounds to the nearest integer value. Math.floor() always rounds down and Math.ceil() always rounds up.
What does Math.abs(-7) return?
Correct answer:
B) 7
Explanation: Math.abs() returns the absolute value, converting negative numbers to their positive equivalent.
What does Math.pow(2, 3) return?
Correct answer:
C) 8.0
Explanation: Math.pow(base, exponent) raises 2 to the power of 3, returning 8.0 as a double.
What happens when a method declares a return type but has no return statement on some path?
Correct answer:
D) A compile error occurs
Explanation: The compiler requires every code path in a non-void method to return a value, producing a missing return statement error otherwise.
What does passing an object to a method actually pass in Java?
Correct answer:
A) A copy of the reference to the object
Explanation: Java is strictly pass-by-value, but for objects the value passed is a copy of the reference. The method can modify the object's state but cannot reassign the caller's variable.
Can a method modify the contents of an array passed as a parameter?
Correct answer:
B) Yes, because both refer to the same array object
Explanation: The reference copy points to the same underlying array, so modifications to elements inside the method are visible to the caller.
What is a varargs parameter, declared as int... numbers?
Correct answer:
C) A parameter accepting a variable number of arguments
Explanation: Varargs allows a method to accept zero or more arguments of the given type, which the method receives as an array.
What is the scope of a variable declared inside a for loop's initialization?
Correct answer:
D) Limited to the loop itself
Explanation: A variable declared in the for statement's initializer exists only within the loop and is inaccessible after the loop completes.
What is shadowing in Java?
Correct answer:
A) A local variable hiding a field with the same name
Explanation: Shadowing occurs when a local variable or parameter shares a name with a field, hiding the field within that scope. The this keyword disambiguates.
What does a constructor with parameters allow?
Correct answer:
B) Initializing an object with specific values at creation
Explanation: A parameterized constructor accepts values used to initialize the new object's fields, letting each instance start in a specific, valid state.
What is constructor chaining using this()?
Correct answer:
C) One constructor invoking another constructor in the same class
Explanation: Calling this(...) as the first statement in a constructor invokes another constructor in the same class, avoiding duplicated initialization logic.
What must be the first statement in a constructor that calls super()?
Correct answer:
D) The super() call itself
Explanation: An explicit call to super() or this() must be the very first statement in a constructor, ensuring initialization proceeds correctly up the hierarchy.
What is the purpose of a static initializer block?
Correct answer:
A) Running setup code once when the class is first loaded
Explanation: A static block executes once when the class is loaded into memory, used for one-time initialization of static fields requiring more than a simple assignment.
What does the keyword void indicate as a return type?
Correct answer:
B) The method returns no value
Explanation: A void method performs its work without returning a value to the caller, though it may still use a bare return statement to exit early.
What is an immutable object?
Correct answer:
C) An object whose state cannot change after construction
Explanation: An immutable object's state is fixed at construction. String is the canonical example. Immutability makes objects inherently thread-safe and easier to reason about.
Which practice helps make a class immutable?
Correct answer:
D) Declaring fields private and final with no setter methods
Explanation: Immutability requires private final fields, no mutating methods, and defensive copying of any mutable objects passed in or returned.
What is the output of: boolean b = (5 == 5.0);
Correct answer:
A) true
Explanation: The int 5 is promoted to the double 5.0 for the comparison, so the values are equal and the expression evaluates to true.
What is integer overflow?
Correct answer:
B) A result exceeding the maximum value the type can represent, wrapping around
Explanation: When arithmetic produces a value beyond the type's range, it silently wraps around to the opposite extreme rather than raising an error, causing subtle bugs.
Which type would you choose for values larger than the int range?
Correct answer:
C) long
Explanation: A long is a 64-bit signed integer, accommodating values far beyond the 32-bit range of int.
What does the char data type store?
Correct answer:
D) A single 16-bit Unicode character
Explanation: A char holds one 16-bit Unicode character. Character literals use single quotes, while String literals use double quotes.
What is the difference between 'A' and "A" in Java?
Correct answer:
A) 'A' is a char literal and "A" is a String object
Explanation: Single quotes denote a primitive char, while double quotes denote a String object. They are different types and behave differently.
What is the primary benefit of writing small, single-purpose methods?
Correct answer:
B) Easier to understand, test, reuse, and debug
Explanation: Small focused methods are easier to name, reason about, test in isolation, and reuse, which substantially reduces defects and maintenance effort.
Which Java keyword is used to create a subclass that inherits from a superclass?
Correct answer:
B) extends
Explanation: A class uses extends to inherit from another class, and implements to adopt an interface.
If this exam is giving you a hard time, let us be your helping hand. Contact us, and we'll provide the assistance you need to pass and move forward.