620 likes | 646 Views
MC Question Strategies. Snooker Questions. By Leon Schram, Plano Texas. Control. Structures. 01. Consider the following code segment. int x = <some integer greater than zero> int n = 0; if (x < 500) { if (x > 750) n = 100; else n = 200; } else {
E N D
MC Question Strategies Snooker Questions By Leon Schram, Plano Texas
Control Structures
01. Consider the following code segment. int x = <some integer greater than zero> int n = 0; if (x < 500) { if (x > 750) n = 100; else n = 200; } else { if (x < 300) n = 300; else n = 200; } System.out.println(n); What is printed as a result of executing the code segment? (A) Unknown without the value of x (B) 0 (C) 100 (D) 200 (E) 300
01. Consider the following code segment. int x = <some integer greater than zero> int n = 0; if (x < 500) { if (x > 750) n = 100; else n = 200; } else { if (x < 300) n = 300; else n = 200; } System.out.println(n); What is printed as a result of executing the code segment? (A) Unknown without the value of x (B) 0 (C) 100 (D) 200 (E) 300
02. Consider the following code segment. int count = 10; for (int p = 0; p < 15; p+=3) { if (p % 2 == 0) count++; se count--; } System.out.println(count); What is printed as a result of executing the code segment? (A) 10 (B) 11 (C) 12 (D) 13 (E) 14
02. Consider the following code segment. int count = 10; for (int p = 0; p < 15; p+=3) { if (p % 2 == 0) count++; else count--; } System.out.println(count); What is printed as a result of executing the code segment? (A) 10 (B) 11 (C) 12 (D) 13 (E) 14
Methods & Parameters
03. Consider the following program. public class Question03 { public static void main (String args[]) { int p = 10; int q = 20; swap(p,q); System.out.println(p + " " + q); } public static void swap(int x, int y) { int temp = x; x = y; y = temp; } } What is printed as a result of executing the program? (A) 10 20 (B) 20 10 (C) 10 10 (D) 20 20 (E) 0 0
03. Consider the following program. public class Question03 { public static void main (String args[]) { int p = 10; int q = 20; swap(p,q); System.out.println(p + " " + q); } public static void swap(int x, int y) { int temp = x; x = y; y = temp; } } What is printed as a result of executing the program? (A) 10 20 (B) 20 10 (C) 10 10 (D) 20 20 (E) 0 0
04. Consider the following program. public class Question04 { public static void main (String args[]) { int[ ] list = {1,2,3,4,5,6,7,8,9}; swap(list,3,4); System.out.println(list[3] + " " + list[4]); } public static void swap(int[ ] x, int p, int q) { int temp = x[p]; x[p] = x[q]; x[q] = temp; } } What is printed as a result of executing the program? (A) 3 4 (B) 4 3 (C) 4 5 (D) 5 4 (E) ArrayIndexOutOfboundsException
04. Consider the following program. public class Question04 { public static void main (String args[]) { int[ ] list = {1,2,3,4,5,6,7,8,9}; swap(list,3,4); System.out.println(list[3] + " " + list[4]); } public static void swap(int[ ] x, int p, int q) { int temp = x[p]; x[p] = x[q]; x[q] = temp; } } What is printed as a result of executing the program? (A) 3 4 (B) 4 3 (C) 4 5 (D) 5 4 (E) ArrayIndexOutOfboundsException
Boolean Logic
05. The Boolean expression !((A < B) && (C > D)) is equivalent to which of the following expressions? (A) (A < B) || (C > D) (B) (A >= B) && (C <= D) (C) (A >= B) || (C <= D) (D) (A > B) && (C < D) (E) (A > B) || (C < D)
05. The Boolean expression !((A < B) && (C > D)) is equivalent to which of the following expressions? (A) (A < B) || (C > D) (B) (A >= B) && (C <= D) (C) (A >= B) || (C <= D) (D) (A > B) && (C < D) (E) (A > B) || (C < D)
06. Consider the following code segment. for (int k = 1; k <= 100; k++) { int n1 = (int) Math.random(); int n2 = (int) Math.random() * 4; boolean bool = n1 == n2; System.out.print(bool + " "); } What is printed as a result of executing the code segment? (A) The code segment always prints true. (B) The code segment always prints false. (C) The code segment prints true roughly 25% of the time and prints false roughly 75% of the time. (D) The code segment prints both true and false, but true more frequently. (E) The code segment prints both true and false, but false more frequently.
06. Consider the following code segment. for (int k = 1; k <= 100; k++) { int n1 = (int) Math.random(); int n2 = (int) Math.random() * 4; boolean bool = n1 == n2; System.out.print(bool + " "); } What is printed as a result of executing the code segment? (A) The code segment always prints true. (B) The code segment always prints false. (C) The code segment prints true roughly 25% of the time and prints false roughly 75% of the time. (D) The code segment prints both true and false, but true more frequently. (E) The code segment prints both true and false, but false more frequently.
OOP Inheritance
class Student extends Person { private int grade; public Student(int g, int a) { super(a); grade = g; System.out.println("Student Constructor"); } public int getGrade() { return grade; } } 07. Person sue = new Person(25); Student tom = new Student(17,12); System.out.println(sue.getAge()); System.out.println(tom.getGrade()); class Person { private int age; public Person(int a) { age = a; System.out.println("Person Constructor"); } public int getAge() { return age; } } (C) Person Constructor Student Constructor (E) Student Constructor Person Constructor (E) Person Constructor Person Constructor What are the first 2 lines of program output? (A) 25 17 (B) Student Constructor Student Constructor
07. Person sue = new Person(25); Student tom = new Student(17,12); System.out.println(sue.getAge()); System.out.println(tom.getGrade()); class Person { private int age; public Person(int a) { age = a; System.out.println("Person Constructor"); } public int getAge() { return age; } } class Student extends Person { private int grade; public Student(int g, int a) { super(a); grade = g; System.out.println("Student Constructor"); } public int getGrade() { return grade; } } (C) Person Constructor Student Constructor (E) Student Constructor Person Constructor (E) Person Constructor Person Constructor What are the first 2 lines of program output? (A) 25 17 (B) Student Constructor Student Constructor
class Student extends Person { private int grade; public Student(int g, int a) { super(a); grade = g; System.out.println("Student Constructor"); } public int getGrade() { return grade; } } 08. Person sue = new Person(25); Student tom = new Student(17,12); System.out.println(sue.getAge()); System.out.println(tom.getGrade()); class Person { private int age; public Person(int a) { age = a; System.out.println("Person Constructor"); } public int getAge() { return age; } } (C) 17 17 (D) 25 25 (E) Person Constructor Student Constructor What are the last 2 lines of program output? (A) 25 17 (B)17 25
class Student extends Person { private int grade; public Student(int g, int a) { super(a); grade = g; System.out.println("Student Constructor"); } public int getGrade() { return grade; } } 08. Person sue = new Person(25); Student tom = new Student(17,12); System.out.println(sue.getAge()); System.out.println(tom.getGrade()); class Person { private int age; public Person(int a) { age = a; System.out.println("Person Constructor"); } public int getAge() { return age; } } (C) 17 17 (D) 25 25 (E) Person Constructor Student Constructor What are the last 2 lines of program output? (A) 25 17 (B)17 25
Static Arrays
09. Consider the following code segment. int[ ] list = {11,22,33,44,55,66,77,88,99}; for (int k = 0; k < list.length; k++) list[k] = list[k]/list[0]; for (int k = 0; k < list.length; k++) System.out.print(list[k] + " "); What is printed as a result of executing the code segment? (A) 11 22 33 44 55 66 77 88 99 (B) 1 2 3 4 5 6 7 8 9 (C) 1 1 1 1 1 1 1 1 1 (D) 1 22 33 44 55 66 77 88 99 (E) 11 22 33 44 55 66 77 88 9
09. Consider the following code segment. int[ ] list = {11,22,33,44,55,66,77,88,99}; for (int k = 0; k < list.length; k++) list[k] = list[k]/list[0]; for (int k = 0; k < list.length; k++) System.out.print(list[k] + " "); What is printed as a result of executing the code segment? (A) 11 22 33 44 55 66 77 88 99 (B) 1 2 3 4 5 6 7 8 9 (C) 1 1 1 1 1 1 1 1 1 (D) 1 22 33 44 55 66 77 88 99 (E) 11 22 33 44 55 66 77 88 9
10. Consider the following code segment. int[ ] list1 = {10,20,30,40,50,60,70,80,90}; int[ ] list2 = list1; for (int k = 0; k < list2.length; k++) list2[k] = list1[0]; for (int k = 0; k < list1.length; k++) list1[k] *= list2[k]; for (int k = 0; k < list1.length; k++) System.out.print(list1[k] + " "); What is printed as a result of executing the code segment? (A) 100 100 100 100 100 100 100 100 100 (B) 100 200 300 400 500 600 700 800 900 (C) 1 2 3 4 5 6 7 8 9 (D) 10 (E) 100
10. Consider the following code segment. int[ ] list1 = {10,20,30,40,50,60,70,80,90}; int[ ] list2 = list1; for (int k = 0; k < list2.length; k++) list2[k] = list1[0]; for (int k = 0; k < list1.length; k++) list1[k] *= list2[k]; for (int k = 0; k < list1.length; k++) System.out.print(list1[k] + " "); What is printed as a result of executing the code segment? (A) 100 100 100 100 100 100 100 100 100 (B) 100 200 300 400 500 600 700 800 900 (C) 1 2 3 4 5 6 7 8 9 (D) 10 (E) 100
Dynamic Arrays
11. Consider the following code segment. ArrayList<String> names = new ArrayList<String>(); names.add("Isolde"); names.add("John"); names.add("Greg"); names.add("Maria"); names.add("Heidi"); names.add(2,"Diana"); names.add(5,"David"); System.out.println(names); What is printed as a result of executing the code segment? (A) [Isolde, John, Diana, Maria, Heidi, David] (B) [Isolde, Diana, Greg, Maria, David] (C) [Isolde, John, Diana, Greg, Maria, David, Heidi] (D) [Isolde, John, Diana, Greg, Maria, Heidi, David] (E) [Isolde, Diana, John, Greg, Maria, David, Heidi]
11. Consider the following code segment. ArrayList<String> names = new ArrayList<String>(); names.add("Isolde"); names.add("John"); names.add("Greg"); names.add("Maria"); names.add("Heidi"); names.add(2,"Diana"); names.add(5,"David"); System.out.println(names); What is printed as a result of executing the code segment? (A) [Isolde, John, Diana, Maria, Heidi, David] (B) [Isolde, Diana, Greg, Maria, David] (C) [Isolde, John, Diana, Greg, Maria, David, Heidi] (D) [Isolde, John, Diana, Greg, Maria, Heidi, David] (E) [Isolde, Diana, John, Greg, Maria, David, Heidi]
12. Consider the following method. /** Precondition: list contains [13, 64, 42, 27, 77, 38, 11] */ public static void mystery(ArrayList<Integer> list) { for (int k = 0; k < list.size(); k++) { if (list.get(k) % 2 == 0) list.remove(k); } } • Which of the following describes the values stored in list after mystery executes? • (A) [13, 64, 42, 27, 77, 38, 11] • (B) [13, 27, 77, 11] • (C) [13, 42, 27, 77, 11] • (D) [64, 42, 38] • An IndexOutOfBoundsException error message
12. Consider the following method. /** Precondition: list contains [13, 64, 42, 27, 77, 38, 11] */ public static void mystery(ArrayList<Integer> list) { for (int k = 0; k < list.size(); k++) { if (list.get(k) % 2 == 0) list.remove(k); } } • Which of the following describes the values stored in list after mystery executes? • (A) [13, 64, 42, 27, 77, 38, 11] • (B) [13, 27, 77, 11] • (C) [13, 42, 27, 77, 11] • (D) [64, 42, 38] • An IndexOutOfBoundsException error message
String Methods
13. Consider the following method. // Precondition: str is a nonempty string of upper-case letters public static String mystery(String str) { String temp = ""; for (int k = 0; k < str.length(); k++) { String t = str.substring(k,k+1); if (t != "A" || t != "E" || t != "I" || t != "O" || t != "U") temp += t; } return temp; } Which of the following is a correct Postcondition, based on the return of method mystery? (A) Postcondition: mystery returns str (B) Postcondition: mystery returns str with all vowels removed (C) Postcondition: mystery returns only the vowels in str (D) Postcondition: mystery returns null (E) It will generate a StringOutOfBoundsException error message
13. Consider the following method. // Precondition: str is a nonempty string of upper-case letters public static String mystery(String str) { String temp = ""; for (int k = 0; k < str.length(); k++) { String t = str.substring(k,k+1); if (t != "A" || t != "E" || t != "I" || t != "O" || t != "U") temp += t; } return temp; } Which of the following is a correct Postcondition, based on the return of method mystery? (A) Postcondition: mystery returns str (B) Postcondition: mystery returns str with all vowels removed (C) Postcondition: mystery returns only the vowels in str (D) Postcondition: mystery returns null (E) It will generate a StringOutOfBoundsException error message
14. What is the output of the following program segment? String s = “Mississippi”; System.out.println(s.indexOf(s.substring(5,7)); • 1 • 2 • (C) 4 • 5 • 7
14. What is the output of the following program segment? String s = “Mississippi”; System.out.println(s.indexOf(s.substring(5,7)); • 1 • 2 • (C) 4 • 5 • 7
Program Algorithms
15. Consider the following code segment. String[ ] stID = {"ABC", "DEF", "GHI", "JKL", "MNO"}; String str1 = "GHI"; boolean found = false; int loc = -1, sub = 0; while (!found && sub < stID.length) { if (stID[sub].equals(str1)) { found = true; loc = sub; } sub++; } The code segment is an example of (A) a sort algorithm. (B) an algorithm to locate the smallest element. (C) an algorithm to locate the largest element. (D) a sequential search algorithm. (E) an algorithm to sum the values of an array.
15. Consider the following code segment. String[ ] stID = {"ABC", "DEF", "GHI", "JKL", "MNO"}; String str1 = "GHI"; boolean found = false; int loc = -1, sub = 0; while (!found && sub < stID.length) { if (stID[sub].equals(str1)) { found = true; loc = sub; } sub++; } The code segment is an example of (A) a sort algorithm. (B) an algorithm to locate the smallest element. (C) an algorithm to locate the largest element. (D) a sequential search algorithm. (E) an algorithm to sum the values of an array.
16. When using a binary search, what is the MAXIMUM number of comparisons necessary to locate a specific item in a list of 1025 elements? (A) 1 (B) 9 (C) 10 (D) 11 (E) 1024
16. When using a binary search, what is the MAXIMUM number of comparisons necessary to locate a specific item in a list of 1025 elements? (A) 1 (B) 9 (C) 10 (D) 11 (E) 1024
Recursive Methods
17. Consider the following code segment and method. int[ ] list = {1,2,3,4}; mystery1(list,0); public static void mystery1(int[ ] x, int n) { if (n < x.length) { System.out.print(x[n] + " "); mystery1(x,n+1); } } What is output by executing the code segment? (A) 1 2 3 4 (B) 1 2 3 (C) 4 3 2 1 (D) 3 2 1 0 (E) 3 2 1
17. Consider the following code segment and method. int[ ] list = {1,2,3,4}; mystery1(list,0); public static void mystery1(int[ ] x, int n) { if (n < x.length) { System.out.print(x[n] + " "); mystery1(x,n+1); } } What is output by executing the code segment? (A) 1 2 3 4 (B) 1 2 3 (C) 4 3 2 1 (D) 3 2 1 0 (E) 3 2 1
18. Consider the following code segment and method. int[ ] list = {1,2,3,4}; mystery2(list,0); public static void mystery2(int[ ] x, int n) { if (n < x.length) { mystery2(x,n+1); System.out.print(x[n] + " "); } } What is output by executing the code segment? (A) 1 2 3 4 (B) 1 2 3 (C) 4 3 2 1 (D) 3 2 1 0 (E) 3 2 1
18. Consider the following code segment and method. int[ ] list = {1,2,3,4}; mystery2(list,0); public static void mystery2(int[ ] x, int n) { if (n < x.length) { mystery2(x,n+1); System.out.print(x[n] + " "); } } What is output by executing the code segment? (A) 1 2 3 4 (B) 1 2 3 (C) 4 3 2 1 (D) 3 2 1 0 (E) 3 2 1
19. Consider the following code segment and method. System.out.println(mystery3(5)); public static int mystery3(int n) { if (n == 0) return n; else return n + mystery3(n-1); } What value is returned by the a call to mystery3(5)? (A) 4 (B) 8 (C) 12 (D) 15 (E) 16
19. Consider the following code segment and method. System.out.println(mystery3(5)); public static int mystery3(int n) { if (n == 0) return n; else return n + mystery3(n-1); } What value is returned by the a call to mystery9(5)? (A) 4 (B) 8 (C) 12 (D) 15 (E) 16
20. Consider the following code segment and method. System.out.println(mystery4(5)); public static int mystery4(int n) { if (n == 1) return 1; else return mystery4(n-1) + mystery4(n-1); } What value is returned by the a call to mystery4(5)? (A) 16 (B) 15 (C) 8 (D) 7 (E) 4
20. Consider the following code segment and method. System.out.println(mystery4(5)); public static int mystery4(int n) { if (n == 1) return 1; else return mystery4(n-1) + mystery4(n-1); } What value is returned by the a call to mystery4(5)? (A) 16 (B) 15 (C) 8 (D) 7 (E) 4