CompSci

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Consider the following code segment. int k = 3; while(k >= 0 && k % 2 == 1) { System.out.print(k + ", "); k = 2 * k - 3; } What is printed as a result of executing this code segment?

An unending sequence of 3's

Consider the following code fragment: int x = 30; int y = 12; int z = 10; System.out.print("Answer is: " + x + y + z); What would be printed?

Answer is: 301210

What will be the result of the following assignment statement? Assume x = 4 and y = 10. int z = x * (-y + 2) / 2;

-16

Consider the following method. public static int method88 (int x) { int count = 0; for (int p = 1; p <= x; p++) for (int q = 0 ; q <=x/2; q++) count++; return count; } What value is returned as a result of the call method88 (5) ?

15

Consider the following code segment: int z = 10 int y = 3 boolean b = true; for (int i = 0; i < 15; i += 5) { z = z + y; b = (z % y == 2) if (!b) { y++; i += 5; } } What is the value of z after the code segment executes?

17

Consider the following code segment: int a = 10; double b = 3.7; int c = 4; int x = (int) (a + b); double y = (double) a / c; double z = (double) (a / c); double w = x + y + z; System.out.println(w);

17.5

Consider the following method. public static int method99 (int x) { while ( x < 5) { int z = 1; while (z < 5) { z++; x += z; } } return x; } What value is returned as a result of the call method99 (4)?

18

Consider the following code segment: ArrayList<Flower> blossom = new ArrayList<Flower>(); blossom.add(new Flower()); blossom.remove(0); blossom.add(new Flower()); Flower b = new Flower(); blossom.add(1, b); blossom.add(new Flower()); blossom.remove(0); blossom.add(new Flower()); blossom.remove(2); blossom.remove(1); blossom.add(new Flower()); What is the size of blossom after running the code?

2

Consider the following code segment: String words [] = {"avocado", "buffet", "carrot", "center", "meaning", "couch", "furniture", "sleep", "peccadillo", "friendly", "potatoes"}; int x = 0; for(int i = 0; i < words.length; i++) { if (words[i].substring(0,3).indexOf('o') >= 0) x++; } System.out.println(x); What is the output?

2

Consider the following program segment: int array2D[ ] [ ] = {1,2,3}, {4,5,6}. {7.8.9}; int x = matrix.length; for (int y = 1; y < x-1; y++) System.out.print(array2D[y][x—y-1] + " "); What will the output be when the program statement listed executes?

2 4

Consider the following code segment. int a = 11; int b = 4; double x = 11; double y = 4; System.out.print(a / b); System.out.print(", "); System.out.print(x / y); System.out.print(", "); System.out.print(a / y); What is printed as a result of executing the code segment?

2, 2.75, 2.75

Consider the following code segment: int[][] numarray = { { 2, 4, 2 }, { 0, 0, 0 } }; System.out.print(numarray[0][0]); System.out.print(numarray[1][0]); What is the output of the given code segment?

20

Consider the following two methods. public static void main(String args[]) { int x = 5; x = method22(x); System.out.println(x); } public static int method22(int y) { for (int z = 1; z <= 5; z ++) y += z; return y; } What is printed as a result of executing the main method?

20

Consider the following code segment. final int[] theArray = {1, 2}; theArray[1] = 3; System.out.print(theArray[1]); What is printed as a result of executing the code segment?

3

Consider the following code segment. int x = <an integer greater than zero> ; int n = 0; if (x < 1000) { if (x > 1500) n = 200; else n = 300; } else { if ( x < 500) n = 400; else n = 300; } System.out.println (n); What is printed as a result of executing the code segment?

300

Consider the following code segment: int x = 0; for (int i = 0; i < 10; i++) { for (int k = 0; k <= 5; k++) { for (int z = 1; z <= 16; z = z *2) { x++; } } } What is the value of x after the code segment executes?

300

Consider the array declaration that follows. int[] a = {1, 3, 5, 6, 7, 8, 9, 11, 13, 14, 15}; How many comparisons would it take if you were to search the array for 3 using a binary search?

4

Consider the following code segment: String q = "qlickity"; String r = "clack"; System.out.println(q.indexOf( r.charAt(4))); What is output?

4

Consider the following statement: System.out.println("I like to eat,\teat,\neat apples and\nbananas,\tI like to ate,\nate, ate ay-ples\t\\and ba-nay-nays"); How many lines of output will there be in the terminal window?

4

The expression 5 + 6 * 3 % 2 - 1 evaluates to

4

What is the output of the following code segment? int x = 41; if (x < 100) { x = x + 5; } if (x < 500) { x = x - 2; } if (x > 10) { x++; } else { x--; } System.out.println(x);

45

Consider the following statement: int i = x % 50; If x is a positive integer, which of the following could NOT be the value of i after the statement above executes?

50

Consider the following code segment. int answer = 2; for (int x = 0; x < 5; p++) { if (x % 2 !=0) answer—; else answer += 2; } System.out.println(count); What is the output as a result of executing this code segment?

6

Consider the following code: public static int apple(int a, int b) { if (a < b) return a; else return b + apple(a-1, b+1); } What is returned as the result of the call apple(4,3)?

6

Given the following int x = 18; int y = 4; What is the value of the following expression? x / y + x % y

6

Consider the following code segment. int k = 20, i = 0; while(k > 10) { i = 3; while(i < k) { i *= 2; if(i % k == 1) { i += k; } } k -= 3; } System.out.println(k + " " + i); What will be the output after execution of this code segment?

8 23

What is the output of the following code segment? int x = some int value; x = x % 20; if(x < 25) System.out.println("Less than 25"); else System.out.println("Greater or equal to 25");

Less than 25

Consider the following code segment: int list [] = /* missing code */; int val = /* missing code */; int n = -1; for (int i = 0; i < list.length; i++) { if (val == list[i]) { n = i; break; } } What algorithm is shown?

Linier Search

Consider the following code segment. for(int i = 1; i < 5; i++) for(int k = i; k < 2; k--) System.out.print(k + " "); What is printed as a result of executing this code segment?

Many digits are printed due to an infinite loop.

Which of the following is true regarding linear searches?

None of the answers are correct.

What is the output of the following program segment? int bonusPoints = 350; int score = 8000; if(score >=6600) bonusPoints += 300; System.out.println("Bonus: " + bonusPoints);

Bonus: 650

What does the String method compareTo() do?

Compares this String with a second String for greater than, equal to, or less than.

You are designing a software solution for a tool rental company. You have decided that the following nouns apply to the requirements: Tool, Customer, Address, Rental Contract, Condition, Rental Date, Daily Rate, Total. Which of these should be represented as instance variables?

Condition, Rental Date, Daily Rate.

Consider the following two classes. class alpha { public alpha() { System.out.println("Executing the alpha constructor"); } } class bravo { private alpha rocket; public bravo() { System.out.println("Executing the bravo constructor"); rocket = new alpha(); } } Assume that the following code segment appears in a client program. What is printed by the code segment? bravo y = new bravo(); alpha z = new alpha();

Executing the bravo constructor Executing the alpha constructor Executing the alpha constructor

Consider the following code segment. The code below is created to display grades as follows: 0 - 74 F 75 - 83 C 84 - 92 B 93 - 100 A int average = <int value such that 0 <=average <=100> ; if (average > 92) System.out.println ("Your grade is A") ; else if (average > 83) System.out.println ("Your grade is B") ; else if ( average > 74) System.out.println ("Your grade is C") ; else System.out.println ("Your grade is F") ; For which values of average does the code segment execute correctly?

For all values in the [0..100] range

The change method is defined as follows: public void change(String[] x) { String temp = x[0].substring(0, 1); x[0] = x[1].substring(0, 1) + w[0].substring(1); x[1] = temp + x[1].substring(1); } What is the output after the following code segment is executed? String[] words = {"Cover", "Hovers"}; change(words): System.out.println(words[0] + " " + words[1]);

Hover Covers

Consider the following Boolean expression: (X || Y) || Y For which of the following conditions is the expression above true? I. whenever either X is true or Y is true. II. whenever both X is true and Y is true. III. whenever both X is not true and Y is not true.

I and II only

Which of the following code segments below indicates that the Purse class depends on the Wallet class?

I and II only

Which of the following correctly adds a new element to the list array? I. ArrayList list = new ArrayList(); list.add(new String("Java")); II. ArrayList list = new ArrayList(); list.add(new Integer(1000)); III. ArrayList list = new ArrayList(); list.add("1000");

I, II and III

The benefits of encapsulation include: I. The fields of a class can be made read-only or write-only. II. A class can have total control over what is stored in its fields. III. The users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code.

I, II, and III

Consider the following. ArrayList names = new ArrayList(); names.add("chase"); names.add("steve"); names.add("mary"); names.add("ellen"); for (int k = 1; k <= names.size(); k++) System.out.print(names.get(k) + " "); What's printed?

IndexOutOfBoundsExeption

Consider the following code segment The code below is created to display grades as follows: 0 - 69 F 70 - 79 C 80 - 89 B 90 - 100 A int average = <int value such that 0 <= average <= 100> ; if (average < 100) System.out.println ("Your grade is A") ; else if (average < 90) System.out.println ("Your grade is B") ; else if (average < 80) System.out.println ("Your grade is C") ; else if (average < 70) System.out.println("Your grade is F") ; For which values of average does te code segment execute correctly?

Only for values in the [90..99] range

Consider the following three classes: Phone, Cell, and LandLine. Which would you choose be an abstract class?

Phone

A class, Purse, is considered to have a dependency on another class, Wallet, under which of the following conditions?

Purse uses objects of Wallet.

The following code segment is intended to compute the product of the first 10 odd numbers, starting with 1. int product = 0; for(int x = 1; x <= 19; x+=2) { product *= x; } System.out.println(product); Which of the following best describes the error?

The variable product is incorrectly initialized. The segment would work as intended if product were initialized to 1.

You have written a program to create a shopping list. As each item is placed into your cart you call method called removeItem and it should remove the item from your list. public static void removeItem(ArrayList<String> list, String remove) { for (String s: list) if (s.equals(remove)) list.remove(s); }

There will most likely be an error because the loop goes past the end of the ArrayList.

You are designing a software solution for a tool rental company. You have decided that the following nouns apply to the requirements: Tool, Customer, Address, Rental Contract, Condition, Rental Date, Daily Rate, Total. Which of these should be represented as classes?

Tool, Customer, Address, Rental Contract.

Which of the following statements about abstract classes is true?

You can create an object from a concrete class, but not from an abstract class.

What will be the result when the following assignment statement is executed? int x= 50 / 10.00;

a run-time error arises because x is an int and 50 / 10.00 is not an int

Of the following data types, which one cannot store a numeric value?

char

Consider the following. ArrayList names = new ArrayList(); names.add("chase"); names.add("steve"); names.add("mary"); names.add("ellen"); for (int p = 0; p < names.size(); p++) System.out.print(names.set(p, "sarah") + " "); System.out.println(); for (int p = 0; p < names.size(); p++) System.out.print(names.get(p) + " "); What is printed?

chase steve mary ellen sarah sarah sarah sarah

When a parameter is a(n) ______ data type, any changes made in a method are preserved.

class

Consider the following: int a = some integer value int b = some integer value int c = some integer value Which of the following will compute the average of the integers?

double average = (a + b + c) / 3.0

Consider the following code fragment: double divisor = 21 / 5; System.out.println("21 / 5 = " + divisor); The terminal window displayed: 21 / 5 = 4.0 The programmer wanted the output to be: 21 / 5 = 4.2 Which of the following will NOT correct the error?

double divisor = (double)(21 / 5);

Consider the following program segment. String s1 = new String("ButterFly"); int start = s1.indexOf("F"); String s2 = s1.substring(start); System.out.println(s2); What will be output when the program segment executes?

fly

Examine the following if statements. Assume the condition evaluates to true. Which if statement correctly executes the instructions?

if (x < 0) { a *= 2; y = x; z = a - y; }

Methods that change the values of the instance variables are called ____ methods.

mutator

If num1 = 5 and num2 = 5.0, all of the following are legal except which assignment statement?

num1= num2;

Consider the folowing code segment. int num1 = 13; int num2 = 5; while(num1 % num2 !=0 && num2 !=0) { num1 += 3; num2 -= 1; } What are the values of num1 and num2 after the loop executes?

num1=16 num2=4

Consider the following. ArrayList names = new ArrayList(); names.add("chase"); names.add("steve"); names.add("mary"); names.add("ellen"); System.out.print(names.set(1, "phil"); for (int k = 0; k < names.size(); k++) System.out.print(names.get(k) + " "); What is displayed?

phil chase phil mary ellen

A well-designed class uses ____ instance variables, accessor methods, and (if needed) mutator methods to implement the OOD principle of encapsulation.

private

Consider the following code segment: public abstract class Appliance { public abstract void setRPMs(); /* other possible abstract methods */ } You wish to create a subclass named SmallAppliance. Which of the following is the correct way to declare this subclass?

public class SmallAppliance extends Appliance { public void setRPMs() { /* missing code */ } }

What return statement may be used in the following method? public static int myMethod() { //...some code }

return 1;

Consider the following code segment String string1 = "Hello"; String string2 = "World"; string2 = string1; string1 += "There!"; After the code is executed, what are the values of string1 and string2?

string1 = "HelloThere!", string2 = "Hello"

An example of an aggregation relationship is

teacher and computer

Consider the following. ArrayList names = new ArrayList(); names.add("tom"); names.add("ann"); names.add("joe"); names.add("sue"); names.remove(1); names.remove(2); System.out.println(); for (int k = 0; k < names.size(); k++) System.out.print(names.get(k) + " "); What's printed?

tom joe

Consider the following code segment. int z = some integer value double a = z; double c = Math.pow(Math.sqrt(a), 2); return (a == c); Which of the following statements about the code segment is true?

true or false may be returned, depending on roundoff errors.

Consider the following method. double echo(double x) { while (x > 0) x = x + 5; } This method:

will not function correctly, as it has no return statement.

Consider the following: public static int echo(int x, int y) { if (y == 1) return x; else return x + echo(x,y-1); } Assume a precondition of x > 0 and y > 0. Which of the following expressions represents the result of calling method echo?

x * y

Consider the following code segment. x = 6; y = 19; z = 2; if (x > y) if (z > x) z++; else z -= 5; y += x; After this code is executed, the values of x,y, and z are:

x = 6, y = 25, z = -3

Assume that x and y are integer variables and have been properly initialized. !(x <= Y) && ( x * y > 0) will always evaluate to true if

x > y and y > 0

! ((x < y) && (w == z)) is the same as which boolean expression?

x >= y || w != z

Consider the following code: public static int echo(int x, int y) { if (y == 1) return x; else return x * echo(x, y-1); } Assume a precondition of x > 0 and y > 0. Which of the following expressions represents the result of calling method echo?

x^y

A method of a class that only retrieves the value(s) of the data member(s) is known as a(n) ____ method.

accessor

Consider the following segment of code. String x = "compute"; int length = word.length( ) ; int num = 3; String y = x.substring(length % num, num); //om String z = x.substring(num + 1); //ute if (y.compareTo (z) < 0) { z += y; } else { y += z; } After this code is executed, what are the values of y and z?

y = "om", z = "uteom"

Consider the following declaration: double [ ] [ ] array; int r; //the row index on array int c; //the column index on array Which of the following statements stores the number of rows of array?

array.length

Consider the following declaration: double [ ] [ ] array; int r; //the row index on array int c; //the column index on array Which of the following statements stores the number of columns of array?

array[0].length

Suppose the abstract class Message is defined below public abstract class Message { private String value; public Message(String initial) { value = initial; } public String getMessage() { return value; } public abstract String autoCorrect(); } A child subclass of Message, TextMessage, is defined. Which methods must TextMessage define?

autoCorrect() only

Consider the following code segment: for (int x = 1; x < 100; x = x * 2) { if (x / 50 = = 0) System.out.print(x + " "); } What is printed as a result of executing the code segment?

1 2 4 8 16 32

Consider the following program code. public class Delta { public static void main(String args[]) { int x = 5; int y = 10; System.out.println(x + " " + y); swap (x,y); System.out.println(x + " " + y); } public static void swap(int a, int b) { int temp = a; a = b; b = temp; } } What is printed as a result of executing the program?

10 20 10 20

Consider the following Strings: String s1 = "gw"; String s2 = "GW"; String s3 = "George Washington"; Given that G comes before g in dictionary order, which of the following is true?

!( s1.equals(s2) && s3.compareTo(s2) < 0)

Consider the following code segment: int x = 10; int y = x / 3; int z = x % 2; x++; System.out.println(z) What is printed as a result of executing the code segment above

0

Examine the following recursive method: public static void recur1(int n) { if(n > 0) recur1(n - 2 ); System.out.print(n + " "); } What is the output when the parameter 6 passed into the method?

0 2 4 6

Consider the following method: public void division6 (int[] a, int[] b, int[] c) { b[0] = a[0] / 6; c[0] = a[0] % 6; } What is the value of y[0] after the following statements are executed? int[] x = {25, 26, 27], y = new int[3]; division6 (x, y, y);

1

Consider the following code segment. int list[] = {1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50}; for (int d = 1; d < list.length; k++) list[d] = list[d] / list[1]; for (int d = 0; d < list.length; k++) System.out.print(list[d] + " "); What will be output when the program segment executes?

1 1 10 15 20 25 30 35 40 45 50

Consider the following code segment and two methods. int square [ ] [ ] = new int [4] [4]; makeSquare(square); showSquare(square); public static void makeSquare(int[ ] [ ] square) { int size = square.length; for (int x = 0; x< size; x++) { int z = x; for (int y = x; y<size; y++) { square [x] [y] = z + y; } } } public static void showSquare(int[ ] [ ] square) { for (int x = 0; x< 5; x++) { for (int y = 0; y<5; c++) System.out.print(square [x] [y] + " "); System.out.prinln(); } } What is printed as a result of executing the code segment?

1 2 3 4 0 1 2 3 0 0 1 2 0 0 0 0

Consider the following code segment: int x = 10; int y = x / 3; int z = x % 2; x++; System.out.println(x) What is printed as a result of executing the code segment above

11

Consider the following two methods. public static void alpha() { int list[] = {111, 999, 222, 888, 333, 777, 444, 666, 555}; beta(list); for (int z = 0; z < list.length; k++) System.out.print(list[z] + " "); System.out.println(); } public static void beta(int list1[]) { int list2[] = list1; int max = list2.length; for (int y = 1; y < max-1; y++) for (int x = 0; x < max-y; x++) { if (list2[x] > list2[x+1]) { int temp = list2[x]; list2[x] = list2[x+1]; list2[x+1] = temp; } } } What is the output when method alpha is called?

111 222 333 444 555 666 777 888 999

Consider the following code segment. int x = 2; while (x < 7) { int y = 0; while (y < 5) { x += y; y++; System.out.println(x + " " + y); } } What is the last output when the segment executes?

12 5

Consider the following method. public static int methodGeneration(int z) { int[] temp = new int[z]; temp[0] = 1; temp[1] = 3; for (int r = 2; r < z; k++) temp[r] = temp[r-1] + temp[r-2]; return temp[z-1]; } What value is returned by the call methodGeneration(10)?

123

Consider the following program code. String b1 = "ABCD"; String b2 = ""; for (int x = 1; x < b1.length(); x++) b2 += b1.substring(0,x); System.out.println(b2); What will be the output when the program segment executes?

AABABC

Consider the following incomplete method. public static int delta(int[] numbers) { int count = 0; for (int x = 0; x < numbers.length; i++) { // code not shown } return count; } Method delta is intended to return the numbers of integers in the array numbers that are evenly divisible by 3. Which of the following code segments could be used to replace // code not shown so that the method delta will work as intended? I. if (x % 3 == 0) { count++; } II. if (numbers[x] % 3 == 0) { count++; } III. while (numbers[x] % 3 == 0) { count++; }

II only

Assuming all variables are declared correctly, which of the following code segments correctly swaps the values x and y? I. x = y; y = x; II. int tmp = x; y = x; x = tmp; III. int tmp = x; x = y; y = tmp;

III only

Assume the following list of integers: 18, 45, 33, 65, 76, 32, 96, 12, 46, 68 After three iterations, the list looks like this: 12, 18, 32, 65, 76, 33, 96, 45, 46, 68 What type of sort was used?

Selection

Consider the following class hierarchy: Square extends Quadrilateral Quadrilateral extends Polygon Which of the following is true?

Square has access to all public methods and variables in Quadrilateral.

What is "encapsulation"?

The enforcement of data hiding within a class

Consider the following two methods. public static void garage(int numList[]) { int max = list .length-1; for (int z = 0; z < max; z++) if numList[z] > numList [z+1]) band(numList, z); } public static void method band(int numList[], int y) { int temp = numList[y]; numList[y] = numList[y+1]; numList[y+1] = temp; } Which of the following correctly describes the results of calling method garage?

The largest number is located in numList[max].

Assume that x, y and z are integer variables and have been properly initialized. if(x != y && z / (x - b) > 45) { // statement 1 } else { //statement 2 } //statement 3 What will happen if x == y is false?

either //statement 1 or //statement 2 will execute

Making a variable public, when it should be declared private, violates the concept of:

encapsulation.


संबंधित स्टडी सेट्स

Rock and Roll Chapter 1, 2, 3, and 4

View Set

Intercultural Communication Quiz Answers

View Set

Plates of associated structures of ethmoid bone

View Set