Consider the following method. public int sol(int lim) { int s = 0; for (int outer = 1; outer
Blog
Consider the following code segment. int val = 1; while (va…
Consider the following code segment. int val = 1; while (val
Consider the following method. public int mystery(int num) {…
Consider the following method. public int mystery(int num) { int x = num; while (x > 0) { if (x / 10 % 2 == 0) return x; x = x / 10; } return x; } What value is returned as a result of the call mystery(1034) ?
Consider the following code segment. int k = 1; while (k < 2...
Consider the following code segment. int k = 1; while (k < 20) { if ((k % 3) == 1) System.out.print(k + " "); k++; } What is printed as a result of executing this code segment?
Consider the following method, which is intended to count th…
Consider the following method, which is intended to count the number of times the letter “A” appears in the string str. public static int countA(String str) { int count = 0; while (str.length() > 0) { int pos = str.indexOf(“A”); if (pos >= 0) { count++; /* missing code */ } else { return count; } } return count; } Which of the following should be used to replace /* missing code */ so that method countA will work as intended?
Consider the following code segment. int j = 1; while (j <...
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; ?
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