CompSci Unit 2

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is returned by method bot given the call shown below? //client code int[] arr = {1,2,3,4,5,6}; System.out.println( bot(arr) ); //method bot public static int bot( int[] ray ) { int stuff = 0; for(int i = 0; i<ray.length; i++) stuff = stuff + ray[i]; return stuff; }

21

Consider the following code segment. int x = 1; while ( /* missing code */ ) { System.out.print(x + " "); x = x + 2; } Consider the following possible replacements for /* missing code */. I. x<6 II. x!=6 III. x<7 Which of the proposed replacements for /* missing code */ will cause the code segment to print only the values 1 3 5?

I and III only

Assume that x and y have been declared and initialized with int values. Consider the following Java expression. (y > 10000) || (x > 1000 && x < 1500) Which of the following is equivalent to the expression given above? (y > 10000) && (x > 1000 || x < 1500) (y > 10000 || x > 1000) || (y > 10000 || x < 1500) (y > 10000 || x > 1000) && (y > 10000 || x < 1500) (y > 10000 && x > 1000) && (y > 10000 && x < 1500) (y > 10000 && x > 1000) || (y > 10000 && x < 1500)

(y > 10000 || x > 1000) && (y > 10000 || x < 1500)

Consider the following code segment. int sum = 0; int k = 1; while (sum < 12 || k < 4) sum += k; System.out.println(sum); What is printed as a result of executing the code segment?

12

Which of the following lines would correctly fill /* code */ in method getStuff() to return the int array named retArray? public int[] getStuff(int[] x, int howMany) { int[] retArray; //assume retArray has been properly instantiated /* code */ } return new int retArray; return x; return new int[0]; return new int[]; return retArray;

return retArray;

What is the returned by the call go(8) ? public static String go(int x) { if (x==5) return "same"; else if (x>8) return "notsame"; return "done"; }

done

What command is used to instantiate a file input stream?

new File( )

Consider the following method. public String mystery(String input) { String output = ""; for (int k = 1; k < input.length(); k = k + 2) { output += input.substring(k, k + 1); } return output; } What is returned as a result of the call mystery("computer") ?

"optr"

Assume that a and b have been defined and initialized as int values. The expression !(!(a != b ) && (b > 7)) is equivalent to which of the following?

(a !=b) || (b <= 7)

What is returned by the call go( 5.0 ) ? public static double go( double w ) { while(w > 0) { w = w - 0.75; } return w; }

-0.25

What is printed by the following code? int[] arr = {7,2,8,1,5, 8,2,6,10,7}; int sum = 0; for (int i=0; i < arr.length; i++) { sum += arr[i]; i = arr[i]; } System.out.println("Sum: "+sum);

17

Which of the following loops will print out the numbers 5, 10, 15, 20, 25? I. int i=5; while(i<=25) { System.out.println(i); i=i+5; } II. for(int i=5;i<=25;i=i+5) { System.out.println(i); } III. for(int i=4;i<24;i=i+5) { System.out.println(i+1); }

I and II only

How many instances of variable declared 'static int x' are instantiated in a program. One for each class where it is declared One for each objected that is instantiated. Only one for an entire program No instance is created.

One for each class where it is declared

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

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

Which of the following correctly fill /* code */ in method isIt() ? //method isIt should return true if all of the numbers //in array are in decreasing ( decending ) order public static boolean isIt(int[] array) { for(int spot=0; spot<array.length-1; spot++) { /* code */ return false; } return true; } if( array[spot] < array[spot-1] ) if( array[spot-1] < array[spot] ) if( array[spot] < array[spot+1] ) if( array < array+1 ) if( spot < spot+1)

if( array[spot] < array[spot+1] )

What command is used to instantiate a file output stream?

new FileOutputStream( )

What components of method signature match a method call to a method declaration? return type access specifier argument list name

return type, argument list, name

What command would actually write an int value 'iVal' to a correctly declared ouput stream identified as `write'?

write.println(iVal)

Consider the following code segment. int[] arr = {1, 2, 3, 4, 5, 6, 7}; for (int k = 3; k < arr.length - 1; k++) { arr[k] = arr[k + 1]; } Which of the following represents the contents of arr as a result of executing the code segment?

{1, 2, 3, 5, 6, 7, 7}

What is returned by method bot given the call shown below? //client code int[] sRay = {9,9,7,1,2,3,4,5,6,2,2,3,7,3}; System.out.println( bot(sRay) ); //method bot public static int bot( int[] ray ) { int stuff = 0; for(int i = 0; i<ray.length; i++) if(ray[i] % 2 == 0) stuff = stuff + ray[i]; return stuff; }

16

What is returned by the call go( 10 ) ? public static String go( int x) { String s = ""; for(int n = x; n > -3; n = n - 3) s = s + n + " "; return s; }

10 7 4 1 -2

What is returned by the call go(5)? public static int go(int x) { int q=0; while( x > 0) { q = q + x; x = x - 1; } return q; }

15

Consider the following code segment shown below. int x = 0; while ( /* missing code */ ) { System.out.print(x + " "); x = x + 3; } Listed below are possible replacements for /* missing code */. I. x <= 12 II. x < 13 III. x <= 13 Which of the proposed replacements for /* missing code */ will cause the code segment to print only the values 0 3 6 9 12?

I, II, and III

Which of the following loops will print out the numbers 3, 5, 7, 9, 11? I. int i=1; while(i<10) { i=i+2; System.out.println(i); } II. int i=3; while(i<=11) { System.out.println(i); i=i+2; } III. for(int i=4;i<=12;i=i+2) { System.out.println(i-1); }

I, II, and III

Which of the following correctly fill /* code */ in method isIt() ? //method isIt should return true if all of the numbers //in array are in increasing ( ascending ) order public static boolean isIt(int[] array) { for(int spot=0; spot<array.length-1; spot++) { /* code */ return false; } return true; } if( spot > spot+1) if( array[spot] > array[spot-1] ) if( array > array+1 ) if( array[spot] > array[spot+1] ) if( array[spot-1] > array[spot] )

if( array[spot] > array[spot+1] )

Consider the following code segment. int[] arr = {7, 2, 5, 3, 0, 10}; for (int k = 0; k < arr.length - 1; k++) { if (arr[k] > arr[k + 1]) System.out.print(k + " " + arr[k] + " "); } What will be printed as a result of executing the code segment?

0 7 2 5 3 3

What is returned by the call go(5) ? public static int go(int x) { int cnt = 0; while(x > 0) { x = x / 2; cnt = cnt + 1; } return cnt; }

3

What is returned by the call go( 30 ) ? public static String go( int x) { String s = ""; for(int n = x; n > 0; n = n - 5) s = s + n + " "; return s; }

30 25 20 15 10 5

What is printed by the following code? int[] arr = {7,2,4,1,5, 8,2,6,10,7}; int sum = 0; int i = 0; while(i < arr.length) { sum += arr[i]; i = arr[i]; } System.out.println("Sum: "+sum);

42

Consider the following code segment. int num = 2574; int result = 0; while (num > 0) { result = result * 10 + num % 10; num /= 10; } System.out.println(result); What is printed as a result of executing the code segment?

4752

Consider the following method. public void mystery(int[] data) { for (int k = 0; k < data.length - 1; k++) data[k + 1] = data[k] + data[k + 1]; } The following code segment appears in another method in the same class. int[] values = {5, 2, 1, 3, 8}; mystery(values); for (int v : values) System.out.print(v + " "); System.out.println(); What is printed as a result of executing the code segment?

5 7 8 11 19

Consider the following method. public static int mystery(int[] arr) { int x = 0; for (int k = 0; k < arr.length; k = k + 2) x = x + arr[k]; return x; } Assume that the array nums has been declared and initialized as follows. int[] nums = {3, 6, 1, 0, 1, 4, 2}; What value will be returned as a result of the call mystery(nums) ?

7

What value is returned as a result of the call mystery(6) ? public static int mystery(int n) { int x = 1; int y = 1; while (n > 2) { x = x + y; y = x - y; n--; } return x; }

8

Consider the following instance variable and incomplete method. The method getSmallest should return the smallest value in tRay. private int[] tRay; //assume the tRay contains values public int getSmallest() { int small = Integer.MAX_VALUE; /* code */ return small; } Which of the following code segments shown below could be used to replace /* code */ so that getSmallest will work as intended? I. for( int i=0; i<tRay.length; i++ ) if( tRay[i] < small ) small = tRay[i]; II. for( int item : tRay ) if( tRay[item] < small ) small = tRay[item]; III. for( int item : tRay ) if( item < small ) small = item;

I and III only

Which of the following expressions is equivalent to the expression shown below? ! ( !a && !b ) I. a && b II. !a || !b III. a || b

III only

What is the following code attempting to calculate? public static int go( int x, int y) { int cnt = 0 for(int n = x; n < y; n = n + 1) if(n % 2 != 0 ) cnt++; return cnt; }

The code is counting the number of odd numbers between x and y.

Consider the following method. public static void arrayMethod(int nums[]) { int j = 0; int k = nums.length - 1; while (j < k) { int x = nums[j]; nums[j] = nums[k]; nums[k] = x; j++; k--; } } Which of the following describes what the method arrayMethod() does to the array nums?Consider the following method. public static void arrayMethod(int nums[]) { int j = 0; int k = nums.length - 1; while (j < k) { int x = nums[j]; nums[j] = nums[k]; nums[k] = x; j++; k--; } } Which of the following describes what the method arrayMethod() does to the array nums?

The contents of the array nums are reversed.

The argument list of the toString method contains? The list is empty Variables to print String containing message to print data types and names of variables to print

The list is empty

Assume that the array arr has been defined and initialized as follows. int[] arr = /* initial values for the array */ ; Which of the following will correctly print all of the odd integers contained in arr but none of the even integers contained in arr ?

for(int x : arr) { if (x % 2 == 1) System.out.println(x); }

Consider the following code segment. int x = 7; int y = 3; if ((x < 10) && (y < 0)) System.out.println("Value is: " + x * y); else System.out.println("Value is: " + x / y); What is printed as a result of executing the code segment?

Value is: 2

What is output by the code below? String m = "h"; int n =0; do{ m=m+"h"; n++; }while(!m.equals("hhhhh")); System.out.println(m + " - " + n);

hhhhh - 4

Which of the following code segments matches the truth table shown below? a b c 0 0 0 1 0 1 0 1 0 0 1 1 a = !b || !c a = b && c a = !b && !c a = b || c a = !b && c

a = !b && c

Consider the following incomplete method that is intended to return an array that contains the contents of its first array parameter followed by the contents of its second array parameter. public static int[] append(int[] a1, int[] a2) { int[] result = new int[a1.length + a2.length]; for (int j = 0; j < a1.length; j++) { result[j] = a1[j]; } for (int k = 0; k < a2.length; k++) { result[ /* index */ ] = a2[k]; } return result; } Which of the following expressions can be used to replace /* index */ so that append will work as intended? Example: a1 1 2 a2 3 4 result 1 2 3 4

k + a1.length

Consider the following method. public int compute(int n, int k) { int answer = 1; for (int i = 1; i <= k; i++) answer *= n; return answer; } Which of the following represents the value returned as a result of the call compute(n, k) ? 2^k k^n n^k n! nk

n^k

What command is used to create an object that formats an output stream?

new PrintWriter( )

What command is used to create an object that parsers a stream?

new Scanner( )

Consider the following method that is intended to determine if the double values d1 and d2 are close enough to be considered equal. For example, given a tolerance of 0.001, the values 54.32271 and 54.32294 would be considered equal. /** @return true if d1 and d2 are within the specified tolerance, * false otherwise */ public boolean almostEqual(double d1, double d2, double tolerance) { /* missing code */ } Which of the following should replace /* missing code */ so that almostEqual will work as intended? return (d1 - d2) >= tolerance; return ((d1 + d2) / 2) >= tolerance; return Math.abs(d1 - d2) <= tolerance; return (d1 - d2) <= tolerance; return ((d1 + d2) / 2) <= tolerance;

return Math.abs(d1 - d2) <= tolerance;

What is output by the code below? int num=8, yum=15; if(num<9 || ++yum>16 && num>0) { System.out.print("snickers"); } System.out.print(yum);

snickers15

Consider the following method that is intended to return the sum of the elements in the array key. public static int sumArray(int[] key) { int sum = 0; for (int i = 1; i <= key.length; i++) { /* missing code */ } return sum; } Which of the following statements should be used to replace /* missing code */ so that sumArray will work as intended? sum += sum + key[i - 1]; sum += key[i - 1]; sum += key[i]; sum = key[i]; sum += sum + key[i];

sum += key[i - 1];

What command do I need to execute when I am finished accessing a file identified as 'theFile'

theFile.close( )


Set pelajaran terkait

E2, Next Move 2, Unit 5 Personality Adjectives

View Set

Chapter 53: Drug Therapy for Seizure Disorders and Spasticity, Pharmacology Drug Therapy for Seizure Disorders and Skeletal Muscle Disorders, Chapter 53: Drug Therapy for Seizure Disorders and Spasticity, Drug therapy for seizure disorders and spasti…

View Set

Lecture 12: epigenetic mechanism of gene regulation

View Set

PrepU Ch 37: Management of Patients with Musculoskeletal Trauma

View Set

Merrill's Ch. 17 - Liver & Biliary System

View Set

MARKETING COMMUNICATIONS: CHAPTER 9

View Set

Business Law 2 Ch. 20 True & False

View Set

Federal Tax Considerations for Life Insurance and Annuities

View Set