Consider the method powerOfTwo shown below: public boolean powerOfTwo(int n) { if (n == 1) // line #1 { return true; } else if (n % 2 == 1) // line #2 { return false; } else { return powerOfTwo(n / 2); // line #3 } } What is the best interpretation of line #1?
Category: Uncategorized
If an element is present in an array of length n, how many e…
If an element is present in an array of length n, how many element visits, in the worst case, are necessary to find it using a linear search?
Consider the method below, which displays the characters fro…
Consider the method below, which displays the characters from a String in reverse order. Each character appears on a separate line. Select the statement that should be used to complete the method so that it performs a recursive method call correctly. public static void printReverse(String word) { if (word.length() > 0) { ___________________________ System.out.println(word.charAt(0)); } }
Using the given definition of the Measurable interface: publ…
Using the given definition of the Measurable interface: public interface Measurable { double getMeasure(); } Consider the following code snippet, assuming that BankAccount has a getBalance method and implements the Measurable interface by providing an implementation for the getMeasure method: Measurable m = new BankAccount(); System.out.println(m.getBalance()); Which of the following statements is true?
Which of the following classes have a user-editable area?
Which of the following classes have a user-editable area?
Consider the following code snippet: Vehicle aVehicle = new…
Consider the following code snippet: Vehicle aVehicle = new Auto(); aVehicle.moveForward(200); If the Auto class inherits from the Vehicle class, and both classes have an implementation of the moveForward method with the same set of parameters and the same return type, which statement is correct?
You need to access values by an integer position. Which col…
You need to access values by an integer position. Which collection type should you use?
A palindrome is a word or phrase that reads the same forward…
A palindrome is a word or phrase that reads the same forward or backward. Consider the following code snippet: public boolean palindrome(String string) { return isPal(string, 0, string.length() – 1); } private boolean isPal(String string, int left, int right) { if (left >= right) { return true; } else if (string.charAt(left) == string.charAt(right)) { return isPal(string, left + 1, right – 1); } else { return false; } } What is the purpose of the palindrome method?
A palindrome is a word or phrase that reads the same forward…
A palindrome is a word or phrase that reads the same forward or backward. Consider the following code snippet: public boolean palindrome(String string) { return isPal(string, 0, string.length() – 1); } private boolean isPal(String string, int left, int right) { if (left >= right) { return true; } else if (string.charAt(left) == string.charAt(right)) { return isPal(string, left + 1, right – 1); } else { return false; } } What is the purpose of the palindrome method?
If the user wants to process keystrokes, which methods of th…
If the user wants to process keystrokes, which methods of the KeyListener interface must be implemented?