Spanish page

Ace your homework & exams now with Quizwiz!

int k = a random number such that 1 ≤ k ≤ n ; for (int p = 2; p <= k; p++) for (int r = 1; r < k; r++) System.out.println("Hello"); What is the minimum number of times that Hello will be printed? A 0 B 1 C 2 D n - 1 E n - 2

A

public int getTheResult(int n) { int product = 1; for (int number = 1; number < n; number++) { if (number % 2 == 0) product *= number; } return product; } What value is returned as a result of the call getTheResult(8) ? A 48 B 105 C 384 D 5040 E 40320

A

Consider the following code segment. for (int j = 0; j < 3; j++) { for (int k = 0; k < 4; k++) { System.out.println("Fun"); } } Which of the following best explains how changing the outer for loop header to for (int j = 0; j <= 3; j++) affects the output of the code segment? A The output of the code segment will be unchanged. B The string "Fun" will be printed more times because the outer loop will execute more times. C The string "Fun" will be printed more times because the inner loop will execute more times in each iteration of the outer loop. D The string "Fun" will be printed fewer times because the outer loop will execute fewer times. E The string "Fun" will be printed fewer times because the inner loop will execute fewer times in each iteration of the outer loop.

B

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") ? A "00000" B "00100" C "00101" D "10110" E "11011"

B

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") ? A "si" B "si the so" C "si the song" D "sig the sog" E Nothing is returned because a StringIndexOutOfBoundsException is thrown.

B

Consider the following method. public static String changeStr(String str) { String result = ""; for (int i = str.length() - 1; i >= str.length() / 2; i -= 2) { result += str.substring(i, i + 1); } return result; } What value is returned as a result of the method call changeStr("12345") ? A "4" B "53" C "531" D "543" E "54321"

B

Consider the following code segment. String str = "abcdef"; for (int rep = 0; rep < str.length() - 1; rep++) { System.out.print(str.substring(rep, rep + 2)); } What is printed as a result of executing this code segment? A abcdef B aabbccddeeff C abbccddeef D abcbcdcdedef E Nothing is printed because an IndexOutOfBoundsException is thrown.

C

Consider the following code segment. for (int x = 0; x <= 4; x++) // Line 1 { for (int y = 0; y < 4; y++) // Line 3 { System.out.print("a"); } System.out.println(); } Which of the following best explains the effect of simultaneously changing x <= 4 to x < 4 in line 1 and y < 4 to y <= 4 in line 3 ? A "a" will be printed fewer times because while each output line will have the same length as before, the number of lines printed will decrease by 1. B "a" will be printed more times because while the number of output lines will be the same as before, the length of each output line will increase by 1. C "a" will be printed the same number of times because while the number of output lines will decrease by 1, the length of each line will increase by 1. D "a" will be printed more times because both the number of output lines and the length of each line will increase by 1. E The output of the code segment will not change in any way.

C

Consider the following code segment. int counter = 0; for (int x = 10; x > 0; x--) { for (int y = x; y <= x; y++) { counter++; // line 6 } } How many times will the statement in line 6 be executed as a result of running the code segment? A 0 B 1 C 10 D 11 E 20

C

Consider the following method. public int sol(int lim) { int s = 0; for (int outer = 1; outer <= lim; outer++) { for (int inner = outer; inner <= lim; inner++) { s++; } } return s; } What value is returned as a result of the call sol(10)? A 20 B 45 C 55 D 100 E 385

C

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 <= str.length(); k++) { String a = str.substring(k, k + len); if (a.equals(check)) { num = k; } } return num; } Assume that check occurs at least once in str. Which of the following best describes the value returned by the what method? A The number of times the string check occurs in str B The index of the first occurrence of check inside str C The index of the last occurrence of check inside str D The number of substrings in str with the same length as check E The number of substrings in str that do not match check

C

Consider the following output. 11 21 2 31 2 3 41 2 3 4 51 2 3 4 5 6 Which of the following code segments will produce the output shown above? A for (int j = 1; j <= 6; j++) { for (int k = 1; k < j; k++) System.out.print(" " + k); System.out.println(); } B for (int j = 1; j <= 6; j++) { for (int k = 1; k <= j; k++) System.out.print(" " + j); System.out.println(); } C for (int j = 1; j <= 6; j++) { for (int k = 1; k <= j; k++) System.out.print(" " + k); System.out.println(); } D for (int j = 1; j < 6; j++) { for (int k = 1; k <= j; k++) System.out.print(" " + k); System.out.println(); } E for (int j = 1; j < 6; j++) { for (int k = 1; k < j; k++) System.out.print(" " + k); System.out.println(); }

C

2.B 1/1 MC pointCorrectly answered Consider the following code segment. int k = 0; while (k < 10) { System.out.print((k % 3) + " "); if ((k % 3) == 0) k = k + 2; else k++; } What is printed as a result of executing the code segment? A 0 2 1 0 2 B 0 2 0 2 0 2 C 0 2 1 0 2 1 0 D 0 2 0 2 0 2 0 E 0 1 2 1 2 1 2

D

Consider the following code segment. String str = "a black cat sat on a table"; int counter = 0; for (int i = 0; i < str.length() - 1; i++) { if (str.substring(i, i + 1).equals("a") && !str.substring(i + 1, i + 2).equals("b")) { counter++; } } System.out.println(counter); What is printed as a result of executing this code segment? A 1 B 2 C 3 D 5 E 6

D

Consider the following code segment. for (int num = 0; num < 10; num += 2) { for (int val = 0; val < 5; val++) { System.out.println("hop"); } } How many times will System.out.println("hop") be executed? A 0 B 5 C 10 D 25 E 50

D

Consider the following code segment. for (int outer = 0; outer < 3; outer++) { for (/* missing loop header */) { System.out.print(outer + "" + inner + "_"); } } Which of the following can be used as a replacement for /* missing loop header */ so that the code segment produces the output 00_01_02_11_12_22_ ? A int inner = 0; inner < 3; inner++ B int inner = 1; inner < 3; inner++ C int inner = outer - 1; inner < 3; inner++ D int inner = outer; inner < 3; inner++ E int inner = outer + 1; inner < 3; inner++

D

Consider the following code segment. for (int outer = 0; outer < n; outer++) { for (int inner = 0; inner <= outer; inner++) { System.out.print(outer + " "); } } If n has been declared as an integer with the value 4, what is printed as a result of executing the code segment? A 0 1 2 3 B 0 0 1 0 1 2 C 0 1 2 2 3 3 3 D 0 1 1 2 2 2 3 3 3 3 E 0 0 1 0 1 2 0 1 2 3

D

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? A num1 = 0, num2 = 3 B num1 = 8, num2 = -1 C num1 = 4, num2 = 1 D num1 = 6, num2 = 0 E The loop will never complete its execution because a division by zero will generate an ArithmeticException.

D

Consider the following code segment. int outerMax = 10; int innerMax = 5; for (int outer = 0; outer < outerMax; outer++) { for (int inner = 0; inner <= innerMax; inner++) { System.out.println(outer + inner); } } How many values will be printed when the code segment is executed? A 45 B 50 C 55 D 60 E 66

D

Consider the following method. public static String rearrange(String str) { String temp = ""; for (int i = str.length() - 1; i > 0; i--) { temp += str.substring(i - 1, i); } return temp; } What, if anything, is returned by the method call rearrange("apple") ? A "appl" B "apple" C "elppa" D "lppa" E Nothing is returned due to a run-time error.

D

Consider the following method. public static void printSome(int num1, int num2) { for (int i = 0; i < num1; i++) { if (i % num2 == 0 && i % 2 == 0) { System.out.print(i + " "); } } } Which of the following method calls will cause "0 10 " to be printed? A printSome(0, 20) B printSome(5, 10) C printSome(10, 5) D printSome(20, 5) E printSome(25, 5)

D

Consider the following method. public void numberCheck(int maxNum) { int typeA = 0; int typeB = 0; int typeC = 0; for (int k = 1; k <= maxNum; k++) { if (k % 2 == 0 && k % 5 == 0) typeA++; if (k % 2 == 0) typeB++; if (k % 5 == 0) typeC++; } System.out.println(typeA + " " + typeB + " " + typeC); } What is printed as a result of the call numberCheck(50) ? A 5 20 5 B 5 20 10 C 5 25 5 D 5 25 10 E 30 25 10

D

The question refer to the following code segment. int k = a random number such that 1 ≤ k ≤ n ; for (int p = 2; p <= k; p++) for (int r = 1; r < k; r++) System.out.println("Hello"); What is the maximum number of times that Hello will be printed? A 2 B n - 1 C n - 2 D (n - 1)2 E n2

D

2.D 1/1 MC pointCorrectly answered Consider the following code segment. int val = 1; while (val <= 6) { for (int k = 0; k <= 2; k++) { System.out.println("Surprise!"); } val++; } How many times is the string "Surprise!" printed as a result of executing the code segment? A 3 B 6 C 12 D 15 E 18

E

Consider the following code segment. /* missing loop header */ { for (int k = 0; k < 4; k++) { System.out.print(k); } System.out.println(); } The code segment is intended to produce the following output. 0123 0123 0123 Which of the following can be used to replace /* missing loop header */ so that the code segment works as intended? for (int j = 0; j < 3; j++) for (int j = 1; j < 3; j++) for (int j = 1; j <= 3; j++) A I only B II only C III only D I and II E I and III

E

Consider the following code segment. for (int k = 0; k < 9; k = k + 2) { if ((k % 2) != 0) { System.out.print(k + " "); } } What, if anything, is printed as a result of executing the code segment? A 0 2 4 6 8 10 B 0 2 4 6 8 C 1 3 5 7 9 D 1 3 5 7 E Nothing is printed.

E

int count = 0; for (int x = 1; x <= 3; x++) { /* missing loop header */ { count++; } } System.out.println(count); Which of the following should be used to replace /* missing loop header */ so that the code segment will print 6 as the value of count ? A for (int y = 0; y <= 2; y++) B for (int y = 0; y < 3; y++) C for (int y = 2; y >= 0; y--) D for (int y = 3; y > 0; y--) E for (int y = 0; y < x; y++)

E

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? A str = str.substring(0, pos); B str = str.substring(0, pos + 1); C str = str.substring(pos - 1); D str = str.substring(pos); E str = str.substring(pos + 1);

E

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; ? A There will be one more value printed because the outer loop will iterate one additional time. B There will be four more values printed because the outer loop will iterate one additional time. C There will be one less value printed because the outer loop will iterate one fewer time. D There will be four fewer values printed because the outer loop will iterate one fewer time. E There will be no change to the output of the code segment.

B

Consider the following two code segments. Assume that the int variables m and n have been properly declared and initialized and are both greater than 0. for (int i = 0; i < m * n; i++){ System.out.print("A");} for (int j = 1; j <= m; j++){ for (int k = 1; k < n; k++) { System.out.print("B"); }} Assume that the initial values of m and n are the same in code segment I as they are in code segment II. Which of the following correctly compares the number of times that "A" and "B" are printed when each code segment is executed? A "A" is printed m fewer times than "B". B "A" is printed n fewer times than "B". C "A" is printed m more times than "B". D "A" is printed n more times than "B". E "A" and "B" are printed the same number of times.

C

What is returned as a result of the call mystery("computer") ? A "computer" B "cmue" C "optr" D "ompute" E

C

mystery ("no") B mystery ("on") C mystery ("nnoo") D mystery ("nono") E mystery ("noon")

E


Related study sets

Module 7 Working with VMware vSphere

View Set

Practice Exam Final questions 310 Adult Health/Med Surge

View Set