1 / 63

01-Control

01-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);.

ananda
Download Presentation

01-Control

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. 01-Control Structures

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

  3. 02. Consider the following code segment. int n = <some integer greater than zero> int count = 0; int p = 0; int q = 0; for (p = 1; p < n; p++) for (q = 1; q < n; q++) count++; System.out.println(count); What is the value of count when the code finishes executing? (A) n2 (B) n2 - 1 (C) (n - 1)2 (D) p * q (E) both C and D

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

  5. 02-Methods & Parameters

  6. 04. Consider the following program. public class Method04 { 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

  7. 05. Consider the following program. public class Method05 { 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 (C) 5 4 (D) Exception error

  8. 06. public class Method06 { public static void main (String args[]) { System.out.println(tango(120,108)); } public static int tango(intn1, intn2) { int temp = 1; int rem = 1; while (rem != 0) { rem = n1 % n2; if (rem == 0) temp = n2; else { n1 = n2; n2 = rem; } } return temp; } } What is printed as a result of executing the program? (A) 120 (B) 108 (C) 12 (D) 9 (E) 1

  9. 03-OOP Encapsulation

  10. 07. Consider the following Aardvark class and code segment. Aardvark a = new Aardvark(100); System.out.println(a.getAardvarks()); class Aardvark { private intnumAardvarks; public void Aardvark(int aardvarks) { numAardvarks = aardvarks; } public intgetAardvarks() { return numAardvarks; } } What is printed by the code segment? (A) 0 (B) 75 (C) The program compiles, but there is a logic error. (D) The program compiles, but there is a runtime error. (E) The program does not compile.

  11. 08. Consider the following code • segment and List class. • List list = new List(100,55); • list.showList(); • System.out.println(); • class List • { • private int[] list; • /* missing constructor */ • public void showList() • { • for (int k = 0; k < list.length; k++) • System.out.print(list[k] + " "); • } • } The output of executing the code segment is a row of one hundred 55 numbers. Which of the following constructor implementations of /* missing constructor */ will make the List class work as intended?

  12. 09. Consider the following two classes and code segment.   class Rumba { private Mambo dance; public Rumba() { System.out.println("Executing the Rumba constructor"); dance = new Mambo(); } } class Mambo { public Mambo() { System.out.println("Executing the Mambo constructor"); } } Mambo m = new Mambo(); Rumba r = new Rumba(); What is printed by the code segment? (A) Executing the Rumba constructor Executing the Mambo constructor (B) Executing the Mambo constructor Executing the Rumba constructor (C) Executing the Rumba constructor Executing the Mambo constructor Executing the Rumba constructor (D) Executing the Mambo constructor Executing the Rumba constructor Executing the Mambo constructor (E) Executing the Mambo constructor Executing the Mambo constructor Executing the Rumba constructor

  13. 04-Boolean Logic

  14. 10. The Boolean expression (A || B) && A is true (A) whenever A is true. (B) whenever B is true. (C) whenever either A is true or B is true. (D) whenever both A is true and B is true. (E) in all cases.

  15. 11. Consider the expression below. A || ( (B || !C) && (!D && F) || !(G && H) && !(!J || !K) ) If all you know is that A equals true, what can you determine about the expression above? (A) The expression evaluates to true. (B) The expression evaluates to false. (C) The expression evaluates to true only if B equals true. (D) The expression evaluates to true only if B equals false. (E) The expression cannot be evaluated with the provided information.

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

  17. 05-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; } } 13. 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 intgetAge() { 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. 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; } } 14. 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 intgetAge() { 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 

  20. 15. Consider the following two classes and code segment. class Person { private int age;   public Person(int a) { age = a; }   public void showData() { System.out.println(age); } } class Student extends Person { private int grade;   public Student(int g, int a) { super(a); grade = g; }   public void showData() { System.out.println(grade); super.showData(); } } Person sue = new Person(25); Student tom = new Student(17,12); sue.showData(); tom.showData(); What is printed by the code segment? (A) 25 17 (B) 17 25 (C) 25 17 12 (D) 17 12 25

  21. 06-Static Arrays

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

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

  24. 18. Consider the following code segment. double [ ][ ] values = new double[10][15]; int sum = 0; Each row in the array holds (in order) a student number, 10 homework grades of equal weight, and 4 exam grades. Choose the code segment below that would sum the homework grades in the sixth row and place them in the integer variable sum. (A) for (int j = 0; j < 15; j++) sum += values[6][j]; (B) for (int j = 0; j < 10; j++) sum += values[5][j]; (C) for (int j = 1; j < 11; j++) sum += values[j][6]; (D) for (int j = 0; j < 10; j++) sum += values[j][5]; (E) for (int j = 1; j < 11; j++) sum += values[5][j];

  25. 07-Dynamic Arrays

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

  27. 20. 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.set(2,names.get(3)); names.set(3,names.get(2)); System.out.println(names); What is printed as a result of executing the code segment? (A) [Isolde, John, Greg, Maria, Greg, Heidi] (B) [Isolde, John, Maria, Greg, Heidi] (C) [Isolde, John, Greg, Maria, Heidi] (D) [Isolde, John, Maria, Maria, Heidi] (E) [Isolde, Greg, John, Maria, Heidi]

  28. 21. Consider the following code segment. ArrayList<String> names1 = new ArrayList<String>(); names1.add("Isolde"); names1.add("John"); names1.add("Greg"); names1.add("Maria"); names1.add("Heidi"); ArrayList<String> names2 = names1; names1.set(1,"Jessica"); names2.set(4,"Haley"); System.out.println(names1); System.out.println(names2); What is printed as a result of executing the code segment? (A) [Isolde, Jessica, Greg, Maria, Heidi] [Isolde, John, Greg, Maria, Haley] (B) [Isolde, Jessica, John, Greg, Maria, Heidi] [Isolde, John, Greg, Maria, Haley, Heidi] (C) [Isolde, John, Jessica, Greg, Maria, Heidi] [Isolde, John, Greg, Maria, Heidi, Haley] (D) [Isolde, John, Jessica, Greg, Maria, Haley, Heidi] [Isolde, John, Jessica, Greg, Maria, Haley, Heidi] (E) [Isolde, Jessica, Greg, Maria, Haley] [Isolde, Jessica, Greg, Maria, Haley]

  29. 08-String Methods

  30. 22. Consider the following method. public static String method1(String str) { String temp = ""; inteSpot = str.indexOf('e'); temp = str.substring(0,eSpot); return temp; } What value does the method return if it is passed Entree as an argument? (A) An empty string (B) E (C) Entr (D) Entre (E) Entree

  31. 23. Consider the following code segment. String s1 = "Invalid"; String s2 = "valid"; s1 = s1.substring(2); booleanisSame; if (s1 == s2) isSame = true; else isSame = false; What is the value of isSame after the code segment executes? (A) True, because the two strings both hold the same value. (B) False, because the == operator compares immediate values, which are memory references for String objects. (C) Nothing, because using == to compare strings will give a run-time error. (D) True, because s1 and s2 now reference the exact same String object. (E) False, because s1 and s2 are not the same variable name.

  32. 24. Consider the following method. public static String method2(String[] values) { String temp = values[0]; for (inti = 1; i < values.length; i++) { if (values[i].compareTo(temp) > 0) temp = values[i]; } return temp; } What will the method return if the following array is sent as the argument?  String[] arr = {"Math","ComputerScience","ComputerApplications","Science","Graphic Design"}; (A) "Math" (B) "Computer Science" (C) "Computer Applications" (D) "Science" (E) "Graphic Design"

  33. 09-OOP Composition

  34. 25. Consider the following program. Car car = new Car("Ford250",350); class Engine { private inthorsePower; public Engine(inthp) { horsePower= hp; } } class Car { private Engine engine; private String carMake; /* MISSING CONSTRUCTOR */ } Question-25 continues on the next slide

  35. 25 continued. Which of the following implementations of MISSING CONSTRUCTOR will correctly initialize the instance variables of Engine and Car ?

  36. 26. public class Test26 { public static void main(String args[]) { Pidgit p = new Pidgit(10,20); } } class Widgit { private intnumWidgits; public Widgit(intnw) { numWidgits = nw; } } class Pidgit extends Widgit { private intnumPidgits; private Widgitwidgit; public Pidgit(intnp, intnw) { numPidgits = np; widgit = new Widgit(nw); } } What is the class interaction? (A)Inheritance (B)Composition (C)Both inheritance & composition (D)No class interaction (E) Improper class interaction

  37. 27. Consider this is a partial BoundedGridclass declaration. public class BoundedGrid<E> extends AbstractGrid<E> { private Object[ ][ ] occupantArray; public BoundedGrid(int rows, int cols) { if (rows <= 0) throw new IllegalArgumentException("rows <= 0"); if (cols <= 0) throw new IllegalArgumentException("cols <= 0"); occupantArray = new Object[rows][cols]; } The BoundedGridclassuses a _______________ array to store data. (A) generic, two-dimensional (B) static, two-dimensional (C) dynamic, two-dimensional (D) generic, one-dimensional array (E) dynamic, one-dimensional array

  38. 10-Program Algorithms

  39. 28. Consider the following code segment. String[ ] stID = {"ABC", "DEF", "GHI", "JKL", "MNO"}; String str1 = "GHI"; boolean found = false; intloc = -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. 29. 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. 30. Consider the following array: int[ ] array = {4, 8, 2, 7, 1, 9, 3, 5}; What would array hold after three iteration passes of the outer loop in an ascending selection sort algorithm? (A) {1, 2, 3, 4, 8, 7, 9, 5} (B) {2, 4, 7, 8, 1, 9, 3, 5} (C) {1, 2, 3, 7, 4, 9, 8, 5} (D) {4, 2, 7, 8, 1, 9, 3, 5} (E) {4, 3, 2, 5, 1, 7, 8, 9}

  42. 12-GridWorld Case Study

  43. 34. Consider the following GridWorld display. How many Bug objects will be left after the next Step iteration? (A) 1 bug (B) 2 bugs (C) 3 bugs (D) 4 bugs (E) 5 bugs

  44. 35. Consider the following MysteryCritter class. class MysteryCritter extends Critter { public void turn() { int add = (int) (Math.random()*91)+90); setDirection(getDirection() + add); } } How many degrees will a MysteryCritter object turn during one step of a GridWorld execution?  (A) 0 (B) 90 (C) 180 (D) A random number in the range of 90 - 180 (E) A random number in the range 0 - 90

  45. 36. You are asked to implement a new GridWorld class, called DynamiteCritter. DynamiteCritterobjects act exactly like Critter objects, except that they only process Rock objects by blowing them up. Implementing this new DynamiteCritterclass will require re-defining (A) all the act methods. (B) the getActors method only. (C) the processActors method only. (D) methods getActors and processActors only. (E) methods getMoveLocations, selectMoveLocation and makeMoveonly.

  46. 13-OOP Polymorphism

  47. 37. Consider the following code segment from the step method used by the ActorWorld class. The step method is called each time the Stepbutton is clicked on the Gridworld display or repeatedly after the Run button is clicked. for (Actor a : actors) { if (a.getGrid() == gr) a.act(); } Which of the following statements completes a correct explanation how the loop executes, shown above? Every object which occupies a cell on the current grid, when the step method is called, will execute according to the act method definition of (A) the Actor class. (B) its own class. (C) its own class, provided the object is still a member of the grid. (D) its own class or a superclass. (E) its own class or a superclass, provided the object is still a member of the grid.

  48. Consider the following class definitions and code segment. ClassAitem1 = new ClassA(); ClassAitem2 = new ClassB(); ClassAitem3 = new ClassC(); item1.method(10); item2.method(20); item3.method(30); class ClassA { public ClassA() { } public void method(int a) {System.out.println("Class A " + a);} } class ClassB extends ClassA { public ClassB(){ } public void method(int b) {System.out.println("Class B " + b);} } class ClassC extends ClassB { public ClassC(){ } } What is printed as a result of executing the code segment? (A) Class A 10 Class A 20 Class A 30 (B) Class A 10 Class B 20 Class B 30 (C) Class A 10 Class B 20 (D) Class A 10 Class B 20 Class B 20 (E) Exception error message

  49. 39. Consider the following interface, two classes and incomplete code segment. abstract interface Shape { public abstract double getArea(); } class Circle implements Shape { private double radius; public Circle (double r) {radius = r;} public double getArea() {return Math.PI * radius * radius;} } class Triangle implements Shape { private double base; private double height; public Triangle (double b, double h) {base = b; height = h;} public double getArea() {return (base * height) / 2;} } Shape[ ] shapes = new Shape[2]; shapes[0] = new Circle(3); shapes[1] = new Triangle(4,3); /* missing code segment */ Question-39 continues on the next slide.

  50. Question 39 Continued. Which of the following implementations of /* missing code segment */ will correctly display the areas of the shapes array elements. Implementation I: for (int k = 0; k < shapes.length; k++) System.out.println(shapes[k].getArea()); Implementation II: for (Shape s: shapes) System.out.println(s.getArea()); Implementation III: for (int k = 0; k < shapes.length; k++) if (shapes[k] instanceof Circle) System.out.println(Circle.getArea()); else if (shapes[k] instanceof Triangle) System.out.println(Triangle.getArea()); (A) Implementation I only (B) Implementation III only (C) Implementations I & II only (D) Implementations II & III only (E) Implementations I, II & III

More Related