Sign in
Home/Oracle/Java Foundations (1Z0-811)/Free questions

Java Foundations (1Z0-811) — Free Practice Questions

10 free sample questions from a bank of 42, with the correct answers and explanations. No signup required — start practising right now.

1Which statement is true about primitive variables?
  • They can be compared with the == operator.
  • They can be compared with the equals method only.
  • They cannot be compared.
  • They can be compared with the compareTo method only.
Answer: A

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.

2Given the code fragment: Which code fragment, when inserted at line n1, enables the code to print Java Programming:1?
Java Foundations (1Z0-811) question 2
  • System.out.println(name + “:” + count);
  • System.out.println(c.name + “:” + count);
  • System.out.println(c.name + “:” + Course.count);
  • System.out.println(Course.name + “:” + c.count);
Answer: C

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.

3Given: Why does the compilation fail?
Java Foundations (1Z0-811) question 3
  • The option variable is not initialized.
  • The order of case labels is incorrect.
  • The option data type is incorrect.
  • The break statement is missing.
Answer: C

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.

4Given: What is the result?
Java Foundations (1Z0-811) question 4
  • [null:-]
  • [ : ]
  • [null:null]
  • [:null]
Answer: A

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.

5Which two Java reserved words are used to implement encapsulation?
  • final
  • static
  • public
  • extends
  • private
Answer: C, E

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.

6Given the code fragment: Which code fragment can be inserted at line n1 to enable the code to print 0.0?
Java Foundations (1Z0-811) question 6
  • Ball b = null; b.weight = 0.0;
  • Ball.weight = 0.0;
  • Ball b = new Ball(0.0);
  • Ball b = new Ball();
Answer: D

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.

7Which two statements are true about the Java Runtime Environment (JRE)?
  • It is responsible for garbage collection.
  • It interprets bytecode stored in a .class file.
  • It contains the JDK and Java APIs.
  • It is platform independent.
  • You must install the JRE to compile a .java file.
Answer: B, D

Answer: B and D

The short version

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.

Key concepts

  • JDK = compiler (javac) + JRE + development tools; JRE = the runtime only
  • JVM - the virtual machine inside the JRE that interprets bytecode and drives the garbage collector
  • Bytecode is platform-neutral; the JRE implementation is not - you install a Windows, macOS or Linux build of the same JRE
  • java (the launcher) finds the JVM and hands it the .class file; the JVM does the loading and linking
  • Garbage collection is a feature of the JVM, implemented by every production JRE, but the exam wants the defining role

Why B and D are correct

B - "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.

Why the others are wrong

  • A. It is responsible for garbage collection. Tempting, because every production JVM does run a collector. But GC is a consequence of running code inside a managed heap, not the JRE's defining responsibility, and the phrasing points at the JVM component rather than the JRE distribution. This option tests whether you conflate "feature the JVM implements" with "role the JRE is defined by" - B is the defining role.
  • C. It contains the JDK and Java APIs. Exactly backwards, and this is the option that catches candidates who have the platform hierarchy upside down. It is the JDK that contains a JRE; the JRE contains the Java SE API classes plus the JVM. The containment runs JDK -> JRE -> JVM, never back the other way.
  • D. not discussed further; it is part of the answer.
  • E. You must install the JRE to compile a .java file. False and it reverses the tool chain. Compilation is 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.

1z0-811 exam tip

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.

8Identify two features of Java.
  • architecture dependent
  • platform independent
  • single threaded
  • robust
Answer: B, D

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.

9Given the code fragment: What is the result?
Java Foundations (1Z0-811) question 9
  • A compilation error occurs.
  • A runtime exception is thrown.
  • Julia - Peter
  • Peter - Julia
Answer: A

Answer: A. A compilation error occurs.

The short version

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.

Key concepts

  • The parser matches every ( with a ); a surplus ) is a lexical/grammar error reported by javac
  • List<String> names = new ArrayList<>() - diamond inference from the declared target type
  • names.iterator() returns an Iterator<String>; hasNext() guards the loop, next() advances it
  • The for loop is written without an update expression - legal, since next() supplies the advance
  • A compilation error happens before any class loading or execution, so no output can be produced

Why A are correct

Look 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.

Why the others are wrong

  • B. A runtime exception is thrown. Nothing executes at all, so there is no runtime phase in which to throw. A runtime exception requires a successfully compiled class to be loaded and run; here the class never gets that far. Choosing B means you found a semantic problem rather than the syntactic one.
  • C. 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());.
  • D. 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.
  • A. not discussed further; it is the answer.

1z0-811 exam tip

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.

10Given: And the code fragment: What is the result?
Java Foundations (1Z0-811) question 10Java Foundations (1Z0-811) question 10
  • A compilation error occurs at line n1.
  • White : null
  • null: null
  • White : White
Answer: A

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.