help me

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

Consider the following classes. public class Ticket { private double price; public Ticket(double p) { price = p; } public double getPrice() { return price; } public String toString() { return "Price is " + getPrice(); } } public class DiscountTicket extends Ticket { public DiscountTicket(double p) { super(p); } public double getPrice() { return super.getPrice() / 2.0; } } The following code segment appears in a class other than Ticket or DiscountTicket. Ticket t = new DiscountTicket(10.0); System.out.println(t); What output, if any, is produced when the code segment is executed?

Price is 5.0

Consider the following code segment, which is intended to print the sum of all elements of an array. int[] arr = {10, 5, 1, 20, 6, 25}; int sum = 0; for (int k = 0; k <= arr.length; k++) { sum += arr[k]; } System.out.println("The sum is " + sum); A runtime error occurs when the code segment is executed. Which of the following changes should be made so that the code segment works as intended?

The for loop header should be replaced with for (int k = 0; k < arr.length; k++).

Consider the following code segment. String str = "abcdef"; for (int rep = 0; rep < str.length() - 1; rep++) { System.out.print(str.substring(rep, rep + 2)); } What is printed as a result of executing this code segment?

abbccddeef

Consider the following code segment. int[] arr = {1, 2, 3, 4, 5, 6, 7}; for (int i = 1; i < arr.length; i += 2) { arr[i] = arr[i - 1]; } Which of the following represents the contents of the array arr after the code segment is executed?

{1, 1, 3, 3, 5, 5, 7}

Consider the following code segment. int[] arr = {3, 1, 0, 4, 2}; for(int j = 0; j < arr.length; j++) { System.out.print(arr[j] + j + " "); } What, if anything, is printed as a result of executing the code segment?

3 2 2 7 6

public SomeClass(int n) { private int num; } num= n; } num= num + more; public int get Num() { public void increment (int more) { } } The following code segment appears in another class. return num; three.getNum()); What is printed as a result of executing the code segment? SomeClass one = new SomeClass(100); SomeClass two = new SomeClass(100); SomeClass three = one; one increment (200); System.out.println(one.getNum() + " " + two.getNum() + " " +

300 100 300

public int compute(int n, int k) { int answer = 1; for (int i = 1; i <= k; i++) { answer *= n; } return answer; }

n^K

Consider the following code segment. int a = 5; int b = 4; int c = 2; a *= 3; b += a; b /= c; System.out.print(b); What is printed when the code segment is executed?

9

onsider the following code segment. int num = 1; for (int k = 2; k < 10; k++) { num = 0; num = num + k; } What will be the value of num after the loop is executed?

9

public class A { public void show() { System.out.print("A"); } } public class B extends A { public void show() { System.out.print("B"); } } A obj = new B(); obj.show();

B

initialized. The code segment is intended to print a value that is 1 more than twice the value of n. /* missing code */ System.out.print(result); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? 1. int result = 2 * n; result = result + 1; II. int result = n + 1; result = result * 2; III. int result = (n + 1) + 2;

I only

Consider the following code segments. I. int k = 1; while (k < 20) { if (k % 3 == 1) System.out.print( k + " "); k = k + 3; } II. for (int k = 1; k < 20; k++) { if (k % 3 == 1) System.out.print( k + " "); } III. for (int k = 1; k < 20; k = k + 3) { System.out.print( k + " "); } Which of the code segments above will produce the following output? 1 4 7 10 13 16 19

I, II, and III

Consider the following class declaration. public class Sample { private int a; private double b; public Sample(int x, double y) { a = x; b = y; } // No other constructors } The following method appears in a class other than Sample. public static void test() { Sample object = new /* missing constructor call */ ; } Which of the following could be used to replace /* missing constructor call */ so that the method will compile without error?

Sample(10, 6.2)

Consider the following data field and method. Method maxHelper is intended to return the largest value among the first numVals values in an array; however, maxHelper does not work as intended. private int[] nums; // precondition: 0 < numVals <= nums.length private int maxHelper(int numVals) { Line 1: int max = maxHelper(numVals - 1); Line 2: if (max > nums[numVals - 1]) return max; else return nums[numVals - 1]; } Which of the following corrects the methodmaxHelperso that it works as intended?

Insert the following statement before Line 1. if (numVals == 1 return nums[0];

Consider the following instance variable and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended. For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10], the call findLongest(10) should return 3, the length of the longest consecutive block of 10's. private int[] nums; public int findLongest(int target) { int lenCount = 0; int maxLen = 0; Line 1: for (val : nums) Line 2: { Line 3: if (val == target) Line 4: { Line 5: lenCount++; Line 6: } Line 7: else Line 8: { Line 9: if (lenCount > maxLen) Line 10: { Line 11: maxLen = lenCount; Line 12: } Line 13: } Line 14: } Line 15: if (lenCount > maxLen) Line 16: { Line 17: maxLen = lenCount; Line 18: } Line 19: return maxLen; } The method findLongest does not work as intended. Which of the following best describes the value returned by a call to findLongest ?

Insert the statement lenCount = 0; between lines 12 and 13.

Consider the following code segment. for (int num = 0; num < 10; num += 2) { for (int val = 0; val < 5; val++) { System.out.println("hop"); } } How many times will System.out.println("hop") be executed?

25

Consider the following two code segments. Assume that the int variables m and n have been properly declared and initialized and are both greater than 0. for (int i = 0; i < m * n; i++) { System.out.print("A"); } for (int j = 1; j <= m; j++) { for (int k = 1; k < n; k++) { System.out.print("B"); } } Assume that the initial values of m and n are the same in code segment I as they are in code segment II. Which of the following correctly compares the number of times that "A" and "B" are printed when each code segment is executed?

"A" is printed m more times than "B".

Consider the following code segment. for (int x = 0; x <= 4; x++) // Line 1 { for (int y = 0; y < 4; y++) // Line 3 { System.out.print("a"); } System.out.println(); } Which of the following best explains the effect of simultaneously changing x <= 4 to x < 4 in line 1 and y < 4 to y <= 4 in line 3 ?

"a" will be printed the same number of times because while the number of output lines will decrease by 1, the length of each line will increase by 1.

Consider the following code segment. for (int outer = 0; outer < n; outer++) { for (int inner = 0; inner <= outer; inner++) { System.out.print(outer + " "); } } If n has been declared as an integer with the value 4, what is printed as a result of executing the code segment?

0 1 1 2 2 2 3 3 3 3

Consider the following code segment. int a = 4; int b = 5; a++; b++; int c = a + b; a -= 1; System.out.println(a + c); What is printed when the code segment is executed?

15

Consider the following code segment. int x = 5; x += 6 * 2; x -= 3 / 2; What value is stored in x after the code segment executes?

16

Consider the following code segment. for (int k = 0; k< 20; k= k+ 2){ if (k % 3== 1) { System.out.print(k+ " "); } } What is printed as a result of executing the code segment?

4 10 16

Consider the following method. public int getTheResult(int n) { int product = 1; for (int number = 1; number < n; number++) { if (number % 2 == 0) product *= number; } return product; } What value is returned as a result of the call getTheResult(8) ?

48

onsider the following method. public int sol(int lim) { int s = 0; for (int outer = 1; outer <= lim; outer++) { for (int inner = outer; inner <= lim; inner++) { s++; } } return s; } What value is returned as a result of the call sol(10)?

55

Consider the following class definitions. public class Game { private String name; public Game(String n) { name = n; } // Rest of definition not shown } public class BoardGame extends Game { public BoardGame(String n) { super(n); } // Rest of definition not shown } The following code segment appears in a class other than Game or BoardGame. Game g1 = new BoardGame("checkers"); BoardGame g2 = new Game("chess"); ArrayList<Game> My_Games = new ArrayList(); My_Games.add(g1); My_Games.add(g2); Which of the following best explains why the code segment does not compile?

A Game object cannot be assigned to the BoardGame reference g2.

Consider the following code segment. for (int r = 3; r > 0; r--) { int c; for (c = 1; c < r; c++) { System.out.print("-"); } for (c = r ; c <= 3; c++) { System.out.print("*"); } System.out.println(); } What is printed as a result of executing the code segment? Select one:

Answer is A. --* -** ***

Consider the following code segments. Code segment 2 is a revision of code segment 1 in which the loop increment has been changed. Code Segment 1 int sum = 0; for (int k = 1; k <= 30; k++) { sum += k; } System.out.println("The sum is: " + sum); Code Segment 2 int sum = 0; for (int k = 1; k <= 30; k = k + 2) { sum += k; } System.out.println("The sum is: " + sum); Code segment 1 prints the sum of the integers from 1 through 30, inclusive. Which of the following best explains how the output changes from code segment 1 to code segment 2 ?

Code segment 2 will print the sum of only the odd integers from 1 through 30, inclusive because it starts k at one, increments k by twos, and terminates when k exceeds 30.

Consider the following while loop. Assume that the int variable k has been properly declared and initialized. while (k < 0) { System.out.print("*"); k++; } Which of the following ranges of initial values for k will guarantee that at least one "*" character is printed? k < 0 k = 0 k > 0

I only

The following questions refer to the following classes: public class First { public String name() { return "First"; } } public class Second extends First { public void whoRules() { System.out.print(super.name() + " rules"); System.out.println(" but " + name() + " is even better"); } public String name() { return "Second"; } } public class Third extends Second { public String name() { return "Third"; } } Consider the following code segment. Second varSecond = new Second(); Third varThird = new Third(); varSecond.whoRules(); varThird.whoRules(); What is printed as a result of executing the code segment?

First rules but Second is even better First rules but Third is even better

Which of the following code segments will print all multiples of 5 that are greater than and less than 100 ? I. for (int k = 1; k < 100; k++) { if (k % 5 == 0) { System.out.print(k + " "); } } II. for (int k = 1; k < 100; k++) { if (k / 5 == 0) { System.out.print(k + " "); } } III. int k = 5; while (k < 100) { System.out.print(k + " "); k = k + 5; }

I and III

Consider an integer array, nums, which has been declared and initialized with one or more integer values. Which of the following code segments updates nums so that each element contains the square of its original value? I. int k = 0; while (k < nums.length) { nums[k] = nums[k] * nums[k]; } II. for (int k = 0; k < nums.length; k++) { nums[k] = nums[k] * nums[k]; } III. for (int n : nums) { n = n * n; }

II only

The following questions refer to the following classes: public class First { public String name() { return "First"; } } public class Second extends First { public void whoRules() { System.out.print(super.name() + " rules"); System.out.println(" but " + name() + " is even better"); } public String name() { return "Second"; } } public class Third extends Second { public String name() { return "Third"; } } Consider the following code segment. /* SomeType1 */ varA = new Second(); /* SomeType2 */ varB = new Third(); varA.whoRules(); varB.whoRules(); Which of the following could be used to replace /* SomeType1 */ and /* SomeType2 */ so that the code segment will compile without error? /* SomeType1 */ /* SomeType2 */ I. First Third II. Second Second III. Third Third

II only

Consider the following instance variable and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended. For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10], the call findLongest(10) should return 3, the length of the longest consecutive block of 10's. private int[] nums; public int findLongest(int target) { int lenCount = 0; int maxLen = 0; Line 1: for (val : nums) Line 2: { Line 3: if (val == target) Line 4: { Line 5: lenCount++; Line 6: } Line 7: else Line 8: { Line 9: if (lenCount > maxLen) Line 10: { Line 11: maxLen = lenCount; Line 12: } Line 13: } Line 14: } Line 15: if (lenCount > maxLen) Line 16: { Line 17: maxLen = lenCount; Line 18: } Line 19: return maxLen; } The method findLongest does not work as intended. Which of the following best describes the value returned by a call to findLongest ?

It is the number of occurrences of the value target in nums.

Consider the following class definitions. public class C1 { public C1() { /* implementation not shown */ } public void m1() { System.out.print("A"); } public void m2() { System.out.print("B"); } } public class C2 extends C1 { public C2() { /* implementation not shown */ } public void m2() { System.out.print("C"); } } The following code segment appears in a class other than C1 or C2. C1 obj1 = new C2(); obj1.m1(); obj1.m2(); The code segment is intended to produce the output AB. Which of the following best explains why the code segment does not produce the intended output?

Method m2 is executed from the subclass instead of the superclass because obj1 is instantiated as a C2 object.

Consider the following code segment. for (int k = 0; k < 9; k = k + 2) { if ((k % 2) != 0) { System.out.print(k + " "); } } What, if anything, is printed as a result of executing the code segment?

Nothing is printed.

Consider the following code segment, which traverses two integer arrays of equal length. If any element of arr1 is smaller than the corresponding (i.e., at the same index) element of minArray, the code segment should replace the element of minArray with the corresponding element of arr1. After the code segment executes, minArray should hold the smaller of the two elements originally found at the same indices in arr1 and minArray and arr1 should remain unchanged. for (int c = 0; c < arr1.length; c++) { if (arr1[c] < minArray[c]) { arr1[c] = minArray[c]; } else { minArray[c] = arr1[c]; } } Which of the following changes will ensure that the code segment always works as intended?

Removing lines 5-8

Consider the following class declarations. public class Base { private int myVal; public Base() { myVal = 0; } public Base(int x) { myVal = x; } } public class Sub extends Base { public Sub() { super(0); } } Which of the following statements will NOT compile?

Sub s3 = new Sub(5);

Consider the following class declarations. public class Alpha { private int answer() { return 10; } } public class Beta { public double sample() { Alpha item = new Alpha(); double temp = item.answer(); return temp * 2.0; } } Which of the following best describes why an error occurs when the classes are compiled?

The answer method cannot be accessed from a class other than Alpha.

Consider the following code segment, where k and count are properly declared and initialized int variables. k++; k++; count++; k--; count++; k--; Which of the following best describes the behavior of the code segment?

The code segment leaves k unchanged and increases count by 2.

In the code segment below, assume that the int variables a and b have been properly declared and initialized. int c = a; int d = b; c += 3; d--; double num = c; num /= d; Which of the following best describes the behavior of the code segment?

The code segment stores the value of (a + 3) / (b - 1) in the variable num.

Consider the following class definitions. public class A { public String message(int i) { return "A" + i; } } public class B extends A { public String message(int i) { return "B" + i; } } The following code segment appears in a class other than A or B. A obj1 = new B(); // Line 1 B obj2 = new B(); // Line 2 System.out.println(obj1.message(3)); // Line 3 System.out.println(obj2.message(2)); // Line 4 Which of the following best explains the difference, if any, in the behavior of the code segment that will result from removing the message method from class A ?

The statement in line 3 will cause a compiler error because the message method for obj1 cannot be found.

Consider the following code segment. for (int j = 0; j < 3; j++) { for (int k = 0; k < 4; k++) { System.out.println("Fun"); } } Which of the following best explains how changing the outer for loop header to for (int j = 0; j <= 3; j++) affects the output of the code segment?

The string "Fun" will be printed more times because the outer loop will execute more times.

The question refer to the following data field and method. private int[] arr; // precondition: arr.length > 0 public void mystery() { int sl = 0; int s2 = 0; for (int k = 0; k < arr.length; k++) { int num = arr[k]; if ((num > 0) && (num % 2 == 0)) sl += num; else if (num < 0) s2 += num; } System.out.println(s1); System.out.println(s2); } Which of the following best describes the value of s1 output by the method mystery ?

The sum of all positive even values in arr

Consider the following code segment. num += num; num *= num; Assume that num has been previously declared and initialized to contain an integer value. Which of the following best describes the behavior of the code segment?

The value of num is the square of twice its original value.

Consider the following method. /** Precondition: bound >= 0 */ public int sum(int bound) { int answer = 0; for (int i = 0; i < bound; i++) { answer += bound; } return answer; } Assume that sum is called with a parameter that satisfies the precondition and that it executes without error. How many times is the test expression i < bound in the for loop header evaluated?

bound + 1

Consider the following output. 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5 Which of the following code segments will produce this output?

d. for (int j = 1; j <= 5; j++) { for (int k = 5; k >= j; k--) { System.out.print(j + " "); } System.out.println(); }

Consider the following code segment. for (int k = 1; k <= 7; k += 2) { System.out.print(k); } Which of the following code segments will produce the same output as the code segment above?

for (int k = 1; k <= 8; k += 2) { System.out.print(k); }

for (int k = 1; k <= 100; k++) { if ((k % 4) == 0) { System.out.println(k); } }

for (int k = 4; k <= 100; k=k+4) { System.out.println(k); } }

Consider the following code segment. int count = 0; for (int k = 0; k < 10; k++) { count++; } System.out.println(count); Which of the following code segments will produce the same output as the code segment above?

int count = 0; for (int k = 9; k >= 0; k--) { count++; } System.out.println(count);

Consider the following method. public int[] addNum(int[] array, int first, int second, int num) { int[] newArray = new int[array.length]; newArray[first] = array[first] + num; newArray[second] = array[second] + num; return newArray; } Which of the following code segments, appearing in the same class as the addNum method, will result in array2 having the contents {0, 0, 13, 0, 9, 0, 0} ?

int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5);

Consider the following method, which is intended to return the product of 3 and the nonnegative difference between its two int parameters. public int threeTimesDiff (int num1, int num2) { return 3 * (num1 - num2); } Which, if any, precondition is required so that the method works as intended for all values of the parameters that satisfy the precondition?

num1 >= num2

Consider the following code segment. int[] numbers = new int[5]; numbers[0] = 2; numbers[1] = numbers[0] + 1; numbers[numbers[0]] = numbers[1]; for (int x = 3; x < numbers.length; x++) { numbers[x] = numbers[x - 1] * 2; } Which of the following represents the contents of the array numbers after the code segment is executed?

{2, 3, 3, 6, 12}

Consider the following code segment. boolean[] oldVals = {true, false, true, true}; boolean[] newVals = new boolean[4]; for (int j = oldVals.length - 1; j >= 0; j--) { newVals[j] = !(oldVals[j]); } What, if anything, will be the contents of newVals as a result of executing the code segment?

{false, true, false, false}

Assume that x and y are variables of type int. Which of the following Java expressions never results in a division by zero?

(x != 0) && ((y / x) == 0)

Consider the following class definitions. public class Data { private int x; public void setX(int n) { x = n; } // ... other methods not shown } public class EnhancedData extends Data { private int y; public void setY(int n) { y = n: } // ... other methods not shown } Assume that the following declaration appears in a client program. EnhancedData item = new EnhancedData(); Which of the following statements would be valid? I. item.y = 16; II. item.setY(16); III. item.setX(25);

II and III only

Consider the following data field and method. Method maxHelper is intended to return the largest value among the first numVals values in an array; however, maxHelper does not work as intended. private int[] nums; // precondition: 0 < numVals <= nums.length private int maxHelper(int numVals) { Line 1: int max = maxHelper(numVals - 1); Line 2: if (max > nums[numVals - 1]) return max; else return nums[numVals - 1]; } Which of the following best describes the conditions under which maxHelper does not work as intended?

Method maxHelper never works as intended.

Consider the following two code segments. Code segment II is a revision of code segment I in which the loop header has been changed. I. for (int k = 1; k <= 5; k++) { System.out.print(k); } II. for (int k = 5; k >= 1; k--) { System.out.print(k); } Which of the following best explains how the output changes from code segment I to code segment II?

The code segments print the same values but in a different order, because code segment I iterates from 1 to 5 and code segment II iterates from 5 to 1.

Consider the following class definition. public class Document { private int pageCount; private int chapterCount; public Document(int p, int c) { pageCount = p; chapterCount = c; } public String toString() { return pageCount + " " + chapterCount; } } The following code segment, which is intended to print the page and chapter counts of a Document object, appears in a class other than Document. Document d = new Document(245, 16); System.out.println( /* missing code */ ); Which of the following can be used as a replacement for /* missing code */ so the code segment works as intended?

d.toString()

Consider the following output. 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 Which of the following code segments will produce the output shown above?

for (int j = 1; j <= 6; j++) { for (int k = 1; k <= j; k++) System.out.print(" " + k); System.out.println(); }

Consider the following code segment. for (int outer = 0; outer < 3; outer++) { for (/* missing loop header */) { System.out.print(outer + "" + inner + "_"); } } Which of the following can be used as a replacement for /* missing loop header */ so that the code segment produces the output 00_01_02_11_12_22_ ?

int inner = outer; inner < 3; inner++

Consider the following code segment. for (int j = 1; j < 10; j += 2) { System.out.print(j); } Which of the following code segments will produce the same output as the code segment above?

int j = 1; while (j < 10) { System.out.print(j); j += 2; }

Assume that an array of integer values has been declared as follows and has been initialized. int[] arr = new int[10]; Which of the following code segments correctly interchanges the value of arr[0] and arr[5] ?

int k = arr[0]; arr[0] = arr[5]; arr[5] = k;

public class TimeRecord { private int hours; private int minutes; // 0 < minutes < 60 /** Constructs a TimeRecord object. * @param h the number of hours * Precondition: h > 0 * @param m the number of minutes * Precondition: 0 < m < 60 */ public TimeRecord(int h, int m) { hours = h; minutes = m; } /** @return the number of hours */ public int getHours() { / implementation not shown / } /** @return the number of minutes * Postcondition: 0 < minutes < 60 */ public int getMinutes() { / implementation not shown / } /** Adds h hours and m minutes to this TimeRecord. * @param h the number of hours * Precondition: h > 0 * @param m the number of minutes * Precondition: m > 0 */ public void advance(int h, int m) { hours = hours + h; minutes = minutes + m; / missing code / } // Other methods not shown } Consider the following declaration that appears in a class other than TimeRecord. TimeRecord[] timeCards = new TimeRecord[100]; Assume that timeCards has been initialized with TimeRecord objects. Consider the following code segment that is intended to compute the total of all the times stored in timeCards. TimeRecord total = new TimeRecord(0,0); for (int k = 0; k < timeCards.length; k++) { / missing

total.advance(timeCards[k].getHours(), timeCards[k].getMinutes())


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

Module 1: American Government and Civic Engagement

View Set

Ortoleva US Government & Politics Quizzes

View Set

Practice questions for N2 exam 5

View Set

Property Insurance- Policy Perils and Exclusions

View Set