Practice: Core Java Rapid-Fire Drill
50 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)The first ten minutes of many technical rounds are rapid fire: short questions, no time to think aloud. The interviewer is checking whether your basics are instant, correct and short, then moves to output tracing and a small coding question.
Rapid-fire round: 30-second answers
A rapid-fire round is a quick series of short questions that checks your basics. A good answer is one or two sentences: the key word first, one reason or example, then stop and wait for the follow-up.
Each answer below is timed in seconds. Read how the key word comes first, then one reason, and then the answer stops.
Example 1 · Five rapid-fire answers, timed
Q: Is Java pass by value or reference?A: Always by value. For objects the reference is copied. (6 s)Q: Why is String immutable?A: Pool sharing, security, stable hash codes, thread safety. (5 s)Q: Checked vs unchecked?A: Checked must be caught or declared, like IOException; unchecked extend RuntimeException, like NullPointerException. (8 s)Q: Does finally always run?A: Yes after try or catch, even with return, unless System.exit or a JVM crash stops it first. (7 s)Q: Can main be overloaded?A: Yes, but only main(String[] args) starts the program. (4 s)Lead with the answer word, add one reason, stop.
What this code does
- Each answer starts with the verdict ("Always by value", "Yes") and adds one reason; none needs more than about 10 seconds.
- Practise the 30 questions in the Interview tab the same way, out loud, with a timer.
Lead with the key word: "Always by value", "Up to two objects", "No, primitives". Add one reason or example, then stop. If you are unsure, say the part you are sure of and offer to reason the rest out. The Interview tab of this lesson holds 30 rapid-fire questions to drill aloud.
Order of execution
Shape of a rapid-fire answer
Put the steps in the order a strong 30-second answer follows.
Predict-the-output set
The Integer cache is a set of ready-made Integer objects for the values -128 to 127. Autoboxing returns the cached object for those values, so == is true there and false for larger values.
Output questions mix the traps from this module. This program prints six small puzzles, so predict each line before you run it.
Example 2 · A mixed predict-the-output set
class Counter { static int created; static { System.out.println("A: Counter loaded"); } Counter() { created++; }}
public class MixedPuzzle { static int tricky() { try { return 10; } finally { System.out.println("B: finally before return"); } }
static void rename(StringBuilder sb, String s) { sb.append("!"); s = s + "!"; }
public static void main(String[] args) { Integer a = 127, b = 127, c = 128, d = 128; System.out.println("1: " + (a == b) + " " + (c == d) + " " + c.equals(d));
String x = "java"; String part = "ja"; final String fin = "ja"; System.out.println("2: " + (x == part + "va") + " " + (x == fin + "va"));
new Counter(); new Counter(); System.out.println("3: " + Counter.created);
System.out.println("4: " + tricky());
StringBuilder sb = new StringBuilder("hi"); String s = "hi"; rename(sb, s); System.out.println("5: " + sb + " " + s);
System.out.println("6: " + ('a' + 1) + " " + (char) ('a' + 1)); }}1: true false true 2: false true A: Counter loaded 3: 2 B: finally before return 4: 10 5: hi! hi 6: 98 b
What this code does
- Line 1 is the Integer cache; line 2 the run-time vs
finalstring join;Aprints whenCounteris first used, once, and line 3 shows the shared static count. Bprints before line 4 becausefinallyruns before the return completes. Line 5: the builder is changed through the shared reference, while theStringparameter is only reassigned.
Trace slowly and write down each variable's value. Remember: Integer == is reliable only inside -128..127, run-time string joins are new objects while final literal joins are pooled, static blocks run once, and finally runs before a return completes (a return in finally wins).
Predict the output
Predict the mixed output
Read each snippet and pick what it prints.
Round 1 of 3
Two coding questions: count characters, reverse words
Counting characters and reversing words are two of the most common fresher coding questions. This program solves both and prints the results for normal and empty input.
Example 3 · Count each character
import java.util.LinkedHashMap;import java.util.Map;
public class CharCount { static Map<Character, Integer> countChars(String s) { Map<Character, Integer> counts = new LinkedHashMap<>(); for (char c : s.toCharArray()) { if (c != ' ') { counts.merge(c, 1, Integer::sum); } } return counts; }
static int[] countLetters(String s) { int[] freq = new int[26]; for (char c : s.toLowerCase().toCharArray()) { if (c >= 'a' && c <= 'z') { freq[c - 'a']++; } } return freq; }
public static void main(String[] args) { System.out.println(countChars("hello world")); System.out.println(countChars(""));
int[] freq = countLetters("Banana"); System.out.println("a=" + freq[0] + " b=" + freq[1] + " n=" + freq['n' - 'a']); }}{h=1, e=1, l=3, o=2, w=1, r=1, d=1}
{}
a=3 b=1 n=2What this code does
merge(c, 1, Integer::sum)puts 1 for a new character and adds 1 otherwise. ALinkedHashMapkeeps first-seen order, so the output is predictable; aHashMapwould not promise any order.- The
int[26]version is faster and usesc - 'a'as the index, but only works for letters. O(n) time either way.
Example 4 · Reverse the words in a sentence
public class ReverseWordsDrill { static String reverseWords(String sentence) { String trimmed = sentence.trim(); if (trimmed.isEmpty()) { return ""; } String[] words = trimmed.split("\\s+"); StringBuilder sb = new StringBuilder(); for (int i = words.length - 1; i >= 0; i--) { sb.append(words[i]); if (i > 0) { sb.append(' '); } } return sb.toString(); }
public static void main(String[] args) { System.out.println("[" + reverseWords("I love Java") + "]"); System.out.println("[" + reverseWords(" placement season is here ") + "]"); System.out.println("[" + reverseWords(" ") + "]"); }}[Java love I] [here is season placement] []
What this code does
trim()removes outer spaces andsplit("\\s+")treats any run of spaces as one gap, so extra spaces disappear from the result.- The early return handles a blank sentence; without it, splitting an empty string would give one empty word.
For counting, a LinkedHashMap<Character, Integer> keeps first-seen order, or an int[26] works for lower-case letters only. For reversing words, trim().split("\\s+") handles extra spaces, then a StringBuilder joins the words backwards. Say both edge cases aloud: empty input and repeated spaces.
Order of execution
Reverse the words of a sentence
Put the steps in the order the solution runs them.
Spot the wrong answer
Some interviewers read out a candidate answer and ask what is wrong with it. This program prints four half-true answers with the wrong word and its fix.
Example 5 · Candidate answers with hidden mistakes
1. "Objects are passed by reference in Java." Wrong word: "by reference". Fix: references are passed by value; reassigning a parameter never changes the caller's variable.2. "finally always runs, no matter what." Wrong word: "always". Fix: not after System.exit() or a JVM crash.3. "StringBuffer is faster because it is synchronized." Wrong idea: locking makes it slower. Fix: StringBuilder is faster; StringBuffer is thread-safe per call.4. "Integer values can be compared with == because of autoboxing." Wrong idea: == compares references. Fix: works only inside the -128..127 cache by luck; use equals.Find the wrong word -> correct term -> one-line proof.
What this code does
- Each wrong answer is a half-truth that sounds right. Naming the exact wrong word shows the interviewer you know the precise rule.
- The proof line is what convinces:
System.exit()for finally, the 128 case for Integer.
The mistakes are almost always the classic half-truths: "objects are passed by reference", "Java is 100% OOP", "finally always runs", "StringBuffer is faster". The method is always the same: find the precise word that is wrong, replace it with the correct term and give a one-line proof.
Find the bug
Spot the wrong answer
Each answer has one wrong line. Tap it, then check the correction.
A candidate answers about method calls.
Round 1 of 3Next step
Check your understanding
12 quick questions