What output will be produced by the following code?
public class Demo
{
public static void main (String[] args)
{
System.out.println (“The output is:”);
foo (23);
System.out. println ();
}
public static void foo (int number)
{
if (number > 0)
{
foo (number / 2);
System.out.print (number % 2);
}
}
}
Explanation of Solution
Given code:
//Definition of class "Demo"
public class Demo
{
//Definition of main class
public static void main(String[] args)
{
//Print the statement
System.out.println("The output is:");
//Call the method "foo()"
foo(23);
//Print new line
System.out.println();
}
//Definition of method "foo"
public static void foo(int number)
{
//Check the condition
if (number > 0)
{
//Call the method "foo()"
foo(number / 2);
//Print the output
System.out.print(number % 2);
}
}
}
Explanation:
- • In the above code, it calls the method “foo()” in the main class.
- • The method “foo()” is defined.
- ○ Inside the method, it checks the condition “number>0”, if the condition satisfies recursively calls the method “foo()”.
- ○ Print the result after determining “number%2”.
Output:
The output is:
10111
Want to see more full solutions like this?
Chapter 11 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Absolute Java (6th Edition)
Starting Out With Visual Basic (7th Edition)
Concepts Of Programming Languages
Starting Out with C++: Early Objects
Starting Out with C++: Early Objects (9th Edition)
Artificial Intelligence: A Modern Approach
- using System;class Program{ public static void Main(string[] args) { int number = 1; while (number <= 1000) { Console.WriteLine(number); number = number + 1; } }} how can this code be fixed to count from 1-20 and then 50-70 and then from 800-1000?arrow_forwardInput class Output { public static void main(String args[]) { int x , y = 1; x = 10; if (x != 10 && x / 0 == 0) System.out.println(y); else System.out.println(++y); } } Find outputarrow_forwardclass operators { public static void main(String args[]) { int x = 8; System.out.println(++x * 3 + " " + x); } } Give result for the code Java Try to do ASAP ?arrow_forward
- Examine this code and determine what happens when it is run: 1 public class Test { int x; 2 3 public Test(String t){ System.out.println("Test"); } public static void main(String[] args) { Test test = new Test ("boo"); System.out.println(test.x); } 4 5 6 7 8 10 } The program has a compile error because Test does not have a default constructor. The program has a compile error because System.out.println method cannot be invoked from the constructor. ) The program runs successfully and prints out: Test O The program has a compile error because you cannot create an object from the class that defines the object.arrow_forwardusing System;class Program{ public static void Main(string[] args) { int number = 1; while (number <= 500) { Console.WriteLine(number); number = number + 1; } }} how would you add the phrase "I found 275!" into this code when it reaches that number?arrow_forwardpublic class Test { } public static void main(String[] args) { String str = "Salom"; System.out.println(str.indexOf('t)); }arrow_forward
- Question public class MyProgram { if (h 18) { public static void main(String[] args) { int h=10, t-22; System.out.print ("Good "); } } else } } What is the output displayed by the following lines of code?" System.out.print ("Morning"); t = t + 2; if (t > 30) System.out.println(" Every one!"); System.out.println("The Your answer system.out.print("Evening"); t = t + 5; temperature is " System.out.println(" Brink water?"); Degree.");arrow_forward2. What is the value of x and y after executing the following program? public class MysteryNums { public static void main(String[] args) { int x = 15; sentence(x , 4); int y = x - 5; sentence(y , x ); System.out.println(x + " & " + y); } %3D %3D public static void sentence(int num1, int num2) { num1 = num1 + num2; } }arrow_forwardlight } What is the output of this Java program? } class Driver { public static void main(String[] args) { int a = 6: int b = 6; int c = 5; int x = 1; if (a > 2) { } } if x = x + 600; (b> 3) { x = x + 60; } if (c < 9) { X = x + 5; System.out.print(x);arrow_forward
- Find error and correct it : public class ProgramLayout { public static void main (String[] args) { final double PI = 3.14159; double x; double sum = x+PI ; System.out.println (sum); } // end classarrow_forwardclass Lab4Q1P3 { public static void main(String args[]) { int max = 10;while (max < 10) {System.out.println("count down: " + max);max--;}}} Do you know what is wrong with the program? I know that there is error on the 7th line but I don't know what to fix.arrow_forwardof code and give their exact output with the tracing steps.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education