Examveda

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);
    }
}

A. 7

B. 8

C. 9

D. 10

Answer: Option B


This Question Belongs to Data Structure >> Miscellaneous On Data Structures

Join The Discussion

Related Questions on Miscellaneous on Data Structures