Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push operands onto the stack.
DSA Java
if (Character.isDigit(ch)) { stack.[1](ch - '0'); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push removes elements instead of adding.
Using peek only views the top element without adding.
✗ Incorrect
We use stack.push() to add operands to the stack for later operations.
2fill in blank
mediumComplete the code to pop two operands from the stack for an operation.
DSA Java
int val2 = stack.[1]();
int val1 = stack.pop();Attempts:
3 left
💡 Hint
Common Mistakes
Using push instead of pop adds elements instead of removing.
Using peek does not remove the element.
✗ Incorrect
We pop operands from the stack to perform the operation in correct order.
3fill in blank
hardFix the error in the switch case to correctly perform multiplication.
DSA Java
switch(ch) {
case '*':
result = val1 [1] val2;
break;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '-' instead of '*' changes the operation.
Using '/' divides instead of multiplying.
✗ Incorrect
The multiplication operator is '*', so val1 * val2 computes the product.
4fill in blank
hardFill both blanks to correctly apply division and push the result back to the stack.
DSA Java
case '/': result = val1 [1] val2; stack.[2](result); break;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' instead of '/' changes the operation.
Using pop instead of push removes elements instead of adding.
✗ Incorrect
Division uses '/' operator and the result is pushed back using stack.push().
5fill in blank
hardFill all three blanks to complete the postfix evaluation loop with correct operator checks and stack operations.
DSA Java
for (int i = 0; i < expression.length(); i++) { char ch = expression.charAt(i); if (Character.isDigit(ch)) { stack.[1](ch - '0'); } else { int val2 = stack.pop(); int val1 = stack.pop(); int result = 0; switch(ch) { case '+': result = val1 [2] val2; break; case '-': result = val1 - val2; break; case '*': result = val1 * val2; break; case '/': result = val1 / val2; break; } stack.[3](result); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push to add elements.
Using wrong operator for addition.
✗ Incorrect
Operands are pushed with push(), '+' operator is used for addition, and results are pushed back with push().
