Question: 10. What is the output of the following Java code?<div class="hk1_style-wrap4"><div class="hk1_style-wrap3"><div class="hk1_style-wrap2"><div class="hk1_style-wrap"><div class="hk1_style"><div class="java"><pre class="de1"><span class="kw1">public</span> <span class="kw1">class</span> CircularQueue <span class="br0">{</span> <span class="kw1">protected</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw4">int</span> CAPACITY <span class="sy0">=</span> <span class="nu0">100</span><span class="sy0">;</span> <span class="kw1">protected</span> <span class="kw4">int</span> size,front,rear<span class="sy0">;</span> <span class="kw1">protected</span> <span class="kw3">Object</span> q<span class="br0">[</span><span class="br0">]</span><span class="sy0">;</span> <span class="kw4">int</span> count <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> &nbsp; <span class="kw1">public</span> CircularQueue<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">this</span><span class="br0">(</span>CAPACITY<span class="br0">)</span><span class="sy0">;</span> <span class="br0">}</span> <span class="kw1">public</span> CircularQueue <span class="br0">(</span><span class="kw4">int</span> n<span class="br0">)</span> <span class="br0">{</span> size <span class="sy0">=</span> n<span class="sy0">;</span> front <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> rear <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> q <span class="sy0">=</span> <span class="kw1">new</span> <span class="kw3">Object</span><span class="br0">[</span>size<span class="br0">]</span><span class="sy0">;</span> <span class="br0">}</span> &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> enqueue<span class="br0">(</span><span class="kw3">Object</span> item<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">if</span><span class="br0">(</span>count <span class="sy0">==</span> size<span class="br0">)</span> <span class="br0">{</span> <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">(</span><span class="st0">"Queue overflow"</span><span class="br0">)</span><span class="sy0">;</span> <span class="kw1">return</span><span class="sy0">;</span> <span class="br0">}</span> <span class="kw1">else</span> <span class="br0">{</span> q<span class="br0">[</span>rear<span class="br0">]</span> <span class="sy0">=</span> item<span class="sy0">;</span> rear <span class="sy0">=</span> <span class="br0">(</span>rear<span class="sy0">+</span><span class="nu0">1</span><span class="br0">)</span><span class="sy0">%</span>size<span class="sy0">;</span> count<span class="sy0">++;</span> <span class="br0">}</span> <span class="br0">}</span> <span class="kw1">public</span> <span class="kw3">Object</span> dequeue<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">if</span><span class="br0">(</span>count <span class="sy0">==</span> <span class="nu0">0</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">(</span><span class="st0">"Queue underflow"</span><span class="br0">)</span><span class="sy0">;</span> <span class="kw1">return</span> <span class="nu0">0</span><span class="sy0">;</span> <span class="br0">}</span> <span class="kw1">else</span> <span class="br0">{</span> <span class="kw3">Object</span> ele <span class="sy0">=</span> q<span class="br0">[</span>front<span class="br0">]</span><span class="sy0">;</span> q<span class="br0">[</span>front<span class="br0">]</span> <span class="sy0">=</span> <span class="kw2">null</span><span class="sy0">;</span> front <span class="sy0">=</span> <span class="br0">(</span>front<span class="sy0">+</span><span class="nu0">1</span><span class="br0">)</span><span class="sy0">%</span>size<span class="sy0">;</span> count<span class="sy0">--;</span> <span class="kw1">return</span> ele<span class="sy0">;</span> <span class="br0">}</span> <span class="br0">}</span> <span class="kw1">public</span> <span class="kw3">Object</span> frontElement<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">if</span><span class="br0">(</span>count <span class="sy0">==</span> <span class="nu0">0</span><span class="br0">)</span> <span class="kw1">return</span> <span class="sy0">-</span><span class="nu0">999</span><span class="sy0">;</span> <span class="kw1">else</span> <span class="br0">{</span> <span class="kw3">Object</span> high<span class="sy0">;</span> high <span class="sy0">=</span> q<span class="br0">[</span>front<span class="br0">]</span><span class="sy0">;</span> <span class="kw1">return</span> high<span class="sy0">;</span> <span class="br0">}</span> <span class="br0">}</span> <span class="kw1">public</span> <span class="kw3">Object</span> rearElement<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">if</span><span class="br0">(</span>count <span class="sy0">==</span> <span class="nu0">0</span><span class="br0">)</span> <span class="kw1">return</span> <span class="sy0">-</span><span class="nu0">999</span><span class="sy0">;</span> <span class="kw1">else</span> <span class="br0">{</span> <span class="kw3">Object</span> low<span class="sy0">;</span> rear <span class="sy0">=</span> <span class="br0">(</span>rear<span class="sy0">-</span><span class="nu0">1</span><span class="br0">)</span><span class="sy0">%</span>size<span class="sy0">;</span> low <span class="sy0">=</span> q<span class="br0">[</span>rear<span class="br0">]</span><span class="sy0">;</span> rear <span class="sy0">=</span> <span class="br0">(</span>rear<span class="sy0">+</span><span class="nu0">1</span><span class="br0">)</span><span class="sy0">%</span>size<span class="sy0">;</span> <span class="kw1">return</span> low<span class="sy0">;</span> <span class="br0">}</span> <span class="br0">}</span> <span class="br0">}</span> <span class="kw1">public</span> <span class="kw1">class</span> CircularQueueDemo <span class="br0">{</span> <span class="kw1">public</span> <span class="kw1">static</span> <span class="kw4">void</span> main<span class="br0">(</span><span class="kw3">String</span> args<span class="br0">[</span><span class="br0">]</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw3">Object</span> var<span class="sy0">;</span> CircularQueue myQ <span class="sy0">=</span> <span class="kw1">new</span> CircularQueue<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span> myQ.<span class="me1">enqueue</span><span class="br0">(</span><span class="nu0">10</span><span class="br0">)</span><span class="sy0">;</span> myQ.<span class="me1">enqueue</span><span class="br0">(</span><span class="nu0">3</span><span class="br0">)</span><span class="sy0">;</span> var <span class="sy0">=</span> myQ.<span class="me1">rearElement</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span> myQ.<span class="me1">dequeue</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span> myQ.<span class="me1">enqueue</span><span class="br0">(</span><span class="nu0">6</span><span class="br0">)</span><span class="sy0">;</span> var <span class="sy0">=</span> mQ.<span class="me1">frontElement</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span> <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">(</span>var<span class="sy0">+</span><span class="st0">" "</span><span class="sy0">+</span>var<span class="br0">)</span><span class="sy0">;</span> <span class="br0">}</span> <span class="br0">}</span></pre></div></div></div></div></div></div>
Options:
3 3
3 6
6 6
10 6
Answer: A