cis206_java chap6

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

1) Which method name is not valid? a. calcAverage() b. calc Average() c. _calcAvg() d. calc_average()

B

12) How does using findMax() improve the code? public static int findMax(int val1, int val2) { int max; if(val1 > val2){ max = val1; } else{ max = val2; } return max; } public static void main(String args[]){ int max1; int max2; int max3; max1 = findMax(15, 7); max2 = findMax(100, 101); max3 = findMax(20, 30); } a. Using findMax() makes main() run faster b. Using findMax() decreases redundant code c. Using findMax() reduces the number of variables d. Using findMax() does not improve the code

B

16) For the given program, how many print statements will execute? public static void printShippingCharge(double weight) { if((weight > 0.0) && (weight <= 10.0)){ System.out.println(weight * 0.75); } else if((weight > 10.0) && (weight <= 15.0)) { System.out.println(weight * 0.85); } else if((weight > 15.0) && (weight <= 20.0)) { System.out.println(weight * 0.95); } } public static void main(String args[]) { printShippingCharge(18); printShippingCharge(6); printShippingCharge(25); } a. 1 b. 2 c. 3 d. 9

B

19) Which is true about testing a method with three integer parameters and one integer return value? a. A good programmer would test all possible input values b. Using a variety of calls involving assert() is a good way to test the method c. Three test vectors are likely sufficient d. Each test case should include a negative value as a parameter

B

22) Which is true regarding how methods work? a. After a method returns, its local variables keep their values, which serve as their initial values the next time the method is called b. A method's local variables are discarded upon a method's return; each new call creates new local variables in memory c. A return address indicates the value returned by the method d. If a method returns a variable, the method stores the variable's value until the method is called again

B

29) 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); } a. p = 3, q = 3 b. p = 4, q = 3 c. p = 3, q = 4 d. Error: Argument names must match parameter names

B

30) What is the output? public static int changeValue(int x) { int y; x = x * 2; y = x + 1; return y; } public static void main(String args[]) { int a; a = 5; System.out.print("a = " + a + ", result = " + changeValue(a)); } a. a = 5, result = 6 b. a = 5, result = 11 c. a = 10, result = 11 d. Error: Cannot assign parameter x

B

33) Which method is called? p = processData(9.5, 2.3); a. public static int processData (double num, int x){...}; b. public static int processData (double num, double x){...}; c. Neither method is called d. Both methods could be called, resulting in a compiler error

B

35) What is the output when calling printName("Juan", 19); public static void printName(String 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(String name, int id, int age) { System.out.print(name + " ID: " + id + " age: " + age); } a. Juan ID: 19 age: 19 b. Juan ID: 19 c. Name ID: 19 d. There is no output

B

39) Creating a single Scanner object in main() and passing the object to other methods is considered good practice because _____ a. creating more than one Scanner object in a program will cause a compile error b. multiple Scanner objects may read data in unexpected ways c. the program will run more quickly d. the program will be less confusing for the person entering data

B

42) What is output? char[] emotion = {'h','a','p','p','y'}; System.out.print(emotion[4]); a. 4 b. y c. happy d. p

B

45) Which XXX completes this method that adds a note to an oversized array of notes? public static void addNote(String[] allNotes, int numNotes, String newNote) { allNotes[numNotes] = newNote; XXX } a. --numNotes; b. ++numNotes; c. no additional statement needed d. ++allNotes;

B

54) Which is true? a. Oversize arrays require fewer parameters than perfect size arrays b. Oversize arrays may have unused elements c. Perfect size arrays may have unused elements d. Oversize arrays can be returned from a method

B

58) What is the content of array ages after calling shift()? public static void shift(int[] nums) { int i; for(i = 0; i < nums.length-1; ++i) { nums[i] = nums[i+1]; } } public static void main(String[] args) { int[] ages = {16, 19, 24, 17}; shift(ages); } a. {16, 19, 24, 17} b. {19, 24, 17, 17} c. (19, 24, 17, 16) d. {16, 16, 19, 24}

B

6) Which is a valid method call for findMax()? public static double findMax(double x, double y){. . .} a. double val = findMax(); b. double val = findMax(4.2, 20.7); c. double val = findMax(19.6); d. int val = findMax(7.9, 20.3);

B

64) The frequency() method is supposed to return the number of occurrences of target within the array. Identify the location of any errors. public static int[] frequency(int[] nums, int target){ int index; int count = 0; for(index = 0; index < nums.length; ++index) { if(nums[index] == target) { ++count; } } return count; } a. The code has no errors b. Only the method signature has an error c. Only the method body has errors d. Both the method signature and body have errors

B

65) What is the likely purpose of someMethod() based on the signature? void someMethod(int[] nums, int value) a. Create a new array b. Change all array elements tovalue c. Return the number of timesvalueoccurs in the array d. Make a copy of the array

B

68) The frequency() method is supposed to return the number of occurrences of target within the array. Identify the location of any errors. public static int frequency(int[] nums, int target){ int index; int count = 0; for(index = 0; index < nums.length; ++index) { if(nums[index] == target) { ++count; } } return count; } a. The code has no errors b. Only the method signature has an error c. Only the method body has errors d. Both the method signature and body have errors

B

71) Which isNOTa valid javadoc tag? a. @version b. @refer c. @author d. @param

B

11) Which is the best stub for a method that calculates an item's tax? a. public static double ComputeTax(double itemPrice) { double TAXRATE = 0.0675; return itemPrice * TAXRATE; } b. public static double ComputeTax(double itemPrice) { double tax; return tax; } c. public static double ComputeTax(double itemPrice) { System.out.println("FIXME: Calculate tax"); return 0.0; } d. public static double ComputeTax(double itemPrice) { double tax = 0.0; }

C

13) A programmer develops code by repeating short sessions of writing, compiling, and testing until the project is finished. This is an example of _____. a. Pair programming b. Modular development c. Incremental development d. Parallel programming

C

15) Which XXX calculates the area using the CalcSquare() method? The formula to calculate the area of a circle is pi * r2 . public static double calcSquare(double x) { return x*x; } public static double calcArea(double r) { final double PI_VAL = 3.14159265; XXX; } public static void main(String args[]) { System.out.println(calcArea(5.0)); } a. PI_VAL * r * r; b. PI_VAL * calcSquare(r); c. return PI_VAL * calcSquare(r); d. return PI_VAL * calcSquare(r) * calcSquare(r);

C

2) In calcArea(), width and height are _____ public static void calcArea(int width, int height){}; a. arguments b. statements c. parameters d. method names

C

24) Which line has an error? 1 public static int computeSumOfSquares(int num1, int num2) { 2 int sum; 3 sum = (num1 * num1) + (num2 * num2); 4 return; 5 } a. Line 1 b. Line 3 c. Line 4 d. None of the lines contains an error

C

25) calcSum() was copied and modified to create calcProduct(). Which line in calcProduct() contains an error? 1 public static int calcSum(int a, int b) { 2 int s; 3 s = a + b; 4 return s; 5 } 6 public static int calcProduct(int a, int b) { 7 int p; 8 p = a * b; 9 return s; 10 } a. Line 7 b. Line 8 c. Line 9 d. There are no errors

C

27) Which is true about arrays and methods? a. Arrays cannot be passed to methods b. Passing an array to a method creates a copy of the array within the method c. An array is passed to a method as a reference d. A programmer must make a copy of an array before passing the array to a method

C

28) What is output? public static void changeRainfall(double [] dailyRain) { dailyRain[0] = 0.1; } public static void main(String args[]) { double [] rainValues = new double[2]; rainValues[0] = 2.9; rainValues[1] = 1.3; changeRainfall(rainValues); System.out.println(rainValues[0]); } a. 2.9 b. 1.3 c. 0.1 d. an array reference

C

32) What is the scope of odometer defined on line 7? 1 public class SimpleCar { 2 private int odometer; 3 public void driveForward(int miles) { 4 odometer = odometer + miles; 5 } 6 public void changeOdometer(int miles) { 7 int odometer; 8 odometer = miles; 9 } 10} a. method driveForward() b. odometer is not defined c. method changeOdometer() d. all of class SimpleCar

C

37) What is the output for the call displayTime(15, 24, 65)? public static void displayTime(int hr, int min, int sec) { 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); } a. 15:24:65 b. 1:24:1 c. Error: hour Error: second 1:24:1 d. Error: second 1:24:1

C

4) Which list correctly defines three parameters of type int, double, and String for createRecord()?public static void createRecord(. . .){}; a. (int num, int cost, int name) b. (num, cost, name) c. (int num, double cost, String name) d. (17, 12.34, "Janet")

C

41) What is output? char[] emotion = {'n','o','t',' ','s','a','d'}; System.out.print(emotion.length); a. 3 b. 6 c. 7 d. Error: index out of bounds

C

44) What is output? public static void replace(int [] allGrades, int examScore) { allGrades[1] = examScore; } public static void main(String args[]) { int[] myGrades = {72,84,75,92,65}; replace(myGrades, 100); System.out.print(myGrades[1]); } a. 72 b. 84 c. 100 d. error

C

50) Which XXX adds an item to anoversizearray? public static int addItem(String[] shoppingList, int listSize, String item) { if(listSize == shoppingList.length) { shoppingList[listSize] = item; XXX } return listSize; } a. return listSize + 1; b. --shoppingList; c. ++listSize; d. --listSize;

C

57) What is output? public static void curveGrades(int curve, int[] grades) { curve = curve + 5; grades[0] = grades[0] + curve; } public static void main(String[] args) { int[] myGrades = {72, 67, 85}; int amt; amt = 10; curveGrades(amt, myGrades); System.out.println(amt + ", " + myGrades[0]); } a. 10, 72 b. 15, 87 c. 10, 87 d. 10, 82

C

67) What is the likely purpose of someMethod() based on the signature? int someMethod(int[] nums) a. Create a new array b. Modify the array contents c. Calculate a value based on the array contents d. Make a copy of the array

C

10) Select the true statement. a. A benefit of methods is to increase redundant code b. A key reason for including methods is to make main() run faster c. Method stubs can not be compiled d. Method stubs are temporary placeholders used during development

D

14) For the following method, which is a valid function call? Assume maxValue is an integer. public static int findMax(int x, int y) { if (x > y){ return x; } else { return y; } } a. maxValue = findMax(15 25); b. maxValue = findMax(); c. maxValue = findMax(15, findMax(35, 25)); d. findMax = maxValue(15, 25);

D

17) What is the output? public static void printFeverCheck (double temp) { final double NORMAL_TEMP = 98.6; final double CUTOFF_TEMP = 95; double degreesOfFever; if (temp > NORMAL_TEMP) { degreesOfFever = temp - NORMAL_TEMP; System.out.print("You have " + degreesOfFever + " degrees of fever."); } else if (temp < CUTOFF_TEMP) { degreesOfFever = CUTOFF_TEMP - temp; System.out.print("Your temperature is " + degreesOfFever + " below 95."; } } public static void main(String args[]) { double bodyTemperature; bodyTemperature = 96.0; System.out.print("Checking for fever..."); printFeverCheck(bodyTemperature); } a. Nothing is output b. Checking for fever... Your temperature is 2.6 below 95. c. Checking for fever...Your temperature is 2.6 below 95. d. Checking for fever...

D

18) Consider a method that is provided a numerical grade (0 - 100) as a parameter and returns a corresponding letter grade (A - F). Which collection of values would be the most appropriate for testing the method? a. 60, 70, and 90 b. A, B, C, D, and F c. all values between -100 and 100 d. -1, 0, 50, 65, 73, 92, 100, and 1000

D

20) Which XXX tests the input value 4 for squareNum()? public static int squareNum(int origNum) { return origNum * origNum; } public static void main(String args[]) { System.out.println("Testing started"); XXX; System.out.println("Testing completed"); } a. test(4) b. squareNum(4) c. assert(test(4)==16) : "4, expecting 16, got: " + test(4); d. assert(squareNum(4)==16) : "4, expecting 16, got: " + squareNum(4);

D

23) Which is true regarding tripleTheValue()? public static int tripleTheValue(int val) { int tripleValue; tripleValue = val * 3; } a. val should be returned b. tripleValue should be defined as double c. there are no errors d. tripleValue should be returned

D

26) What is output? public static void changeRainfall(double [] dailyRain){ dailyRain[0] = 0.1; } public static void main(String args[]){ double [] rainValues = new double[2]; rainValues[0] = 2.9; rainValues[1] = 1.3; changeRainfall(rainValues); System.out.println(rainValues); } a. 2.9 b. 1.3 c. 0.1 d. an array reference

D

31) What is the scope of odometer defined on line 2 1 public class SimpleCar { 2 private int odometer; 3 public void driveForward(int miles) { 4 odometer = odometer + miles; 5 } 6 public void changeOdometer(int miles) { 7 int odometer; 8 odometer = miles; 9 } 10} a. method driveForward() b. odometer is not defined c. method changeOdometer() d. all of class SimpleCar

D

36) What is the output for the call displayTime(2, 24, 65)? public static void displayTime(int hr, int min, int sec) { 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); } a. 2:24:65 b. 2:24:1 c. Error: second 2:24:65 d. Error: second 2:24:1

D

52) Select the proper signature for a method that removes the last item in anoversizearray. a. String[ ] removeLast(String[ ] shoppingList, int listSize) b. int removeLast(String[ ] shoppingList, String item) c. void removeLast(String[ ] shoppingList, int listSize) d. int removeLast(String[ ] shoppingList, int listSize)

D

55) Which method call would print the items in a perfect size array? Assume shoppingList is a perfect sized array, and printItems is a function that prints a. printItems(shoppingList, listSize); b. shoppingList = printItems(shoppingList); c. listSize = printItems(shoppingList); d. printItems(shoppingList);

D

60) Which statement is true? void swap(int age, int [] grades) { a. the value of age is stored in the heap b. the reference to grades is stored in the heap c. the elements in grades can not be modified within the method d. the value of grades[2] is stored in the heap

D

61) What is copiedNums' length after the code segment? int[] originalNums = {1,2,3,4,5}; int[] copiedNums; copiedNums = copy(originalNums, 2); public static int[] copy(int[] nums, int changeAmt) { int[] modifiedNums = new int[nums.length * changeAmt]; int index; for(index = 0; index < nums.length; ++index){ modifiedNums[index] = nums[index]; } return modifiedNums; } a. 0 b. 5 c. 7 d. 10

D

62) What is copiedNums' length after the code segment? int[] originalNums = {1, 2, 3, 4}; int[] copiedNums = {0, 0, 0}; copiedNums = copy(originalNums, 2); public static int[] copy(int[] nums, int changeAmt) { int[] modifiedNums = new int[nums.length * changeAmt]; int index; for(index = 0; index < nums.length; ++index){ modifiedNums[index] = nums[index]; } return modifiedNums; } a. 3 b. 4 c. 6 d. 8

D

66) Which XXX in firstHalf() that returns a new array with a copy of the first half of all elements in array nums. public static int[] firstHalf(int[] nums) { int index; int size = nums.length / 2; XXX for(index = 0; index < size; ++index) { halfNums[index] = nums[index]; } return halfNums; } a. int[ ] halfNums; b. int halfNums = new int[size]; c. int[ ] halfNums = new int[nums.length]; d. int[ ] halfNums = new int[size];

D

69) Which statement is true? a. Javadoc comments are placed in separate files from source code b. The @loop tag should be used when appropriate c. Javadoc comments are intended for the initial developer rather than future programmers who will interface with the code d. Javadoc tags should precede methods and class definitions

D

7) Given: public static void printName(String first, String last){ System.out.println(last + ", " + first); } What is printed with the following method call? printName("Bob", "Henderson"); a. Bob, Henderson b. Henderson,Bob c. Bob Henderson d. Henderson, Bob

D

9) The following program generates an error. Why? public static void printSum(int num1, int num2) { System.out.print(num1 + num2); } public static void main(String args[]) { int y; y = printSum(4, 5); return 0; } a. printSum() is missing a "return;" statement. b. The values 4 and 5 cannot be passed directly to printSum() c. main() has a return statement that returns the value 0 d. printSum() has void return type, so cannot be assigned to a variable

D

8) How many items are returned from printValues()? public static void printValues(int a, int b){. . .} a. 0 b. 1 c. 2 d. 3

0

38) How many Scanner objects should be added to the program? public static String readFirst() { // read first name from input stream } public static String readLast() { // read last name from input stream } public static String readStreet() { // read street address from input stream } public static void main(String args[]) { String personInfo = readFirst() + readLast() + readStreet(); } a. 0 b. 1 c. 3 d. 4

1

5) How many items are returned from calcAverage()? public static int calcAverage(int a, int b, int c){. . .} a. 0 b. 1 c. 2 d. 3

1

47) Which is the best capacity for anoversizearray that records daily phone sales during a month. Maximum number of sales for a single day is 100. a. 30 b. 31 c. 99 d. 100

100

51) What is the length of array csGrades at the end of main()? public static int addGrade(int[] allGrades, int grade, int listSize) { allGrades[listSize] = grade; ++listSize; return listSize; } public static void main(String[] args) { final int MAX_GRADES = 100; int[] courseGrades = new int[MAX_GRADES]; int numGrades = 0; numGrades = addGrade(csGrades, 74, numGrades); numGrades = addGrade(csGrades, 74, numGrades); numGrades = addGrade(csGrades, 74, numGrades); } a. 3 b. 74 c. 99 d. 100

100

21) While performing main(), what is the value of tripleValue at XXX? public static int tripleTheValue(int val) { int tripleValue; tripleValue = val * 3; return tripleValue; } public static void main(String args[]) { int result; result = tripleTheValue(4); XXX } a. not defined b. 4 c. 3 d. 12

12

3) Which statement correctly calls calcArea() with two int arguments? a. calcArea(4, 12); b. public void calcArea(int w, int h); c. calcArea(int w, int h); d. calcArea( );

A

34) What is the output when calling printName("Juan", 429, 19); public static void printName(String 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(String name, int id, int age) { System.out.print(name + " ID: " + id + " age: " + age); } a. Juan ID: 429 age: 19 b. Juan ID: 429 c. There is no output d. Name ID: 19 age: 429

A

40) What will be the value of sum at the end of main() with the provided input? Input: 1 2 3 public static int readNum() { Scanner scnr = new Scanner(System.in); return scnr.nextInt(); } public static void main(String [] args) { int sum = readNum() + readNum() + readNum(); } a. Unpredictable b. 3 c. 6 d. 123

A

43) Which isnota good use of a perfect size array? a. university student IDs b. names of the week c. names of the months d. names of marker colors in a box of 8

A

46) Which is true for the declaration of anoversizearray that records daily sales for any month? int MAX_DAYS = 31; int[] dailySales = new int[MAX_DAYS]; int dailySalesSize = 0; a. MAX_DAYS should be a constant b. dailySalesSize should be declared as double c. MAX_DAYS should be assigned 365 d. dailySalesSize should be assigned MAX_DAYS

A

53) ShoppingList is anoversizearray of items to be purchased. Which method call should be used to add an item? a. addItem(shoppingList, listSize, item); b. shoppingList = addItem(shoppingList, listSize, item); c. listSize = addItem(shoppingList, listSize, item); d. addItem(shoppingList, item);

A

59) What is output? public static void swap(int val1, int val2) { int temp; temp = val1; val1 = val2; val2 = temp; } public static void main(String[] args) { int x; int y; x = 10; y = 20; swap(x, y); System.out.println(x + ", " + y); } a. 10, 20 b. 20, 20 c. 10, 10 d. 20, 10

A

63) What is copiedNums' length after the code segment? int[] originalNums = {1,2,3,4,5}; int[] copiedNums; copy(originalNums, 3); public static int[] copy(int[] nums, int changeAmt) { int[] modifiedNums = new int[nums.length * changeAmt]; int index; for(index = 0; index < nums.length; ++index){ modifiedNums[index] = nums[index]; } return modifiedNums; } a. null b. 0 c. 5 d. 15

A

70) Which tool parses source code to generate HTML documentation? a. Javadoc b. Block tag c. Doc comment d. Compiler

A

49) Select the proper method signature that counts the number of times a given item appears in anoversizearray. XXX countMatches(String[ ] shoppingList, YYY String item) a. XXX: String[ ] | YYY: int listSize, b. XXX: int | YYY: listSize, c. XXX: void | YYY: int listSize, d. XXX: int | YYY: (nothing)

b

72) Used with Javadoc documentation, a keyword beginning with the "@" character is a ___. a. inline comment b. block tag c. Javadoc comment d. method signature

b

56) Which type of array can be used for the method signature? void reverseItems(String[] shoppingList, int startIndex, int stopIndex) a. Only perfect size arrays b. Only oversize arrays c. Both oversize and perfect size arrays d. Neither array type is appropriate

c

48) Which XXX removes the first item in anoversizearray? public static int removeFirstItem(String[] shoppingList, int listSize) { int i; for(i = 0; i < listSize-1; ++i) { XXX } if(listSize > 0) { --listSize; } return listSize; } a. shoppingList[i+1] = shoppingList[i]; b. shoppingList[i] = shoppingList[i-1]; c. --shoppingList[i]; d. shoppingList[i] = shoppingList[i+1];

d


Ensembles d'études connexes

Life Insurance Exam Test Questions

View Set

Food Safety and Sanitation-Quiz 2

View Set

Introduction to Statistics Chapter 4-5

View Set

Final Exam pre tests broke down.

View Set