final review

pdf

School

York University *

*We aren’t endorsed by this school

Course

1022

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

21

Uploaded by freecloud19

Report
2020/12/22 Final Test: Attempt review Dashboard / My courses / LE/EECS1022 A - Programming_for Mobile Computing_(Fall 2020-2021) / Tests / Final Test Started on Tuesday, 15 December 2020, 10:29 PM State Finished Completed on Tuesday, 15 December 2020, 11:33 PM Time taken 1 hour 4 mins Grade 24.00 out of 32.00 (75%) Question 1 Incorrect Mark 0.00 out of 1.00 Given the following function f1, give the best order of growth of the running time (i.e., The time complexity using Big-O notation). public int fl(int[] a, int key) { int lo = U, mid, hi = a.length-!; while (lo <= hi) { mid = (lo + hi)/Z; if (key < a[mid]) hi = mid - 1; else if (a[mid] < key) lo = mid + |; else return mid; // success: find the key return the index of } return -1; // failure: key is not in the array: Select one; Oo(1) O(N) O(log N) O(NlogN) © O(N?) O( N2 log N) Compilation errors none of the above The correct answer is: O(log N) https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 1/21
2020/12/22 Final Test: Attempt review Question 2 Correct Mark 1.00 out of 1.00 In Java, which of the following statements are CORRECT. . ArrayList implements List interface. Il. HashMap implements Map interface Il ArrayList stores two objects key and value. Select one; | only. Il only. Il only. | and III. ° land Il Il and III. l, Il and Ill. none of the above The correct answer is: | and Il. https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 2/21
2020/12/22 Final Test: Attempt review Question 3 Correct Mark 1.00 out of 1.00 Given the following function f2, give the best order of growth of the running time (i.e., The time complexity using Big-O notation). public int f2 (int N) { int x = 0; for (Ant 1= 1; 1 < N5 i+¢) { for(int j =0; j < i; j++) { X++; } } return x; Select one; O(1) O(N) O(logN) O(NlogN) o Q(NZ) v Runtime Exception Compilation errors none of the above The correct answer is: O( N2 ) https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 3/21
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2020/12/22 Final Test: Attempt review Question 4 Correct Mark 1.00 out of 1.00 Which of the following statements are CORRECT I. The Java compiler always adds a default constructor to a user defined class. II. Each instantiated object will have its own copy of a class variable. ITI. A class can have multiple constructors. Select one; | only. Il only. © 1l only. | and IIl. | and II. Il and Il1. l, Il and Ill. none of the above The correct answer is: lll only. Question B Correct Mark 1.00 out of 1.00 What is the output from the following Java program fragment? | | try { ~ foxr (Int 1. = 1; i <€ 3; a+¥) ¢ | System.out.print (i) ; | System.out.println(l / 0); | } } catch (Exception ex) { } e S Write the answer without adding anything to it (such as extra quotes, leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 1 v The correct answer is: 1 https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 4/21
2020/12/22 Final Test: Attempt review Question 6 Correct Mark 1.00 out of 1.00 For what value of the parameter k would the return of this method be 57 If there is more than one such value, write any one of them (and only one) as your answer. public int methodA(int k) { int total = 0; For (dnt 1 = 2:52) if (i <= 10) i++; if (i > 10) break; iF (1 »= k) total++; } return total; } Write the answer without adding anything to it (such as extra quotes, leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 6 v The correct answer is: 6 Question 7 Incorrect Mark 0.00 out of 1.00 What is the value of String s4 after the following Java program fragment is executed? String S1 = "EECS$yokr$universityz"; String S2 = "Hi"; String S3 = "student”; String S4 = Sl.substring(S1.indexOf("r")+1, S1.indexOf("z")) + S3.substring(@) + S2; Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: "S$universitysHi" x The correct answer is: "$universitystudentHi" https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 5/21
2020/12/22 Final Test: Attempt review Question 8 Correct Mark 1.00 out of 1.00 Which of the following Java statements will print true I. System.out.println("Hello, ABC".matches ("ABC")) ; II. System.out.println("xyz, ABC".matches(".*ABC.*")); ITII. system.out.println("XYZ, ABC".matches(".ABC.*")) ; Select one: | only. ° 1l only. v Il only. | and III. | and II. Il and Il1. l, Il and IIl. none of the above The correct answer is: Il only. Question 9 Correct Mark 1.00 out of 1.00 Which of the following contains string(s) that DO NOT match the regular expression ab*c$? Select one: - "abc" "fac" © "1223" v none of the above The correct answer is; "1223" https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 6/21
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2020/12/22 Final Test: Attempt review Question 10 Correct Mark 1.00 out of 1.00 Evaluate the value of the following Java expression. Use the appropriate Java literal to express the value (e.g., 7 for an int, 7.0 for a double, "hello" for a string, true or false for a boolean). You do not need to show your work. (50 - 5 *4) /10 - 7 Write the answer without adding anything to it (such as leading or trailing text, = signs, ..). Write XXX in case of errors. Answer: -4 v The correct answer is: -4 Question 11 Correct Mark 1.00 out of 1.00 For what value of the parameter m would the return of this method be -17? If there is more than one such value, write any one of them (and only one) as your answer. public static int compute(int m) { int k = 15; int b = 0; while (b++ < m) { k -= 2; } return k; } —_ Write the answer without adding anything to it (such as extra quotes, leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 8 v The correct answer is: 8 https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 - 7/21
2020/12/22 Final Test: Attempt review Question 12 Incorrect Mark 0.00 out of 1.00 What is the value stored in the n variable after executing the following Java program fragment? int n = 10; switch (n) { case 10: n =n + 5; case 15: n = n + 5; case 20: n n - 3; case 25: n n + 4; case 30: n=n - 5; } Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 15 P 9 The correct answer is: 16 Question 13 Correct Mark 1.00 out of 1.00 Evaluate the value of the following Java expression. Use the appropriate Java literal to express the value (e.g., 7 for an int, 7.0 for a double, "hello" for a string, true or false for a boolean). You do not need to show your work. 10+ 5**4 /3 -13 % 3 Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 15 v The correct answer is: 15 https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 8/21
2020/12/22 Final Test: Attempt review Question 14 Correct Mark 1.00 out of 1.00 For what value of the parameter m would the return of this method be 37 If there is more than one such value, write any one of them (and only one) as your answer. public int method(int m) { int count = 9; int[]J[] a={ {9, 2, 5, 10 }, { 6, 15, 8, 9 }, {20, 14 }, { 22, 30 }, {4, 71} }; for (int[] e : for (int k : e) count++; return count; Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 6 v The correct answer is: 6 Question 15 Incorrect Mark 0.00 out of 1.00 For what value of the parameter m would the return of this method be "York"? If there is more than one such value, write any one of them (and only one) as your answer. public String method(char m) { String s = "Hello EECS York University"; int p = s.index0f(m); String s1 =s.substring(p, p + 4); return sl; Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 11 b The correct answer is: 'Y' https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 9/21
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2020/12/22 Final Test: Attempt review Question 16 Correct Mark 1.00 out of 1.00 For what value of the parameter m would the return of this method be "345"? If there is more than one such value, write any one of them (and only one) as your answer. public String calc(String m) { final String s = "DayNightUpDown"; final String n = "0©1234567890246"; String result = ""; int p = s.indexOf(m); if (p >=0) { result = n.substring(p, p + m.length()); } return result; Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: "“Nig" v The correct answer is: "Nig" Question 17 Correct Mark 1.00 out of 1.00 For what value of the parameter m would the return of this method be -67 If there is more than one such value, write any one of them (and only one) as your answer. public static int compute(int m) | { int r = m; for (int i = 9; i < 3; i++) { for (int j = 1; j <= 7; j++) { re=l } } return r; ) | Write the answer without adding anything to it (such as extra quotes, leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 15 v The correct answer is: 15 https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 10/21
2020/12/22 Final Test: Attempt review Question 18 Correct Mark 1.00 out of 1.00 What is the value of a[2] after the following Junit test question is executed? public static double[] method(int[] a, double x) { double[] b = new double[ ]; b[0] = a[0] + x; b[l] = a[l] + x; return b; } public static void method(int[] a, int m) { double[] b = method(a, ©¢.0); af[0] = a[0] * m; a[Z] = (int) (b[U] + bI[1]); m=m+ 5; } @Test public void question() { dntl] a = {§1, 4, 3, 8 ¥; method(a,4) ; } Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 17 v The correct answer is; 17 Question 19 Incorrect Mark 0.00 out of 1.00 In Java, which of the following statements are CORRECT about import statement I. import statment has no runtime or performance implications. II. We can use import statement to imports a single class or all public classes within a given package. III. Import statement can appear at any line inside your code file. Select one; | only. © 1l only. Il only. | and II1. | and II. Il and Il1. l, Il and Ill. none of the above The correct answer is: | and Il. https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 11/21
2020/12/22 Final Test: Attempt review Question 20 Correct Mark 1.00 out of 1.00 What is the value of a[@] after executing the following Java program fragment? String[] a = "Welcome to Programming for Mobile Computing”.split("o"); Write the answer without adding anything to it (such as extra quotes, leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: "Welc" v The correct answer is; "Welc" Question 21 Correct Mark 1.00 out of 1.00 Give the shortest value for the String variable y such that that the output of this fragment is true. If there is more than one such value, write any one of them (and only one) as your answer. Pattern pattern = Pattern.compile("A{3,5}[bc]+de$"); Matcher matcher = pattern.matcher(y); System.out.print(matcher.find()); —_— Write the answer without adding anything to it (such as extra quotes, leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: "AAAbde" v The correct answer is: "AAAbde" https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 12/21
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2020/12/22 Question 22 Correct Mark 1.00 out of 1.00 Given class Box defined below public class Box { private int side = 9; public Box() { } public Box(int s) {side = s;} public Box(Box b) { side=b.getSide(); public int getSide() {return side;} public void setSide(int side) { this.side = side; } } What will be the value of k after the following Java code is executed? List<Box> alist = new ArrayList<>(); alist.add(new Box(1)); alist.add(new Box(3)); List<Box> blist = new ArraylList<>(); for (Box b: alist) blist.add(b); alist.get(0).setSide(4); int k = blist.get(9).getSide(); Write the answer without adding anything to it (such as leading or trailing text, = signs, Answer: 4 The correct answer is: 4 https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 Final Test: Attempt review ...). Write XXX in case of errors. v 13/21
2020/12/22 Final Test: Attempt review Question 23 Correct Mark 1.00 out of 1.00 Given class Student below: public class Student { public String name; public Student(String name) { this.name = name; What is the size of aMap after executing the following Java program fragment? Map<Student,String> aMap = new HashMap<>(); aMap.put(new Student("StdA"), "A"); aMap.put(new Student("StdB"), "B"); aMap.put(new Student("StdA"), "A"); Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 3 v The correct answer is: 3 Question 24 Correct Mark 1.00 out of 1.00 For what value of the parameter m would the return of this method be 37 If there is more than one such value, write any one of them (and only one) as your answer. public int method(int m) { int[]a={1, 3,0, 5, 7, 8, 12 }; int count = 0; for (int n : a) { count++; if (m==m) { break; } } return count; Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 0 The correct answer is: 0 https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 v 14/21
2020/12/22 Final Test: Attempt review Question 25 Incorrect Mark 0.00 out of 1.00 What is the size of aset after the following Java code is executed? Str‘ing[] a - {"AAA", "AA", "AAA", IIBBBII}; Set<String> aset = new HashSet<>(Arrays.asList(a)); aset.addAll(Arrays.asList(a)); Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 6 X The correct answer is: 3 Question 26 Correct Mark 1.00 out of 1.00 For what value of the parameter m would the return of this method be 37 If there is more than one such value, write any one of them (and only one) as your answer. public int method (int m) { Map<Integer, Integer> amap = new HashMap<>(); amap.put(1, 1);amap.put(3, 12); amap.put(2, m);amap.put(-1, 23); if (amap.containsKey(6)) return amap.get(2)+amap.get(-1); else return amap.get(2)+amap.get(1); } Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 2 v The correct answer is: 2 https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 15/21
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2020/12/22 Final Test: Attempt review Question 27 Correct Mark 1.00 out of 1.00 Given the following function f3, give the best order of growth of the running time (i.e., The time complexity using Big-O notation). public int 3 (int N) { int x = 0; for (int i = 1; i <= N; i++) { for(int j =0; j < N - i; j++) { X++; } ¥ return x; Select one: O(1) O(N) O(logN) O(Nlog N) © O(N?) v Runtime Exception Compilation errors none of the above The correct answer is: O( N2 ) Question 28 Correct Mark 1.00 out of 1.00 What will be the value of element at index 3 of the alist after the following Java code is executed? Integer[] a = {11,8,3,7,5,11}; List<Integer> alist = new ArraylList<>(Arrays.asList(a)); for(int i=0; i<alist.size()-1;i++) { if (alist.get(i)<alist.get(i+l)) alist.set(i+1l, alist.get(i)*3); } Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 9 v The correct answer is: 9 https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 16/21
2020/12/22 Final Test: Attempt review Question 29 Correct Mark 1.00 out of 1.00 What is the size of aset after executing the following Java program fragment? String pattern = "(ab)+c?"; String text = "ab+c abb aabb cc aaabbb abe ac ae"; Set<String> aSet = new HashSet<>(); Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); while (m.find()) { aSet.add(text.substring(m.start(), m.end())); } Write the answer without adding anything to it (such as extra quotes, leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 1 v The correct answer is: 1 Question 30 Correct Mark 1.00 out of 1.00 Suppose that we have four String variables, s1, s2, s3, and s4 and that we want to set s4 to the result of inserting s3 into s1 before and after the first occurrence of s2 in s1. For example, if s1 is "abcdeacd”, s2 is "cd" and s3 is "XYZ", s4 should be set to "abXYZcdXYZeacd". Which of the following statements does that (assume that s2 does occur in s1)? Select one: a. s4 = s1.substring(0, s1.indexOf(s2)) + s3; b. s4 s1.substring(0, s1.indexOf(s2)) + s3 + s2 + s3 + s1.substring(s1.indexOf(s2)); © ¢ s4 = sl.substring(0, s1.indexOf(s2)) + s3 + s2 + s3 + s1.substring(s1.indexOf(s2)+s2.length()); d. s4 = s1.replaceAll(s2,s3); e. s4 = sl.replaceFirst(s2,s3); The correct answer is: s4 = s1.substring(0, s1.indexOf(s2)) + s3 + s2 + s3 + s1.substring(s1.indexOf(s2)+s2.length()); https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 17/21
2020/12/22 Final Test: Attempt review Question 31 Incorrect Mark 0.00 out of 1.00 If we create an object of the class below using "TestD objD = new TestD();" then the value of the instance variable var1 of objD would be: public class TestD { private int varl; public TestD() { this.varl = 9; } public void TestD() { this.varl = 15; } } Write the answer without adding anything to it (such as extra quotes, leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 15 b 9 The correct answer is: 9 Question 32 Incorrect Mark 0.00 out of 1.00 What is the value stored in the instance variable a of the object objA (TestE shown below) after executing this client fragment? Client fragment; TestE objA = new TestE() ; TestE objB new TestE(2); TestE ob]jC = new TestE(4); The TestE class: public class TestE { private int a = 1; public static int b = 3; public statiec String str =""; public TestE() { this.a = 3; b++; str+="A"; } public TestE(int c) { this.a += c; b++; str+="B"; } Write the answer without adding anything to it (such as leading or trailing text, = signs, ...). Write XXX in case of errors. Answer: 4 The correct answer is: 3 https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 18/21
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2020/12/22 Final Test: Attempt review - Test2 Jump to... https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 19/21
2020/12/22 Final Test: Attempt review https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 20/21
2020/12/22 Final Test: Attempt review https://eclass.yorku.ca/eclass/mod/quiz/review.php?attempt=863173&cmid=431492 21/21
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help