Java MCQ Test
20 random core Java MCQs with answers: data types, operators, strings, Scanner, conditions, loops, arrays, methods, collections, HashMap, exceptions, lambdas and streams.
- 20 random questions
- 20 minutes
- 48 in the question bank
No sign-up needed. Answers and explanations are shown at the end.
About this test
Core Java is the first round for most Java developer jobs and a staple of campus placement tests. These questions cover the language itself: types and casting, String behaviour, loops and arrays, methods, collections, exceptions and streams.
Take the Java OOP test next for classes, inheritance and polymorphism.
Each attempt picks new questions at random from the question bank. You can go back and change answers until you finish or the timer runs out. At the end you see your score, the right answer and a short explanation for every question, and the lessons to revise for the ones you missed.
Topics covered
- Java Introduction
- Installing Java and Running Programs
- Java Variables and Data Types
- Java Operators and Expressions
- Java Strings
- Java User Input with Scanner
- Java If, Else and Switch
- Java Loops
- Java Arrays
- Java Methods
- Java ArrayList and Collections
- Java HashMap and Maps
- Java Exception Handling
Sample questions with answers
1. What does the javac command produce from Hello.java?
- Machine code for your CPU
- Bytecode in
Hello.class - A Windows .exe file
Show answer
B. Bytecode in Hello.class — javac compiles source into platform-independent bytecode that the JVM runs.
2. How do you run the compiled class Greeting?
javac Greetingjava Greeting.classjava Greeting
Show answer
C. java Greeting — Pass the class name without the .class extension.
3. What does (int) 9.99 give?
- 9
- 10
- 9.99
Show answer
A. 9 — Casting to int drops the decimal part; it doesn't round.
4. What does 7 / 2 evaluate to in Java?
- 4
- 3.5
- 3
Show answer
C. 3 — int divided by int is integer division.
5. How should you compare the content of two Strings?
a == ba = ba.equals(b)
Show answer
C. a.equals(b) — == compares references; equals compares characters.
6. Why does nextLine() return an empty string right after nextInt()?
- nextInt leaves the Enter (newline) behind
- Scanner is broken
- The user typed nothing
Show answer
A. nextInt leaves the Enter (newline) behind — Consume the leftover newline, or read lines and parse them.
7. What happens in a classic switch case without break?
- Execution falls through into the next case
- Compile error
- The switch ends
Show answer
A. Execution falls through into the next case — Classic switch falls through; the arrow form (->) never does.
8. Which loop always runs at least once?
- while
- do-while
- for
Show answer
B. do-while — do-while checks its condition after the body.
9. What is the last valid index of an array of length 5?
- 4
- 5
- 6
Show answer
A. 4 — Indexes start at 0, so the last one is length - 1.
10. Is Java pass-by-value or pass-by-reference?
- Always pass-by-reference
- Always pass-by-value (object references are copied)
- Depends on the type
Show answer
B. Always pass-by-value (object references are copied) — Methods receive copies; for objects, a copy of the reference.