Question

Predict the output generated at the marked println lines in the following program. The pro- gram...

  1. Predict the output generated at the marked println lines in the following program. The pro- gram makes use of the class Prob1. Please enter each answer in the space provided.

import java.util.Stack; public class Prob1 {

   public static int mystery(int m,
      if(n == 0) return 0;
      if(n % 2 == 0)
         return mystery(m+m, n/2);
      return mystery(m+m, n/2) + m;

}

int n) {

args) {
29)); //------------------(a)

public static void main(String[] System.out.println(mystery(2,

String str = "Harry Potter";
str.toLowerCase();
str.substring(0, 7);
System.out.println(str); //-----------------------------(b)

Stack<Integer> stack = new Stack<>(); int n = 50;
while (n > 0) {

stack.push(n % 2);

n = n/2;

}

while (!stack.isEmpty())
System.out.print(stack.pop()); //--------------------(c)

System.out.println();

} }

(a) What is printed at line (a)?

Answer:

(b) What is the output at (b)?

Answer:

(c) What is printed at line (c)?

Answer:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code and Answer:-

import java.util.Stack;
public class Prob1 {

   public static int mystery(int m,int n) {
       if(n == 0) return 0;
       if(n % 2 == 0)
           return mystery(m+m, n/2);
       return mystery(m+m, n/2) + m;
   }
   public static void main(String[] args) {
       System.out.println(mystery(2,29));//(a)58
       String str = "Harry Potter";
       str.toLowerCase();
       str.substring(0, 7);
       System.out.println(str); //(b)Harry Potter
       Stack<Integer> stack = new Stack<>();
       int n = 50;
       while (n > 0) {
           stack.push(n % 2);
           n = n/2;
       }
       while (!stack.isEmpty())
           System.out.print(stack.pop()); //(c)110010
           System.out.println();
      
   }
}

Output:-

(a) What is printed at line (a)?

Answer:58

(b) What is the output at (b)?

Answer:Harry Potter

(c) What is printed at line (c)?

Answer:110010

Output On Eclipse IDE:-

Explanation:-

Firstly I predict the answer and verify answer for you ,i have check this program on eclipse IDE.

Thank You Sir/Madam.Have a Great Day!!!!

Add a comment
Know the answer?
Add Answer to:
Predict the output generated at the marked println lines in the following program. The pro- gram...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is wrong with my code, when I pass in 4 It will not run, without...

    What is wrong with my code, when I pass in 4 It will not run, without the 4 it will run, but throw and error. I am getting the error   required: no arguments found: int reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class LinkedDropOutStack public class Help { /** * Program entry point for drop-out stack testing. * @param args Argument list. */ public static void main(String[] args)...

  • Convert infix to postfix, and evaluate postfix using custom Stack created using a singly linked list....

    Convert infix to postfix, and evaluate postfix using custom Stack created using a singly linked list. This is only supposed to use THAT method, calling a normal Stack will give me a zero. I do have the conversion to postfix, but there may be error in there. But the main problem currently is the evaluation of postfix. I keep getting an error that I made for an empty stack, which I will include. For testing it is only supposed to...

  • 12. Predict the output generated at the marked println lines in the following program. The program...

    12. Predict the output generated at the marked println lines in the following program. The program makes use of the class Employee that is also given. Please enter your answers in the space provided below the code. public class Employee { private String name; private double salary; public Employee(String name, double salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public double getSalary() { return salary; } public void raiseSalary(double percent) { double...

  • Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of...

    Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...

  • Here's the problem that I have to solve: Write a Java program that uses a Stack...

    Here's the problem that I have to solve: Write a Java program that uses a Stack data structure to evaluate postfix expressions. Your program takes a postfix expression as an input,for example:3 42.3+ 5.25* ,from the user and calculates/display the result of the expression. What I'm having trouble is getting it to reading and calculating it as postfix Here's my code: import java.util.Scanner; public class Assignment2 {     public static void main(String[] args)     {         Scanner scan = new...

  • JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array...

    JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...

  • Question 8 Identify the COMPILER ERRORS and EXCEPTIONS in this program. This program is saved in...

    Question 8 Identify the COMPILER ERRORS and EXCEPTIONS in this program. This program is saved in a file named MyProgram.java. public class MYProgram { public static void main( String [] args ) { int [] arrayOfIntegers = { 15, 20, 30 50, 60 }; for( int i = 0; i <= arrayOfIntegers.length(); i++ ) { System.out.println( "Hello" + i * 2 ) int i = 100 * arrayOfIntegers[ i ]; doSomething( i ); } } Public static void DoSomething( )...

  • *JAVA* Can somebody take a look at my current challenge? I need to throw a different...

    *JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> {    private int top, size;    private E arrS[];    private static final int MAX_STACK_SIZE = 10;    public ArrayStack() {        this.arrS = (E[]) new Object[MAX_STACK_SIZE];        this.top = size;        this.size = 0;...

  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • Im try to create a java program that checks to see if a given boolean expression...

    Im try to create a java program that checks to see if a given boolean expression is a tautology or not my code so far is as follows: public static class TreeNode    {        char data;        TreeNode left;        TreeNode right;               TreeNode(char item)        {            data = item;            left = null;            right = null;        }    } public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT