Consider a class that uses the following variables to implement an array-based stack:

```
String [] s = new String[100];
int top = -1; // Note top == -1 indicates stack is empty

```

a method that implements a String peek() operation can be written as
A)
if (top == -1)
throw new RuntimeException("Empty Stack");
else
return s[top -1];
B)
if (top > -1)
return s[top];
else
throw new RuntimeException("Empty Stack");
C)
top--;
if (top == -1)
throw new RuntimeException("Empty Stack");
else
return s[top];
D)
if (top == 0)
throw new RuntimeException("Empty Stack");
else
{
top--;
return s[top];
}

B)
if (top > -1)
return s[top];
else
throw new RuntimeException("Empty Stack");

Computer Science & Information Technology

You might also like to view...

The procedure to install and configure BranchCache is the same for all the types of content you may want to cache.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

____ can be used to alter the assigned operator priority and improve the readability of relational expressions.

A. Colons B. Semicolons C. Parentheses D. Braces

Computer Science & Information Technology