Consider the following code segment. int j = 1; while (j < 5) { int k = 1; while (k < 5) { System.out.println(k); k++; } j++; } Which of the following best explains the effect, if any, of changing the first line of code to int j = 0; ?
Blog
Consider the following code segment. int num1 = 0; int num2…
Consider the following code segment. int num1 = 0; int num2 = 3; while ((num2 != 0) && ((num1 / num2) >= 0)) { num1 = num1 + 2; num2 = num2 – 1; } What are the values of numl and num2 after the while loop completes its execution?
Consider the following code segments. I. int k = 1; …
Consider the following code segments. I. int k = 1; while (k < 20) { if (k % 3 == 1) System.out.print( k + " "); k = k + 3; } II. for (int k = 1; k < 20; k++) { if (k % 3 == 1) System.out.print( k + " "); } III. for (int k = 1; k < 20; k = k + 3) { System.out.print( k + " "); } Which of the code segments above will produce the following output? 1 4 7 10 13 16 19
Consider the following method. public static int what(Strin…
Consider the following method. public static int what(String str, String check) { int num = -1; int len = check.length(); for (int k = 0; k + len
Consider the following code segment. int counter = 0; for (…
Consider the following code segment. int counter = 0; for (int x = 10; x > 0; x–) { for (int y = x; y
The following method is intended to print the number of digi…
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?
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?