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
- Input 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_forwardExamine 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_forwardpublic class Test { } public static void main(String[] args) { String str = "Salom"; System.out.println(str.indexOf('t)); }arrow_forward
- 2. 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_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
- 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_forward15. What is the output of the program given below? public class Temp { public static void main(String[] args) { int x = 1, y = 2, z = 3; int ans = func1 (x, y) - z; System.out.println (ans); } } public static int funcl (int a, int b) { } 0 Error 02 int f = a + b; 05 return f;arrow_forward
- Java - How do I output this statement as a horizontal statement with a newline? Everytime I use System.out.println, it makes the entire output vertical instead of horizontal. Program below. import java.util.Scanner; public class LabProgram { public static void main(String[] args) { int x; int y; Scanner scan = new Scanner(System.in); x = scan.nextInt(); y = scan.nextInt(); if(y >= x){ for(int i=x; i<=y; i = i + 5) { System.out.print(i+ " "); (The problem line) } } else{ System.out.print("Second integer can't be less than the first."); } }}arrow_forwardThe following code segment has an error. Find the error and suggest a correction.arrow_forwardpublic String getJoblitle() { return joblitle; 11 12 13 44 45 46 47 48 49 50 public void setJoblitle(String joblitle) { this.jobTitle - joblitle; public int getCurrentEmploynent() { return currentEmploynent; 1. public void selCurrentEnployent (int current Fmployment) { this.currentEmploynent - currentEmployment; 53 54 55 56 -57 goverride public String tostring () { return "CurrentEmploynentStatsclass [areaName=" + arealame + 58 + "year=" + year + ", jobtitle-" + joblitle , currentEnploynent-" + currenttmployment + "]": 59 61 62 63 64 } What algorithm does this method perform? public statie Currentiploynentstatsclass mysteryethod (CurrentEmploynentstatsclass] array) ( int holder = array[0).getCurrenttmploynent(); Currenttmploynentstatsclass variable = array[®]; for(int i- 0, i holder) { holder = holderx; variable - array[1]; return variable; 36. What is the above method returning? 37. What is the Big O time of the above method?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