Question

instructions These questions should test if you have understood the contents of Chapter 18 "Recursion". 1....

instructions

These questions should test if you have understood the contents of Chapter 18 "Recursion".

1. What will this method return if you call it this: xMethod (4)

static int xMethod (int n) { 
  if (n == 1) 
   return 1; 
  else 
   return n + xMethod (n - 1); 
}

1. 10

2. 11

3. 12

4. 9

2. Analyze the following code:

public class Test { 
  public static void main (String [] args) { 
    int [] x = {1, 2, 3, 4, 5}; 
    xMethod (x, 5); 
  } 
  public static void xMethod (int [] x, int length) { 
    System.out.print (x [length - 1] + ""); 
    xMethod (x, length - 1); 
  } 
}

1. The program will display 5 4 3 2 1

2. The program will display 1 2 3 4 5

3. The program will display 5 4 3 2 1 and cast an ArrayIndexOutOfBoundsException

4. The program will display 1 2 3 4 5 and cast an ArrayIndexOutOfBoundsException

3. What must return value be for the isPalindrome to use the help method correctly?

public static boolean isPalindrome (String s) { 
  return isPalindrome (s, 0, s.length () - 1); 
} 
public static boolean isPalindrome (String s, int low, int high) { 
  if (high <= low) 
    return true; 
  else if (s.charAt (low)! = s.charAt (high)) 
    return false; 
  else 
    return _______________________________; 
}

1. isPalindrome (s, low, high - 1)

2. isPalindrome (s)

3. isPalindrome (s, low, high)

4. isPalindrome (s, low + 1, high - 1)

5. isPalindrome (s, low + 1, high)

4. What must the check be for this method to check if the text string is a palindrome?

public static boolean isPalindrome (String s) { 
  if (s.length () <= 1) // Base case 
    return true; 
  else if _____________________________ 
    return false; 
  else 
    return isPalindrome (s.substring (1, s.length () - 1)); 
}

1. (s.charAt (1)! = s.charAt (s.length ()))

2. (s.charAt (0)! = s.charAt (s.length () - 1))

3. (s.charAt (0)! = s.charAt (s.length ()))

4. (s.charAt (1)! = s.charAt (s.length () - 1))

5. With the code of the Tower of Hanoi as shown in the book, how many times will the moveDisks method be called 3 disks?

1. 14

2. 3

3. 7

4. 10

6. Analyze the code for the following program:

A:

public class Test { 
  public static void main (String [] args) { 
    xMethod (5); 
  } 
  public static void xMethod (int length) { 
    if (length> 1) { 
      System.out.print ((length - 1) + ""); 
      xMethod (length - 1); 
    } 
  } 
}

B:

public class Test { 
  public static void main (String [] args) { 
    xMethod (5); 
  } 
  public static void xMethod (int length) { 
    while (length> 1) { 
      System.out.print ((length - 1) + ""); 
      xMethod (length - 1); 
    } 
  } 
}

1. Both programs will print 1 2 3 4 5 for display.

2. A will end normally while B will endlessly.

3. B will end normally while A will endlessly.

4. Both programs will print 5 4 3 2 1 for display.

5. Both programs will print 4 3 2 1 for display.

6. Both programs will print 1 2 3 4 for display.

7. Which of the following are true ?

1. Recursive methods run faster than iterative methods.

2. Recursive methods often take up more memory than iterative methods.

3. Recursive methods can always be replaced with iterative methods.

4. In some cases, recursive methods will provide much more natural and simpler solutions, while other solutions may be ugly and difficult.

8. What will the following class print?

public class Test1 { 
  public static void main (String [] args) { 
    System.out.println (f2 (2, 0)); 
  } 
  public static int f2 (int n, int result) { 
    if (n == 0) 
      return 0; 
    else 
      return f2 (n - 1, n + result); 
  } 
}

1. 0

2. 1

3. 3

4. 2

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

2.
3. The program will display 5 4 3 2 1 and cast an ArrayIndexOutOfBoundsException

3.
4. isPalindrome (s, low + 1, high - 1)

4.
2. (s.charAt (0)! = s.charAt (s.length () - 1))

5.
3. 7

6.
2. A will end normally while B will endlessly.

7.
2. Recursive methods often take up more memory than iterative methods.

8.
1. 0


Add a comment
Know the answer?
Add Answer to:
instructions These questions should test if you have understood the contents of Chapter 18 "Recursion". 1....
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
  • Given the following code: public static void foo3(String s) { if (s.length() >0) { System.out.print(s.charAt(s.length() -1));...

    Given the following code: public static void foo3(String s) { if (s.length() >0) { System.out.print(s.charAt(s.length() -1)); foo3(s.substring(0, s.length() -1)); } } What is the output of: foo3(“”); 2, You coded the following in the file Test.java : System.out.println( foo(5)); //more code here public static int foo(int n) //line 9 { if (n = = 0)    return 1; else    System.out.println(n* foo(n-1) ); }                                    //line 15 At compile time, you get the following error: Text.java: 15: missing return statement }                                ...

  • JAVA Write a method that accepts a String as an argument. The method should use recursion...

    JAVA Write a method that accepts a String as an argument. The method should use recursion to display each individual character in the String. Then, modify the method you just wrote so it displays the String backwards. The following code does not correctly display the String backwards. It merely moves the first character of the String to the end: public static void displayCharacter(String s)    {        if(s.length() == 0)            return;        else       ...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...

  • Debug following methods(longestRun(finds how many times a character got repeated), findLastP(finds last p in a string),...

    Debug following methods(longestRun(finds how many times a character got repeated), findLastP(finds last p in a string), findFirstP(finds first p in a string)) in java language. public class simpleLoops { /** * @param args */ public static void main(String[] args) {    System.out.println(longestRun("aabbbccd"));    System.out.println("Expected 3");    System.out.println(longestRun("aaa"));    System.out.println("Expected 3");    System.out.println(longestRun("aabbbb"));    System.out.println("Expected 4");          int count = countP("Mississippi"); System.out.println(count);    int result = findLastP("Mississippi"); System.out.println(result); result = findFirstP("stop"); System.out.println(result); result = findFirstP("xxxyyyzzz"); System.out.println(result); } /** *...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus";...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println ()

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

    Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...

  • Consider the encryption code  Encryption Code true false - stringContains is an encryption algorithm that can be...

    Consider the encryption code  Encryption Code true false - stringContains is an encryption algorithm that can be decrpyted true false - Running reverseString twice will have no effect true false - Running incLetters(s,4) then incLetters(s,-4) will have no effect true false - using maxDigit as an encryption algorighm, it can be decrypted USE CODE BELOW: package encryption; import java.util.Scanner; public class Encryption { public static void main(String[] args) { Scanner scanner = new Scanner (System.in); String password, encryptedPassword, salt; int increment;...

  • 1 Problem Description Instructions. You are provided one skeleton program named LCS.java . The source files...

    1 Problem Description Instructions. You are provided one skeleton program named LCS.java . The source files are available on Canvas in a folder named HW6 . Please modify the skeleton code to solve the following tasks. • Task 1 (100 pts). Implement the lcs length() function as discussed in Lecture 11. • Note: You should not return the double-array b and c as in the pseu- docode. Instead, return the length of the longest common subsequence. • Hint: To get...

  • (20 pts) Fill in the missing code: This recursive method returns “even” if the length of...

    (20 pts) Fill in the missing code: This recursive method returns “even” if the length of a give String is even, and “odd” if the length of the String is odd. public static String foo(String s) { if (s.length() ==0)    return “even”; else if (s.length() = = 1)    return “odd”; else     //your code goes here } (40 pts) You coded the following in the file Test.java : System.out.println( foo(5)); //more code here public static int foo(int n)...

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