Final Exam Review (Intro to Comp. Sci. using Java)
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
________ is invoked to create an object. A) A constructor B) A method with the void return type C) A method with a return type D) The main method
A) A constructor
How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < i; j++) System.out.println(i * j) A. 100 B. 20 C. 10 D. 45
A. 100
_________ is a simple but incomplete version of a method. A. A stub B. A main method C. A non-main method D. A method developed using top-down approach
A. A stub
Which of the following statements are true? A. Local variables do not have default values. B. Data fields have default values. C. A variable of a primitive type holds a value of the primitive type. D. A variable of a reference type holds a reference to where an object is stored in the memory. E. You may assign an int value to a reference variable.
A. Local variables do not have default values. B. Data fields have default values. C. A variable of a primitive type holds a value of the primitive type. D. A variable of a reference type holds a reference to where an object is stored in the memory.
Which of the following is correct? A. String[] list = new String{"red", "yellow", "green"}; B. String[] list = new String[]{"red", "yellow", "green"}; C. String[] list = {"red", "yellow", "green"}; D. String list = {"red", "yellow", "green"}; E. String list = new String{"red", "yellow", "green"};
A. String[] list = new String[]{"red", "yellow", "green"}; D. String[] list = {"red", "yellow", "green"};
Analyze the following code: 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] + " "); } } A. The program displays 1 2 3 4 B. The program displays 0 0 C. The program displays 0 0 3 4 D. The program displays 0 0 0 0
A. The program displays 1 2 3 4
Analyze the following code: 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; } } A. The program displays 1 2 3 4 5. B. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException. C. The program displays 5 4 3 2 1. D. The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.
A. The program displays 1 2 3 4 5.
Analyze the following code: public class Test { public static void main(String[] args) { System.out.println(xMethod(5, 500L)); } public static int xMethod(int n, long l) { System.out.println("int, long"); return n; } public static long xMethod(long n, long l) { System.out.println("long, long");return n; } } A. The program displays int, long followed by 5. B. The program displays long, long followed by 5. C. The program runs fine but displays things other than 5. D. The program does not compile because the compiler cannot distinguish which xmethod to invoke
A. The program displays int, long followed by 5.
Analyze the following code. public class Test { public static void main(String[] args) { System.out.println(m(2)); } public static int m(int num) { return num; } public static void m(int num) { System.out.println(num); } } A. The program has a compile error because the two methods m have the same signature. B. The program has a compile error because the second m method is defined, but not invoked in the main method. C. The program runs and prints 2 once. D. The program runs and prints 2 twice
A. The program has a compile error because the two methods
Is the following loop correct?for ( ; ; ); A. Yes B. No
A. Yes
What balance after the following code is executed? int balance = 10; while (balance >= 1) { if (balance < 9) continue; balance = balance - 9; } A. -1 B. 0 C. 1 D. 2 E. The loop does not end //loop has to have a false to terminate or in this case a break.while (true) {__if (balance < 9) break;__balance = balance - 9;}
A. Yes
What is the representation of the third element in an array called a? A. a[2] B. a(2) C. a[3] D. a(3)
A. a[2]
To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy? A. add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0. B. add 1.00, 0.99, 0.98, ..., 0.02, 0.01 in this order to a sum variable whose initial value is 0.
A. add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.
(char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character __________. A. between 'a' and 'z' B. between 'a' and 'y' C. between 'b' and 'z' D. between 'b' and 'y'
A. between 'a' and 'z'
Suppose int i = 5, which of the following can be used as an index for array double[] t = new double[100]? A. i B. (int)(Math.random() * 100)) C. i + 10 D. i + 6.5 E. Math.random() * 100
A. i B. (int)(Math.random() * 100)) C. i + 10
The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is known as __________. A. information hiding B. encapsulation C. method hiding D. simplifying method
A. information hiding B. encapsulation
Which code fragment would correctly identify the number of arguments passed via the command line to a Java application, excluding the name of the class that is being invoked? A. int count = args.length; B. int count = args.length - 1; C. int count = 0; while (args[count] != null) count ++; D. int count=0; while (!(args[count].equals(""))) count ++;
A. int count = args.length;
The reverse method is defined in this section. What is list1 after executing the following statements? int[] list1 = {1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); A. list1 is 1 2 3 4 5 6 B. list1 is 6 5 4 3 2 1 C. list1 is 0 0 0 0 0 0 D. list1 is 6 6 6 6 6 6
A. list1 is 1 2 3 4 5 6
Which of the following statements are correct to invoke the printMax method in Listing 7.5 in the textbook? A. printMax(1, 2, 2, 1, 4); B. printMax(new double[]{1, 2, 3}); C. printMax(1.0, 2.0, 2.0, 1.0, 4.0); D. printMax(new int[]{1, 2, 3});
A. printMax(1, 2, 2, 1, 4); B. printMax(new double[]{1, 2, 3}); C. printMax(1.0, 2.0, 2.0, 1.0, 4.0);
Suppose your method does not return any value, which of the following keywords can be used as a return type? A. void B. int C. double D. public E. None of the above
A. void
Do the following two programs produce the same result? Program I: public class Test { public static void main(String[] args) { int[] list = {1, 2, 3, 4, 5}; reverse(list); for (int i = 0; i < list.length; i++) System.out.print(list[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; } } Program II: 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; } } A. Yes B. No
A.Yes
Given the following method static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; } } What is k after invoking nPrint("A message", k)? int k = 2; nPrint("A message", k); A. 0 B. 1 C. 2 D. 3
B. 1
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); A. 0 B. 1 C. 2 D. 3 E. 4
B. 1
What is output of the following code: public class Test { public static void main(String[] args) { int[] x = {120, 200, 016}; for (int i = 0; i < x.length; i++) System.out.print(x[i] + " "); } } A. 120 200 16 B. 120 200 14 C. 120 200 20 D. 016 is a compile error. It should be written as 16.
B. 120 200 14
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________. A. 3.4 B. 2.0 C. 3.5 D. 5.5 E. undefined
B. 2.0
How many elements are in array double[] list = new double[5]? A. 4 B. 5 C. 6 D. 0
B. 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); A. 5 B. 6 C. 7 D. 8 E. 9
B. 6
Which of the following statements are correct? A. A reference variable is an object. B. A reference variable references to an object. C. A data field in a class must be of a primitive type. D. A data field in a class can be of an object type.
B. A reference variable references to an object. D. A data field in a class can be of an object type.
Does the method call in the following method cause compile errors? public static void main(String[] args) { Math.pow(2, 4); } A. Yes B. No
B. No
Does the return statement in the following method cause compile errors? public static void main(String[] args) { int max = 0; if (max != 0) System.out.println(max); else return; } A. Yes B. No
B. No
Analyze the following code. public class Test { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { System.out.println("max(int, double) is invoked"); if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { System.out.println("max(double, int) is invoked"); if (num1 > num2) return num1; else return num2; } } A. The program cannot compile because you cannot have the print statement in a non-void method. B. The program cannot compile because the compiler cannot determine which max method should be invoked. C. The program runs and prints 2 followed by "max(int, double)" is invoked. D. The program runs and prints 2 followed by "max(double, int)" is invoked. E. The program runs and prints "max(int, double) is invoked" followed by 2
B. The program cannot compile because the compiler cannot determine which max method should be invoked.
Analyze the following code: 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] + " "); } } A. The program displays 1 2 3 4 B. The program displays 0 0 C. The program displays 0 0 3 4 D. The program displays 0 0 0 0
B. The program displays 0 0
Analyze the following code: class Test { public static void main(String[] args) { System.out.println(xmethod(5)); } public static int xmethod(int n, long t) { System.out.println("int"); return n; } public static long xmethod(long n) { System.out.println("long");return n; } } A. The program displays int followed by 5. B. The program displays long followed by 5. C. The program runs fine but displays things other than 5. D. The program does not compile because the compiler cannot distinguish which xmethod to invoke.
B. The program displays long followed by 5.
Analyze the following code: public class Test { public static void main(String[] args) { A a = new A(); a.print(); } } class A { String s; A(String s) { this.s = s; } void print() { System.out.println(s); } } A. The program has a compile error because class A is not a public class. B. The program has a compile error because class A does not have a default constructor. C. The program compiles and runs fine and prints nothing. D. The program would compile and run if you change A a = new A() to A a = new A("5").
B. The program has a compile error because class A does not have a default constructor. D. The program would compile and run if you change A a = new A() to A a = new A("5").
_________ is to implement one method in the structure chart at a time from the top to the bottom. A. Bottom-up approach B. Top-down approach C. Bottom-up and top-down approach D. Stepwise refinement
B. Top-down approach
Which of the following should be defined as a void method? A. Write a method that prints integers from 1 to 100. B. Write a method that returns a random integer from 1 to 100. C. Write a method that checks whether current second is an integer from 1 to 100. D. Write a method that converts an uppercase letter to lowercase.
B. Write a method that returns a random integer from 1 to 100.
Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of System.out.println(java.util.Arrays.toString(scores))? A. {1, 20, 30, 40, 50} B. [1, 20, 30, 40, 50] C. {1 20 30 40 50} D. [1 20 30 40 50]
B. [1, 20, 30, 40, 50]
(int)('a' + Math.random() * ('z' - 'a' + 1)) returns a random number __________. A. between 0 and (int)'z' B. between (int)'a' and (int)'z' C. between 'a' and 'z' D. between 'a' and 'y'
B. between (int)'a' and (int)'z'
Which of the following statements are valid? A. int i = new int(30); B. double d[] = new double[30]; C. int[] i = {3, 4, 3, 2}; D. char[] c = new char(); E. char[] c = new char[4]{'a', 'b', 'c', 'd'};
B. double d[] = new double[30]; C. int[] i = {3, 4, 3, 2};
The default value for data field of a boolean type, numeric type, object type is ___________, respectively. A. true, 1, Null B. false, 0, null C. true, 0, null D. true, 1, null E. false, 1, null
B. false, 0, null
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. A. stack B. heap C. memory block D. dynamic memory
B. heap
What is the output 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); A. i is 5 isPrime is true B. i is 5 isPrime is false C. i is 6 isPrime is true D. i is 6 isPrime is false //normally once the loop finishes at i = 5 it exits the loop adding +1 to i making it i = 6 but the break; statement prevents the addition of +1. It stops everything at i = 5.
B. i is 5 isPrime is false
Use the selectionSort method presented in this section to answer this question. What is list1 after executing the following statements? double[] list1 = {3.1, 3.1, 2.5, 6.4}; selectionSort(list1); A. list1 is 3.1, 3.1, 2.5, 6.4 B. list1 is 2.5, 3.1, 3.1, 6.4 C. list1 is 6.4, 3.1, 3.1, 2.5 D. list1 is 3.1, 2.5, 3.1, 6.4
B. list1 is 2.5 3.1, 3.1, 6.4
The reverse method is defined in the textbook. What is list1 after executing the following statements? int[] list1 = {1, 2, 3, 4, 5, 6}; list1 = reverse(list1); A. list1 is 1 2 3 4 5 6 B. list1 is 6 5 4 3 2 1 C. list1 is 0 0 0 0 0 0 D. list1 is 6 6 6 6 6 6
B. list1 is 6 5 4 3 2 1
The signature of a method consists of ____________. A. method name B. method name and parameter list C. return type, method name, and parameter list D. parameter list
B. method name and parameter list
What is the number of iterations in the following loop? for (int i = 1; i <= n; i++) { // iteration } A. 2*n B. n C. n - 1 D. n + 1
B. n
Arguments to methods always appear within __________. A. brackets B. parentheses C. curly braces D. quotation marks
B. parentheses
When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as _________. A. method invocation B. pass by value C. pass by reference D. pass by name
B. pass by value
Which of the following is the best for generating random integer 0 or 1? A. (int)Math.random() B. (int)Math.random() + 1 C. (int)(Math.random() + 0.5) D. (int)(Math.random() + 0.2) E. (int)(Math.random() + 0.8)
C. (int)(Math.random() + 0.5)
If a key is not in the list, the binarySearch method returns _________. A. insertion point B. insertion point - 1 C. -(insertion point + 1) D. -insertion point
C. -(insertion point + 1)
In the following code, what is the output for list1? public 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] + " "); } } A. 1 2 3 B. 1 1 1 C. 0 1 2 D. 0 1 3
C. 0 1 2
In the following code, what is the output for list2? public 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] + " "); } } A. 1 2 3 B. 1 1 1 C. 0 1 2 D. 0 1 3
C. 0 1 2
Given the following program: public class Test { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.print(args[i] + " "); } } } What is the output, if you run the program using java Test 1 2 3 A. 3 B. 1 C. 1 2 3 D. 1 2
C. 1 2 3
How many times will the following code print "Welcome to Java"? int count = 0; while (count < 10) { System.out.println("Welcome to Java"); count++; } A. 8 B. 9 C. 10 D. 11 E. 0
C. 10
Assume int[] t = {1, 2, 3, 4}. What is t.length? A. 0 B. 3 C. 4 D. 5
C. 4
Given the following four patterns, Pattern A 1 12 123 1234 12345 123456 Pattern B 123456 12345 1234 123 12 1 Pattern C __________1 ________12 ______123 ____1234 __12345 123456 Pattern D 123456 __12345 ____1234 ______123 ________12 __________1 Which of the pattern is produced by the following code? for (int i = 1; i <= 6; i++) { for (int j = 6; j >= 1; j--) System.out.print(j <= i ? j + " " : " " + " "); System.out.println();} A. Pattern A B. Pattern B C. Pattern C D. Pattern D
C. Pattern C
Analyze 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(); } } } A. The program does not compile because the Scanner input = new Scanner(System.in); statement is inside the loop. B. The program compiles, but does not run because the Scanner input = new Scanner(System.in); statement is inside the loop. C. 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. D. The program compiles, but does not run because there is not prompting message for entering the input. //The loop just needs the following: for (initial-action; loop-continuation-condition; action-after-each-iteration) { _____________// Loop body; _____________Statement(s); } //efficient is not spelled correctly (effient) on the original which I am leaving so people can search for the question based on the original
C. 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
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 + " "); } } A. The program displays 2.5, 3, 4 B. The program displays 2.5 3 4 C. The program displays 2.5 3.0 4.0 D. The program displays 2.5, 3.0 4.0 E. The program has a syntax error because value is undefined.
C. 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]); } } A. The program has a compile error because new int[2] is assigned to a. B. The program has a runtime error because a[1] is not initialized. C. The program displays a[1] is 0. D. The program displays a[1] is 1.
C. 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); } } A. The program has a compile error because xMethod(new double[]{3, 3}) is incorrect. B. The program has a compile error because xMethod(new double[5]) is incorrect. C. The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect. D. The program has a runtime error because a is null.
C. 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) { final 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] + " "); } } A. The program displays 1 2 3 4. B. The program displays 0 0. C. The program has a compile error on the statement x = new int[2], because x is final and cannot be changed. D. The elements in the array x cannot be changed, because x is final.
C. The program has a compile error on the statement x = new int[2], because x is final and cannot be changed.
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; System.out.println(x[i]); } } A. The program displays 0 1 2 3 4. B. The program displays 4. C. The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException. D. The program has a compile error because i is not defined in the last statement in the main method.
C. 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]); } } A. The program has a compile error because the size of the array wasn't specified when declaring the array. B. The program has a runtime error because the array elements are not initialized. C. The program runs fine and displays x[0] is 0. D. The program has a runtime error because the array element x[0] is not defined.
C. The program runs fine and displays x[0] is 0.
Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion. A. a heap B. storage area C. a stack D. an array
C. a stack
How can you get the word "abc" in the main method from the following call? java Test "+" 3 "abc" 2 A. args[0] B. args[1] C. args[2] D. args[3]
C. args[2]
(int)(Math.random() * (65535 + 1)) returns a random number __________. A. between 1 and 65536 B. between 1 and 65535 C. between 0 and 65535 D. between 0 and 65536
C. between 0 and 65535
How can you initialize an array of two characters to 'a' and 'b'? A. char[] charArray = new char[2]; charArray = {'a', 'b'}; B. char[2] charArray = {'a', 'b'}; C. char[] charArray = {'a', 'b'}; D. char[] charArray = new char[]{'a', 'b'};
C. char[] charArray = {'a', 'b'}; D. char[] charArray = new char[]{'a', 'b'};
What is the correct term for numbers[99]? A. index B. index variable C. indexed variable D. array variable E. array
C. indexed variable
Which of the following are incorrect? A. int[] a = new int[2]; B. int a[] = new int[2]; C. int[] a = new int(2); D. int a = new int[2]; E. int a() = new int[2];
C. int[] a = new int(2); D. int a = new int[2]; E. int a() = new int[2];
The __________ method sorts the array scores of the double[] type. A. java.util.Arrays(scores) B. java.util.Arrays.sorts(scores) C. java.util.Arrays.sort(scores) D. Njava.util.Arrays.sortArray(scores)
C. java.util.Arrays.sort(scores)
What is the number of iterations in the following loop? for (int i = 1; i < n; i++) { // iteration } A. 2*n B. n C. n - 1 D. n + 1
C. n - 1
All Java applications must have a method __________. A. public static Main(String[] args) B. public static Main(String args[]) C. public static void main(String[] args) D. public void main(String[] args) E. public static main(String[] args)
C. public static void main(String[] args)
When you pass an array to a method, the method receives __________. A. a copy of the array B. a copy of the first element C. the reference of the array D. the length of the array
C. the reference of the array
When you return an array from a method, the method returns __________. A. a copy of the array B. a copy of the first element C. the reference of the array D. the length of the array
C. the reference of the array
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] + " "); } } A. 1 2 3 4 5 6 B. 2 3 4 5 6 6 C. 2 3 4 5 6 1 D. 1 1 1 1 1 1
D. 1 1 1 1 1 1
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 + " "); A. 1 2 3 4 5 6 B. 6 1 2 3 4 5 C. 6 2 3 4 5 1 D. 1 1 2 3 4 5 E. 2 3 4 5 6 1
D. 1 1 2 3 4 5
Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return? A. 0 B. -1 C. 1 D. 2 E. -2
D. 2
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++; } } A. 0 0 B. 1 1 C. 2 2 D. 2 1 E. 1 2
D. 21
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________. A. 0 B. 1 C. 2 D. 3 E. 4
D. 3
A variable defined inside a method is referred to as __________. A. a global variable B. a method variable C. a block variable D. a local variable
D. a local variable
You should fill in the blank in the following code with ______________. public class Test { public static void main(String[] args) { System.out.print("The grade is " + getGrade(78.5)); System.out.print("\nThe grade is " + getGrade(59.5)); } public static _________ getGrade(double score) { if (score >= 90.0) return 'A'; else if (score >= 80.0) return 'B'; else if (score >= 70.0) return 'C'; else if (score >= 60.0) return 'D'; else return 'F'; } } A. int B. double C. boolean D. char E. void
D. char
Given the following method static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; } } What is the output of the call nPrint('a', 4)? A. aaaaa B. aaaa C. aaa
D. invalid call
What is k after the following block executes? { int k = 2; nPrint("A message", k); } System.out.println(k); A. 0 B. 1 C. 2 D. k is not defined outside the block. So, the program has a compile error
D. k is not defined outside the block. So, the program has a compile error
For the binarySearch method in Section 7.10.2, what is low and high after the first iteration of the while loop when invoking binarySearch(new int[]{1, 4, 6, 8, 10, 15, 20}, 11)? A. low is 0 and high is 6 B. low is 5 and high is 5 C. low is 3 and high is 6 D. low is 4 and high is 6 E. low is 6 and high is 5
D. low is 5 and high is 4
Which of the following declarations are correct? A. public static void print(String... strings, double... numbers) B. public static void print(double... numbers, String name) C. public static double... print(double d1, double d2) D. public static void print(double... numbers) E. public static void print(int n, double... numbers)
D. public static void print(double... numbers) E. public static void print(int n, double... numbers)
Suppose a method p has the following heading: public static int[] p() What return statement may be used in p()? A. return 1; B. return {1, 2, 3}; C. return int[]{1, 2, 3}; D. return new int[]{1, 2, 3};
D. return new int[]{1, 2, 3};
Consider the following incomplete code: public class Test { public static void main(String[] args) { System.out.println(f(5)); } public static int f(int number) { // Missing body } } The missing method body should be ________. A. return "number"; B. System.out.println(number); C. System.out.println("number"); D. return number;
D. return number;
The __________ method copies the sourceArray to the targetArray. A. System.copyArrays(sourceArray, 0, targetArray, 0, sourceArray.length); B. System.copyarrays(sourceArray, 0, targetArray, 0, sourceArray.length); C. System.arrayCopy(sourceArray, 0, targetArray, 0, sourceArray.length); D. System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
D.System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 3) return? A. 0 B. -1 C. 1 D. 2 E. -2
E. -2
Use the selectionSort method presented in this section to answer this question. Assume list is {3.1, 3.1, 2.5, 6.4, 2.1}, what is the content of list after the first iteration of the outer loop in the method? A. 3.1, 3.1, 2.5, 6.4, 2.1 B. 2.5, 3.1, 3.1, 6.4, 2.1 C. 2.1, 2.5, 3.1, 3.1, 6.4 D. 3.1, 3.1, 2.5, 2.1, 6.4 E. 2.1, 3.1, 2.5, 6.4, 3.1
E. 2.1, 3.1, 2.5, 6.4, 3.1
What is the output for y? int y = 0; for (int i = 0; i < 10; ++i) { y += i; } System.out.println(y); A. 10 B. 11 C. 12 D. 13 E. 45
E. 45
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"); } A. BD B. ABC C. AC D. BC E. AB
E. 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); A. BCD B. ABCD C. B D. CDE E. CD
E. CD
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]); } } A. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}. B. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3}; C. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0}; D. The program compiles and runs fine and the output "Value is 1.0" is printed. E. The program compiles and runs fine and the output "Value is 2.0" is printed.
E. The program compiles and runs fine and the output "Value is 2.0" is printed.
You should fill in the blank in the following code with ______________. public class Test { public static void main(String[] args) { System.out.print("The grade is"); printGrade(78.5); System.out.print("The grade is "); printGrade(59.5); } public static __________ printGrade(double score) { if (score >= 90.0) { System.out.println('A'); } else if (score >= 80.0) { System.out.println('B'); } else if (score >= 70.0) { System.out.println('C'); } else if (score >= 60.0) { System.out.println('D'); } else {System.out.println('F'); } } } A. int B. double C. boolean D. char E. void
E. void
What is the output of the following code? int x = 0; while (x < 4) { x = x + 1; } System.out.println("x is " + x); A. x is 0 B. x is 1 C. x is 2 D. x is 3 E. x is 4
E. x is 4
An object is an instance of a
class
Given the declaration Circle x = new Circle(), which of the following statement is most accurate. A. x contains an int value. B. x contains an object of the Circle type. C. x contains a reference to a Circle object. D. You can assign an int value to x.
x contains a reference to a Circle object.