1 / 62

Snooker Questions

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 {

shelen
Télécharger la présentation

Snooker Questions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MC Question Strategies Snooker Questions By Leon Schram, Plano Texas

  2. Control Structures

  3. 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

  4. 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

  5. 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

  6. 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

  7. Methods & Parameters

  8. 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

  9. 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

  10. 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

  11. 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

  12. Boolean Logic

  13. 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)

  14. 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)

  15. 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.

  16. 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.

  17. OOP Inheritance

  18. 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

  19. 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

  20. 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 

  21. 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 

  22. Static Arrays

  23. 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

  24. 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

  25. 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

  26. 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

  27. Dynamic Arrays

  28. 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]

  29. 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]

  30. 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

  31. 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

  32. String Methods

  33. 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

  34. 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

  35. 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

  36. 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

  37. Program Algorithms

  38. 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.

  39. 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.

  40. 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

  41. 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

  42. Recursive Methods

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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

  50. 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

More Related