What is the output of the following questions 1

Info icon This preview shows pages 13–17. Sign up to view the full content.

View Full Document Right Arrow Icon
What is the output of the following questions? 1) Consider the following code snippet: public void descending(int n) { if (n <= 0) return; System.out.println(n); descending(n-1); } What is the output of descending(6); 6 5 4 3 2 1 2) Consider the following code snippet: public int exp(int x, int m) { if (m == 0) { return 1; } if (m == 1) { return x; } return x * exp(x, m-1); } What is the output of exp(4,3); 64 3) What the following code will print? LinkedList<String> staff = new LinkedList<String>(); staff.addFirst("John"); staff.addFirst("Diana"); staff.addFirst("Tom"); System.out.println(staff.removeFirst()); System.out.println(staff.removeFirst()); System.out.println(staff.removeFirst()); Tom Diana Harry
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4) What will be the output of the program? 5) public class Example 6) { 7) public static void main(String [] args) 8) { 9) double values[] = {- 2.3 , - 1.0 , 0.25 , 4 }; 10) int cnt = 0 ; 11) for ( int x= 0 ; x < values.length; x++) 12) { 13) if (Math.round(values[x] + . 5 ) == Math.ceil(values[x])) 14) { 15) ++cnt; 16) } 17) } 18) System.out.println( "same results " + cnt + " time(s)" ); 19) } 20) } Ans : same results 2 time(s) 5) What will be the output of the program? public class NFE { public static void main(String [] args) { String s = "42" ; try { s = s.concat( ".5" ); double d = Double.parseDouble(s); int x = ( int ) Math.ceil(d); System.out.println(x); } catch (NumberFormatException e) { System.out.println( "bad number" ); } } } Answer : 43
Image of page 14