Chapter 5 and 7 Test Review

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

In the following code, what is the printout for list1? class Test { public static void main(String[] args) { int[] list1 = {1, 2, 3}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list1.length; i++) System.out.print(list1[i] + " "); } }

0 1 2

In the following code, what is the printout for list2? class Test { public static void main(String[] args) { int[] list1 = {1, 2, 3}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list2.length; i++) System.out.print(list2[i] + " "); } }

0 1 2

What is the output of the following code? double[] myList = {1, 5, 5, 5, 5, 1}; double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; } } System.out.println(indexOfMax);

1

The array index is not limited to int type.

False

The element in the array must be of a primitive data type.

False

The name length is a method in an array

False

You can always convert a for loop to a while loop.

True

_______ declares an array of char.

char[] chars && char chars[]

The JVM stores the array in an area of memory, called _______, which is used for dynamic memory allocation where blocks of memory are allocated and freed in an arbitrary order.

heap

What is the printout after the following loop terminates? int number = 25; int i; boolean isPrime = true; for (i = 2; i < number; i++) { if (number % i == 0) { isPrime = false; break; } } System.out.println("i is " + i + " isPrime is " + isPrime);

i is 5 isPrime is false

What is the printout after the following loop terminates? int number = 25; int i; boolean isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } } System.out.println("i is " + i + " isPrime is " + isPrime);

i is 6 isPrime is false

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.

2.0

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________.

3

How many elements are in array double[] list = new double[5]?

5

What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; sum += item; if (sum > 4) break; } while (item < 5);

6

Which of the following loops prints "Welcome to Java" 10 times? A: for (int count = 1; count <= 10; count++) { System.out.println("Welcome to Java"); } B: for (int count = 0; count < 10; count++) { System.out.println("Welcome to Java"); } C: for (int count = 1; count < 10; count++) { System.out.println("Welcome to Java"); } D: for (int count = 0; count <= 10; count++) { System.out.println("Welcome to Java"); }

AB

Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100? A: double sum = 0; for (int i = 1; i <= 99; i++) { sum = i / (i + 1); } System.out.println("Sum is " + sum); B: double sum = 0; for (int i = 1; i < 99; i++) { sum += i / (i + 1); } System.out.println("Sum is " + sum); C: double sum = 0; for (int i = 1; i <= 99; i++) { sum += 1.0 * i / (i + 1); } System.out.println("Sum is " + sum); D: double sum = 0; for (int i = 1; i <= 99; i++) { sum += i / (i + 1.0); } System.out.println("Sum is " + sum); E: double sum = 0; for (int i = 1; i < 99; i++) { sum += i / (i + 1.0); } System.out.println("Sum is " + sum);

CD

Analyze the following code. int[] list = new int[5]; list = new int[6];

The code can compile and run fine. The second line assigns a new array to list.

Analyze the following code. int x = 1; while (0 < x) && (x < 100) System.out.println(x++);

The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of parentheses.

Which of the loop statements always have their body executed at least once.

The do-while loop

What would be the result of attempting to compile and run the following code? public class Test { public static void main(String[] args) { double[] x = new double[]{1, 2, 3}; System.out.println("Value is " + x[1]); } }

The program compiles and runs fine and the output "Value is 2.0" is printed.

Analze the following code: import java.util.Scanner; public class Test { public static void main(String[] args) { int sum = 0; for (int i = 0; i < 100000; i++) { Scanner input = new Scanner(System.in); sum += input.nextInt(); } } }

The program compiles and runs, but it is not efficient and unnecessary to execute the Scanner input = new Scanner(System.in); statement inside the loop. You should move the statement before the loop.

What's the output of the program? public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for (int i = 0; i < x.length; i++) System.out.print(x[i] + " "); } }

The program displays 0 0

What's the output of the program? public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for (int i = 0; i < y.length; i++) System.out.print(y[i] + " "); } }

The program displays 1 2 3 4

What's the output of the program? public class Test { public static void main(String[] args) { int[] oldList = {1, 2, 3, 4, 5}; reverse(oldList); for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " "); } public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList; } }

The program displays 1 2 3 4 5.

(for-each loop) Analyze the following code: public class Test { public static void main(String[] args) { double[] x = {2.5, 3, 4}; for (double value: x) System.out.print(value + " "); } }

The program displays 2.5 3.0 4.0

Analyze the following code: public class Test { public static void main(String[] args) { int[] a = new int[4]; a[1] = 1; a = new int[2]; System.out.println("a[1] is " + a[1]); } }

The program displays a[1] is 0.

Analyze the following code: public class Test1 { public static void main(String[] args) { xMethod(new double[]{3, 3}); xMethod(new double[5]); xMethod(new double[3]{1, 2, 3}); } public static void xMethod(double[] a) { System.out.println(a.length); } }

The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect.

Analyze the following code: public class Test { public static void main(String[] args) { int[] x = new int[5]; int i; for (i = 0; i < x.length; i++) x[i] = i; //Hint: What is i's value after the loop? System.out.println(x[i]); } }

The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.

Analyze the following code. public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0] is " + x[0]); } }

The program runs fine and displays x[0] is 0.

The while loop and the do loop are equivalent in their expressive power; in other words, you can rewrite a while loop using a do loop, and vice versa.

True

You can always convert a while loop to a for loop.

True

What is i after the following for loop? int y = 0; for (int i = 0; i < 10; i++) { y += i; }

Undefined

Will the following program terminate? int balance = 10; while (true) { if (balance < 9) break; balance = balance - 9; }

Yes

What is the representation of the third element in an array called a?

a[2]

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

the reference of the array

When you return an array from a method, the method returns __________.

the reference of the array

Assume the signature of the method xMethod is as follows. public static void xMethod(double[] a) Which of the following could be used to invoke xMethod?

xMethod(new double[2]);

What is the output of the following code? int[] myList = {1, 2, 3, 4, 5, 6}; for (int i = myList.length - 2; i >= 0; i--) { myList[i + 1] = myList[i]; } for (int e: myList) System.out.print(e + " ");

1 1 2 3 4 5

The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; }

1 3 5 7 9

How many times will the following code print "Welcome to Java"? int count = 0; while (count < 10) { System.out.println("Welcome to Java"); count++; }

10

Assume int[] t = {1, 2, 3, 4}. What is t.length?

5

How can you initialize an array of two characters to 'a' and 'b'?

char[] charArray = {'a', 'b'}; && char[] charArray = new char[]{'a', 'b'};

What is the number of iterations in the following loop: for (int i = 1; i < n; i++) { // iteration }

n - 1

What is output of the following code? public class Test { public static void main(String[] args) { int list[] = {1, 2, 3, 4, 5, 6}; for (int i = 1; i < list.length; i++) list[i] = list[i - 1]; for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); } }

1 1 1 1 1 1

How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 10);

10

How many times will the following code print "Welcome to Java"? int count = 0; while (count < 10) { count++; System.out.println("Welcome to Java"); }

10

What is the output for y? int y = 0; for (int i = 0; i < 5; i++) { y += i; } System.out.println(y);

10

What is the value in count after the following loop is executed? int count = 0; do { count++; System.out.println("Welcome to Java"); } while (count < 10); System.out.println(count);

10

Show the output of the following code: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4, 5}; increase(x); int[] y = {1, 2, 3, 4, 5}; increase(y[0]); System.out.println(x[0] + " " + y[0]); } public static void increase(int[] x) { for (int i = 0; i < x.length; i++) x[i]++; } public static void increase(int y) { y++; } }

2 1

Analyze the following code: public class Test { public static void main (String args[]) { int i = 0; for (i = 0; i < 10; i++); System.out.println(i + 4); } }

The program compiles despite the semicolon (;) on the for loop line, and displays 14. && The for loop in this program is same as for (i = 0; i < 10; i++) { }; System.out.println(i + 4);

Analyze the following code. int count = 0; while (count < 100) { // Point A System.out.println("Welcome to Java!"); count++; // Point B } // Point C

count < 100 is always true at Point A && count < 100 is always false at Point C

The __________ method sorts the array scores of the double[] type.

java.util.Arrays.sort(scores)

What is the number of iterations in the following loop: for (int i = 1; i <= n; i++) { // iteration }

n


Set pelajaran terkait

SCSM-450_Ch11_Managing Capacity and Demand

View Set

Chemical Dependency, Abuse, and Neglect Lecture

View Set

Unit 4 Chapters 10-15 The Nursing Process

View Set

Astronomy 101 - Exam ch. 13, 14 & 15

View Set