Chapter 6: User-Defined Methods
Which list correctly defines three parameters of type int, double and String for createRecord()?public static void createRecord(. . .){}; Question options: (int num, int cost, int name) (num, cost, name) (int num, double cost, String name) (17, 12.34, "Janet")
(int num, double cost, String name)
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? Question options: 60, 70, and 90 A, B, C, D, and F all values between -100 and 100 -1, 0, 50, 65, 73, 92, 100, and 1000
-1, 0, 50, 65, 73, 92, 100, and 1000
How many items are returned from printValues()?public static void printValues(int a, int b){. . .} Question options: 0 1 2 3
0
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]); } Question options: 2.9 1.3 0.1 an array reference
0.1
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(); } Question options: 0 1 3 4
1
How many items are returned from calcAverage()? public static int calcAverage(int a, int b, int c){. . .} Question options: 0 1 2 3
1
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]; }r eturn modifiedNums; } Question options: 0 5 7 10
10
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); } Question options: 10, 20 20, 20 10, 10 20, 10
10, 20
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]); } Question options: 10, 72 15, 87 10, 87 10, 82
10, 87
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]); } Question options: 72 84 100 error
100
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); } Question options: 3 74 99 100
100
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); } Question options: 1 2 3 9
2
What is output? char[] emotion = {'n','o','t',' ','s','a','d'}; System.out.print(emotion.length); Question options: 3 6 7 Error: index out of bounds
7
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; } Question options: 3 4 6 8
8
Which is true regarding how methods work? Question options: After a method returns, its local variables keep their values, which serve as their initial values the next time the method is called A method's local variables are discarded upon a method's return; each new call creates new local variables in memory A return address indicates the value returned by the method If a method returns a variable, the method stores the variable's value until the method is called again
A method's local variables are discarded upon a method's return; each new call creates new local variables in memory
Which is true about arrays and methods? Question options: Arrays cannot be passed to methods Passing an array to a method creates a copy of the array within the method An array is passed to a method as a reference A programmer must make a copy of an array before passing the array to a method
An array is passed to a method as a reference
What is the output? public static double checkForFever (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."; } return degreesOfFever; } public static void main(String args[]) { double bodyTemperature; double degreesOfFever; bodyTemperature = 96.0; System.out.print("Checking for fever..."); degreesOfFever = checkForFever(bodyTemperature); } Question options: Nothing is output Checking for fever...Your temperature is 2.6 below 95. Checking for fever...Your temperature is 2.6 below 95. Checking for fever...
Checking for fever...
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); } Question options: 15:24:65 1:24:1 Error: hour Error: second 1:24:1 Error: second 1:24:1
Error: hour Error: second 1:24:1
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); } Question options: 2:24:65 2:24:1 Error: second 2:24:65 Error: second 2:24:1
Error: second 2:24:1
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"); Question options: Bob, Henderson Henderson,Bob Bob Henderson Henderson, Bob
Henderson, Bob
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); } Question options: Juan ID: 19 age: 19 Juan ID: 19 Name ID: 19 There is no output
Juan ID: 19
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); } Question options: Juan ID: 429 age: 19 Juan ID: 429 There is no output Name ID: 19 age: 429
Juan ID: 429 age: 19
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 } Question options: Line 1 Line 3 Line 4 None of the lines contains an error
Line 4
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 } Question options: Line 7 Line 8 Line 9 There are no errors
Line 9
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; } Question options: The code has no errors Only the method signature has an error Only the method body has errors Both the method signature and body have errors
Only the method signature has an error
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; } Question options: The code has no errors Only the method signature has an error Only the method body has errors Both the method signature and body have errors
The code has no errors
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);} Question options: Using findMax() makes main() run faster Using findMax() decreases redundant code Using findMax() reduces the number of variables Using findMax() does not improve the code
Using findMax() decreases redundant code
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)); } Question options: a = 5, result = 6 a = 5, result = 11 a = 10, result = 11 Error: Cannot assign parameter x
a = 5, result = 11
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} Question options: method driveForward() odometer is not defined method changeOdometer() all of class SimpleCar
all of class SimpleCar
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); } Question options: 2.9 1.3 0.1 an array reference
an array reference
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"); } Question options: test(4) squareNum(4) assert(test(4)==16) : "4, expecting 16, got: " + test(4); assert(squareNum(4)==16) : "4, expecting 16, got: " + squareNum(4);
assert(squareNum(4)==16) : "4, expecting 16, got: " + squareNum(4);
Which method name is not valid? Question options: calcAverage() calc Average() _calcAvg() calc_average()
calc Average()
Which statement correctly calls calcArea() with two int arguments? Question options: calcArea(4, 12); public void calcArea(int w, int h); calcArea(int w, int h); calcArea( );
calcArea(4, 12);
Which is a valid method call for findMax()? public static double findMax(double x, double y){. . .} Question options: double val = findMax(); double val = findMax(4.2, 20.7); double val = findMax(19.6); int val = findMax(7.9, 20.3);
double val = findMax(4.2, 20.7);
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} Question options: method driveForward() odometer is not defined method changeOdometer() all of class SimpleCar
method changeOdometer()
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 } Question options: not defined 4 3 12
not defined
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; } Question options: null 0 5 15
null
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); } Question options: p = 3, q = 3 p = 4, q = 3 p = 3, q = 4 Error: Argument names must match parameter names
p = 4, q = 3
In calcArea(), width and height are _____ public static void calcArea(int width, int height){}; Question options: arguments statements parameters method names
parameters
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; } Question options: printSum() is missing a "return;" statement. The values 4 and 5 cannot be passed directly to printSum() main() has a return statement that returns the value 0 printSum() has void return type, so cannot be assigned to a variable
printSum() has void return type, so cannot be assigned to a variable
Which method is called? p = processData(9.5, 2.3); Question options: public static int processData (double num, int x){...}; public static int processData (double num, double x){...}; Neither method is called Both methods could be called, resulting in a compiler error
public static int processData (double num, double x){...};
Which XXX removes the first item in an oversize array? public static int removeFirstItem(String[] shoppingList, int listSize) { int i; for(i = 0; i < listSize-1; ++i) { XXX } if(listSize > 0) { --listSize; } return listSize; } Question options: shoppingList[i+1] = shoppingList[i]; shoppingList[i] = shoppingList[i-1]; --shoppingList[i]; shoppingList[i] = shoppingList[i+1];
shoppingList[i] = shoppingList[i+1];
Which is true regarding tripleTheValue()? public static int tripleTheValue(int val) { int tripleValue; tripleValue = val * 3; } Question options: val should be returned tripleValue should be defined as double there are no errors tripleValue should be returned
tripleValue should be returned
What is output? char[] emotion = {'h','a','p','p','y'}; System.out.print(emotion[4]); Question options: 4 y happy p
y
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); } Question options: {16, 19, 24, 17} {19, 24, 17, 17} (19, 24, 17, 16) {16, 16, 19, 24}
{19, 24, 17, 17}