Question: 4. What is the output of the following program?<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> <span class="kw3">Stack</span> <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,top <span class="sy0">=</span> <span class="sy0">-</span><span class="nu0">1</span><span class="sy0">;</span> <span class="kw1">protected</span> <span class="kw3">Object</span> stk<span class="br0">[</span><span class="br0">]</span><span class="sy0">;</span> &nbsp; <span class="kw1">public</span> <span class="kw3">Stack</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> stk <span class="sy0">=</span> <span class="kw1">new</span> <span class="kw3">Object</span><span class="br0">[</span>CAPACITY<span class="br0">]</span><span class="sy0">;</span> <span class="br0">}</span> &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> push<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>size_of_stack<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">"Stack 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> top<span class="sy0">++;</span> stk<span class="br0">[</span>top<span class="br0">]</span><span class="sy0">=</span>item<span class="sy0">;</span> <span class="br0">}</span> <span class="br0">}</span> <span class="kw1">public</span> <span class="kw3">Object</span> pop<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">if</span><span class="br0">(</span>top<span class="sy0">&lt;</span><span class="nu0">0</span><span class="br0">)</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="br0">}</span> <span class="kw1">else</span> <span class="br0">{</span> <span class="kw3">Object</span> ele<span class="sy0">=</span>stk<span class="br0">[</span>top<span class="br0">]</span><span class="sy0">;</span> top<span class="sy0">--;</span> size_of_stack<span class="sy0">--;</span> <span class="kw1">return</span> ele<span class="sy0">;</span> <span class="br0">}</span> <span class="br0">}</span> <span class="br0">}</span> &nbsp; <span class="kw1">public</span> <span class="kw1">class</span> StackDemo <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">Stack</span> myStack <span class="sy0">=</span> <span class="kw1">new</span> <span class="kw3">Stack</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span> myStack.<span class="me1">push</span><span class="br0">(</span><span class="nu0">10</span><span class="br0">)</span><span class="sy0">;</span> <span class="kw3">Object</span> element1 <span class="sy0">=</span> myStack.<span class="me1">pop</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span> <span class="kw3">Object</span> element2 <span class="sy0">=</span> myStack.<span class="me1">pop</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>element2<span class="br0">)</span><span class="sy0">;</span> <span class="br0">}</span> <span class="br0">}</span></pre></div></div></div></div></div></div>
Options:
stack is full
20
0
-999
Answer: D