The following method is intended to print the number of digits in the parameter num. public int numDigits(int num) { int count = 0; while (/* missing condition */) { count++; num = num / 10; } return count; } Which of the following can be used to replace /* missing condition */ so that the method will work as intended?
Blog
Consider the following code segment. int a = 24; int b = 30;…
Consider the following code segment. int a = 24; int b = 30; while (b != 0) { int r = a % b; a = b; b = r; } System.out.println(a); What is printed as a result of executing the code segment?
Consider the following method. public static String abMetho…
Consider the following method. public static String abMethod(String a, String b) { int x = a.indexOf(b); while (x >= 0) { a = a.substring(0, x) + a.substring(x + b.length()); x = a.indexOf(b); } return a; } What, if anything, is returned by the method call abMethod(“sing the song”, “ng”) ?
Consider the following method. /** Precondition: Strings on…
Consider the following method. /** Precondition: Strings one and two have the same length. */ public static String combine(String one, String two) { String res = “”; for (int k = 0; k < one.length(); k++) { if (one.substring(k, k + 1).equals(two.substring(k, k + 1))) { res += one.substring(k, k + 1); } else { res += "0"; } } return res; } What is returned as a result of the call combine("10110", "01100") ?
Consider the following code segment. int num = 1; for (int…
Consider the following code segment. int num = 1; for (int k = 2; k < 10; k++) { num = 0; num = num + k; } What will be the value of num after the loop is executed?
Consider the following code segment. int count = 5; while (…
Consider the following code segment. int count = 5; while (count < 100) { count = count * 2; } count = count + 1; What will be the value of count as a result of executing the code segment?
Consider the following methods, which appear in the same cla…
Consider the following methods, which appear in the same class. public int function1(int i, int j) { return i + j; } public int function2(int i, int j) { return j – i; } Which of the following statements, if located in a method in the same class, will initialize the variable x to 11?
Consider the following code segment. int outerMax = 10; int…
Consider the following code segment. int outerMax = 10; int innerMax = 5; for (int outer = 0; outer < outerMax; outer++) { for (int inner = 0; inner
Consider the following code segment. int value = 15; while (…
Consider the following code segment. int value = 15; while (value < 28) { System.out.println(value); value++; } What are the first and last numbers output by the code segment?
Consider the following code segment. int num = 5; num *= 2;…
Consider the following code segment. int num = 5; num *= 2; num %= 6; What is the value of num after the code segment is executed?