๐ฃ๏ธโ2๏ธโฃ1๏ธโฃ Dear followers, let's prepare for Java 21 certification together!
1๏ธโฃ How would you answer this question:
Given:
What is printed?
A) 7
B) 6
C) 0
D) An exception is thrown at runtime.
E) Compilation fails.
#PathToJava21 #java #certification
๐ก Solution
The given code contains an issue that causes a compilation error. Let's analyze it step by step.
Code Analysis:
BiFunction concat = (s1, s2) -> s1 + s2;
Function length = s -> s.length();
int result = concat.compose(length).apply("Java ", "21");
System.out.println(result);
- 1. Understanding BiFunction:
BiFunction concat = (s1, s2) -> s1 + s2;
concat takes two String parameters (s1, s2) and returns a String (concatenation of the two).
- 2. Understanding Function:
Function length = s -> s.length();
length takes a String and returns its length (Integer).
- 3. Issue with compose Method:
concat.compose(length)
The compose method in Function works as follows:
default Function compose(Function before)
This means compose can only be used on unary functions (Function), not BiFunction.
Since concat is a BiFunction, it does not have a compose method, leading to a compilation error.
- Answer:
Since BiFunction does not have a compose method, compilation fails.
Thus, the correct answer is: E) Compilation fails.
(Working with Streams and Lambda expressions)
2๏ธโฃ How would you answer this question:
Given:
What is printed?
A) true
B) false
C) An exception is thrown at runtime.
D) Compilation fails.
#PathToJava21 #java #certification
๐ก Solution
The given code will fail to compile, so the correct answer is:
D) Compilation fails.
Explanation:
- Enum Constructor Issue:
public FrenchCheese(int agingWeeks) {
Enums in Java cannot have a public or protected constructor.
Enum constructors must be private (or package-private by default).
If you attempt to declare it as public, the compilation fails.
Error Message: The compilation error would be something like:
modifier 'public' not allowed here
- Corrected Code:
To fix the issue, change the constructor to private:
Now, if you run the program, it will print:
false
Because FrenchCheese.BRIE and FrenchCheese.CAMEMBERT are distinct enum instances, even though they have the same agingWeeks value.
If the code were corrected, the correct answer would be B) false, but as given, the answer remains D) Compilation fails.
(Using Object-Oriented Concepts in Java)
3๏ธโฃ How would you answer this question:
Which are legal variable declarations? (choose 2)
A) var a;
B) int a = 1, int b = 2;
C) int a = b = 1;
D) int a = 1, b = 2;
E) int a; var b = (a = 7);
#PathToJava21 #java #certification
๐ก Solution
Let's analyze each option based on the given grammar rules.
Breakdown of the given grammar:
- A FieldDeclaration consists of:
{FieldModifier} UnannType VariableDeclaratorList ;
- A VariableDeclaratorList consists of:
VariableDeclarator {, VariableDeclarator}
- A VariableDeclarator consists of:
VariableDeclaratorId [= VariableInitializer]
- A VariableDeclaratorId consists of:
Identifier [Dims]
- A VariableInitializer allows for an assignment.
Now, let's evaluate each choice:
- A) var a;
var is a reserved keyword in Java, but it requires an initializer when used for type inference.
INVALID because var a; lacks an initializer.
- B) int a = 1, int b = 2;
The declaration includes int twice in a single statement.
INVALID because int should be specified only once before the first variable.
- C) int a = b = 1;
This relies on b being declared beforehand, which is not the case.
INVALID because b is used before being declared.
- D) int a = 1, b = 2;
VariableDeclaratorList allows multiple declarators, and b = 2 is a valid VariableDeclarator.
VALID โ
- E) int a; var b = (a = 7);
a is declared without initialization, but later assigned 7 in (a = 7), which is a valid expression.
var requires an initializer, and (a = 7) evaluates to 7, making it valid.
VALID โ
- Correct answers:
โ D) int a = 1, b = 2;
โ E) int a; var b = (a = 7);
[JLS] Field Declarations: https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-VariableDeclaratorList
(Using Object-Oriented Concepts in Java)
4๏ธโฃ How would you answer this question:
Given:
What should be inserted in placeholders?
A) writeInt, readInt
B) writeByte, readByte
C) writeDouble, readDouble
D) write, read
#PathToJava21 #java #certification
๐ก Solution
The correct answer is:
C) writeDouble, readDouble
Explanation:
- The prices array contains double values.
- DataOutputStream provides methods to write primitive data types in a machine-independent way.
- The corresponding method to write a double value is writeDouble(double v).
- When reading the values back, DataInputStream provides the readDouble() method to correctly retrieve double values.
Why the other options are incorrect:
- A) writeInt, readInt
writeInt(int v) and readInt() work with int values, not double.
Using them would result in data corruption or a EOFException due to incorrect interpretation.
- B) writeByte, readByte
writeByte(int v) and readByte() only handle a single byte (8 bits).
double values require 8 bytes, so this would lead to incorrect data storage and retrieval.
- D) write, read
There is no write(double) or read(double) method in DataOutputStream and DataInputStream.
The correct methods for double values are writeDouble() and readDouble().
Thus, the correct choice is C) writeDouble, readDouble.
(Using Object-Oriented Concepts in Java)
5๏ธโฃ How would you answer this question:
Given:
What will be printed?
A) Nothing
B) TGV
C) An exception is thrown at runtime.
D) Class TGV does not compile.
E) Class Eurostar does not compile.
#PathToJava21 #java #certification
๐ก Solution
The given code results in a compilation error. Let's analyze why.
Code Analysis:
* Class TGV (Base Class)
package tgv;
public class TGV {
protected String name = "TGV";
}
- The name variable is protected, meaning it can be accessed:
1. Within the same package (tgv).
2. By subclasses (even if they are in a different package).
* Class Eurostar (Subclass in Another Package)
package tgv.international;
import tgv.TGV;
public class Eurostar extends TGV {
public static void main( String[] args ) {
TGV eurostar = new Eurostar();
System.out.print(eurostar.name);
}
}
- The issue is in the following line:
System.out.print(eurostar.name);
- Here, eurostar is declared as TGV, not Eurostar.
- Even though Eurostar extends TGV, protected members cannot be accessed through a superclass reference outside the package.
- Protected members can only be accessed within a subclass using this or a direct subclass reference.
* Why Does This Cause a Compilation Error?
- If you modify the code to use this.name within Eurostar, it would work:
System.out.print(((Eurostar) eurostar).name);
or
System.out.print(new Eurostar().name);
- But as written, eurostar.name is not accessible from a superclass reference outside the package.
* Correct Answer:
๐ E) Class Eurostar does not compile.
Controlling Access to Members of a Class: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
(Using Object-Oriented Concepts in Java)
6๏ธโฃ How would you answer this question:
You increased some thread priorities while lowering others, but lower-priority threads rarely run.
What concurrency concept describes this phenomenon?
A) Deadlock
B) Livelock
C) Race condition
D) Starvation
#PathToJava21 #java #certification
https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A
๐ก Solution
The correct answer is D) Starvation.
Explanation:
Starvation occurs when lower-priority threads are unable to execute because higher-priority threads keep consuming CPU time.
In many systems, the thread scheduler favors higher-priority threads, potentially preventing lower-priority ones from running, leading to starvation.
To mitigate starvation, you can:
- Use fair scheduling algorithms that ensure all threads get CPU time.
- Implement priority aging, where long-waiting low-priority threads gradually increase in priority.
- Use thread pools or fair locks (e.g., ReentrantLock with fairness enabled in Java).
Why are the other options incorrect?
A) Deadlock โ Happens when two or more threads wait on each other indefinitely, preventing progress.
B) Livelock โ Threads keep responding to each otherโs actions but make no real progress.
C) Race Condition โ Occurs when multiple threads access shared resources unpredictably, leading to inconsistent results.
https://docs.oracle.com/javase/tutorial/essential/concurrency/starvelive.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html
https://blogs.oracle.com/javamagazine/post/java-thread-synchronization-raceconditions-locks-conditions
(Managing Concurrent Code Execution)