Exam 4

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Given the following method definition: public static int mystery(int a[]) { int m = a[0]; for (int i = 0; i < a. length; i++) { if (m > a[i]) m = a[i]; } return m; } What would be returned by mystery if it was passed the following array? int a[] = {34 , 18 , 34 , 38 , 27 , 37 , 39 , 21 , 19};

18

Which method(s) would produce the following output if they were passed the parameter, "hamster"? hamster hamste hamst hams ham ha h

I only I. public static void mystery(String wo) { System.out.println(wo); if (wo.length() > 0) mystery( wo.substring(0, wo.length() - 1)); }

Consider the following methods: public static void printSport(double n) { System.out.print("football "); printSport((int)(n)); } public static void printSport(int n) { System.out.print("basketball "); } What is output by the method call printSport(8)?

basketball

What is output by the following code? String q = "adjective"; String r = "stinky"; System.out.println( q.charAt( r.indexOf ('t')));

d

The following is intended to return the location of the first instance of the String the user enters from the keyboard, -1 if not found. String names [] = new String[20]; //assume array is initialized System.out.println("Enter a name to search for: "); String lookingFor = scan.nextLine(); int found = -1; for (int i = 0; i < names.length; i++) { if ( /* Missing Code */ ) { found = i; break; } } Which of the following could replace /* Missing Code* / so that it works as intended?

lookingFor.equals(names[i])

What return statement may be used in p()? public static int[] p() { // ... }

return new int[]{1, 2, 3};

What is output by the following code? public static void stuff(int w) { w -= 2; } public static void main(String a[]) { int n = 2; stuff(n); System.out.print(n); }

2

What is output by the following code? System.out.print(21.0/5); What is output?

4.2

What does the following algorithm do? public static void mystery(int nums[]) { for (int i = 0; i < nums.length; i++) { if (nums[i] % 2 != 0) nums[i]++; } }

Changes all the values in the array to even numbers.

Consider the following recursive method: public static int recur(int x) { if (x >= 0) return x + recur(x - 1); return 0; } What is returned by the method call recur(9)?

45

Assume the following method has been defined: public static int mystery(String a[], int x) { int c = 0; for (int i = 0; i < a.length; i++) { if (a[i].length() > x) c++; } return c; } What is output by the following code? String b [] = {"aardvark", "banana", "cougar", "daikon", "elephant", "fog", "get"}; System.out.println(mystery(b, 5));

5

What is output by the following code? int a [] = {64 , 66 , 67 , 37 , 73 , 70 , 95 , 52 , 81 , 82}; for (int i = 0; i < a.length; i++) { a[i] = a[i] / 10; } for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); }

6 6 6 3 7 7 9 5 8 8

Consider the following methods: public static double average(int nums[]) { int sum =0; for (int i = 0; i < nums.length; i++) { sum += nums[i]; } return (1.0 * sum) / nums.length; } //average public static int[] mystery(String a[]) { int temp [] = new int[a.length]; for (int i = 0; i < a.length; i++) { temp[i] = a[i].length(); } return temp; } //mystery What is output by running the following? String spelling[] = {"against", "forms", "belief", "government", "democratic", "movement", "understanding", "single", "followed", "scenario"}; System.out.println( average( mystery(spelling)));

8.1

Consider the following method checking whether a student has passing grades: public boolean isPassing(int finalExam, int cumulativeAverage) { /* Missing Code */ } A student can pass a class if one of the following is true: The final exam is at least 98 The cumulative average is over 60 Which of the following correctly replaces /* Missing Code */ so that the method works as intended?

I and II only I. if ((finalExam >= 98) || (cumulativeAverage > 60)) return true; return false; II. boolean pass = false; if (finalExam >= 98) pass = true; if (cumulativeAverage > 60) pass = true; return pass;

What does the following method do? public static int mystery(int a[], int x) { int c = 0; for(int i = 0; i < a.length; i++) { if (a[i] == x) c++; } return c; }

Returns a count of the number of times x appears in the array.

What does the following algorithm do? public static boolean mystery(int nums[]) { for (int i = 1; i < nums.length; i++) if (nums[i - 1] >= nums[i]) return false; return true; }

Returns true if each element of the array is greater than the element before.

Consider the following method: static void nPrint(String a, int n) { System.out.println(a.charAt(n)); } What potential error could this method cause?

Run time error - index out of range

What mistake is in the following code? public static double mystery (double a) { System.out.println(a * 3.14); }

The double should be changed to void since the method does not return a value.

When you pass an array to a method, the method receives ______.

a copy of the reference to the array

Consider a method defined with the header: public static void doStuff(int x) Which of the following method calls is legal?

doStuff(9);


Kaugnay na mga set ng pag-aaral

Standard Form of a Line: Assignment

View Set

Chapter 6-11 Nursing Assistant Study Guide

View Set

Ch. 12: Management of Human Resources

View Set

CLINICAL DOCUMENTATION (Domain 1)

View Set

Entrepreneurial Management Chapter 5

View Set