Java
Java
1. What role does the “transient” keyword serve in JAVA, and how can it
be implemented effectively ?
OR
● Inheritance example: The Vehicle class extends the Engine class, inheriting its fields and
methods. This establishes an "is-a" relationship, where Vehicle "is a" type of Engine.
public class Vehicle extends Engine {
// Fields and methods specific to the Vehicle class
}
● Composition example: The Vehicle class holds an Engine class instance as its
field, forming a "has-a" relationship. This means that Vehicle "has a" an Engine.
3. What output does the code below produce, and what is the reason
behind it?
The code output is "String method Invoked." In Java, null can be assigned
to any object reference type, but it's not an object itself. When overloading
methods, the Java compiler selects the most specific parameter type,
favoring the method with the String input parameter because it's more
import java.io.File;
//Constuctor
this.fileName = fileName;
this.directory = directory;
@Override
if(result) {
if (files != null) {
if (file.isDirectory()) {
// Recursively search in nested directories
searchFile(fileName, file);
} else if (file.getName().equalsIgnoreCase(fileName)) {
return true;
return false;
if (directories != null) {
This approach allows us to search for the file in parallel, reducing the overall
search time.
import java.util.HashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;
lock.writeLock().lock();
try {
map.put(key, value);
} finally {
lock.writeLock().unlock();
lock.readLock().lock();
try {
return map.get(key);
} finally {
lock.readLock().unlock();
}
public boolean containsKey(K key) {
lock.readLock().lock();
try {
return map.containsKey(key);
} finally {
lock.readLock().unlock();
lock.readLock().lock();
try {
return map.size();
} finally {
lock.readLock().unlock();
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
public DistributedCache() {
cache.put(key, entry);
return entry.getValue();
return null;
cache.remove(key);
this.value = value;
this.expirationTime = expirationTime;
V getValue() {
return value;
boolean isExpired() {
private ThreadSafeSingleton() {
Solution :
public class ThreadSafeSingleton {
private ThreadSafeSingleton() {
if (instance == null) {
synchronized (ThreadSafeSingleton.class) {
if (instance == null) {
instance = new ThreadSafeSingleton();
return instance;
Explanation :
1. The class has a private constructor to prevent instantiation from
outside the class.
2. The instance variable is declared as volatile to ensure that changes
made by one thread are immediately visible to other threads.
3. The getInstance() method checks if the instance is null, and if so, it
enters a synchronized block to prevent multiple threads from creating
multiple instances simultaneously.
4. Inside the synchronized block, it checks again if the instance is null to
handle the race condition that might occur if multiple threads reach
the synchronized block at the same time.
5. If the instance is still null inside the synchronized block, it creates a
new instance of the singleton class.
6. Finally, it returns the singleton instance.