10 free sample questions from a bank of 42, with the correct answers and explanations. No signup required — start practising right now.
A is correct: primitives hold plain values, not references, so == performs a value comparison directly - int 5 == long 5 is true. equals() is an object method for reference comparison, and compareTo() belongs to the Comparable interface, which primitives do not implement.
C is correct. count is static on Course and name is an instance field, so line n1 needs c.name for the per-object value and Course.count for the class-level value: new Course("Java Programming") sets name and bumps count to 1, giving Java Programming:1. name/count alone are not in scope, and Course.name is not static.
C is correct. A switch selector may be char, byte, short, int, their wrappers, String or an enum - never boolean - so switch(option) with case true: is rejected at compile time as an incompatible label type. Nothing needs to throw, case order is free, and a missing break only causes fall-through.
A is the keyed answer and the closest option: sName is a reference and defaults to null, printing "null", while the char grade defaults to the unprintable \u0000, not a dash. The vendor commentary concedes the exact option is missing, so read A as the [null:<char>] shape rather than a literal dash.
C and E are correct. Encapsulation is built from access modifiers: private hides a member inside the class and public exposes it, so the pair brackets the hiding/exposing boundary. final pins a value, static is about class-level sharing rather than visibility, and extends is inheritance.
D is correct: Ball declares only a double weight, so new Ball() uses the compiler-generated no-arg constructor and weight keeps its default of 0.0. Ball.weight is not static, there is no Ball(double) constructor, and a null reference would throw NullPointerException on the field access.
Answer: B and D
The JRE is the layer that executes compiled code: it loads .class files, verifies their bytecode, and interprets/JIT-compiles the instructions. It is also written once per platform family and runs anywhere that platform exists, so it is platform independent. Together those are B and D.
javac) + JRE + development tools; JRE = the runtime onlyjava (the launcher) finds the JVM and hands it the .class file; the JVM does the loading and linkingB - "It interprets bytecode stored in a .class file." That is the JRE's central job. javac has already turned source into bytecode; the JRE is what loads it, links it against the class library, verifies it, and executes it (interpreting at first, then handing hot methods to the JIT for native compilation). Without a JRE a .class file is inert data. D - "It is platform independent." Java's whole portability promise rests here: the bytecode you ship is identical on every platform, and only the JRE implementation differs. You download the JRE build for your OS, run the same .class, get the same result - that is what makes Java platform independent at the language level.
javac, which ships in the JDK. The JRE is needed to run the resulting .class files. In modern JDK distributions the JRE is bundled inside the JDK, but the component that compiles is unambiguously the JDK's javac.Learn the nesting as one chain: JDK contains JRE contains JVM. Compilation needs the JDK; execution needs the JRE. If an option reverses the arrow - "the JRE contains the JDK" - it is wrong by construction, whatever else it says.
B and D are correct. Java bytecode is platform neutral, which is what makes it platform independent, and its type system plus automatic GC make it robust. architecture dependent is the opposite of the design goal, and single threaded is wrong since every JVM is multi-threaded.
Answer: A. A compilation error occurs.
The exhibit builds a List<String>, adds "Julia" and "Peter", then loops with an Iterator and calls System.out.println(itr.next())); - note the extra closing parenthesis before the semicolon. An unbalanced ) makes the statement unparseable, so nothing runs at all: the result is a compile-time error, not any of the printed values.
( with a ); a surplus ) is a lexical/grammar error reported by javacList<String> names = new ArrayList<>() - diamond inference from the declared target typenames.iterator() returns an Iterator<String>; hasNext() guards the loop, next() advances itfor loop is written without an update expression - legal, since next() supplies the advanceLook closely at the last statement: System.out.println(itr.next()));. println( opens one parenthesis, itr.next() opens and closes its own, and then there is a second ) before the ;. That extra ) has nothing to match. javac rejects the file during parsing, so ArrayList is never instantiated, add is never called, and itr.next() is never evaluated. The result is a compilation error. This is exactly why the vendor commentary for the row insists you "keep an eye on the code" and points at the stray parenthesis - and it is why none of the printing options can be right.
Julia - then Peter. This is the output the loop would produce if the statement were well-formed - hasNext() is true for "Julia", then for "Peter", and each next() prints on its own line via println. It is the intended distractor, and it is only reachable with a correctly balanced println(itr.next());.Peter - then Julia. The right shape of output in the wrong order. The list is built with add("Julia") before add("Peter"), and an ArrayList preserves insertion order, so iteration yields "Julia" before "Peter". Reversing them would require the list to be sorted or built in the opposite order.On any "what is the result" item, count the parentheses before you trace the logic. A single stray ) or ; converts the question into a compilation-error item and makes every printed-output distractor irrelevant. Read the characters, then read the code.

A is correct. Product declares the copy constructor Product(Product), and any explicit constructor suppresses the compiler-generated no-arg one, so new Product() at line n1 is an error. Without it p1.color would be null and the copy would stay null, never White.
Want the full bank of 42 questions for Java Foundations (1Z0-811)? See all practice exams.