Oracle Java Foundations (1Z0-811) — Questions and Answers
Question 1: What is the effect of calling `Collections.sort(list, Comparator.reverseOrder())`?
- Sorts list in natural ascending order
- Throws UnsupportedOperationException
- Sorts list in descending order (Correct answer)
- Randomly shuffles the list
Correct answer: Sorts list in descending order
`Comparator.reverseOrder()` produces a comparator that imposes the reverse of the natural ordering, sorting the list in descending order.
Question 2: Which of the following is a valid enhanced for loop (for-each) in Java SE 11?
- for(int i : 10) { }
- for(String s : stringArray) { } (Correct answer)
- for each(String s in list) { }
- foreach(String s : list) { }
Correct answer: for(String s : stringArray) { }
The enhanced for loop syntax uses `for(Type variable : iterable)` and works with arrays and any Iterable collection.
Question 3: What does calling 'super()' inside a constructor do?
- Calls the parent class constructor (Correct answer)
- References the current class instance
- Invokes a static method from the parent
- Creates a new instance of the parent class
Correct answer: Calls the parent class constructor
When 'super()' is used in a constructor, it calls the parent class's constructor to initialize inherited fields.
Question 4: What is the -classpath option used for when running the java command?
- To specify the path where Java source files are located
- To set the path for finding Java libraries and classes during runtime (Correct answer)
- To define the location of the module-info.java file
- To configure the Java Virtual Machine (JVM) memory settings
Correct answer: To set the path for finding Java libraries and classes during runtime
When running the `java` command, the `-classpath` (or `-cp`) option is used to specify the path where the Java Virtual Machine (JVM) should look for user-defined classes and packages. This path includes directories containing `.class` files and JAR files, ensuring that the JVM can locate all necessary components for the application to execute correctly at runtime.
Question 5: What is the correct way to call a static method `calculate` from the same class `MathUtil`?
- super.calculate()
- this.calculate()
- MathUtil.calculate() or just calculate() within the class (Correct answer)
- new MathUtil().calculate()
Correct answer: MathUtil.calculate() or just calculate() within the class
Static methods are called on the class name (e.g., `MathUtil.calculate()`) or simply by method name within the same class without an object reference.
Question 6: What is the output of this code? int x = 0; while (x < 5) { x += 2; } System.out.println(x);
- 6 (Correct answer)
- 5
- 4
- 0
Correct answer: 6
x goes 0→2→4→6; when x becomes 6, the condition 6<5 is false, so the loop exits with x=6.
Question 7: What is the result of integer division `7 / 2` in Java?
- 3.0
- 3.5
- 4
- 3 (Correct answer)
Correct answer: 3
Integer division in Java truncates the decimal portion, so 7 divided by 2 yields 3, not 3.5.
Question 8: Which method converts a `Stream` to an `Optional` containing the first element?
- peek()
- findAny()
- findFirst() (Correct answer)
- limit(1)
Correct answer: findFirst()
`Stream.findFirst()` returns an `Optional` describing the first element of the stream, or an empty Optional if the stream is empty.
Question 9: What makes a professional competent in Arrays and Collections?
- A combination of education, practical experience, ongoing learning, and adherence to professional standards (Correct answer)
- Only certifications matter
- Only formal education matters
- Only experience matters
Correct answer: A combination of education, practical experience, ongoing learning, and adherence to professional standards
Competence requires the integration of knowledge, skills, experience, and professional conduct.
Question 10: What is the importance of communication skills in Arrays and Collections?
- Communication skills cannot be learned
- Clear communication prevents errors, builds relationships, and ensures understanding among all parties (Correct answer)
- Technical skills are sufficient without communication
- Communication is only important for managers
Correct answer: Clear communication prevents errors, builds relationships, and ensures understanding among all parties
Effective communication is essential for coordination, error prevention, and stakeholder relationships.
Question 11: What is the return type of `Map.entrySet()`?
- Set<K, V>
- List<Map.Entry<K,V>>
- Collection<Map.Entry<K,V>>
- Set<Map.Entry<K,V>> (Correct answer)
Correct answer: Set<Map.Entry<K,V>>
`Map.entrySet()` returns a `Set<Map.Entry<K,V>>` containing all key-value pair entries in the map.
Question 12: What is the result of the expression `10 % 3` in Java?
- 3.33
- 3
- 1 (Correct answer)
- 0
Correct answer: 1
The `%` operator returns the remainder of integer division, so 10 divided by 3 leaves a remainder of 1.
Question 13: What makes a professional competent in Exception Handling?
- Only certifications matter
- Only experience matters
- Only formal education matters
- A combination of education, practical experience, ongoing learning, and adherence to professional standards (Correct answer)
Correct answer: A combination of education, practical experience, ongoing learning, and adherence to professional standards
Competence requires the integration of knowledge, skills, experience, and professional conduct.
Question 14: Which `Map` method returns the value associated with a key, or a default if the key is absent?
- merge()
- getOrDefault() (Correct answer)
- computeIfAbsent()
- putIfAbsent()
Correct answer: getOrDefault()
`Map.getOrDefault(key, defaultValue)` returns the mapped value or the specified default without modifying the map.
Question 15: Which access modifier makes a member accessible to subclasses in different packages but not to unrelated classes?
- default
- private
- public
- protected (Correct answer)
Correct answer: protected
The 'protected' access modifier allows access from subclasses regardless of package and from classes within the same package.
Question 16: Which of the following best describes method overloading?
- Same method name, same parameter list, different return type
- A subclass providing its own implementation of a parent method
- Same method name, different parameter lists in the same class (Correct answer)
- Using the `@Override` annotation on a method
Correct answer: Same method name, different parameter lists in the same class
Overloading means defining multiple methods with the same name but different parameter signatures in the same class.
Question 17: How should ethical dilemmas be handled in Exception Handling?
- Follow established ethical guidelines, consult with supervisors, and prioritize stakeholder welfare (Correct answer)
- Make decisions based solely on personal preference
- Always choose the cheapest option
- Ignore ethical concerns
Correct answer: Follow established ethical guidelines, consult with supervisors, and prioritize stakeholder welfare
Ethical decision-making requires following professional guidelines and prioritizing the welfare of all stakeholders.
Question 18: What is the role of the `java.base` module in the Java module system?
- It is the foundational module that every other module implicitly requires (Correct answer)
- It contains only the java.lang package
- It must be explicitly listed in every module-info.java
- It is an optional module for backward compatibility
Correct answer: It is the foundational module that every other module implicitly requires
`java.base` defines the core JDK APIs and is implicitly required by all modules; you never need to declare `requires java.base`.
Question 19: Which operator is used for short-circuit logical AND in Java?
- && (Correct answer)
- &
- |
- AND
Correct answer: &&
The `&&` operator performs a short-circuit logical AND, meaning the second operand is not evaluated if the first is false.
Question 20: What is the range of a Java 'byte' data type?
- -32768 to 32767
- -128 to 127 (Correct answer)
- -256 to 255
- 0 to 255
Correct answer: -128 to 127
The byte type is an 8-bit signed integer with a range of -128 to 127.
Question 21: What is the difference between `==` and `.equals()` when comparing String objects in Java?
- `==` compares content; `.equals()` compares references
- `==` compares references; `.equals()` compares content (Correct answer)
- Both compare content
- Both compare references
Correct answer: `==` compares references; `.equals()` compares content
`==` compares object references (memory addresses), while `.equals()` on String compares the actual character content.
Question 22: Which keyword is used in Java to create a subclass that inherits from a parent class?
- implements
- inherits
- extends (Correct answer)
- super
Correct answer: extends
The 'extends' keyword is used to create a subclass that inherits fields and methods from a parent class.
Question 23: What are the key regulatory requirements affecting Arrays and Collections?
- There are no regulations
- Only federal regulations apply
- Regulations are optional guidelines
- Compliance with applicable laws, industry standards, and licensing requirements specific to the field (Correct answer)
Correct answer: Compliance with applicable laws, industry standards, and licensing requirements specific to the field
Practitioners must comply with all applicable regulations, which may include federal, state, and industry-specific requirements.
Question 24: What is the scope of a variable declared inside a method body?
- Class scope
- Local scope (Correct answer)
- Package scope
- Global scope
Correct answer: Local scope
A variable declared inside a method has local scope and is accessible only within that method.
Question 25: What is the output of `int i = 0; while(i < 3) { System.out.print(i + " "); i++; }`?
- 1 2 3
- 0 1 2 (Correct answer)
- 0 1 2 3
- 1 2
Correct answer: 0 1 2
The loop prints i (starting at 0) and increments it after each print, running while i < 3, so it outputs 0, 1, and 2.
Question 26: Can a `static` method access instance variables directly?
- No, because static methods belong to the class, not an instance (Correct answer)
- Yes, but only if the instance variable is final
- Yes, if the variable is declared public
- Yes, always
Correct answer: No, because static methods belong to the class, not an instance
Static methods do not have access to instance variables directly because they are not associated with any object instance.
Question 27: Which interface does `HashMap` directly implement?
- LinkedMap
- TreeMap
- Map (Correct answer)
- SortedMap
Correct answer: Map
`HashMap` directly implements the `Map` interface and extends `AbstractMap`.
Question 28: When compiling with modules, which flag adds a module to the set of root modules without editing module-info.java?
- --patch-module
- --upgrade-module-path
- --add-modules (Correct answer)
- --module-path
Correct answer: --add-modules
`--add-modules` lets you include additional modules in the module graph without modifying module descriptors.
Question 29: What does the compound assignment operator `+=` do?
- Checks if the left value is greater than the right
- Adds two values and stores the result in a new variable
- Adds the right operand to the left operand and assigns the result to the left operand (Correct answer)
- Concatenates two strings only
Correct answer: Adds the right operand to the left operand and assigns the result to the left operand
The `+=` operator adds the right operand to the left operand and stores the result back in the left operand (e.g., `x += 3` is equivalent to `x = x + 3`).
Question 30: Which of these correctly declares a `Set` that preserves insertion order?
- new HashSet<>()
- new TreeSet<>()
- new ArrayDeque<>()
- new LinkedHashSet<>() (Correct answer)
Correct answer: new LinkedHashSet<>()
`LinkedHashSet` maintains insertion order using a doubly-linked list alongside the hash table.
Question 31: What is the output of `int x = 5; System.out.println(x++);`?
- 4
- 6
- Compilation error
- 5 (Correct answer)
Correct answer: 5
The post-increment operator `x++` returns the current value of x (5) before incrementing it to 6.
Question 32: Which keyword is used to define a method that belongs to the class rather than an instance?
- static (Correct answer)
- abstract
- final
- void
Correct answer: static
The `static` keyword declares a method that belongs to the class itself and can be called without creating an instance.
Question 33: Which of the following correctly describes the `break` statement in a `switch` block?
- It prevents fall-through to the next case (Correct answer)
- It exits the entire program
- It is automatically inserted by the compiler
- It skips only one case label
Correct answer: It prevents fall-through to the next case
Without a `break` statement, execution falls through to the next case in a switch block; `break` prevents this by exiting the switch.
Question 34: What is the purpose of the "module-info.java" file in a Java module?
- To handle logging and error reporting
- To specify the modules that the current module depends on and the packages it exports (Correct answer)
- To define the entry point of the Java application
- To manage the Java Virtual Machine (JVM) configuration
Correct answer: To specify the modules that the current module depends on and the packages it exports
The `module-info.java` file is the module descriptor in the Java Platform Module System (JPMS). It explicitly defines a module's name, declares its dependencies on other modules using `requires`, and specifies which of its packages are accessible to other modules using `exports`. This file is crucial for enforcing strong encapsulation and reliable configuration in modular applications.
Question 35: What is the scope of a variable declared inside a for-loop initializer?
- Only the loop block and update expression (Correct answer)
- The entire method
- The entire class
- Only the loop body, not the condition
Correct answer: Only the loop block and update expression
A variable declared in the for-loop initializer (e.g., int i=0) is scoped to the entire for statement including its body, condition, and update.
Question 36: How does technology impact modern practices in Exception Handling?
- Technology is only useful for record keeping
- Technology improves efficiency, accuracy, and access to information while requiring new skills (Correct answer)
- Technology replaces all human judgment
- Technology has no impact on this field
Correct answer: Technology improves efficiency, accuracy, and access to information while requiring new skills
Technology enhances capabilities while also requiring practitioners to develop new competencies.
Question 37: What does the "java.util.concurrent" package primarily provide?
- Utilities for file and I/O operations
- Utilities for network communication
- Utilities for database access
- Utilities for concurrent programming and synchronization (Correct answer)
Correct answer: Utilities for concurrent programming and synchronization
The `java.util.concurrent` package is specifically designed to provide a rich set of utilities for concurrent programming in Java. It includes classes for managing threads, thread pools, concurrent data structures (like `ConcurrentHashMap`), and synchronization aids (like `Semaphore` and `CountDownLatch`), making it easier to write robust multi-threaded applications.
Question 38: How does technology impact modern practices in Arrays and Collections?
- Technology replaces all human judgment
- Technology has no impact on this field
- Technology improves efficiency, accuracy, and access to information while requiring new skills (Correct answer)
- Technology is only useful for record keeping
Correct answer: Technology improves efficiency, accuracy, and access to information while requiring new skills
Technology enhances capabilities while also requiring practitioners to develop new competencies.
Question 39: Which approach is most effective for problem-solving in Exception Handling?
- Systematic analysis followed by evidence-based decision making (Correct answer)
- Ignoring the problem until it resolves itself
- Always asking someone else
- Random trial and error
Correct answer: Systematic analysis followed by evidence-based decision making
Systematic, evidence-based approaches lead to more reliable solutions than ad hoc methods.
Question 40: Which statement about 'var' in Java 11 is correct?
- var can be used for local variable type inference (Correct answer)
- var can be used for instance variable declarations
- var can be used for method parameters
- var is a reserved keyword in Java 11
Correct answer: var can be used for local variable type inference
In Java 10+, 'var' enables local variable type inference, allowing the compiler to infer the type from the initializer.
Question 41: Which file in a JAR is responsible for specifying the Main-Class and other metadata?
- WEB-INF/web.xml
- META-INF/module-info.class
- META-INF/services/
- META-INF/MANIFEST.MF (Correct answer)
Correct answer: META-INF/MANIFEST.MF
The `MANIFEST.MF` file inside `META-INF/` stores JAR metadata including `Main-Class`, `Class-Path`, and custom attributes.
Question 42: Which option passed to `javac` enables preview features in Java 11?
- --enable-preview combined with --release 11 (Correct answer)
- --preview-features
- --enable-preview alone
- --experimental
Correct answer: --enable-preview combined with --release 11
Preview features require both `--enable-preview` and `--release <N>` (or `-source` / `-target`) so the compiler knows the exact release context.
Question 43: What is the correct way to declare a singleton class in Java?
- Use the `singleton` keyword in the class declaration
- Make the class abstract with a single subclass
- Declare the class as final with all public constructors
- Make the constructor private and provide a static factory method (Correct answer)
Correct answer: Make the constructor private and provide a static factory method
A singleton restricts instantiation by using a private constructor and a static method that returns the single instance.
Question 44: What is the result of `true || false && false` in Java?
- null
- false
- true (Correct answer)
- Compilation error
Correct answer: true
Due to operator precedence, `&&` is evaluated before `||`, so the expression becomes `true || (false && false)` = `true || false` = `true`.
Question 45: Which statement about the `return` statement is true?
- It must always appear at the very end of a method
- It can only return primitive types
- It can only be used in void methods
- It immediately exits the method and optionally returns a value (Correct answer)
Correct answer: It immediately exits the method and optionally returns a value
The `return` statement immediately terminates method execution and can pass a value back to the caller if the method is non-void.
Question 46: What does it mean for a method to be `final`?
- It cannot accept any parameters
- It must be called last in a program
- It runs only once per application lifecycle
- It cannot be overridden in a subclass (Correct answer)
Correct answer: It cannot be overridden in a subclass
A `final` method cannot be overridden by subclasses, ensuring its behavior remains unchanged in the inheritance hierarchy.
Question 47: When can a subclass override a parent method with a narrower (more specific) return type?
- Always, regardless of types
- Only when the method is declared abstract
- Never — return types must match exactly
- When the return type is a subtype of the parent method's return type (covariant return) (Correct answer)
Correct answer: When the return type is a subtype of the parent method's return type (covariant return)
Java allows covariant return types: the overriding method may return a subtype of the parent method's declared return type.
Question 48: Which operator performs a bitwise AND in Java?
- &&
- ~
- & (Correct answer)
- AND
Correct answer: &
The single `&` operator performs a bitwise AND on integer types, comparing each corresponding bit of two operands.
Question 49: How can you create a class that cannot be subclassed?
- By using the private keyword
- By using the static keyword
- By using the abstract keyword
- By using the final keyword (Correct answer)
Correct answer: By using the final keyword
To prevent a class from being subclassed, you must use the `final` keyword in its declaration. A `final` class cannot be extended, meaning no other class can inherit from it. This is often used for security reasons or to ensure immutability and prevent unintended modifications to class behavior.
Question 50: Which control flow statement is used to exit the current iteration of a loop and proceed to the next?
- break
- return
- continue (Correct answer)
- exit
Correct answer: continue
The `continue` statement skips the rest of the current loop iteration and jumps to the loop condition check for the next iteration.
Question 51: What is the result of `!true && false` in Java?
- true
- Compilation error
- false (Correct answer)
- null
Correct answer: false
`!true` evaluates to `false`, and `false && false` evaluates to `false` due to short-circuit evaluation.
Question 52: Which of the following is true about variable shadowing in Java?
- It causes a compilation error
- It occurs when a local variable has the same name as an instance variable (Correct answer)
- It only applies to primitive types
- It only occurs with static variables
Correct answer: It occurs when a local variable has the same name as an instance variable
Variable shadowing occurs when a local variable or parameter has the same name as an instance variable, hiding the instance variable within that scope.
Question 53: Which statement about abstract methods is correct?
- Abstract methods must have a body
- Abstract methods can be declared static
- Abstract methods can only be declared in interfaces
- Abstract methods have no body and must be implemented by concrete subclasses (Correct answer)
Correct answer: Abstract methods have no body and must be implemented by concrete subclasses
An abstract method has no implementation body and requires any concrete (non-abstract) subclass to provide its implementation.
Question 54: Which method on `Queue` retrieves and removes the head, or returns null if empty?
- element()
- poll() (Correct answer)
- peek()
- remove()
Correct answer: poll()
`Queue.poll()` retrieves and removes the head element, returning null if the queue is empty.
Question 55: Which of the following correctly defines method overloading in Java?
- A method calls itself recursively
- A method is declared in an interface and implemented in a class
- A subclass provides a specific implementation of a method defined in the parent class
- Multiple methods in the same class share the same name but have different parameter lists (Correct answer)
Correct answer: Multiple methods in the same class share the same name but have different parameter lists
Method overloading occurs when multiple methods in the same class have the same name but differ in the number or types of parameters.
Question 56: What is the output of the following code? import java.util.*; <br> public class Test { <br> public static void main(String[] args) { <br> List<String> list = Arrays.asList("A", "B", "C"); <br> list.add("D"); <br> } <br> }
- Runtime error
- Compilation error (Correct answer)
- The list will contain ["A", "B", "C", "D"]
- The list will contain ["A", "B", "C"] and "D" will not be added
Correct answer: Compilation error
The `Arrays.asList()` method returns a fixed-size `List` backed by the original array. This list does not support structural modifications such as adding or removing elements. Attempting to call the `add()` method on this type of list will result in an `UnsupportedOperationException` at runtime, not a compilation error, because the `add` method is syntactically valid but semantically unsupported by the specific list implementation.
Question 57: Which of the following correctly declares a varargs parameter in a method?
- public void print(String... args) (Correct answer)
- public void print(String[] ...args)
- public void print(*String args)
- public void print(varargs String args)
Correct answer: public void print(String... args)
Varargs is declared using an ellipsis (`...`) after the type, allowing a method to accept zero or more arguments of that type.
Question 58: Which of the following is the ternary operator syntax in Java?
- condition :: valueIfTrue ? valueIfFalse
- if(condition) valueIfTrue else valueIfFalse
- condition ? valueIfTrue : valueIfFalse (Correct answer)
- condition ? valueIfFalse : valueIfTrue
Correct answer: condition ? valueIfTrue : valueIfFalse
The ternary operator syntax is `condition ? valueIfTrue : valueIfFalse`, returning the first value when condition is true.
Question 59: What is the result of `String.format("%05d", 42)`?
- "42 "
- " 0042"
- "42000"
- "00042" (Correct answer)
Correct answer: "00042"
The format specifier `%05d` pads the integer with leading zeros to a width of 5, producing "00042".
Question 60: How should ethical dilemmas be handled in Arrays and Collections?
- Always choose the cheapest option
- Ignore ethical concerns
- Make decisions based solely on personal preference
- Follow established ethical guidelines, consult with supervisors, and prioritize stakeholder welfare (Correct answer)
Correct answer: Follow established ethical guidelines, consult with supervisors, and prioritize stakeholder welfare
Ethical decision-making requires following professional guidelines and prioritizing the welfare of all stakeholders.
Oracle Java Foundations (1Z0-811)
The Oracle Java Foundations exam (1Z0-811) validates foundational knowledge of Java programming including core syntax, data types, operators, control flow, object-oriented concepts, arrays, and exception handling. It is the entry-level Oracle Java certification designed for students and beginners new to Java development.
Exam Rules
- You can skip questions and return to them later
- Flag questions for review before submitting
- No feedback shown until you submit the entire exam
- Unanswered questions count as wrong — answer everything
- 10 pretest questions are mixed in and don't affect your score
- Timer auto-submits when time runs out
- Your progress is auto-saved every 30 seconds