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 (7th 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
- 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_forwardpublic class Test { } public static void main(String[] args) { String str = "Salom"; System.out.println(str.indexOf('t)); }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_forward
- light } 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_forwardFind 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_forward//Assignment 06 */public static void main[](String[] args) { String pass= "ICS 111"; System.out.printIn(valPassword(pass));} /* public static boolean valPassword(String password){ if(password.length() > 6) { if(checkPass(password) { return true; } else { return false; } }else System.out.print("Too small"); return false;} public static boolean checkPass (String password){ boolean hasNum=false; boolean hasCap = false; boolean hasLow = false; char c; for(int i = 0; i < password.length(); i++) { c = password.charAt(1); if(Character.isDigit(c)); { hasNum = true; } else if(Character.isUpperCase(c)) { hasCap = true; } else if(Character.isLowerCase(c)) { hasLow = true; } } return true; { return false; } }arrow_forward
- class 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_forwardJava Input class main { public static void main(String args[]) { byte x = 64; int i; byte y; i = x << 2; y = (byte) (x << 2) System.out.print(i + " " + y); } } Find outputarrow_forwardclass isinfinite_output { public static void main(String args[]) { Double d = new Double(1 / 0.); boolean x = d.isInfinite(); System.out.print(x); } } What will the given code prints on the output screen.arrow_forward
- What missing code would generate output as: Z--Y---Z-Aarrow_forwardclass Variables { public static void main(String[] args) { .arrow_forward1- public class Con{ public static void main(String[] args) int[] ar=(2,5,7,9}; int i=2; final int j=1; 6. for (int x:ar) 8- { 9 if(ar[i]arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- 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