COP3804

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is displayed by the method call displayTime(2, 24, 65)? if(hr < 1 || hr > 12){ System.out.print("Error: hour "); Hr = 1; } if(min < 1 || min > 59){ System.out.print("Error: minute "); Min = 1; } if(sec <1 || sec > 59){ System.out.print("Error:second "); Sec = 1; } System.out.println(hr + ":" + min +":" + sec); }

Error: second 2:24:1

What is the output for the following program segment? public class Vehicle{ protected String vehivleName; public void setName(String name){ vehicleName = name; } public String toString(){ Return vehicleName; } } public class Boat extends Vehicle{ private int numEngines; public Boat(int num){ numEngines = num; } public String toString(){ return super.toString() + " " + numEngines; } } Boat fastBoat = new Boat(2); fastBoat.setName("Firebird"); System.out.println(fastBoat);

Firebird 2

What is the output of the following code segment? Public class Vehicle{ public void drive(){ System.out.println("Driving vehicle"); } } public class Plane extends Vehicle{ @Override public void drive(){ System.out.println("Flying plane"); } } public static void main(String args[]){ Vehicle myVehicle = new Plane(); myVehicle.drive(); }

Flying plane

What is the output of the following code segment? Deque<String>myColors = new linkedlist<String>(); myColors.addFirst("Violet"); myColors.addLast("Indigo"); myColors.addLast("Blue"); myColors.addFirst("Green"); System.out.println(myColors.removeFirst()); System.out.println(myColors.removeLast());

Green Blue

What is the output of the following code segment? Import java.util.ArrayList; public class SimpleCar{ @Override public String toString(){ return "I drive fast"; } } public static void main(String[] args){ ArrayList<Object> myStuff; myStuff = new ArrayList <Object>(); myStuff.add(new String("Greetings")); myStuff.add(new Object()); myStuff.add(new SimpleCar()); for(Object item: myStuff) System.out.println(item.toSting()); }

Greetings java.lang.Object@169b I drive fast

Which type of relationship exists between School and Student? public class Student{ private String name; } public class School{ Student s; }

Has-a

Which UML diagram best represents the relationship depicted in the following code? public abstract class People{ private int age; private String name; public abstract void printInfo(); } public class Student extends People{ private int grade; public void printInfo(){}; } public class Teacher extends People{ private int experience; public void printInfo(){}; }

PEOPLE. Teachers and Students point up towards People.

Which of the following is not true?

Quicksort randomly divides the data into low and high segments and recursively sorts each segment

Given Queue<String> colors; with elements["Red", "Green", "Blue"], what is the output of the following code segment? System.out.println(colors.peek()); colors.poll(); System.out.println(colors.peek());

Red Green

Which of the following is true about the merge sort algorithm?

The merge sort algorithm treats the input as two segments, recursively sorts each segment, and then merges the two sorted segments to produce a sorted list

What does the method m() given below do? Public class Roster{ Private ArrayList<Student>list; Public Student m(){ Student student1 = list.get(0); Double gpa = student1.getGPA(); for(int i = 1; i < list.size(); ++i){ Student s = list.get(i); if(s.getGPA() > gpa){ student1 = s; } } Return student1; } }

Returns the student with the highest GPA

Which sorting algorithm treats the input as two segments, a sorted segment and an unsorted segment, and repeatedly moves the smallest value from the unsorted segment to the end of the sorted segment?

Selection sort

What is the output for the following program: public interface Printable{ public void print(); } public abstract class people{ protected String name; public people(String name){ This.name = name; } } public class Student extends people implements Printable{ public Student(String name){ super(name); } public void print(){ System.out.println("Student: " + name); } } public class Teacher extends people implements Printable{ public Teacher(String name){ super(name); } public void print(){ System.out.println("Teacher: " + name); } } public class Test{ public static void show(ArrayList<printable>Printables){ for(printable s: printables) s.print(); } public static void main(String[] args){ ArrayList<Printable>printables = new ArrayList<Printable>(); printables.add(new Teacher("Adam Smith")); printables.add(new Student("Hector Rivas")); printables.add(new Student("Mary Jones")); show(printables); } }

Teacher: Adam Smith Student: Hector Rivas Student: Mary Jones

Method overloading facilitates compile-time polymorphism

True

What is the output of the following code segment? Public class Student{ private double myGPA; private int myCredits; Public void increaseCredits(int amount){ myCredits = myCredits + amount; } Public void setCredits(int credits){ myCredits = credits; } Public int getCredits(){ Return myCredits; } Public static void main(String [] args){ Student s = new Student(); s.setCredit(6); s.increaseCredits(12); System.out.println(s.getCredits()); } }

18

What is the output produced by method call f(1, 2, 0);? public void f(int a, int r, int counter){ int val; val = a*r; System.out.print(val + " "); if(counter > 4) System.out.print("End"); else f(val, r, counter + 1); }

2 4 8 16 32 64 End

What is the second value checked when searching for 15 using binary search? int[] a = [1, 3, 4, 5, 6, 10, 12, 15, 20, 30, 40];

20

What is the final value of y? Int x = 6; Int y = 2; if(x < 10){ if(y<5){ Y = y + 1; } Else{ Y = 7; } } Else{ Y = y+10; }

3

Using selection sort, how many times longer will sorting a list of 40 elements take compared to a list of 5 elements?

32

How many elements of a sorted array with 16 elements will a binary search algorithm check when searching for a value that is less than all the elements in the array?

4

How many elements of the array int[] a=[4, 7, 12, 17, 19, 22] will be checked by the following search method when called as: search(a, 6, 16) public int search(int[] s, int size, int v){ int i = 0; while(i < size) if(s[i] == v) return(i); else if (s[i] > v) return(-1); else i = i + 1; return(-1); }

4

What is the size of groceryList after the code segment given below executes? ArrayList<String> groceryList; groceryList = new ArrayList<String>(); groceryList.add("Bread"); groceryList.add("Apples"); groceryList.add("Grape Jelly"); groceryList.add("Peanut Butter"); groceryList.set(1, "Frozen Pizza");

4

What is the output of the following program? Public static void main(String[] args){ Int i = 4; While (i >= 0){ System.out.print(i + " "); for(int j = 0; j < i; j++) if(j%2 == 1) System.out.print(j + " "); I = i-1; } }

4 1 3 3 1 2 1 1 0

Given an array of size 10, how many comparisons will be made by insertion sort in the worst-case?

45

How many elements of the following array will be checked when searching for 15 using linear search? int[] a =[1, 10, 3, 6, 15, 20, 5, 12, 4]

5

What is the output of the program segment given below? ArrayList<Integer> numList = new ArrayList<Integer>(); Int count; for(count = 1; count < 10; ++count){ numList.add(count); } System.out.println(numList.get(4));

5

Given Deque<Integer>intDeque; with elements [3, 4, 5] what is the output of the code segment given below? System.out.println(intDeque.peekLast()); System.out.println(intDeque.peekLast());

5 5

What is the output produced by method call couNum(5);? public void countNum(int num){ if(num <= 0) System.out.println("Done"); else{ System.out.print(num + " "); countNum(num - 2); } }

5 3 1 Done

What is in the array x after the following program segment completes execution? int i; int[] x ={4, 7, 3, 0, 8}; for(i = 0; i < 4; ++i){ X[i] = x[i+1]; }

7, 3, 0, 8, 8

What is the output of the following program segment? public static void swap(int[] first, int[] second){ int[] temp = first; first = second; second = temp; } public static void main(String[] args){ int[] a = {72, 67, 85}; int[] b = {1, 5, 2, -4, 3, -6}; for(int x : a) System.out.print(x + " "); System.out.println(); for(int x : b) System.out.print(x + " "); System.out.println(); swap(a, b); for(int x : a) System.out.print(x + " "); System.out.println(); for(int x : b) System.out.print(x + " "); System.out.println(); }

72 67 85 1 5 2 -4 3 -6 72 67 85 1 5 2 -4 3 -6

What is the output for the programsegment given below? public static void add(int value, int[] a){ value = value + 5; for(int x : a){ x = x + value; System.out.print(x + " "); } System.out.println(); } public static void main (String[] args){ int[] a = {72, 67, 85}; int amt = 10; add(amt, a); for(int x : a) System.out.print(x + " "); System.out.println(); } }

87 82 100 72 67 85

How many elements of the following array will be checked when searching for 19 using linear search? int[] a =[1, 10, 3, 6, 15, 20, 5, 12, 4]

9

Which function best represents the number of operations in the worst-case for the following code fragment? Assume that each operation takes 1 unit of time to complete. start = 0; while(start < N) ++start;

f(N) = 2N + 2

Which of the following declares an array of size MAX and initializes all its elements to -1?

final int MAX = 4; int[] num = new int[MAX]; int i; for(i = 0; i < MAX; ++i) Num[i] = -1;

Let array a be defined as: char array[] = {'a', 'b', 'c', 'd', 'e'}; What are the calls to the method find given below after statement: int i=find(array, 0, 4, 'e') public int find(char a[], int low, int high, char key){ if(high >= low){ int mid= low + (high - low) / 2; if(a[mid] == key) return mid; if(a[mid] > key) return find(low, mid, key); else return find(mid + 1, high, key); return -1; } }

find(a, 3, 4, key) and find(a, 4, 4, key)

Which method of the base class has been overridden in the derived class? public class Vehicle{ public void setID(int pID){} public String getName(){} } public class Plane extends Vehicle{ public Plane(){} public void setID(int pID1, int pID2){} public void getName(String name){} public String getName(){} }

getName()

What makes the following a correct binary search method?(i.e. what should replace the line /* What goes in here ?*/) public static boolean binSearch(int[] s, int size, int v){ int low = 0; int high = size - 1; int i; while(high >= low){ /*What goes in here?*/ if(s[i] == v) return(true); else if(s[i] > v) high = i - 1; else low = i + 1; } return (false); }

i = (high + low)/2;

Which of the following code segments displays the smallest value in the array a defined below: Int[] a = new int[100];

int min = a[0]; for(int i = 0; i < a.length; i++) if(min > a[i]) min = a[i] System.out.println(min);

Which statement declares a two-dimensional integer array a with 3 rows and 4 columns?

int[][] a = new int[3][4];

A(n) ___ provides abstract methods and no other code.

interface

Which statement in main() generates an error? public class Player{ public void setName(String newName){} public void setAge(int newAge){} } public class SoccerPlayer extends Player{ public void setDetails(String newName){} public String getLeague(){} } public static void main(String args[]){ String leagueName; Player newPlayer = new Player(); SoccerPlayer playerObject = new SoccerPlayer(); playerObject.setName("Jessicca Smith"); playerObject.setAger(21); leagueName = newPlayer.getLeague(); }

leagueName = newPlayer.getLeague();

What is the error in the following abstract class? public abstract class people{ private String name; public people(String name){ this.name = name; } private void m1(){ System.out.println("m1()"); m2(); } private abstract void m2(); }

m2() is private. Abstract methods cannot be private.

Which of the following facilitates runtime polymorphism?

method overriding

Let ArrayList, myList contain 10,000 integers and assume that the memory location after the last element of myList is free. Which operation of the following operation is performed the slowest?

myList.add(0, 0)

Let LinkedList myList contain 10,000 items. Which operation is performed slowest?

myList.get(5000)

What is stored in score1, score 2,and grade? Integer score1 = 72; Int score2 = 85; Character grade = 'C';

obj reference, 85, obj reference

An ___ method shares the ame name but a different number of parameters with another method in the same class

overloaded

A derived class method with the same name, parameters, and return type as a base class method is said to ___ the base class's method.

override

What is the output? Public static void swapValues(int x, int y){ Int tmp; Tmp = x; X = y; Y = tmp; } Public static void main(String args[]){ Int p = 4, q = 3; swapValues(p, q); System.out.print("p =" + p + ",q = " + q); }

p = 4, q = 3

Which members of the base class Player are inherited by SoccerPlayer? public class Player{ private int playerAge; private String playerName; public void setName(Sting newName){} public void setAge(int newAge){} public void printDetails(){} } public class SoccerPlayer extends Player{ private String teamName; public void setDetails(String newName){} public String getLeague(){} }

playerAge, playerName, setName, setAge, and printDetails

Select the signature for a helper method that calculates the student's GPA in the class Student given below. Public class Student{ Private doublemyGPA; ... }

private void calcGPA()

Which of the following may be a default constructor of the class Employee? Public class Employee{ Private double mySalary; ... }

public Employee(){ mySalary = 10000; }

Which of the following methods correctly returns the value of its parameter in binary?

public String convertToBinary(int num){ if(num <= 1) return num +""; //converts num to a string else return (convertToBinary(num/2)+(num%2)); }

Which of the following is a valid abstract class?

public abstract class A {public abstract void m();}

Which of the following Node classes contains the correct insertAfter() method to insert a node after the current node?

public class ItemNode{ private int data; private Node next; public void insertAfter(Node p){ Node tempNode = this.next; this.next = p; p.next = tempNode; } }

Which of the following methods correctly raises its first parameter (base) to the power its second parameter (e) and returns the results(i.e. returns base^e)

public double raiseToPower(double base, double e){ if(e > 0) return (base* raiseToPower(base, e-1)); else return 1; }

Which of the following is a valid interface?

public interface int1{ public void m1(); public void m1(int a); }

Which of the following assigns 8 to the 7th element of the array scores defined below: Int[] scores = new int[10];

scores[6] = 8;

Select the statement that can be used to assign the argument of themethod setGPA to the instance member gpa? Public class Student{ Private double gpa; Public void setGPA(double gpa){ ... } }

this.gpa = gpa;

What is the content of the array{10, 20, 80, 40, 90, 70} after the first partition of quick sort?

{10, 20, 70, 40, 90, 80}

Which of the following is true?

Concrete classes do not have any abstract methods

What is the worst-case time complexity of the quick sort?

O(n^2)

Which condition(s) cause the following recursive method reach its base case(s)? public int find(char array[], int low, int high. char key){ if (high >= low){ int mid = low + (high - low) / 2; if(array[mid] == key) return mid; if(array[mid] > key) return find(array, low, mid, key); else return find(array, mid + 1, high, key); } return -1; }

(high < low)||(high >= low) && (array[mid] == key)

Given function Multiply(u, v) that returns u * v, and Add(u, v) that returns u + v, which expression corresponds to Add(Multiply(x, Add(y, z)), w)

(x*(y+z)) + w

What is the output of the following program segment? public static void swap(int[] first, int[] second){ int temp; for(int i = 0; i < first.length; i++){ temp = first[i]; first[i] = second[i]; second[i] = temp; } } public static void main(String[] args){ int[] a = {1, 5, 2}; int[] b = {-4, 3, -6}; swap(a,b); for (int x : a) System.out.print(x + " "); System.out.println(); for(int x : b) System.out.print(x + " "); System.out.println(); }

-4 3 -6 1 5 2

How many objects of type Car are created by the code segment given below? Car mustang = new Car(); Car prius; Int miles; Truck tundra= new Truck();

1

How many times is the method count() given below called after main() calls count(9)? public void count(int num){ if(num >= 10) System.out.println(num); else{ System.out.print("Bye"); count(num + 1); } }

1

What is the value returned by method call f(-5); ? public static int f(int v){ if(v <= 0) return 1; else return v*f(v - 1); }

1

What is y after executing the statements? X = 4; Y = x + 1; X = 3; Y = y*2;

10

What is the output? Int num = 10; while(num <= 15){ System.out.print(num + " "); if(num == 12){ break; } Num = num+1; } System.out.print("Done");

10 11 12 Done

How many elements of a sorted array with 16 elements will a linear search algorithm check when searching for a value that is less than all elements in the array?

16

What is the worst-case time complexity of the Merge sort?

O(nlog(base2)(n))

What is printed by this program? public interface Printable{ public void print(); } public class Student implements Printable{ private String name; public Student(String name){ this.name = name; } public void print(){ System.out.println("Student: " + name); } } public class Teacher implements Printable{ private String name; public Teacher(String name){ this.name = name; } public void print(){ System.out.println("Teacher: " + name); } } public class Test2{ public static void show(Printable p){ p.print(); } public static void main(String[] args){ Printable p1 = new Teacher("Adam Smith"); Printable p2 = p1; show(p2); p1 = new Student("Hector Rivas"); show(p2); } }

???

What is the output of the following code segment? public class A{ public void m1(){ System.out.println("A: m1"); } } public class B extends A{ @Override public void m1(){ System.out.println("B: m1"); } public void m2(){ System.out.println("B: m2"); } } public static void main(String[] args){ B obj1 = new A(); obj1.m2(); }

???

Which of the following is true?

A class can implement multiple interfaces

If x is 0 and y is 10, which of the following expressions evaluates to false?

A: (x==0)&&(y==20)

What is the output? Int n = 3; for(int i = 1; i <= n; ++i) { Int result = 1; Result = result * i; System.out.print(result + " "); }

A: 1 2 3

What is the output of the following code segment? public class A{ public void m1(){ System.out.println("A: m1"); m2(); } public void m2(){ System.out.println("A: m2"); } } public class B extends A{ @Override public void m1(){ System.out.println("B: m1"); m2(); } @Override public void m2(){ System.out.println("B: m2"); } public static void main(String[] args){ A obj1 = new A(); B obj2 = new B(); obj1.m1(); obj1 = obj2; obj1.m1(); }

A: m1 A: m2 B: m1 B: m2

What is the output of the following code segment? public class A{ public void m1(){ System.out.println("A: m1"); } public void m2(){ System.out.println("A: m2"); } } public class B extends A{ @Override public void m1(){ System.out.println("B: m1"); } } public static void main(String[] args){ A obj1 = new B(); obj1.m2(); }

A:m2

Which of the following statements is true?

An array is passed to a method as a reference

What is the output of the following code segment? public class A{ public void m1(){ System.out.println("A: m1"); m2(); } public void m2(){ System.out.println("A: m2"); } } public class B extends A{ @Override public void m1(){ System.out.println("B: m1"); m2(); } } public static void main(String[] args){ A obj1 = new B(); obj1.m1(); }

B:m1 A:m2

What is the output of the following code segment? public class A{ public void m1(){ System.out.println("A: m1"); m2(); } public void m2(){ System.out.println("A: m2"); } } public class B extends A{ @Override public void m1(){ System.out.println("B: m1"); m2(); } @Override public void m2(){ System.out.println("B: m2"); } } public static void main(String[] args){ A obj1 = new B(); obj1.m1(); }

B:m1 B:m2

Considering classes Vehicle and Car given below, which statement is true? public class Vehicle { protected String name; private int ID; } public class Car extends Vehicle{ protected int miles; }

Car members have access to Vehicle name

What is displayed by the following code fragment? int[] a = {1, 5, 2}; for (int i = a.length; i > 0; i--) System.out.print(a[i] + " ");

Error

What is displayed by the method call printName("Juan", 19); Public static void printName(Sting name, int id) { System.out.print(name + " ID: " + id); } Public static void printName(int id) { System.out.print("Name" + " ID: " + id); } Public static void printName(Sting name, int id, int age) { System.out.print(name + " ID: " + id + " age: " + age); }

Juan ID: 19

In the following UML class diagram, +calculateScore(); int depicts a ___

Method name which returns an int value and with a public access specifier

What is the output of the program segment? public class KitchenAppliance{ protected String appName; protected String appUse; public void setDetails(String name, String use){ appName = name; appUse = use; } public void printDetails(){ System.out.println("Name: " + appName); System.out.println("Use: " + appUse); } } public class Blender extends KitchenAppliance{ private double appPrice; void setPrice(double price){ appPrice = price; } public void printDetails(){ super.printDetails(); System.out.println("Price: $" + appPrice ); } public static void main(String[] args){ Blender mxCompany = newBlender(); mxCompany.setDetails("Blender", "blends food"); mxCompany.setPrice(145.99); mxCompany.printDetails(); }

Name: Blender Use: blends food Price: $145.99 Quiz answer which had only wrong answers as options: Name: MyMixer Use: blends food Price: $145.99

Which of the following correctly implements linear search algorithm?

None of the search methods correctly implements linear search

What is the output of the following code segment? public class A{ public void m1(){ System.out.println("A: m1"); } } public class B extends A{ @Override public void m1(){ System.out.println("B: m1"); } } public static void main(String[] args){ A obj1; ...//some valid code obj1.m1(); }

Not enough information to determine the output

What is the Big O notation for the worst-case runtime of the code segment given below? count = 0; extra = 0; for(i = 0; i < N; ++i) if(numbers[i] < 0){ ++count; ++extra; }

O(N)

Which of the following is not a true statement?

We can create instances of an abstract class using an operator

Which of the following may lead to infinite recursion?

Writing a recursive method that does not always reach a base case

Given LinkedList<String>dances; with elements ["Salsa", "Jazz", "Waltz", ] what does items contain after the following code segment is executed? String str; ListIterator<String>listIterator; listIterator = dances.listIterator(); str = listIterator.next(); listIterator.set("Hip Hop");

["Hip Hop", "Jazz", "Waltz"]

Given LinkedList<String>dances; with elements ["Salsa", "Jazz", "Waltz", ] what does items contain after the following code block is exectued? dances.remove(0); dances.set(1, "Jive");

["Jazz", "Jive"]

Given LinkedList<Integer>items; with elements [1, 4, 3, 5] what does items contain after items.remove(3); is executed?

[1, 4, 3]

Which of the following lists is sorted in ascending order?

[3, 6, 9, 17, 44]

Given Queue<Integer> nums=new LinkedList<Integer>(); what are nums' elements after the following code segment is executed? nums.add(5); nums.add(2); nums.add(6); nums.remove(); nums.remove();

[6]

What is the content of the array [34, 8, 64, 51, 32, 21] after the second iteration of insertion sort?

[8, 34, 64, 51, 32, 21]

The ___ in a recursive method stops the recursion

base case

Which of the following types is a primitive type?

boolean


Ensembles d'études connexes

Chapter 1 An Introduction to Accounting

View Set

Dimensions of Strength Training Exam 1

View Set

Chapter 15: Managing Employee Motivation and Performance

View Set