What will be the output of the following code in java?
import java.util.ArrayList;
public class LRU {
public static void main(String[] args) {
int page_frame = 3;
int page_ref_string[] = {1, 2, 4, 1, 0, 3, 2, 0, 5, 4};
ArrayList<Integer> s=new ArrayList<>(page_frame);
int count=0;
int page_faults=0;
for(int i:page_ref_string)
{
if(!s.contains(i))
{
if(s.size()==page_frame)
{
s.remove(0);
s.add(page_frame-1,i);
}
else
s.add(count,i);
page_faults++;
++count;
}
else
{
s.remove((Object)i);
s.add(s.size(),i);
}
}
System.out.println(page_faults);
}
}
import java.util.ArrayList;
public class LRU {
public static void main(String[] args) {
int page_frame = 3;
int page_ref_string[] = {1, 2, 4, 1, 0, 3, 2, 0, 5, 4};
ArrayList<Integer> s=new ArrayList<>(page_frame);
int count=0;
int page_faults=0;
for(int i:page_ref_string)
{
if(!s.contains(i))
{
if(s.size()==page_frame)
{
s.remove(0);
s.add(page_frame-1,i);
}
else
s.add(count,i);
page_faults++;
++count;
}
else
{
s.remove((Object)i);
s.add(s.size(),i);
}
}
System.out.println(page_faults);
}
}A. 7
B. 8
C. 9
D. 10
Answer: Option B
Related Questions on Miscellaneous on Data Structures
Which data structure is used to implement a binary heap efficiently?
A. Array
B. Linked List
C. Stack
D. Queue
In which scenario would you use a Bloom Filter?
A. For implementing a stack-based algorithm
B. To maintain a balanced binary tree
C. For efficient sorting of elements
D. To test membership in a large dataset
A. Queue
B. Stack
C. Heap
D. Array

Join The Discussion