Introduction To Programming : Java : Chapter 1,2,3,4,5,6,8,9

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

________ consists of a set of separate programs for developing and testing Java programs, each of which is invoked from a command line. A. Java language specification B. Java API C. Java JDK D. Java IDE

C. Java JDK

Which of the following is the correct expression of character 4? A. 4 B. "4" C. '\0004' D. '4'

D. '4' Explanation: You have to write '4'.

What is the value of (double)(5/2)? A. 2 B. 2.5 C. 3 D. 2.0 E. 3.0

D. 2.0

24 % 5 is _____ A. 1 B. 2 C. 3 D. 4 E. 0

D. 4

What is the value of i printed? public class Test { public static void main(String[] args) { int j = 0; int i = ++j + j * 5; System.out.println("What is i? " + i); } } A. 0 B. 1 C. 5 D. 6

D. 6 Explanation: Operands are evaluated from left to right in Java. The left-hand operand of a binary operator is evaluated before any part of the right-hand operand is evaluated. This rule takes precedence over any other rules that govern expressions. Therefore, ++j is evaluated first, and returns 1. Then j*5 is evaluated, returns 5.

Math.pow(2, 3) returns __________. A. 9 B. 8 C. 9.0 D. 8.0

D. 8.0

____________ is a device to connect a computer to a local area network (LAN). A. Regular modem B. DSL C. Cable modem D. NIC

D. NIC

To assign a double variable d to a float variable x, you write A. x = (long)d B. x = (int)d; C. x = d; D. x = (float)d;

D. x = (float)d;

The extension name of a Java source code file is A. .java B. .obj C. .class D. .exe

A. .java

________ is an object-oriented programming language. A. Java B. C++ C. C D. C# E. Pascal

A. Java B. C++ D. C#

Which of the following is a constant, according to Java naming conventions? A. MAX_VALUE B. Test C. read D. ReadInt E. COUNT

A. MAX_VALUE E. COUNT

_____________ is a program that runs on a computer to manage and control a computer's activities. A. Operating system B. Java C. Modem D. Interpreter E. Compiler

A. Operating system

Which of the following are correct ways to declare variables? A. int length; int width; B. int length, width; C. int length; width; D. int length, int width;

A. int length; int width; B. int length, width;

Which JDK command is correct to run a Java application in ByteCode.class? A. java ByteCode B. java ByteCode.class C. javac ByteCode.java D. javac ByteCode E. JAVAC ByteCode

A. java ByteCode

Computer can execute the code in ____________. A. machine language B. assembly language C. high-level language D. none of the above

A. machine language

Which of the following are the reserved words? A. public B. static C. void D. class

A. public B. static C. void D. class

Which of the following are correct names for variables according to Java naming conventions? A. radius B. Radius C. RADIUS D. findArea E. FindArea

A. radius D. findArea

Every letter in a Java keyword is in lowercase? A. true B. false

A. true

Which of the following statements are the same? (A) x -= x + 4 (B) x = x + 4 - x (C) x = x - (x + 4) A. (A) and (B) are the same B. (A) and (C) are the same C. (B) and (C) are the same D. (A), (B), and (C) are the same

B. (A) and (C) are the same

The expression (int)(76.0252175 * 100) / 100 evaluates to _________. A. 76.02 B. 76 C. 76.0252175 D. 76.03

B. 76

One byte has ________ bits. A. 4 B. 8 C. 12 D. 16

B. 8

___________ translates high-level language program into machine language program. A. An assembler B. A compiler C. CPU D. The operating system

B. A compiler

__________ is the brain of a computer. A. Hardware B. CPU C. Memory D. Disk

B. CPU

____________ are instructions to the computer. A. Hardware B. Software C. Programs D. Keyboards

B. Software C. Programs

Which of these data types requires the most amount of memory? A. long B. int C. short D. byte

A. long

If a number is too large to be stored in a variable of the float type, it _____________. A. causes overflow B. causes underflow C. causes no error D. cannot happen in Java

A. causes overflow

Which of the following are storage devices? A. floppy disk B. hard disk C. flash stick D. CD-ROM

A. floppy disk B. hard disk C. flash stick D. CD-ROM

To use JOptionPane in your program, you may import it using: A. import javax.swing.JOptionPane; B. import javax.swing.*; C. import javax.*; D. import javax.*.JOptionPane;

A. import javax.swing.JOptionPane; B. import javax.swing.*;

____________ is an operating system. A. Java B. C++ C. Windows XP D. Visual Basic E. Ada

C. Windows XP

-25 % 5 is _____ A. 1 B. 2 C. 3 D. 4 E. 0

E. 0

If a program compiles fine, but it produces incorrect result, then the program suffers __________. A. a compilation error B. a runtime error C. a logic error

C. a logic error

Which of the following is equivalent to x != y? A. ! (x == y) B. x > y && x < y C. x > y || x < y D. x >= y || x <= y

A. ! (x == y) C. x > y || x < y

Assume x = 4 and y = 5, Which of the following is true? A. !(x == y) B. x != y C. x == y D. x >= y

A. !(x == y) B. x != y

Which of the following is a valid identifier? A. $343 B. class C. 9X D. 8+9 E. radius

A. $343 E. radius

Which of the following are valid specifiers for the printf statement? A. %4c B. %10b C. %6d D. %8.2d E. %10.2e

A. %4c B. %10b C. %6d E. %10.2e

An int variable can hold __________. A. 'x' B. 120 C. 120.0 D. "x" E. "120"

A. 'x' B. 120 Explanation: Choice (A) is also correct, because a character can be implicitly cast into an int variable. The Unicode value of character is assignment to the int variable. In this case, the code is 120 (see Appendix B).

Which of the Boolean expressions below is incorrect? A. (true) && (3 => 4) B. !(x > 0) && (x > 0) C. (x > 0) || (x < 0) D. (x != 0) || (x = 0) E. (-10 < x < 0)

A. (true) && (3 => 4) D. (x != 0) || (x = 0) E. (-10 < x < 0) Explanation: a: (3 => 4) should be (3 >= 4), d: (x = 0) should be (x == 0), and e: should be (-10 < x) && (x < 0)

Which of the following statements are true? A. (x > 0 && x < 10) is same as ((x > 0) && (x < 10)) B. (x > 0 || x < 10) is same as ((x > 0) || (x < 10)) C. (x > 0 || x < 10 && y < 0) is same as (x > 0 || (x < 10 && y < 0)) D. (x > 0 || x < 10 && y < 0) is same as ((x > 0 || x < 10) && y < 0)

A. (x > 0 && x < 10) is same as ((x > 0) && (x < 10)) B. (x > 0 || x < 10) is same as ((x > 0) || (x < 10)) C. (x > 0 || x < 10 && y < 0) is same as (x > 0 || (x < 10 && y < 0))

What is y after the following statement is executed? x = 0; y = (x > 0) ? 10 : -10; A. -10 B. 0 C. 10 D. 20 E. Illegal expression

A. -10

Suppose x is 1. What is x after x -= 1? A. 0 B. 1 C. 2 D. -1 E. -2

A. 0

Which of the following are the same as 1545.534? A. 1.545534e+3 B. 0.1545534e+4 C. 1545534.0e-3 D. 154553.4e-2

A. 1.545534e+3 B. 0.1545534e+4 C. 1545534.0e-3 D. 154553.4e-2

How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) System.out.println(i * j); A. 100 B. 20 C. 10 D. 45

A. 100

What is the value of myCount.count displayed? public class Test { public static void main(String[] args) { Count myCount = new Count(); int times = 0; for (int i=0; i<100; i++) increment(myCount, times); System.out.println( "myCount.count = " + myCount.count); System.out.println("times = "+ times); } public static void increment(Count c, int times) { c.count++; times++; } } class Count { int count; Count(int c) { count = c; } Count() { count = 1; } } A. 101 B. 100 C. 99 D. 98

A. 101

What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; sum += item; if (sum >= 4) continue; } while (item < 5); A. 15 B. 16 C. 17 D. 18

A. 15

What will be displayed by System.out.println('z' - 'a')? A. 25 B. 26 C. a D. z

A. 25

What is Math.floor(3.6)? A. 3.0 B. 3 C. 4 D. 5.0

A. 3.0

Note that the Unicode for character A is 65. The expression 'A' + 1 evaluates to ________. A. 66 B. B C. A1 D. Illegal expression

A. 66

_______ is a construct that defines objects of the same type. A. A class B. An object C. A method D. A data field

A. A class

________ is invoked to create an object. A. A constructor B. The main method C. A method with a return type D. A method with the void return type

A. A constructor

Which of the following statements are true? A. A default constructor is provided automatically if no constructors are explicitly declared in the class. B. At least one constructor must always be defined explicitly. C. Every class has a default constructor. D. The default constructor is a no-arg constructor.

A. A default constructor is provided automatically if no constructors are explicitly declared in the class. D. The default constructor is a no-arg constructor.

__________ 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

The java.util.Date class is introduced in this section. Analyze the following code and choose the best answer: Which of the following code in A or B, or both creates an object of the Date class: A: public class Test { public Test() { new java.util.Date(); } } B: public class Test { public Test() { java.util.Date date = new java.util.Date(); } } A. A. B. B. C. Neither

A. A. B. B. Explanation: Both (A) and (B) are fine. In A, an object is created without explicit reference. This is known as anonymous object.

The following code fragment reads in two numbers: Scanner input = new Scanner(System.in); int i = input.nextInt(); double d = input.nextDouble(); What are the correct ways to enter these two numbers? A. Enter an integer, a space, a double value, and then the Enter key. B. Enter an integer, two spaces, a double value, and then the Enter key. C. Enter an integer, an Enter key, a double value, and then the Enter key. D. Enter a numeric value with a decimal point, a space, an integer, and then the Enter key.

A. Enter an integer, a space, a double value, and then the Enter key. B. Enter an integer, two spaces, a double value, and then the Enter key. C. Enter an integer, an Enter key, a double value, and then the Enter key.

What is the output of the following code? char ch = 'F'; if (ch >= 'A' && ch <= 'Z') System.out.println(ch); A. F B. f C. nothing D. F f

A. F

________ is the physical aspect of the computer that can be seen. A. Hardware B. Software C. Operating system D. Application program

A. Hardware

Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is correct? I: if (age < 16) System.out.println("Cannot get a driver's license"); if (age >= 16) System.out.println("Can get a driver's license"); II: if (age < 16) System.out.println("Cannot get a driver's license"); else System.out.println("Can get a driver's license"); III: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age >= 16) System.out.println("Can get a driver's license"); IV: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age > 16) System.out.println("Can get a driver's license"); else if (age == 16) System.out.println("Can get a driver's license"); A. I B. II C. III D. IV

A. I B. II C. III

The following program displays __________. public class Test { public static void main(String[] args) { String s = "Java"; StringBuilder buffer = new StringBuilder(s); change(s); System.out.println(s); } private static void change(String s) { s = s + " and HTML"; } } A. Java B. Java and HTML C. and HTML D. nothing is displayed

A. Java

________ is Architecture-Neutral. A. Java B. C++ C. C D. Ada E. Pascal

A. Java

________ is interpreted. A. Java B. C++ C. C D. Ada E. Pascal

A. Java

Java compiler translates Java source code into _________. A. Java bytecode B. machine code C. assembly code D. another high-level language code

A. Java bytecode

________ is a technical definition of the language that includes the syntax and semantics of the Java programming language. A. Java language specification B. Java API C. Java JDK D. Java IDE

A. Java language specification

_________ is a software that interprets Java bytecode. A. Java virtual machine B. Java compiler C. Java debugger D. Java API

A. Java virtual machine

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 statements are true? A. Multiple constructors can be defined in a class. B. Constructors do not have a return type, not even void. C. Constructors must have the same name as the class itself. D. Constructors are invoked using the new operator when an object is created.

A. Multiple constructors can be defined in a class. B. Constructors do not have a return type, not even void. C. Constructors must have the same name as the class itself. D. Constructors are invoked using the new operator when an object is created.

_____________ is a formal process that seeks to understand the problem and document in detail what the software system needs to do. A. Requirements specification B. Analysis C. Design D. Implementation E. Testing

A. Requirements specification

Which of the following statements is preferred to create a string "Welcome to Java"? A. String s = "Welcome to Java"; B. String s = new String("Welcome to Java"); C. String s; s = "Welcome to Java"; D. String s; s = new String("Welcome to Java");

A. String s = "Welcome to Java"; Explanation: (a) is better than (b) because the string created in (a) is interned. Since strings are immutable and are ubiquitous in programming, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. Such an instance is called interned. The JVM (a) is simpler than (c).

Suppose s1 and s2 are two strings. Which of the following statements or expressions is incorrect? A. String s3 = s1 - s2; B. boolean b = s1.compareTo(s2); C. char c = s1[0]; D. char c = s1.charAt(s1.length());

A. String s3 = s1 - s2; B. boolean b = s1.compareTo(s2); C. char c = s1[0]; D. char c = s1.charAt(s1.length());

__________ returns a string. A. String.valueOf(123) B. String.valueOf(12.53) C. String.valueOf(false) D. String.valueOf(new char[]{'a', 'b', 'c'})

A. String.valueOf(123) B. String.valueOf(12.53) C. String.valueOf(false) D. String.valueOf(new char[]{'a', 'b', 'c'})

Java was developed by ____________. A. Sun Microsystems B. Microsoft C. Oracle D. IBM E. Cisco Systems

A. Sun Microsystems

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. Explanation: The contents of the array oldList have not been changed as result of invoking the reverse method. Initially, list refers to the same heap as oldList array through pass-by-sharing; then newList is generated and stored in a different heap; by list = newList, list now refers to the same heap as newList, and list is no longer related to oldList any more.

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. class Test { public static void main(String[] args) { String s; System.out.println("s is " + s); } } A. The program has a compilation error because s is not initialized, but it is referenced in the println statement. B. The program has a runtime error because s is not initialized, but it is referenced in the println statement. C. The program has a runtime error because s is null in the println statement. D. The program compiles and runs fine.

A. The program has a compilation error because s is not initialized, but it is referenced in the println statement.

Analyze the following code: public class Test { public static void main(String args[]) { NClass nc = new NClass(); nc.t = nc.t++; } } class NClass { int t; private NClass() { } } A. The program has a compilation error because the NClass class has a private constructor. B. The program does not compile because the parameter list of the main method is wrong. C. The program compiles, but has a runtime error because t has no initial value. D. The program compiles and runs fine.

A. The program has a compilation error because the NClass class has a private constructor. Explanation: You cannot use the private constructor to create an object.

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 m have the same signature. Explanation: You cannot override the methods based on the type returned.

Analyze the following code: public class Test { public static void main(String[] args) { double radius; final double PI= 3.15169; double area = radius * radius * PI; System.out.println("Area is " + area); } } A. The program has compile errors because the variable radius is not initialized. B. The program has a compile error because a constant PI is defined inside a method. C. The program has no compile errors but will get a runtime error because radius is not initialized. D. The program compiles and runs fine.

A. The program has compile errors because the variable radius is not initialized.

Analyze the following code: if (x < 100) && (x > 10) System.out.println("x is between 10 and 100"); A. The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses. B. The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses and the println(?) statement must be put inside a block. C. The statement compiles fine. D. The statement compiles fine, but has a runtime error.

A. The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses.

Which of the following statements are true? A. Use the private modifier to encapsulate data fields. B. Encapsulating data fields makes the program easy to maintain. C. Encapsulating data fields makes the program short. D. Encapsulating data fields helps prevent programming errors.

A. Use the private modifier to encapsulate data fields. B. Encapsulating data fields makes the program easy to maintain. D. Encapsulating data fields helps prevent programming errors.

Which of the following statements are correct? A. When creating a Random object, you have to specify the seed or use the default seed. B. If two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical. C. The nextInt() method in the Random class returns the next random int value. D. The nextDouble() method in the Random class returns the next random double value.

A. When creating a Random object, you have to specify the seed or use the default seed. B. If two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical. C. The nextInt() method in the Random class returns the next random int value. D. The nextDouble() method in the Random class returns the next random double value.

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.

A. Write a method that prints integers from 1 to 100.

Are the following four statements equivalent? number += 1; number = number + 1; number++; ++number; A. Yes B. No

A. Yes

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

Do the following two statements in (I) and (II) result in the same value in sum? (I): for (int i = 0; i<10; ++i) { sum += i; } (II): for (int i = 0; i<10; i++) { sum += i; } A. Yes B. No

A. Yes

Is the following loop correct? for (; ; ); A. Yes B. No

A. Yes

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

A. Yes

Which of the following is true? A. You can add characters into a string buffer. B. You can delete characters into a string buffer. C. You can reverse the characters in a string buffer. D. The capacity of a string buffer can be automatically adjusted.

A. You can add characters into a string buffer. B. You can delete characters into a string buffer. C. You can reverse the characters in a string buffer. D. The capacity of a string buffer can be automatically adjusted.

If you forget to put a closing quotation mark on a string, what kind error will be raised? A. a compilation error B. a runtime error C. a logic error

A. a compilation error

Every statement in Java ends with ________. A. a semicolon (;) B. a comma (,) C. a period (.) D. an asterisk (*)

A. a semicolon (;)

Suppose the xMethod() is invoked from a main method in a class as follows, xMethod() is _________ in the class. public static void main(String[] args) { xMethod(); }

A. a static method

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.

What is the return value of "SELECT".substring(4, 4)? A. an empty string B. C C. T D. E

A. an empty string Explanation: If beginIndex is endIndex, substring(beginIndex, endIndex) returns an empty string with length 0. It would be a runtime error, if beginIndex > endIndex.

(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'

Which of the following assignment statements is correct? A. char c = 'd'; B. char c = 100; C. char c = "d"; D. char c = "100";

A. char c = 'd'; B. char c = 100; Explanation: Choice (B) is also correct, because an int value can be implicitly cast into a char variable. The Unicode of the character is the int value. In this case, the character is d (see Appendix B).

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

A. count < 100 is always true at Point A E. count < 100 is always false at Point C

The StringBuilder methods _____________ not only change the contents of a string buffer, but also returns a reference to the string buffer. A. delete B. append C. insert D. reverse E. replace

A. delete B. append C. insert D. reverse E. replace

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

To check if a string s contains the suffix "Java", you may write A. if (s.endsWith("Java")) ... B. if (s.lastIndexOf("Java") >= 0) ... C. if (s.substring(s.length() - 4).equals("Java")) ... D. if (s.substring(s.length() - 5).equals("Java")) ... E. if (s.charAt(s.length() - 4) == 'J' && s.charAt(s.length() - 3) == 'a' && s.charAt(s.length() - 2) == 'v' && s.charAt(s.length() - 1) == 'a') ...

A. if (s.endsWith("Java")) ... C. if (s.substring(s.length() - 4).equals("Java")) ... E. if (s.charAt(s.length() - 4) == 'J' && s.charAt(s.length() - 3) == 'a' && s.charAt(s.length() - 2) == 'v' && s.charAt(s.length() - 1) == 'a') ... Explanation: s.lastIndexOf('Java') >= 0 does not indicate that Java is the suffix of the string.

To check if a string s contains the prefix "Java", you may write A. if (s.startsWith("Java")) ... B. if (s.indexOf("Java") == 0) ... C. if (s.substring(0, 4).equals("Java")) ... D. if (s.charAt(0) == 'J' && s.charAt(1) == 'a' && s.charAt(2) == 'v' && s.charAt(3) == 'a') ...

A. if (s.startsWith("Java")) ... B. if (s.indexOf("Java") == 0) ... C. if (s.substring(0, 4).equals("Java")) ... D. if (s.charAt(0) == 'J' && s.charAt(1) == 'a' && s.charAt(2) == 'v' && s.charAt(3) == 'a') ...

You should add the static keyword in the place of ? in Line ________ in the following code: 1 public class Test { 2 private int age; 3 4 public ? int square(int n) { 5 return n * n; 6 } 7 8 public ? int getAge() { 9 } 10} A. in line 4 B. in line 8 C. in both line 4 and line 8 D. none

A. in line 4

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

Suppose a Scanner object is created as follows: Scanner input = new Scanner(System.in); What method do you use to read an int value? A. input.nextInt(); B. input.nextInteger(); C. input.int(); D. input.integer();

A. input.nextInt();

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 6.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); Explanation: The last one printMax(new int[]{1, 2, 3}); is incorrect, because the array must of the double[] type.

Which of the following is the correct header of the main method? A. public static void main(String[] args) B. public static void main(String args[]) C. public static void main(String[] x) D. public static void main(String x[]) E. static void main(String[] args)

A. public static void main(String[] args) B. public static void main(String args[]) C. public static void main(String[] x) D. public static void main(String x[]) A. public static void main(String[] args) B. public static void main(String args[]) C. public static void main(String[] x) D. public static void main(String x[])

What is the output of the following code? public class Test { public static void main(String[] args) { String s1 = new String("Welcome to Java!"); String s2 = new String("Welcome to Java!"); if (s1.equals(s2)) System.out.println("s1 and s2 have the same contents"); else System.out.println("s1 and s2 have different contents"); } }

A. s1 and s2 have the same contents

What is the output of the following code? public class Test { public static void main(String[] args) { String s1 = "Welcome to Java!"; String s2 = "Welcome to Java!"; if (s1 == s2) System.out.println("s1 and s2 reference to the same String object"); else System.out.println("s1 and s2 reference to different String objects"); } } A. s1 and s2 reference to the same String object B. s1 and s2 reference to different String objects

A. s1 and s2 reference to the same String object

What is the output of the following code? public class Test { public static void main(String[] args) { String s1 = "Welcome to Java!"; String s2 = s1; if (s1 == s2) System.out.println("s1 and s2 reference to the same String object"); else System.out.println("s1 and s2 reference to different String objects"); } } A. s1 and s2 reference to the same String object B. s1 and s2 reference to different String objects

A. s1 and s2 reference to the same String object

_________ returns the last character in a StringBuilder variable named strBuf? A. strBuf.charAt(strBuf.length() - 1) B. strBuf.charAt(strBuf.capacity() - 1) C. StringBuilder.charAt(strBuf.length() - 1) D. StringBuilder.charAt(strBuf.capacity() - 1)

A. strBuf.charAt(strBuf.length() - 1)

What code may be filled in the blank without causing syntax or runtime errors: public class Test { java.util.Date date; public static void main(String[] args) { Test test = new Test(); System.out.println(_________________); } } A. test.date B. date C. test.date.toString() D. date.toString()

A. test.date Explanation: b and d cause compile errors because date is an instance variable and cannot be accessed from static context. c is wrong because test.date is null, causing NullPointerException.

Suppose s1 and s2 are two strings. What is the result of the following code? s1.equals(s2) == s2.equals(s1) A. true B. false

A. true

What is 1 + 1 + 1 + 1 + 1 == 5? A. true B. false C. There is no guarantee that 1 + 1 + 1 + 1 + 1 == 5 is true.

A. true

What is the value of the following expression? true || true && false A. true B. false

A. true

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

Suppose Character x = new Character('a'), __________________ returns true. A. x.equals(new Character('a')) B. x.compareToIgnoreCase('A') C. x.equalsIgnoreCase('A') D. x.equals('a') E. x.equals("a")

A. x.equals(new Character('a')) D. x.equals('a') Explanation: (B) and (C) are wrong because no methods compareToIgnoreCase and equalsIgnoreCase are in the Character class. (E) is wrong because a character is not a string.

What is the return value of "SELECT".substring(0, 5)? A. "SELECT" B. "SELEC" C. "SELE" D. "ELECT"

B. "SELEC"

Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative? A. 1 < x < 100 && x < 0 B. ((x < 100) && (x > 1)) || (x < 0) C. ((x < 100) && (x > 1)) && (x < 0) D. (1 > x > 100) || (x < 0)

B. ((x < 100) && (x > 1)) || (x < 0)

To check whether a char variable ch is an uppercase letter, you write ___________. A. (ch >= 'A' && ch >= 'Z') B. (ch >= 'A' && ch <= 'Z') C. (ch >= 'A' || ch <= 'Z') D. ('A' <= ch <= 'Z')

B. (ch >= 'A' && ch <= 'Z')

Which of the following expression results in 45.37? A. (int)(45.378 * 100) / 100 B. (int)(45.378 * 100) / 100.0 C. (int)(45.378 * 100 / 100) D. (int)(45.378) * 100 / 100.0

B. (int)(45.378 * 100) / 100.0

The order of the precedence (from high to low) of the operators binary +, *, &&, || is: A. &&, ||, *, + B. *, +, &&, || C. *, +, ||, && D. ||, &&, *, +

B. *, +, &&, ||

What is Math.sin(Math.PI / 6)? A. 1 B. 0.5 C. 0.4 D. 1.5

B. 0.5

Which of the following is a possible output from invoking Math.random()? A. 3.43 B. 0.5 C. 0.0 D. 1.0

B. 0.5 C. 0.0

'3' - '2' + 'm' / 'n' is ______. 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

Which of the following expressions will yield 0.5? A. 1 / 2 B. 1.0 / 2 C. (double) (1 / 2) D. (double) 1 / 2 E. 1 / 2.0

B. 1.0 / 2 D. (double) 1 / 2 E. 1 / 2.0 Explanation: 1 / 2 is an integer division, which results in 0.

Suppose x=10 and y=10 what is x after evaluating the expression (y > 10) && (x++ > 10). A. 9 B. 10 C. 11

B. 10

Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x++ > 10). A. 9 B. 10 C. 11

B. 10

Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x-- > 10). A. 9 B. 10 C. 11

B. 10 Explanation: For the || operator, the right operand is not evaluated, if the left operand is evaluated as true.

Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x-- > 10)? A. 9 B. 10 C. 11

B. 10 Explanation: For the && operator, the right operand is not evaluated, if the left operand is evaluated as false.

What is the result of 45 / 4? A. 10 B. 11 C. 11.25 D. 12

B. 11

(Tricky) 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 Explanation: 016 is an octal number. The prefix 0 indicates that a number is in octal.

What is the output of the following program? import java.util.Date; public class Test { public static void main(String[] args) { Date date = new Date(1234567); m1(date); System.out.print(date.getTime() + " "); m2(date); System.out.println(date.getTime()); } public static void m1(Date date) { date = new Date(7654321); } public static void m2(Date date) { date.setTime(7654321); } } A. 1234567 1234567 B. 1234567 7654321 C. 7654321 1234567 D. 7654321 7654321

B. 1234567 7654321

"abc".compareTo("aba") returns ___________. A. 1 B. 2 C. -1 D. -2 E. 0

B. 2

What is y after the following switch statement is executed? int x = 3; int y = 4; switch (x + 3) { case 6: y = 0; case 7: y = 1; default: y += 1; } A. 1 B. 2 C. 3 D. 4 E. 0

B. 2

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

If you enter 1 2 3, when you run this program, what will be the output? import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three numbers: "); double number1 = input.nextDouble(); double number2 = input.nextDouble(); double number3 = input.nextDouble(); // Compute average double average = (number1 + number2 + number3) / 3; // Display result System.out.println(average); } } A. 1.0 B. 2.0 C. 3.0 D. 4.0

B. 2.0

Math.pow(4, 1.0 / 2) returns __________. A. 2 B. 2.0 C. 0 D. 1.0 E. 1

B. 2.0

What is the value of (double)5/2? A. 2 B. 2.5 C. 3 D. 2.0 E. 3.0

B. 2.5

What is Math.max(Math.min(3, 6), 2)? A. 2 B. 3 C. 4 D. 2.0 E. 3.0

B. 3

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

B. 6

The "less than or equal to" comparison operator in Java is __________. A. < B. <= C. =< D. << E. !=

B. <=

What is displayed by the following code? String[] tokens = "A,B;C;D".split("[,;]"); for (int i = 0; i < tokens.length; i++) System.out.print(tokens[i] + " "); A. A,B;C;D B. A B C D C. A B C;D D. A B;C;D

B. A B C D

Which of the following statement is most accurate? A. A reference variable is an object. B. A reference variable refers to an object. C. An object may contain other objects. D. An object may contain the references of other objects.

B. A reference variable refers to an object. D. An object may contain the references of other objects.

__________ represents an entity in the real world that can be distinctly identified. A. A class B. An object C. A method D. A data field

B. An object

Which of the following statements is correct? A. Every line in a program must end with a semicolon. B. Every statement in a program must end with a semicolon. C. Every comment line must end with a semicolon. D. Every method must end with a semicolon. E. Every class must end with a semicolon.

B. Every statement in a program must end with a semicolon.

Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is the best? I: if (age < 16) System.out.println("Cannot get a driver's license"); if (age >= 16) System.out.println("Can get a driver's license"); II: if (age < 16) System.out.println("Cannot get a driver's license"); else System.out.println("Can get a driver's license"); III: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age >= 16) System.out.println("Can get a driver's license"); IV: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age > 16) System.out.println("Can get a driver's license"); else if (age == 16) System.out.println("Can get a driver's license"); A. I B. II C. III D. IV

B. II

Suppose income is 4001, what is the output of the following code: if (income > 3000) { System.out.println("Income is greater than 3000"); } else if (income > 4000) { System.out.println("Income is greater than 4000"); } A. no output B. Income is greater than 3000 C. Income is greater than 3000 followed by Income is greater than 4000 D. Income is greater than 4000 E. Income is greater than 4000 followed by Income is greater than 3000

B. Income is greater than 3000

The __________ method parses a string s to an int value. A. integer.parseInt(s); B. Integer.parseInt(s); C. integer.parseInteger(s); D. Integer.parseInteger(s);

B. Integer.parseInt(s);

________ contains predefined classes and interfaces for developing Java programs. A. Java language specification B. Java API C. Java JDK D. Java IDE

B. Java API

________ provides an integrated development environment (IDE) for rapidly developing Java programs. Editing, compiling, building, debugging, and online help are integrated in one graphical user interface. A. Java language specification B. Java API C. Java JDK D. Java IDE

B. Java API

The following program displays __________. public class Test { public static void main(String[] args) { String s = "Java"; StringBuilder buffer = new StringBuilder(s); change(buffer); System.out.println(buffer); } private static void change(StringBuilder buffer) { buffer.append(" and HTML"); } } A. Java B. Java and HTML C. and HTML D. nothing is displayed

B. Java and HTML Explanation: Inside the method, the content of the StringBuilder object is changed to Java and HTML. Therefore, the output from buffer is Java and HTML.

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

Will System.out.println((char)4) display 4? A. Yes B. No

B. No

Will the following program terminate? int balance = 10; while (true) { if (balance < 9) continue; balance = balance - 9; } A. Yes B. No

B. No

The __________ method displays an input dialog for reading a string. A. String string = JOptionPane.showMessageDialog(null, "Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE); B. String string = JOptionPane.showInputDialog(null, "Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE); C. String string = JOptionPane.showInputDialog("Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE); D. String string = JOptionPane.showInputDialog(null, "Enter a string"); E. String string = JOptionPane.showInputDialog("Enter a string");

B. String string = JOptionPane.showInputDialog(null, "Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE); D. String string = JOptionPane.showInputDialog(null, "Enter a string"); E. String string = JOptionPane.showInputDialog("Enter a string");

Which correctly creates an array of five empty Strings? A. String[] a = new String [5]; B. String[] a = {"", "", "", "", ""}; C. String[5] a; D. String[ ] a = new String [5]; for (int i = 0; i < 5; a[i++] = null);

B. String[] a = {"", "", "", "", ""};

_____________ seeks to analyze the data flow and to identify the system's input and output. A. Requirements specification B. System analysis C. Design D. Implementation E. Testing

B. System analysis

Which of the following statements is correct to display Welcome to Java on the console? A. System.out.println('Welcome to Java'); B. System.out.println("Welcome to Java"); C. System.println('Welcome to Java'); D. System.out.print('Welcome to Java'); E. System.out.print("Welcome to Java");

B. System.out.println("Welcome to Java"); E. System.out.print("Welcome to Java");

Which of the following statement prints smith\exam1\test.txt? A. System.out.println("smith\exam1\test.txt"); B. System.out.println("smith\\exam1\\test.txt"); C. System.out.println("smith\"exam1\"test.txt"); D. System.out.println("smith"\exam1"\test.txt");

B. System.out.println("smith\\exam1\\test.txt");

Suppose i is an int type variable. Which of the following statements display the character whose Unicode is stored in variable i? A. System.out.println(i); B. System.out.println((char)i); C. System.out.println((int)i); D. System.out.println(i + " ");

B. System.out.println((char)i);

Analyze the following code. boolean even = false; if (even) { System.out.println("It is even!"); } A. The code displays It is even! B. The code displays nothing. C. The code is wrong. You should replace if (even) with if (even == true) D. The code is wrong. You should replace if (even) with if (even = true)

B. The code displays nothing.

Analyze the following code. public class Test { public static void main(String[] args) { int n = 2; xMethod(n); System.out.println("n is " + n); } void xMethod(int n) { n++; } } A. The code has a compile error because xMethod does not return a value. B. The code has a compile error because xMethod is not declared static. C. The code prints n is 1. D. The code prints n is 2. E. The code prints n is 3.

B. The code has a compile error because xMethod is not declared static.

Analyze the following code. import javax.swing.*; public class ShowErrors { public static void main(String[] args) { int i; int j; String s = JOptionPane.showInputDialog(null, "Enter an integer", "Input", JOptionPane.QUESTION_MESSAGE); j = Integer.parseInt(s); i = (i + 4); } } A. The program cannot compile because j is not initialized. B. The program cannot compile because i does not have an initial value when it is used in i = i + 4; C. The program compiles but has a runtime error because i does not have an initial value when it is used in i = i + 4; D. The program compiles and runs fine.

B. The program cannot compile because i does not have an initial value when it is used in i = i + 4;

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.

What is wrong in the following code? class TempClass { int i; public void TempClass(int j) { int i = j; } } public class C { public static void main(String[] args) { TempClass temp = new TempClass(2); } } A. The program has a compilation error because TempClass does not have a default constructor. B. The program has a compilation error because TempClass does not have a constructor with an int argument. C. The program compiles fine, but it does not run because class C is not public. D. The program compiles and runs fine.

B. The program has a compilation error because TempClass does not have a constructor with an int argument. Explanation: The program would be fine if the void keyword is removed from public void TempClass(int j).

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 newS) { s = newS; } void print() { System.out.println(s); } } A. The program has a compilation error because class A is not a public class. B. The program has a compilation error because class A does not have a no-arg 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 compilation error because class A does not have a no-arg 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

What will be displayed by the following switch statement? char ch = 'a'; switch (ch) { case 'a': case 'A': System.out.print(ch); break; case 'b': case 'B': System.out.print(ch); break; case 'c': case 'C': System.out.print(ch); break; case 'd': case 'D': System.out.print(ch); } A. abcd B. a C. aa D. ab E. abc

B. a

In Java, the word true is ________. A. a Java keyword B. a Boolean literal C. same as value 1 D. same as value 0

B. a Boolean literal

Java ___________ can run from a Web browser. A. applications B. applets C. servlets D. Micro Edition programs

B. applets

Why do computers use zeros and ones? A. because combinations of zeros and ones can represent any numbers and characters. B. because digital devices have two stable states and it is natural to use one state for 0 and the other for 1. C. because binary numbers are simplest. D. because binary numbers are the bases upon which all other number systems are built.

B. because digital devices have two stable states and it is natural to use one state for 0 and the other for 1.

(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'

A block is enclosed inside __________. A. parentheses B. braces C. brackets D. quotes

B. braces

An object is an instance of a __________. A. program B. class C. method D. data

B. class

Assume java.util.Date[] dates = new java.util.Date[10], which of the following statements are true? A. dates is null. B. dates[0] is null. C. dates = new java.util.Date[5] is fine, which assigns a new array to dates. D. dates = new Date() is fine, which creates a new Date object and assigns to dates.

B. dates[0] is null. C. dates = new java.util.Date[5] is fine, which assigns a new array to dates.

Which of the following statements is 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}; Explanation: e would be corrected if it is char[] c = new char[]{'a', 'b', 'c', 'd'};

What will be displayed by the second println statement in the main method? public class Foo { int i; static int s; public static void main(String[] args) { Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } public Foo() { i++; s++; } } A. f2.i is 1 f2.s is 1 B. f2.i is 1 f2.s is 2 C. f2.i is 2 f2.s is 2 D. f2.i is 2 f2.s is 1

B. f2.i is 1 f2.s is 2 Explanation: i is an instance variable and s is static, shared by all objects of the Foo class.

What is the output of the following code? boolean even = false; System.out.println((even ? "true" : "false")); A. true B. false C. nothing D. true false

B. false

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

According to Java naming convention, which of the following names can be variables? A. FindArea B. findArea C. totalLength D. TOTAL_LENGTH E. class

B. findArea C. totalLength

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 will be displayed 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

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

Which of the following is the correct statement to return a string from an array a of characters? A. toString(a) B. new String(a) C. convertToString(a) D. String.toString(a)

B. new String(a)

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

Assume s is "ABCABC", the method __________ returns an array of characters. A. toChars(s) B. s.toCharArray() C. String.toChars() D. String.toCharArray() E. s.toChars()

B. s.toCharArray()

What is the output of the following code? public class Test { public static void main(String[] args) { String s1 = new String("Welcome to Java!"); String s2 = new String("Welcome to Java!"); if (s1 == s2) System.out.println("s1 and s2 reference to the same String object"); else System.out.println("s1 and s2 reference to different String objects"); } } A. s1 and s2 reference to the same String object B. s1 and s2 reference to different String objects

B. s1 and s2 reference to different String objects

What is the output of the following code? public class Test { public static void main(String[] args) { String s1 = new String("Welcome to Java"); String s2 = s1; s1 += "and Welcome to HTML"; if (s1 == s2) System.out.println("s1 and s2 reference to the same String object"); else System.out.println("s1 and s2 reference to different String objects"); } } A. s1 and s2 reference to the same String object B. s1 and s2 reference to different String objects

B. s1 and s2 reference to different String objects

Assume int[] list1 = {3, 4, 1, 9, 13}, int[] list2 = {3, 4, 1, 9, 13}, and int[] list3 = {4, 3, 1, 9, 13}, what is System.out.println(java.util.Arrays.equals(list1, list2) + " " + java.util.Arrays.equals(list1, list3));

B. true false

A Java character is stored in __________. A. one byte B. two bytes C. three bytes D. four bytes

B. two bytes Explanation: Java characters use Unicode encoding.

To add a value 1 to variable x, you write A. 1 + x = x; B. x += 1; C. x := 1; D. x = x + 1; E. x = 1 + x;

B. x += 1; D. x = x + 1; E. x = 1 + x;

Suppose x = 1, y = -1, and z = 1. What will be displayed by the following statement? (Please indent the statement correctly first.) if (x > 0) if (y > 0) System.out.println("x > 0 and y > 0"); else if (z > 0) System.out.println("x < 0 and z > 0"); A. x > 0 and y > 0 B. x < 0 and z > 0 C. x < 0 and z < 0 D. none

B. x < 0 and z > 0

Assume x = 4 and y = 5, Which of the following is true? A. x < 5 && y < 5 B. x < 5 || y < 5 C. x > 5 && y > 5 D. x > 5 || y > 5

B. x < 5 || y < 5

To assign a value 1 to variable x, you write A. 1 = x; B. x = 1; C. x := 1; D. 1 := x; E. x == 1;

B. x = 1;

What is the output of the following code? int x = 0; if (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

B. x is 1

What is x after the following statements? int x = 1; x *= x + 1; A. x is 1. B. x is 2. C. x is 3. D. x is 4.

B. x is 2.

What is y displayed? public class Test { public static void main(String[] args) { int x = 1; int y = x + x++; System.out.println("y is " + y); } } A. y is 1. B. y is 2. C. y is 3. D. y is 4.

B. y is 2. Explanation: When evaluating x + x++, x is evaluated first, which is 1. X++ returns 1 since it is post-increment and 2. Therefore y is 1 + 1.

Which of the following is the correct statement to return JAVA? A. toUpperCase("Java") B. "Java".toUpperCase("Java") C. "Java".toUpperCase() D. String.toUpperCase("Java")

C. "Java".toUpperCase()

____________________ returns true. A. "peter".compareToIgnoreCase("Peter") B. "peter".compareToIgnoreCase("peter") C. "peter".equalsIgnoreCase("Peter") D. "peter".equalsIgnoreCase("peter") E. "peter".equals("peter")

C. "peter".equalsIgnoreCase("Peter") D. "peter".equalsIgnoreCase("peter") E. "peter".equals("peter")

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)

Which of the following lines is not a Java comment? A. /** comments */ B. // comments C. -- comments D. /* comments */ E. ** comments **

C. -- comments E. ** comments **

The extension name of a Java bytecode file is A. .java B. .obj C. .class D. .exe

C. .class

What will be displayed by the following code? 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

What will be displayed by the following code? 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; do { System.out.println("Welcome to Java"); count++; } while (count < 10); A. 8 B. 9 C. 10 D. 11 E. 0

C. 10

How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (++count < 10); A. 8 B. 9 C. 10 D. 11 E. 0

C. 10

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

C. 10

How many times will the following code print "Welcome to Java"? int count = 0; while (count++ < 10) { System.out.println("Welcome to Java"); } A. 8 B. 9 C. 10 D. 11 E. 0

C. 10

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

C. 10

The statement System.out.printf("%5d", 123456) outputs ___________. A. 12345 B. 23456 C. 123456 D. 12345.6

C. 123456

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

C. 2

The expression 4 + 20 / (3 - 1) * 2 is evaluated to A. 4 B. 20 C. 24 D. 9 E. 25

C. 24

What will be displayed when the following code is executed? int number = 6; while (number > 0) { number -= 3; System.out.print(number + " "); } A. 6 3 0 B. 6 3 C. 3 0 D. 3 0 -3 E. 0 -3

C. 3 0

Assume int[] t = {1, 2, 3, 4}. What is t.length? A. 0 B. 3 C. 4 D. 5

C. 4

What is Math.round(3.6)? A. 3.0 B. 3 C. 4 D. 4.0

C. 4

What is Math.ceil(3.6)? A. 3.0 B. 3 C. 4.0 D. 5.0

C. 4.0

What is Math.rint(3.6)? A. 3.0 B. 3 C. 4.0 D. 5.0

C. 4.0 Explanation: Note that rint returns a double value

What is the value of i printed in the following code? public class Test { public static void main(String[] args) { int j = 0; int i = j++ + j * 5; System.out.println("What is i? " + i); } } A. 0 B. 1 C. 5 D. 6

C. 5 Explanation: Same as before, except that j++ evaluates to 0.

____________ is the Java assignment operator. A. == B. := C. = D. =:

C. =

The equal comparison operator in Java is __________. A. <> B. != C. == D. ^=

C. ==

What is displayed by the following code? System.out.print("A,B;C".replaceAll(",;", "#") + " "); System.out.println("A,B;C".replaceAll("[,;]", "#")); A. A B C A#B#C B. A#B#C A#B#C C. A,B;C A#B#C D. A B C A B C

C. A,B;C A#B#C

Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to ________. A. 66 B. B C. A1 D. Illegal expression

C. A1

Suppose TestCircle1 and Circle1 in Listing 8.1 are in two separate files named TestCircle1.java and Circle1.java, respectively. What is the outcome of compiling TestCircle.java and then Circle.java? A. Only TestCircle1.java compiles. B. Only Circle1.java compiles. C. Both compile fine. D. Neither compiles successfully.

C. Both compile fine.

Which is the advantage of encapsulation? A. Only public methods are needed. B. Making the class final causes no consequential changes to other code. C. It changes the implementation without changing a class's contract and causes no consequential changes to other code. D. It changes a class's contract without changing the implementation and causes no consequential changes to other code.

C. It changes the implementation without changing a class's contract and causes no consequential changes to other code.

The expression "Java " + 1 + 2 + 3 evaluates to ________. A. Java123 B. Java6 C. Java 123 D. java 123 E. Illegal expression

C. Java 123

What is displayed by the following statement? System.out.println("Java is neat".replaceAll("is", "AAA")); A. JavaAAAneat B. JavaAAA neat C. Java AAA neat D. Java AAAneat

C. Java AAA neat

What is Math.asin(0.5)? A. 90 B. 30 C. Math.PI / 6 D. Math.PI / 2

C. Math.PI / 6

The __________ method returns a raised to the power of b. A. Math.power(a, b) B. Math.exponent(a, b) C. Math.pow(a, b) D. Math.pow(b, a)

C. Math.pow(a, b)

Suppose s is a string with the value "java". What will be assigned to x if you execute the following code? char x = s.charAt(4); A. 'a' B. 'v' C. Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.

C. Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.

Given the following four patterns, Pattern A Pattern B Pattern C Pattern D 1 1 2 3 4 5 6 1 1 2 3 4 5 6 1 2 1 2 3 4 5 2 1 1 2 3 4 5 1 2 3 1 2 3 4 3 2 1 1 2 3 4 1 2 3 4 1 2 3 4 3 2 1 1 2 3 1 2 3 4 5 1 2 5 4 3 2 1 1 2 1 2 3 4 5 6 1 6 5 4 3 2 1 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

_______ is the code with natural language mixed with Java code. A. Java program B. A Java statement C. Pseudocode D. A flowchart diagram

C. Pseudocode

Analyze the following code and choose the best answer: public class Foo { private int x; public static void main(String[] args) { Foo foo = new Foo(); System.out.println(foo.x); } } A. Since x is private, it cannot be accessed from an object foo. B. Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code. C. Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code. D. You cannot create a self-referenced object; that is, foo is created inside the class Foo.

C. Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code. Explanation: (A) is incorrect, since x can be accessed by an object of Foo inside the Foo class. (B) is incorrect because x is non-static, it cannot be accessed in the main method without creating an object. (D) is incorrect, since it is permissible to create an instance of the class within the class. The best choice is (C).

The __________ method immediately terminates the program. A. System.terminate(0); B. System.halt(0); C. System.exit(0); D. System.quit(0); E. System.stop(0);

C. System.exit(0);

Suppse number contains integer value 4, which of the following statement is correct? A. System.out.printf("%3d %4d", number, Math.pow(number, 1.5)); B. System.out.printf("%3d %4.2d", number, Math.pow(number, 1.5)); C. System.out.printf("%3d %4.2f", number, Math.pow(number, 1.5)); D. System.out.printf("%3f %4.2d", number, Math.pow(number, 1.5)); E. System.out.printf("%3f %4.2f", number, Math.pow(number, 1.5));

C. System.out.printf("%3d %4.2f", number, Math.pow(number, 1.5)); Explanation: Math.pow(number, 1.5) is a floating-point value and must be formatted using the specifier f, not d. number is an int and must be formatted using the specifier d.

Assume int[] scores = {3, 4, 1, 9, 13}, which of the following statement displays all the element values in the array? A. System.out.println(scores); B. System.out.println(scores.toString()); C. System.out.println(java.util.Arrays.toString(scores)); D. System.out.println(scores[0]);

C. System.out.println(java.util.Arrays.toString(scores));

Analyze the following code. int[] list = new int[5]; list = new int[6]; A. The code has compile errors because the variable list cannot be changed once it is assigned. B. The code has runtime errors because the variable list cannot be changed once it is assigned. C. The code can compile and run fine. The second line assigns a new array to list. D. The code has compile errors because you cannot assign a different size array to list.

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

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 effient 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.

C. The program compiles and runs, but it is not effient 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[]) { int i = 0; for (i = 0; i < 10; i++); System.out.println(i + 4); } } A. The program has a compile error because of the semicolon (;) on the for loop line. B. The program compiles despite the semicolon (;) on the for loop line, and displays 4. C. The program compiles despite the semicolon (;) on the for loop line, and displays 14. D. The for loop in this program is same as for (i = 0; i < 10; i++) { }; System.out.println(i + 4);

C. The program compiles despite the semicolon (;) on the for loop line, and displays 14. D. The for loop in this program is same as for (i = 0; i < 10; i++) { }; System.out.println(i + 4); Explanation: This is a logic error. System.out.println(i + 4) is not a part the for loop because the for loop ends with the last semicolon at for (i=0; i<10; i++);

(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 + " "); } } 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. class Test { public static void main(String[] args) { StringBuilder strBuf = new StringBuilder(4); strBuf.append("ABCDE"); System.out.println("What's strBuf.charAt(5)? " + strBuf.charAt(5)); } } A. The program has a compilation error because you cannot specify initial capacity in the StringBuilder constructor. B. The program has a runtime error because because the buffer's capacity is 4, but five characters "ABCDE" are appended into the buffer. C. The program has a runtime error because the length of the string in the buffer is 5 after "ABCDE" is appended into the buffer. Therefore, strBuf.charAt(5) is out of range. D. The program compiles and runs fine.

C. The program has a runtime error because the length of the string in the buffer is 5 after "ABCDE" is appended into the buffer. Therefore, strBuf.charAt(5) is out of range.

Analyze the following fragment: double sum = 0; double d = 0; while (d != 10.0) { d += 0.1; sum += sum + d; } A. The program does not compile because sum and d are declared double, but assigned with integer value 0. B. The program never stops because d is always 0.1 inside the loop. C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers. D. After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9

C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.

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.

Analyze the following code: public class Test { public static void main(String[] args) { int n = 10000 * 10000 * 10000; System.out.println("n is " + n); } } A. The program displays n is 1000000000000 B. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program is aborted. C. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program continues to execute because Java does not report errors on overflow. D. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an underflow and the program is aborted. E. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an underflow and the program continues to execute because Java does not report errors on underflow.

C. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program continues to execute because Java does not report errors on overflow.

Analyze the following program fragment: int x; double d = 1.5; switch (d) { case 1.0: x = 1; case 1.5: x = 2; case 2.0: x = 3; } A. The program has a compile error because the required break statement is missing in the switch statement. B. The program has a compile error because the required default case is missing in the switch statement. C. The switch control variable cannot be double. D. No errors.

C. The switch control variable cannot be double.

What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0? A. true B. false C. There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true.

C. There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true. Explanation: becuase floating-point numbers are approximated.

What is displayed by the following code? public static void main(String[] args) { String[] tokens = "Welcome to Java".split("o"); for (int i = 0; i < tokens.length; i++) { System.out.print(tokens[i] + " "); } } A. Welcome to Java B. Welc me to Java C. Welc me t Java D. Welcome t Java

C. Welc me t Java

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

Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class. public MyClass() { xMethod(); } A. a static method B. an instance method C. a static method or an instance method

C. a static method or an instance method

A method that is associated with an individual object is called __________. A. a static method B. a class method C. an instance method D. an object method

C. an instance method

What is the exact output of the following code? double area = 3.5; System.out.print("area"); System.out.print(area); A. 3.53.5 B. 3.5 3.5 C. area3.5 D. area 3.5

C. area3.5

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

Suppose x is a char variable with a value 'b'. What will be displayed by the statement System.out.println(++x)? A. a B. b C. c D. d

C. c

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'};

The keyword __________ is required to declare a class. A. public B. private C. class D. All of the above.

C. class

To improve readability and maintainability, you should declare _________ instead of using literal values such as 3.14159. A. variables B. methods C. constants D. classes

C. constants

What will be displayed by the third println statement in the main method? public class Foo { int i; static int s; public static void main(String[] args) { Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } public Foo() { i++; s++; } } A. f3.i is 1 f3.s is 1 B. f3.i is 1 f3.s is 2 C. f3.i is 1 f3.s is 3 D. f3.i is 3 f3.s is 1 E. f3.i is 3 f3.s is 3

C. f3.i is 1 f3.s is 3

Programming style is important, because ______________. A. a program may not compile if it has a bad style B. good programming style can make a program run faster C. good programming style makes a program more readable D. good programming style helps reduce programming errors

C. good programming style makes a program more readable D. good programming style helps reduce programming errors

Which of the following assignment statements is incorrect? A. i = j = k = 1; B. i = 1; j = 1; k = 1; C. i = 1 = j = 1 = k = 1; D. i == j == k == 1;

C. i = 1 = j = 1 = k = 1; D. i == j == k == 1;

Analyze the following code: int i = 3434; double d = 3434; System.out.printf("%5.1f %5.1f", i, d); A. The code compiles and runs fine to display 3434.0 3434.0. B. The code compiles and runs fine to display 3434 3434.0. C. i is an integer, but the format specifier %5.1f specifies a format for double value. The code has an error.

C. i is an integer, but the format specifier %5.1f specifies a format for double value. The code has an error. Explanation: i is an integer, but the format specifier %5.1f specifies a format for double value. Type does not match

Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true. A. if (isPrime = true) B. if (isPrime == true) C. if (isPrime) D. if (!isPrime = false) E. if (!isPrime == false)

C. if (isPrime)

Which of the following code displays the area of a circle if the radius is positive. A. if (radius != 0) System.out.println(radius * radius * 3.14159); B. if (radius >= 0) System.out.println(radius * radius * 3.14159); C. if (radius > 0) System.out.println(radius * radius * 3.14159); D. if (radius <= 0) System.out.println(radius * radius * 3.14159);

C. if (radius > 0) System.out.println(radius * radius * 3.14159);

What is the correct term for numbers[99]? A. index B. index variable C. indexed variable D. array variable E. array

C. indexed variable

To declare an int variable number with initial value 2, you write A. int number = 2L; B. int number = 2l; C. int number = 2; D. int number = 2.0;

C. int number = 2;

Which of the following is 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];

Which of following is not a correct method in Character? A. isLetterOrDigit(char) B. isLetter(char) C. isDigit() D. toLowerCase(char) E. toUpperCase()

C. isDigit() E. toUpperCase() Explanation: isDigit() should be isDigit(char) and toUpperCase() should be toUpperCase(char)

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)

The JDK command to compile a class in the file Test.java is A. java Test B. java Test.java C. javac Test.java D. javac Test E. JAVAC Test.java

C. javac Test.java

The following code displays ___________. double temperature = 50; if (temperature >= 100) System.out.println("too hot"); else if (temperature <= 40) System.out.println("too cold"); else System.out.println("just right"); A. too hot B. too cold C. just right D. too hot too cold just right

C. just right

The speed of the CPU is measured in __________. A. megabytes B. gigabytes C. megahertz D. gigahertz

C. megahertz D. gigahertz

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

Suppose you wish to provide an accessor method for a boolean property finished, what signature of the method should be? A. public void getFinished() B. public boolean getFinished() C. public boolean isFinished() D. public void isFinished()

C. public boolean isFinished()

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)

The main method header is written as: A. public static void main(string[] args) B. public static void Main(String[] args) C. public static void main(String[] args) D. public static main(String[] args) E. public void main(String[] args)

C. public static void main(String[] args)

Assume s is "ABCABC", the method __________ returns a new string "aBCaBC". A. s.toLowerCase(s) B. s.toLowerCase() C. s.replace('A', 'a') D. s.replace('a', 'A') E. s.replace("ABCABC", "aBCaBC")

C. s.replace('A', 'a') E. s.replace("ABCABC", "aBCaBC")

Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect? A. String s = new String("new string"); B. String s3 = s1 + s2 C. s1 >= s2 D. int i = s1.length E. s1.charAt(0) = '5'

C. s1 >= s2 D. int i = s1.length E. s1.charAt(0) = '5'

What is the output of the following code? public class Test { public static void main(String[] args) { String s1 = new String("Welcome to Java!"); String s2 = s1.toUpperCase(); if (s1 == s2) System.out.println("s1 and s2 reference to the same String object"); else if (s1.equals(s2)) System.out.println("s1 and s2 have the same contents"); else System.out.println("s1 and s2 have different contents"); } }

C. s1 and s2 have different contents

Assume StringBuilder strBuf is "ABCDEFG", after invoking _________, strBuf contains "AEFG". A. strBuf.delete(0, 3) B. strBuf.delete(1, 3) C. strBuf.delete(1, 4) D. strBuf.delete(2, 4)

C. strBuf.delete(1, 4)

Assume StringBuilder strBuf is "ABCDEFG", after invoking _________, strBuf contains "ABCRRRRDEFG". A. strBuf.insert(1, "RRRR") B. strBuf.insert(2, "RRRR") C. strBuf.insert(3, "RRRR") D. strBuf.insert(4, "RRRR")

C. strBuf.insert(3, "RRRR")

Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } A. The variable t is not initialized and therefore causes errors. B. The variable t is private and therefore cannot be accessed in the main method. C. t is non-static and it cannot be referenced in a static context in the main method. D. The variable x is not initialized and therefore causes errors. E. The program compiles and runs fine.

C. t is non-static and it cannot be referenced in a static context in the main method.

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

When invoking a method with an object argument, ___________ is passed. A. the contents of the object B. a copy of the object C. the reference of the object D. the object is copied, then the reference of the copied object

C. the reference of the object

To prevent a class from being instantiated, _____________________ A. don't use any modifiers on the constructor. B. use the public modifier on the constructor. C. use the private modifier on the constructor. D. use the static modifier on the constructor.

C. use the private modifier on the constructor.

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.

C. x contains a reference to a Circle object.

Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate? A. x contains an array of ten int values. B. x contains an array of ten objects of the Circle type. C. x contains a reference to an array and each element in the array can hold a reference to a Circle object. D. x contains a reference to an array and each element in the array can hold a Circle object.

C. x contains a reference to an array and each element in the array can hold a reference to a Circle object.

What is y displayed in the following code? public class Test1 { public static void main(String[] args) { int x = 1; int y = x = x + 1; System.out.println("y is " + y); } } A. y is 0. B. y is 1 because x is assigned to y first. C. y is 2 because x + 1 is assigned to x and then x is assigned to y. D. The program has a compile error since x is redeclared in the statement int y = x = x + 1.

C. y is 2 because x + 1 is assigned to x and then x is assigned to y. Explanation: The = operator is right-associative.

What is y displayed in the following code? public class Test { public static void main(String[] args) { int x = 1; int y = x++ + x; System.out.println("y is " + y); } } A. y is 1. B. y is 2. C. y is 3. D. y is 4.

C. y is 3. Explanation: When evaluating x++ + x, x++ is evaluated first, which does two things: 1. returns 1 since it is post-increment. x becomes 2. Therefore y is 1 + 2.

The statement System.out.printf("%10s", "123456") outputs ___________. (Note: * represents a space) A. 123456**** B. 23456***** C. 12345***** D. ****123456

D. ****123456

"AbA".compareToIgnoreCase("abC") returns ___________. A. 1 B. 2 C. -1 D. -2 E. 0

D. -2

-24 % -5 is _____ A. 3 B. -3 C. 4 D. -4 E. 0

D. -4

-24 % 5 is _____ A. -1 B. -2 C. -3 D. -4 E. 0

D. -4

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

The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; } A. 1 2 3 4 5 6 7 8 9 B. 1 2 3 4 5 6 7 8 9 10 C. 1 2 3 4 5 D. 1 3 5 7 9 E. 2 4 6 8 10

D. 1 3 5 7 9

Math.pow(4, 1 / 2) returns __________. A. 2 B. 2.0 C. 0 D. 1.0 E. 1

D. 1.0

The statement System.out.printf("%3.1e", 1234.56) outputs ___________. A. 0.1e+04 B. 0.123456e+04 C. 0.123e+04 D. 1.2e+03 E. 1.23+03

D. 1.2e+03

How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 10); A. 8 B. 9 C. 10 D. 11 E. 0

D. 11

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. 2 1

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

Suppose x is 1. What is x after x += 2? A. 0 B. 1 C. 2 D. 3 E. 4

D. 3

Which of the following expression results in a value 1? A. 2 % 1 B. 15 % 4 C. 25 % 5 D. 37 % 6

D. 37 % 6 Explanation: 2 % 1 is 0, 15 % 4 is 3, 25 % 5 is 0, and 37 % 6 is 1

What is Math.rint(3.5)? A. 3.0 B. 3 C. 4 D. 4.0 E. 5.0

D. 4.0 Explanation: rint returns the nearest even integer as a double since 3.5 is equally close to 3.0 and 4.0.

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

D. 45

The Unicode of 'a' is 97. What is the Unicode for 'c'? A. 96 B. 97 C. 98 D. 99

D. 99

Analyze the following code: Code 1: int number = 45; boolean even; if (number % 2 == 0) even = true; else even = false; Code 2: int number = 45; boolean even = (number % 2 == 0); A. Code 1 has compile errors. B. Code 2 has compile errors. C. Both Code 1 and Code 2 have compile errors. D. Both Code 1 and Code 2 are correct, but Code 2 is better.

D. Both Code 1 and Code 2 are correct, but Code 2 is better.

The __________ method parses a string s to a double value. A. double.parseDouble(s); B. Double.parsedouble(s); C. double.parse(s); D. Double.parseDouble(s);

D. Double.parseDouble(s);

Identify the problems in the following code. public class Test { public static void main(String argv[]) { System.out.println("argv.length is " + argv.length); } } A. The program has a compile error because String argv[] is wrong and it should be replaced by String[] args. B. The program has a compile error because String args[] is wrong and it should be replaced by String args[]. C. If you run this program without passing any arguments, the program would have a runtime error because argv is null. D. If you run this program without passing any arguments, the program would display argv.length is 0.

D. If you run this program without passing any arguments, the program would display argv.length is 0. Explanation: The parameter for the main method is an array of String. The declaration String argv[] is correct. When you run the program without passing arguments, argv is new String[0]. Thus, argv.length is 0. See the NOTE box in the section, 'Passing Arguments to Java Programs.'

The __________ method displays a message dialog box. A. JOptionPane.showMessage(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE); B. JOptionPane.displayMessage(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE); C. JOptionPane.displayMessageDialog(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE); D. JOptionPane.showMessageDialog(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE); E. JOptionPane.showMessageDialog(null, "Welcome to Java!");

D. JOptionPane.showMessageDialog(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE); E. JOptionPane.showMessageDialog(null, "Welcome to Java!");

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);

Suppose you define a Java class as follows: public class Test { } In order to compile this program, the source code should be stored in a file named A. Test.class B. Test.doc C. Test.txt D. Test.java E. Any name with extension .java

D. Test.java

Analyze the following statement: double sum = 0; for (double d = 0; d < 10;) { d += 0.1; sum += sum + d; } A. The program has a compile error because the adjustment is missing in the for loop. B. The program has a compile error because the control variable in the for loop cannot be of the double type. C. The program runs in an infinite loop because d<10 would always be true. D. The program compiles and runs fine.

D. The program compiles and runs fine.

Analyze the following code. public class Test { int x; public Test(String t) { System.out.println("Test"); } public static void main(String[] args) { Test test = new Test(); System.out.println(test.x); } } A. The program has a compile error because System.out.println method cannot be invoked from the constructor. B. The program has a compile error because x has not been initialized. C. The program has a compile error because you cannot create an object from the class that defines the object. D. The program has a compile error because Test does not have a default constructor.

D. The program has a compile error because Test does not have a default constructor.

Analyze the following code. public class Test { public static void main(String[] args) { int month = 09; System.out.println("month is " + month); } } A. The program displays month is 09 B. The program displays month is 9 C. The program displays month is 9.0 D. The program has a syntax error, because 09 is an incorrect literal value.

D. The program has a syntax error, because 09 is an incorrect literal value. Explanation: Any numeric literal with the prefix 0 is an octal value. But 9 is not an octal digit. An octal digit is 0, 1, 2, 3, 4, 5, 6, or 7.

Analyze the following code: boolean even = false; if (even = true) { System.out.println("It is even!"); } A. The program has a compile error. B. The program has a runtime error. C. The program runs, but displays nothing. D. The program runs and displays It is even!.

D. The program runs and displays It is even!.

What is the output of the following code? String s = "University"; s.replace("i", "ABC"); System.out.println(s); A. UnABCversity B. UnABCversABCty C. UniversABCty D. University

D. University Explanation: No method in the String class can change the content of the string. String is an immutable class.

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

Variables that are shared by every instances of a class are __________. A. public variables B. private variables C. instance variables D. class variables

D. class variables

If you attempt to add an int, a byte, a long, and a double, the result will be a __________ value. A. byte B. int C. long D. double

D. double

What is displayed by the following code? System.out.print("Hi, ABC, good".matches("ABC ") + " "); System.out.println("Hi, ABC, good".matches(".*ABC.*")); A. false fasle B. true fasle C. true true D. false true

D. false true

To declare a constant MAX_LENGTH inside a method with value 99.98, you write A. final MAX_LENGTH = 99.98; B. final float MAX_LENGTH = 99.98; C. double MAX_LENGTH = 99.98; D. final double MAX_LENGTH = 99.98;

D. final double MAX_LENGTH = 99.98;

Suppose the input for number is 9. What is the output from running the following program? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int number = input.nextInt(); int i; boolean isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } } System.out.println("i is " + i); if (isPrime) System.out.println(number + " is prime"); else System.out.println(number + " is not prime"); } } A. i is 3 followed by 9 is prime B. i is 3 followed by 9 is not prime C. i is 4 followed by 9 is prime D. i is 4 followed by 9 is not prime

D. i is 4 followed by 9 is not prime

What will be displayed 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); 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

D. i is 6 isPrime is false

Which of the following assignment statements is illegal? A. float f = -34; B. int t = 23; C. short s = 10; D. int t = (int)false; E. int t = 4.5;

D. int t = (int)false; E. int t = 4.5;

Given the following method static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; } } What will be displayed by the call nPrint('a', 4)? A. aaaaa B. aaaa C. aaa D. invalid call

D. invalid call Explanation: Invalid call because char 'a' cannot be passed to string message

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 6.9.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 0 and high is 3 C. low is 3 and high is 6 D. low is 4 and high is 6 E. low is 0 and high is 5

D. low is 4 and high is 6

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) Explanation: Only one variable-length parameter may be specified in a method and this parameter must be the last parameter. The method return type cannot be a variable-length parameter.

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;

Assume s is " abc ", the method __________ returns a new string "abc". A. s.trim(s) B. trim(s) C. String.trim(s) D. s.trim()

D. s.trim()

To add number to sum, you write (Note: Java is case-sensitive) A. number += sum; B. number = sum + number; C. sum = Number + sum; D. sum += number; E. sum = sum + number;

D. sum += number; E. sum = sum + number;

What is i after the following for loop? int y = 0; for (int i = 0; i<10; ++i) { y += i; } A. 9 B. 10 C. 11 D. undefined

D. undefined Explanation: The scope of i is inside the loop. After the loop, i is not defined.

How many JFrame objects can you create and how many can you display? A. one B. two C. three D. unlimited

D. unlimited

Assume x = 4, Which of the following is true? A. !(x == 4) B. x != 4 C. x == 5 D. x != 5

D. x != 5

What is x after the following statements? int x = 2; int y = 1; x *= y + 1; A. x is 1. B. x is 2. C. x is 3. D. x is 4.

D. x is 4.

What will be displayed by the following code? double x = 5.5; int y = (int)x; System.out.println("x is " + x + " and y is " + y); A. x is 5 and y is 6 B. x is 6.0 and y is 6.0 C. x is 6 and y is 6 D. x is 5.5 and y is 5 E. x is 5.5 and y is 5.0

D. x is 5.5 and y is 5 Explanation: The value is x is not changed after the casting.

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 Explanation: The binarySearch method returns the index of the search key if it is contained in the list. Otherwise, it returns ?insertion point - 1. The insertion point is the point at which the key would be inserted into the list. In this case the insertion point is 1. Note that the array index starts from 0.

25 % 1 is _____ A. 1 B. 2 C. 3 D. 4 E. 0

E. 0

What is the value of times displayed? public class Test { public static void main(String[] args) { Count myCount = new Count(); int times = 0; for (int i=0; i<100; i++) increment(myCount, times); System.out.println( "myCount.count = " + myCount.count); System.out.println("times = "+ times); } public static void increment(Count c, int times) { c.count++; times++; } } class Count { int count; Count(int c) { count = c; } Count() { count = 1; } } A. 101 B. 100 C. 99 D. 98 E. 0

E. 0

What is Math.sin(Math.PI / 2)? A. 1 B. 0.5 C. 0.4 D. 1.5 E. 1.0

E. 1.0

The statement System.out.printf("%3.1f", 1234.56) outputs ___________. A. 123.4 B. 123.5 C. 1234.5 D. 1234.56 E. 1234.6

E. 1234.6

What is Math.sqrt(4.0)? A. 2 B. 2.5 C. 1 D. 3.0 E. 2.0

E. 2.0

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 Explanation: y should be 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

Which of the following operators are right-associative. A. * B. + (binary +) C. % D. && E. =

E. =

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

Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if (number % 2 == 0) even = true; else even = false; Code 2: even = (number % 2 == 0) ? true: false; Code 3: even = number % 2 == 0; A. Code 2 has a compile error, because you cannot have true and false literals in the conditional expression. B. Code 3 has a compile error, because you attempt to assign number to even. C. All three are correct, but Code 1 is preferred. D. All three are correct, but Code 2 is preferred. E. All three are correct, but Code 3 is preferred.

E. All three are correct, but Code 3 is preferred. Explanation: Code 3 is the simplest. Code 1 and Code 2 contain redundant code.

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. Explanation: new double[]{1, 2, 3} is correct. This is the syntax I have not covered in this edition, but will be covered in the future edition. In this question, double[] x = new double[]{1, 2, 3} is equivalent to double[] x = {1, 2, 3};

Analyze the following code. public class Test { int x; public Test(String t) { System.out.println("Test"); } public static void main(String[] args) { Test test = null; System.out.println(test.x); } } A. The program has a compile error because test is not initialized. B. The program has a compile error because x has not been initialized. C. The program has a compile error because you cannot create an object from the class that defines the object. D. The program has a compile error because Test does not have a default constructor. E. The program has a runtime NullPointerException because test is null while executing test.x.

E. The program has a runtime NullPointerException because test is null while executing test.x.

What will be displayed by the following switch statement? char ch = 'b'; switch (ch) { case 'a': System.out.print(ch); case 'b': System.out.print(ch); case 'c': System.out.print(ch); case 'd': System.out.print(ch); } A. abcd B. bcd C. b D. bb E. bbb

E. bbb

To declare a constant MAX_LENGTH as a member of the class, you write A. final static MAX_LENGTH = 99.98; B. final static float MAX_LENGTH = 99.98; C. static double MAX_LENGTH = 99.98; D. final double MAX_LENGTH = 99.98; E. final static double MAX_LENGTH = 99.98;

E. final static double MAX_LENGTH = 99.98;

Assume StringBuilder strBuf is "ABCCEFC", after invoking _________, strBuf contains "ABTTEFT". A. strBuf.replace('C', 'T') B. strBuf.replace("C", "T") C. strBuf.replace("CC", "TT") D. strBuf.replace('C', "TT") E. strBuf.replace(2, 7, "TTEFT")

E. strBuf.replace(2, 7, "TTEFT")

The System.currentTimeMillis() returns ________________ . A. the current time. B. the current time in milliseconds. C. the current time in milliseconds since midnight. D. the current time in milliseconds since midnight, January 1, 1970. E. the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time).

E. the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time).

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


संबंधित स्टडी सेट्स

QUIZ Module 2: Lesson 1: Mortgage Loans: Structures and Types

View Set

Penny Book - Obstetrics & Gynecology

View Set

NUR 125 PrepU Chapter 32: Management of Patients with Immune Deficiency Disorders

View Set

Professional Educational Development Exam Terms

View Set

ATI TEAS 7 - English & Language Usage

View Set

Child Development Cb: Unit 3 Lesson 2/ 4 "Gross Motor Development"

View Set

Ethics In Criminal Justice Midterm Review

View Set