1Z0-819 Java Fundamentals & Object-Oriented Programming — Questions and Answers
Question 1: What is the main purpose of the Java Virtual Machine (JVM)?
- To compile Java source code.
- To convert Java bytecode to machine code at runtime (Correct answer)
- To write Java programs.
- To store Java libraries.
Correct answer: To convert Java bytecode to machine code at runtime
The Java Virtual Machine (JVM) is responsible for executing Java bytecode. After Java source code is compiled into bytecode, the JVM translates this platform-independent bytecode into machine-specific instructions at runtime. This 'write once, run anywhere' capability is a core feature of Java, enabled by the JVM's role as an interpreter and runtime environment.
Question 2: Which keyword is used to create a subclass in Java?
- inherits
- instanceof
- extends (Correct answer)
- super
Correct answer: extends
In Java, the `extends` keyword is used to establish an inheritance relationship between classes. When a class `A` `extends` class `B`, `A` becomes a subclass (or child class) of `B`, inheriting its public and protected methods and fields. This promotes code reuse and establishes an 'is-a' relationship.
Question 3: What is encapsulation in object-oriented programming?
- Exposing all class variables directly.
- Using private fields with public getters/setters (Correct answer)
- Avoiding methods in a class.
- Extending multiple classes.
Correct answer: Using private fields with public getters/setters
Encapsulation is an object-oriented programming principle that bundles data (fields) and methods that operate on the data within a single unit (a class). It involves restricting direct access to some of an object's components, typically by making fields `private`. Access to these private fields is then controlled through public `getter` and `setter` methods, ensuring data integrity and controlled modification.
Question 4: What does the 'static' keyword denote in Java?
- Instance-level access.
- Class-level access without needing an object (Correct answer)
- A method that returns null.
- A variable that cannot be changed.
Correct answer: Class-level access without needing an object
The `static` keyword in Java indicates that a member (field or method) belongs to the class itself, rather than to any specific instance of the class. This means you can access `static` members directly using the class name, without needing to create an object of that class. `static` methods can only access `static` variables and call `static` methods.
Question 5: Which of the following is not a Java access modifier?
- public
- protected
- friendly
- private
Java has four primary access modifiers: `public`, `protected`, `private`, and the default (package-private, which has no keyword). The term 'friendly' is sometimes informally used to describe the default access level, but it is not a reserved keyword or a formal access modifier in Java. Therefore, `friendly` is not a Java access modifier.
Question 6: What is polymorphism in Java?
- Using multiple classes in one file.
- Compiling code to multiple platforms.
- One interface, many implementations (Correct answer)
- Writing code with multiple threads.
Correct answer: One interface, many implementations
Polymorphism, meaning 'many forms,' is a core OOP concept allowing objects to take on multiple forms. In Java, this often manifests as an object of a subclass being treated as an object of its superclass, or an object implementing an interface being treated as an object of that interface. This enables a single interface or method to be used for different underlying data types or implementations, promoting flexibility and extensibility.
Question 7: What is the result of using 'final' with a variable in Java?
- The variable is deleted.
- The variable can be changed later.
- The variable is assigned only once (Correct answer)
- The variable becomes static.
Correct answer: The variable is assigned only once
When the `final` keyword is used with a variable in Java, it means that the variable's value can only be assigned once. After its initial assignment, the value of a `final` variable cannot be changed, making it a constant. This applies to both primitive types (value itself is constant) and reference types (the reference itself is constant, not the object it points to).
Question 8: What is inheritance in Java?
- Copying code between classes.
- Allowing a class to reuse another class's code via 'extends' (Correct answer)
- Running multiple classes simultaneously.
- Preventing method reuse.
Correct answer: Allowing a class to reuse another class's code via 'extends'
Inheritance is a fundamental OOP concept where a new class (subclass or child class) derives properties and behaviors from an existing class (superclass or parent class). In Java, this is achieved using the `extends` keyword. It promotes code reusability, allowing subclasses to inherit and potentially override methods and fields from their superclass, establishing an 'is-a' relationship.
Question 9: Which interface must be implemented to sort objects in Java?
- Serializable
- Runnable
- Cloneable
- Comparable (Correct answer)
Correct answer: Comparable
To sort objects in Java based on their 'natural order,' the class of these objects must implement the `Comparable` interface. This interface requires the implementation of the `compareTo()` method, which defines how instances of the class should be ordered relative to each other. For custom sorting criteria, the `Comparator` interface is used.
What is the main purpose of the Java Virtual Machine (JVM)?