Sign in or 

| 1.1 | Which statement is true about a method? Select the one correct answer. a. A method is an implementation of an abstraction. b. A method is an attribute defining the property of a particular abstraction. c. A method is a category of objects. d. A method is an operation defining the behavior for a particular abstraction. e. A method is a blueprint for making operations. |
| 1.2 | Which statement is true about an object? Select the one correct answer. a. An object is what classes are instantiated from. b. An object is an instance of a class. c. An object is a blueprint for creating concrete realization of abstractions. d. An object is a reference to an attribute. e. An object is a variable. |
| 1.3 | Which line contains a constructor in this class definition? public class Counter { // (1) int current, step; public Counter(int startValue, int stepValue) { // (2) set(startValue); setStepValue(stepValue); } public int get() { return current; } // (3) public void set(int value) { current = value; } // (4) public void setStepValue(int stepValue) { step = stepValue; } // (5)}Select the one correct answer. a. Code marked with (1) is a constructor. b. Code marked with (2) is a constructor. c. Code marked with (3) is a constructor. d. Code marked with (4) is a constructor. e. Code marked with (5) is a constructor. |
| 1.4 | Given that Thing is a class, how many objects and how many reference variables are created by the following code? Thing item, stuff;item = new Thing();Thing entity = new Thing(); Select the two correct answers. a. One object is created. b. Two objects are created. c. Three objects are created. d. One reference variable is created. e. Two reference variables are created. f. Three reference variables are created. |
| 1.5 | Which statement is true about an instance method? Select the one correct answer. a. An instance member is also called a static member. b. An instance member is always a field. c. An instance member is never a method. d. An instance member belongs to an instance, not to the class as a whole. e. An instance member always represents an operation. |
| 1.6 | How do objects pass messages in Java? Select the one correct answer. a. They pass messages by modifying each other's fields. b. They pass messages by modifying the static variables of each other's classes. c. They pass messages by calling each other's instance methods. d. They pass messages by calling static methods of each other's classes. |
| 1.7 | Given the following code, which statements are true? class A { int value1;}class B extends A { int value2;} Select the two correct answers. a. Class A extends class B. b. Class B is the superclass of class A. c. Class A inherits from class B. d. Class B is a subclass of class A. e. Objects of class A have a field named value2. f. Objects of class B have a field named value1. |
| 1.8 | What command in the Java 2 SDK should be used to compile the following code contained in a file called SmallProg.java? public class SmallProg { public static void main(String[] args) { System.out.println("Good luck!"); } }Select the one correct answer. a. java SmallProg b. javac SmallProg c. java SmallProg.java d. javac SmallProg.java e. java SmallProg main |
| 1.9 | What command in the Java 2 SDK should be used to execute the main() method of a class named SmallProg? Select the one correct answer. a. java SmallProg b. javac SmallProg c. java SmallProg.java d. java SmallProg.class e. java SmallProg.main() |
| 2.1 | Which of the following is not a legal identifier? Select the one correct answer. a. a2z b. ödipus c. 52pickup d. _class e. ca$h f. total# |
| 2.2 | Which statement is true? Select the one correct answer. a. new and delete are keywords in the Java language. b. try, catch, and thrown are keywords in the Java language. c. static, unsigned, and long are keywords in the Java language. d. exit, class, and while are keywords in the Java language. e. return, goto, and default are keywords in the Java language. f. for, while, and next are keywords in the Java language. |
| 2.3 | Is this a complete and legal comment? /* // */ Select the one correct answer. a. No, the block comment (/* ... */) is not ended since the single-line comment (// ...) comments out the closing part. b. It is a completely valid comment. The // part is ignored by the compiler. c. This combination of comments is illegal and the compiler will reject it. |
| 2.4 | Which of the following do not denote a primitive data value in Java? Select the two correct answers. a. "t" b. 'k' c. 50.5F d. "hello" e. false |
| 2.5 | Which of the following primitive data types are not integer types? Select the three correct answers. a. Type boolean b. Type byte c. Type float d. Type short e. Type double |
| 2.6 | Which integral type in Java has the exact range from -2147483648 (-231) to 2147483647 (231-1), inclusive? Select the one correct answer. a. Type byte b. Type short c. Type int d. Type long e. Type char |
| 2.7 | Which of the following lines are valid declarations?Select the three correct answers. a. char a = '\u0061'; b. char 'a' = 'a'; c. char \u0061 = 'a'; d. ch\u0061r a = 'a'; e. ch'a'r a = 'a'; |
| 2.8 | Given the following code within a method, which statement is true?int a, b;b = 5;Select the one correct answer. a. Local variable a is not declared. b. Local variable b is not declared. c. Local variable a is declared but not initialized. d. Local variable b is declared but not initialized. e. Local variable b is initialized but not declared. |
| 2.9 | In which of these variable declarations will the variable remain uninitialized unless explicitly initialized?Select the one correct answer. a. Declaration of an instance variable of type int. b. Declaration of a static variable of type float. c. Declaration of a local variable of type float. d. Declaration of a static variable of type Object. e. Declaration of an instance variable of type int[]. |
| 2.10 | What will be the result of attempting to compile this class?import java.util.*;package com.acme.toolkit;public class AClass { public Other anInstance;}class Other { int value;}Select the one correct answer. a. The class will fail to compile, since the class Other has not yet been declared when referenced in class AClass. b. The class will fail to compile, since import statements must never be at the very top of a file. c. The class will fail to compile, since the package declaration can never occur after an import statement. d. The class will fail to compile, since the class Other must be defined in a file called Other.java. e. The class will fail to compile, since the class Other must be declared public. f. The class will compile without errors. |
| 2.11 | Is an empty file a valid source file?Answer true or false. |
| 2.12 | Which of these are valid declarations of the main() method in order to start the execution of a Java application?Select the two correct answers. a. static void main(String[] args) { /* ... */ } b. public static int main(String[] args) { /* ... */ } c. public static void main(String args) { /* ... */ } d. final public static void main(String[] arguments) { /* ... */ } e. public int main(Strings[] args, int argc) { /* ... */ } f. static public void main(String args[]) { /* ... */ } |
| 2.13 | Which of the following are reserved keywords?Select the three correct answers. a. public b. static c. void d. main e. String f. args |
| 3.1 | Given char c = 'A'; What is the simplest way to convert the character value in c into an int? Select the one correct answer. a. int i = c; b. int i = (int) c; c. int i = Character.getNumericValue(c); | |
| 3.2 | What will be the result of attempting to compile and run the following class? public class Assignment { public static void main(String[] args) { int a, b, c; b = 10; a = b = c = 20; System.out.println(a); }} Select the one correct answer. a. The code will fail to compile, since the compiler will recognize that the variable c in the assignment statement a = b = c = 20; has not been initialized. b. The code will fail to compile because the assignment statement a = b = c = 20; is illegal. c. The code will compile correctly and will display 10 when run. d. The code will compile correctly and will display 20 when run. | |
| 3.3 | What will be the result of attempting to compile and run the following program? public class MyClass { public static void main(String[] args) { String a, b, c; c = new String("mouse"); a = new String("cat"); b = a; a = new String("dog"); c = b; System.out.println(c); }} Select the one correct answer. a. The program will fail to compile. b. The program will print mouse when run. c. The program will print cat when run. d. The program will print dog when run. e. The program will randomly print either cat or dog when run. | |
| 3.4 | Which of the following expressions will be evaluated using floating-point arithmetic? Select the three correct answers. a. 2.0 * 3.0 b. 2 * 3 c. 2/3 + 5/7 d. 2.4 + 1.6 e. 0x10 * 1L * 300.0 | |
| 3.5 | What is the value of the expression (1 / 2 + 3 / 2 + 0.1)? Select the one correct answer. a. 1 b. 1.1 c. 1.6 d. 2 e. 2.1 | |
| 3.6 | What will be the result of attempting to compile and run the following program? public class Integers { public static void main(String[] args) { System.out.println(0x10 + 10 + 010); } } Select the one correct answer. a. The program will not compile. The compiler will complain about the expression 0x10 + 10 + 010. b. When run, the program will print 28. c. When run, the program will print 30. d. When run, the program will print 34. e. When run, the program will print 36. f. When run, the program will print 101010. | |
| 3.7 | Which of the following expressions are valid? Select the three correct answers. a. (- 1 -) b. (+ + 1) c. (+-+-+-1) d. (--1) e. (1 * * 1) f. (- -1) | |
| 3.8 | What is the value of evaluating the following expression (- -1-3 * 10 / 5-1)? Select the one correct answer. a. –8 b. –6 c. 7 d. 8 e. 10 f. None of the above. | |
| 3.9 | Which of these assignments are valid? Select the four correct answers. a. short s = 12; b. long l = 012; c. int other = (int) true; d. float f = -123; e. double d = 0x12345678; | |
| 3.10 | Which statements are true?Select the three correct answers. a. The result of the expression (1 + 2 + "3") would be the string "33". b. The result of the expression ("1" + 2 + 3) would be the string "15". c. The result of the expression (4 + 1.0f) would be the float value 5.0f. d. The result of the expression (10/9) would be the int value 1. e. The result of the expression ('a' + 1) would be the char value 'b'. | |
| 3.11 | What happens when you try to compile and run the following program?public class Prog1 { public static void main(String[] args) { int k = 1; int i = ++k + k++ + + k; System.out.println(i); }}Select the one correct answer. a. The program will not compile. The compiler will complain about the expression ++k + k++ + + k. b. The program will compile and will print the value 3 when run. c. The program will compile and will print the value 4 when run. d. The program will compile and will print the value 7 when run. e. The program will compile and will print the value 8 when run. | |
| 3.12 | Which is the first incorrect line that will cause a compile time error in the following program?public class MyClass { public static void main(String[] args) { char c; int i; c = 'a'; // (1) i = c; // (2) i++; // (3) c = i; // (4) c++; // (5) }}Select the one correct answer. a. The line labeled (1) b. The line labeled (2) c. The line labeled (3) d. The line labeled (4) e. The line labeled (5) f. None of the lines are incorrect. The program will compile just fine. | |
| 3.13 | What happens when you try to compile and run the following program?public class Cast { public static void main(String[] args) { byte b = 128; int i = b; System.out.println(i); }}Select the one correct answer. a. The compiler will refuse to compile it, since you cannot assign a byte to an int without a cast. b. The program will compile and will print 128 when run. c. The compiler will refuse to compile it, since 128 is outside the legal range of values for a byte. d. The program will compile, but will throw a ClassCastException when run. e. The program will compile and will print 255 when run. | |
| 3.14 | What will the following program print when run?public class EvaluationOrder { public static void main(String[] args) { int[] array = { 4, 8, 16 }; int i=1; array[++i] = --i; System.out.println(array[0] + array[1] + array[2]); }}Select the one correct answer. a. 13 b. 14 c. 20 d. 21 e. 24 | |
| 3.15 | Which of the following expressions evaluates to true?Select the two correct answers. a. (false | true) b. (null != null) c. (4 <= 4) d. (!true) e. (true & false) | |
| 3.16 | Which statements are true?Select the two correct answers. a. The remainder operator % can only be used with integral operands. b. Identifiers in Java are case insensitive. c. The arithmetic operators *, /, and % have the same level of precedence. d. A short value ranges from -128 to +127 inclusive. e. (+15) is a legal expression. | |
| 3.17 | Which statements are true about the lines of output printed by the following program?public class BoolOp { static void op(boolean a, boolean b) { boolean c = a != b; boolean d = a ^ b; boolean e = c == d; System.out.println(e); } public static void main(String[] args) { op(false, false); op(true, false); op(false, true); op(true, true); }}Select the three correct answers. a. All lines printed are the same. b. At least one line contains false. c. At least one line contains true. d. The first line contains false. e. The last line contains true. | |
| 3.18 | What happens during execution of the following program?public class OperandOrder { public static void main(String[] args) { int i = 0; int[] a = {3,6}; a[i] = i = 9; System.out.println(i + " " + a[0] + " " + a[1]); }}Select the one correct answer. a. Throws an exception of type ArrayIndexOutOfBoundsException b. Prints "9 9 6" c. Prints "9 0 6" d. Prints "9 3 6" e. Prints "9 3 9" | |
| 3.19 | Which statements are true about the output of the following program?public class Logic { public static void main(String[] args) { int i = 0; int j = 0; boolean t = true; boolean r; r = (t & 0<(i+=1)); r = (t && 0<(i+=2)); r = (t | 0<(j+=1)); r = (t || 0<(j+=2)); System.out.println(i + " " + j); }}Select the two correct answers. a. The first digit printed is 1. b. The first digit printed is 2. c. The first digit printed is 3. d. The second digit printed is 1. e. The second digit printed is 2. f. The second digit printed is 3. | |
| 3.20 | What would be printed during execution of the following program? public class MyClass { public static void main(String[] args) { test(1<<32, "1<<32"); test(1<<31, "1<<31"); test(1<<30, "1<<30"); test(1, "1" ); test(0, "0" ); test(-1, "-1" ); } public static void test(int i, String exp) { if ((i >> 1) != (i >>> 1)) System.out.println(exp); }} Select the two correct answers. a. "1<<32" b. "1<<31" c. "1<<30" d. "1" e. "0" f. "-1" | |
| 3.21 | Which of the following are not operators in Java? Select the two correct answers. a. % b. <<< c. & d. %= e. >>> f. <= g. &&= | |
| 3.22 | Given a variable x of type int (which may contain a negative value), which are correct ways of doubling the value of x, barring any wrapping of out-of-range intermediate values? Select the four correct answers. a. x << 1; b. x = x * 2; c. x *= 2; d. x += x; e. x <<= 1; | |
| 3.23 | Which of the following operators can be used both as an integer bitwise operator and a boolean logical operator? Select the three correct answers. a. ^ b. ! c. & d. | e. ~ | |
| 3.24 | Given these declarations, which of the following expressions are valid? byte b = 1;char c = 1;short s = 1;int i = 1; Select the three correct answers. a. s = b * 2; b. i = b << s; c. b <<= s; d. c = c + b; e. s += i; | |
| 3.25 | What will be printed when the following program is run? public class ParameterPass { public static void main(String[] args) { int i = 0; addTwo(i++); System.out.println(i); } static void addTwo(int i) { i += 2; } } Select the one correct answer. a. 0 b. 1 c. 2 d. 3 | |
| 3.26 | What will be the result of attempting to compile and run the following class? public class Passing { public static void main(String[] args) { int a = 0; int b = 0; int[] bArr = new int[1]; bArr[0] = b; inc1(a); inc2(bArr); System.out.println("a=" + a + " b=" + b + " bArr[0]=" + bArr[0]); } public static void inc1(int x) { x++; } public static void inc2(int[] x) { x[0]++; } } Select the one correct answer. a. The code will fail to compile, since x[0]++; is not a legal statement. b. The code will compile and will print "a=1 b=1 bArr[0]=1" when run. c. The code will compile and will print "a=0 b=1 bArr[0]=1" when run. d. The code will compile and will print "a=0 b=0 bArr[0]=1" when run. e. The code will compile and will print "a=0 b=0 bArr[0]=0" when run. | |
| 3.27 | Given the class // Filename: Args.java public class Args { public static void main(String[] args) { System.out.println(args[0] + " " + args[args.length-1]); } } what would be the result of executing the following on the command line? java Args In politics stupidity is not a handicap Select the one correct answer. a. The program will throw ArrayIndexOutOfBoundsException. b. The program will print "java handicap". c. The program will print "Args handicap". d. The program will print "In handicap". e. The program will print "Args a". f. The program will print "In a". | |
| 3.28 | Which statements would cause a compilation error if inserted in the location indicated in the following program? public class ParameterUse { static void main(String[] args) { int a = 0; final int b = 1; int[] c = { 2 }; final int[] d = { 3 }; useArgs(a, b, c, d); } static void useArgs(final int a, int b, final int[] c, int[] d) { // INSERT STATEMENT HERE. } } Select the two correct answers. a. a++; b. b++; c. b = a; d. c[0]++; e. d[0]++; f. c = d; | |
| 4.1 | Given the following declaration, which expression returns the size of the array, assuming the array has been initialized? int[] array; Select the one correct answer. a. array[].length() b. array.length() c. array[].length d. array.length e. array[].size() f. array.size() |
| 4.2 | Is it possible to create arrays of length zero? Select the one correct answer. a. Yes, you can create arrays of any type with length zero. b. Yes, but only for primitive data types. c. Yes, but only for arrays of object references. d. No, you cannot create zero-length arrays, but the main() method may be passed a zero-length array of String references when no program arguments are specified. e. No, it is not possible to create arrays of length zero in Java. |
| 4.3 | Which one of the following array declaration statements is not legal? Select the one correct answer. a. int []a[] = new int [4][4]; b. int a[][] = new int [4][4]; c. int a[][] = new int [][4]; d. int []a[] = new int [4][]; e. int [][]a = new int [4][4]; |
| 4.4 | Which of these array declaration statements are not legal? Select the two correct answers. a. int[] i[] = { { 1, 2 }, { 1 }, {}, { 1, 2, 3 } }; b. int i[] = new int[2] {1, 2}; c. int i[][] = new int[][] { {1, 2, 3}, {4, 5, 6} }; d. int i[][] = { { 1, 2 }, new int[ 2 ] }; e. int i[4] = { 1, 2, 3, 4 }; |
| 4.5 | What would be the result of attempting to compile and run the following program? // Filename: MyClass.javaclass MyClass { public static void main(String[] args) { int size = 20; int[] arr = new int[ size ]; for (int i = 0; i < size; ++i) { System.out.println(arr[i]); } }} Select the one correct answer. a. The code will fail to compile because the array type int[] is incorrect. b. The program will compile, but will throw an ArrayIndexOutOfBoundsException when run. c. The program will compile and run without error, but will produce no output. d. The program will compile and run without error and will print the numbers 0 through 19. e. The program will compile and run without error and will print 0 twenty times. f. The program will compile and run without error and will print null twenty times. |
| 4.6 | Given the following program, which statement is true? class MyClass { public static void main(String[] args) { String[] numbers = { "one", "two", "three", "four" }; if (args.length == 0) { System.out.println("no arguments"); } else { System.out.println(numbers[ args.length ] + " arguments"); } }} Select the one correct answer. a. The program will fail to compile. b. The program will throw a NullPointerException when run with zero program arguments. c. The program will print "no arguments" and "two arguments" when called with zero and three program arguments, respectively. d. The program will print "no arguments" and "three arguments" when called with zero and three program arguments, respectively. e. The program will print "no arguments" and "four arguments" when called with zero and three program arguments, respectively. f. The program will print "one arguments" and "four arguments" when called with zero and three program arguments, respectively. |
| 4.7 | What would be the result of trying to compile and run the following program? public class DefaultValuesTest { int[] ia = new int[1]; boolean b; int i; Object o; public static void main(String[] args) { DefaultValuesTest instance = new DefaultValuesTest(); instance.print(); } public void print() { System.out.println(ia[0] + " " + b + " " + i + " " + o); }} Select the one correct answer. a. The program will fail to compile because of uninitialized variables. b. The program will throw a java.lang.NullPointerException when run. c. The program will print "0 false NaN null". d. The program will print "0 false 0 null". e. The program will print "null 0 0 null". f. The program will print "null false 0 null". |
| 4.8 | Which one of these is a valid method declaration? Select the one correct answer. a. void method1 { /* ... */ } b. void method2() { /* ... */ } c. void method3(void) { /* ... */ } d. method4() { /* ... */ } e. method5(void) { /* ... */ } |
| 4.9 | Given the following code, which statements can be placed at the indicated position without causing compilation errors? public class ThisUsage { int planets; static int suns; public void gaze() { int i; // ... insert statements here ... } } Select the three correct answers. a. i = this.planets; b. i = this.suns; c. this = new ThisUsage(); d. this.i = 4; e. this.suns = planets; |
| 4.10 | Given the following pairs of method declarations, which statements are true? void fly(int distance) {} int fly(int time, int speed) { return time*speed; } void fall(int time) {} int fall(int distance) { return distance; } void glide(int time) {} void Glide(int time) {} Select the two correct answers. a. The first pair of methods will compile correctly and overload the method name fly. b. The second pair of methods will compile correctly and overload the method name fall. c. The third pair of methods will compile correctly and overload the method name glide. d. The second pair of methods will not compile correctly. e. The third pair of methods will not compile correctly. |
| 4.11 | Given a class named Book, which one of these is a valid constructor declaration for the class? Select the one correct answer. a. Book(Book b) {} b. Book Book() {} c. private final Book() {} d. void Book() {} e. public static void Book(String[] args) {} f. abstract Book() {} |
| 4.12 | Which statements are true? Select the two correct answers. a. All classes must define a constructor. b. A constructor can be declared private. c. A constructor can return a value. d. A constructor must initialize all the fields of a class. e. A constructor can access the non-static members of a class. |
| 4.13 | What will be the result of attempting to compile the following program? public class MyClass { long var; public void MyClass(long param) { var = param; } // (1) public static void main(String[] args) { MyClass a, b; a = new MyClass(); // (2) b = new MyClass(5); // (3) } } Select the one correct answer. a. A compilation error will occur at (1), since constructors cannot specify a return value. b. A compilation error will occur at (2), since the class does not have a default constructor. c. A compilation error will occur at (3), since the class does not have a constructor which takes one argument of type int. d. The program will compile correctly. |
| 4.14 | Given the following class, which of these are valid ways of referring to the class from outside of the package net.basemaster? package net.basemaster; public class Base { // ... } Select the two correct answers. a. By simply referring to the class as Base. b. By simply referring to the class as basemaster.Base. c. By simply referring to the class as net.basemaster.Base. d. By importing with net.basemaster.* and referring to the class as Base. e. By importing with net.* and referring to the class as basemaster.Base. |
| 4.15 | Which one of the following class definitions is a valid definition of a class that cannot be instantiated? Select the one correct answer. a. class Ghost {b. abstract void haunt();c. } |
| 4.16 | Which one of the following class definitions is a valid definition of a class that cannot be extended? Select the one correct answer. a. class Link { } b. abstract class Link { } c. native class Link { } d. static class Link { } e. final class Link { } f. private class Link { } g. abstract final class Link { } |
| 4.17 | Given the following definition of a class, which fields are accessible from outside the package com.corporation.project?package com.corporation.project;public class MyClass { int i; public int j; protected int k; private int l;}Select the two correct answers. a. Field i is accessible in all classes in other packages. b. Field j is accessible in all classes in other packages. c. Field k is accessible in all classes in other packages. d. Field k is accessible in subclasses only in other packages. e. Field l is accessible in all classes in other packages. f. Field l is accessible in subclasses only in other packages. |
| 4.18 | How restrictive is the default accessibility compared to public, protected, and private accessibility?Select the one correct answer. a. Less restrictive than public. b. More restrictive than public, but less restrictive than protected. c. More restrictive than protected, but less restrictive than private. d. More restrictive than private. e. Less restrictive than protected from within a package, and more restrictive than protected from outside a package. |
| 4.19 | Which statement is true about accessibility of members?Select the one correct answer. a. Private members are always accessible from within the same package. b. Private members can only be accessed by code from within the class of the member. c. A member with default accessibility can be accessed by any subclass of the class in which it is defined. d. Private members cannot be accessed at all. e. Package/default accessibility for a member can be declared using the keyword default. |
| 4.20 | Which statements are true about the use of modifiers? Select the two correct answers. a. If no accessibility modifier (public, protected, and private) is specified for a member declaration, the member is only accessible for classes in the package of its class and subclasses of its class anywhere. b. You cannot specify accessibility of local variables. They are only accessible within the block in which they are declared. c. Subclasses of a class must reside in the same package as the class they extend. d. Local variables can be declared static. e. Objects themselves do not have any accessibility modifiers, only the object references do. |
| 4.21 | Given the following source code, which comment line can be uncommented without introducing errors? abstract class MyClass { abstract void f(); final void g() {} // final void h() {} // (1) protected static int i; private int j; } final class MyOtherClass extends MyClass { // MyOtherClass(int n) { m = n; } // (2) public static void main(String[] args) { MyClass mc = new MyOtherClass(); } void f() {} void h() {} // void k() { i++; } // (3) // void l() { j++; } // (4) int m; } Select the one correct answer. a. final void h() {} // (1)b. MyOtherClass(int n) { m = n; } // (2)c. void k() { i++; } // (3)d. void l() { j++; } // (4) |
| 4.22 | What would be the result of attempting to compile and run the following program? class MyClass { static MyClass ref; String[] arguments; public static void main(String[] args) { ref = new MyClass(); ref.func(args); } public void func(String[] args) { ref.arguments = args; } } Select the one correct answer. a. The program will fail to compile, since the static method main() cannot have a call to the non-static method func(). b. The program will fail to compile, since the non-static method func() cannot access the static variable ref. c. The program will fail to compile, since the argument args passed to the static method main() cannot be passed on to the non-static method func(). d. The program will fail to compile, since the method func() cannot assign the value of the static variable ref to the non-static variable arguments. e. The program will compile, but will throw an exception when run. f. The program will compile and run successfully. |
| 4.23 | Given the following member declarations, which statement is true? int a; // (1) static int a; // (2) int f() { return a; } // (3) static int f() { return a; } // (4) Select the one correct answer. a. Declarations (1) and (3) cannot occur in the same class definition. b. Declarations (2) and (4) cannot occur in the same class definition. c. Declarations (1) and (4) cannot occur in the same class definition. d. Declarations (2) and (3) cannot occur in the same class definition. |
| 4.24 | Which statement is true? Select the one correct answer. a. A static method can call other non-static methods in the same class by using the this keyword. b. A class may contain both static and non-static variables and both static and non-static methods. c. Each object of a class has its own instance of each static variable. d. Instance methods may access local variables of static methods. e. All methods in a class are implicitly passed a this parameter when called. |
| 4.25 | What, if anything, is wrong with the following code? abstract class MyClass { transient int j; synchronized int k; final void MyClass() {} static void f() {} } Select the one correct answer. a. The class MyClass cannot be declared abstract. b. The field j cannot be declared transient. c. The field k cannot be declared synchronized. d. The method MyClass() cannot be declared final. e. The method f() cannot be declared static. f. Nothing is wrong with the code; it will compile without errors. |
| 4.26 | Which one of these is not a legal member declaration within a class? Select the one correct answer. a. static int a; b. final Object[] fudge = { null }; c. abstract int t; d. native void sneeze(); e. final static private double PI = 3.14159265358979323846; |
| 4.27 | Which statements are true about modifiers? Select the two correct answers. a. Abstract classes can contain final methods. b. Fields can be declared native. c. Non-abstract methods can be declared in abstract classes. d. Classes can be declared native. e. Abstract classes can be declared final. |
| 4.28 | Which statement is true? Select the one correct answer. a. Transient fields will not be saved during serialization. b. Constructors can be declared abstract. c. The initial state of an array object constructed with the statement int[] a = new int[10] will depend on whether the array variable a is a local variable or a field. d. A subclass of a class with an abstract method must provide an implementation for the abstract method. e. Only static methods can access static members. |
| 5.1 | What will be the result of attempting to compile and run the following class? public class IfTest { public static void main(String[] args) { if (true) if (false) System.out.println("a"); else System.out.println("b"); }} Select the one correct answer. a. The code will fail to compile because the syntax of the if statement is incorrect. b. The code will fail to compile because the compiler will not be able to determine which if statement the else clause belongs to. c. The code will compile correctly and display the letter a when run. d. The code will compile correctly and display the letter b when run. e. The code will compile correctly, but will not display any output. |
| 5.2 | Which statements are true? Select the three correct answers. a. The conditional expression in an if statement can have method calls. b. If a and b are of type boolean, the expression (a = b) can be the conditional expression of an if statement. c. An if statement can have either an if clause or an else clause. d. The statement if (false) ; else ; is illegal. e. Only expressions which evaluate to a boolean value can be used as the condition in an if statement. |
| 5.3 | What, if anything, is wrong with the following code? void test(int x) { switch (x) { case 1: case 2: case 0: default: case 4: }} Select the one correct answer. a. The variable x does not have the right type for a switch expression. b. The case label 0 must precede case label 1. c. Each case section must end with a break statement. d. The default label must be the last label in the switch statement. e. The body of the switch statement must contain at least one statement. f. There is nothing wrong with the code. |
| 5.4 | Which of these combinations of switch expression types and case label value types are legal within a switch statement? Select the one correct answer. a. switch expression of type int and case label value of type char. b. switch expression of type float and case label value of type int. c. switch expression of type byte and case label value of type float. d. switch expression of type char and case label value of type long. e. switch expression of type boolean and case value of type boolean. |
| 5.5 | What will be the result of attempting to compile and run the following code? class MyClass { public static void main(String[] args) { boolean b = false; int i = 1; do { i++; b = ! b; } while (b); System.out.println(i); } } Select the one correct answer. a. The code will fail to compile, since b is an invalid conditional expression for the do-while statement. b. The code will fail to compile, since the assignment b = ! b is not allowed. c. The code will compile without error and will print 1 when run. d. The code will compile without error and will print 2 when run. e. The code will compile without error and will print 3 when run. |
| 5.6 | What will be the output when running the following program? public class MyClass { public static void main(String[] args) { int i=0; int j; for (j=0; j<10; ++j) { i++; } System.out.println(i + " " + j); } } Select the two correct answers. a. The first number printed will be 9. b. The first number printed will be 10. c. The first number printed will be 11. d. The second number printed will be 9. e. The second number printed will be 10. f. The second number printed will be 11. |
| 5.7 | Which one of these for statements is valid? Select the one correct answer. a. int j=10; for (int i=0, j+=90; i<j; i++) { j--; } b. for (int i=10; i=0; i--) {} c. for (int i=0, j=100; i<j; i++, --j) {;} d. int i, j; for (j=100; i<j; j--) { i += 2; } e. int i=100; for ((i>0); i--) {} |
| 5.8 | What will be the result of attempting to compile and run the following program? class MyClass { public static void main(String[] args) { int i = 0; for ( ; i<10; i++) ; // (1) for (i=0; ; i++) break; // (2) for (i=0; i<10; ) i++; // (3) for ( ; ; ) ; // (4) } } Select the one correct answer. a. The code will fail to compile, since the for statement (1) is missing the expression in the first section. b. The code will fail to compile, since the for statement (2) is missing the expression in the middle section. c. The code will fail to compile, since the for statement (3) is missing the expression in the last section. d. The code will fail to compile, since the for statement (4) is invalid. e. The code will compile without error, and the program will run and terminate without any output. f. The code will compile without error, but will never terminate when run. |
| 5.9 | Which statements are valid when occurring on their own?> Select the three correct answers. a. while () break; b. do { break; } while (true); c. if (true) { break; } d. switch (1) { default: break; } e. for (;true;) break; |
| 5.10 | Given the following code fragment, which of the following lines will be a part of the output? outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { if (i == j) { continue outer; } System.out.println("i=" + i + ", j=" + j); } } Select the two correct answers. a. i=1, j=0 b. i=0, j=1 c. i=1, j=2 d. i=2, j=1 e. i=2, j=2 f. i=3, j=3 g. i=3, j=2 |
| 5.11 | What will be the result of attempting to compile and run the following code? class MyClass { public static void main(String[] args) { for (int i = 0; i<10; i++) { switch(i) { case 0: System.out.println(i); } if (i) { System.out.println(i); } } } } Select the one correct answer. a. The code will fail to compile, owing to an illegal switch expression in the switch statement. b. The code will fail to compile, owing to an illegal conditional expression in the if statement. c. The code will compile without error and will print the numbers 0 through 10 when run. d. The code will compile without error and will print the number 0 when run. e. The code will compile without error and will print the number 0 twice when run. f. The code will compile without error and will print the numbers 1 through 10 when run. |
| 5.12 | Which of the following implementations of a max() method will correctly return the largest value? // (1) int max(int x, int y) { return (if (x > y) { x; } else { y; }); } // (2) int max(int x, int y) { return (if (x > y) { return x; } else { return y; }); } // (3) int max(int x, int y) { switch (x < y) { case true: return y; default: return x; }; } // (4) int max(int x, int y) { if (x>y) return x; return y; } Select the one correct answer. a. Implementation labeled (1). b. Implementation labeled (2). c. Implementation labeled (3). d. Implementation labeled (4). |
| 5.13 | Given the following code, which statement is true? class MyClass { public static void main(String[] args) { int k=0; int l=0; for (int i=0; i <= 3; i++) { k++; if (i == 2) break; l++; } System.out.println(k + ", " + l); } } Select the one correct answer. a. The program will fail to compile. b. The program will print 3, 3 when run. c. The program will print 4, 3 when run if break is replaced by continue. d. The program will fail to compile if break is replaced by return. e. The program will fail to compile if break is simply removed. |
| 5.14 | Which statements are true? Select the two correct answers. a. {{}} is a valid statement block. b. { continue; } is a valid statement block. c. block: { break block; } is a valid statement block. d. block: { continue block; } is a valid statement block. e. The break statement can only be used in a loop (while, do-while or for) or a switch statement. |
| 5.15 | Which digits, and in which order, will be printed when the following program is run? public class MyClass { public static void main(String[] args) { int k=0; try { int i = 5/k; } catch (ArithmeticException e) { System.out.println("1"); } catch (RuntimeException e) { System.out.println("2"); return; } catch (Exception e) { System.out.println("3"); } finally { System.out.println("4"); } System.out.println("5"); } } Select the one correct answer. a. The program will only print 5. b. The program will only print 1 and 4, in that order. c. The program will only print 1, 2, and 4, in that order. d. The program will only print 1, 4, and 5, in that order. e. The program will only print 1, 2, 4, and 5, in that order. f. The program will only print 3 and 5, in that order. |
| 5.16 | Given the following program, which statements are true? public class Exceptions { public static void main(String[] args) { try { if (args.length == 0) return; System.out.println(args[0]); } finally { System.out.println("The end"); } } } Select the two correct answers. a. If run with no arguments, the program will produce no output. b. If run with no arguments, the program will print "The end". c. The program will throw an ArrayIndexOutOfBoundsException. d. If run with one argument, the program will simply print the given argument. e. If run with one argument, the program will print the given argument followed by "The end". |
| 5.17 | What will be the result of attempting to compile and run the following program? public class MyClass { public static void main(String[] args) { RuntimeException re = null; throw re; } } Select the one correct answer. a. The code will fail to compile, since the main() method does not declare that it throws RuntimeException in its declaration. b. The program will fail to compile, since it cannot throw re. c. The program will compile without error and will throw java.lang.RuntimeException when run. d. The program will compile without error and will throw java.lang.NullpointerException when run. e. The program will compile without error and will run and terminate without any output. |
| 5.18 | Which statements are true? Select the two correct answers. a. If an exception is uncaught in a method, the method will terminate and normal execution will resume. b. An overriding method must declare that it throws the same exception classes as the method it overrides. c. The main() method of a program can declare that it throws checked exceptions. d. A method declaring that it throws a certain exception class may throw instances of any subclass of that exception class. e. finally blocks are executed if, and only if, an exception gets thrown while inside the corresponding try block. |
| 5.19 | Which digits, and in which order, will be printed when the following program is compiled and run? public class MyClass { public static void main(String[] args) { try { f(); } catch (InterruptedException e) { System.out.println("1"); throw new RuntimeException(); } catch (RuntimeException e) { System.out.println("2"); return; } catch (Exception e) { System.out.println("3"); } finally { System.out.println("4"); } System.out.println("5"); } // InterruptedException is a direct subclass of Exception. static void f() throws InterruptedException { throw new InterruptedException("Time for lunch."); } } Select the one correct answer. a. The program will print 5. b. The program will print 1 and 4, in that order. c. The program will print 1, 2, and 4, in that order. d. The program will print 1, 4, and 5, in that order. e. The program will print 1, 2, 4, and 5, in that order. f. The program will print 3 and 5, in that order. |
| 5.20 | Which digits, and in which order, will be printed when the following program is run? public class MyClass { public static void main(String[] args) throws InterruptedException { try { f(); System.out.println("1"); } finally { System.out.println("2"); } System.out.println("3"); } // InterruptedException is a direct subclass of Exception. static void f() throws InterruptedException { throw new InterruptedException("Time to go home."); } } Select the one correct answer. a. The program will print 2 and throw InterruptedException. b. The program will print 1 and 2, in that order. c. The program will print 1, 2, and 3, in that order. d. The program will print 2 and 3, in that order. e. The program will print 3 and 2, in that order. f. The program will print 1 and 3, in that order. |
| 5.21 | What is wrong with the following code? public class MyClass { public static void main(String[] args) throws A { try { f(); } finally { System.out.println("Done."); } catch (A e) { throw e; } } public static void f() throws B { throw new B(); } } class A extends Throwable {} class B extends A {} Select the one correct answer. a. The main() method must declare that it throws B. b. The finally block must follow the catch block in the main() method. c. The catch block in the main() method must declare that it catches B rather than A. d. A single try block cannot be followed by both a finally and a catch block. e. The declaration of class A is illegal. |
| 5.22 | What is the minimal list of exception classes that the overriding method f() in the following code must declare in its throws clause before the code will compile correctly? class A { // InterruptedException is a direct subclass of Exception. void f() throws ArithmeticException, InterruptedException { div(5, 5); } int div(int i, int j) throws ArithmeticException { return i/j; } } public class MyClass extends A { void f() /* throws [...list of exceptions...] */ { try { div(5, 0); } catch (ArithmeticException e) { return; } throw new RuntimeException("ArithmeticException was expected."); } } Select the one correct answer. a. Does not need to specify any exceptions. b. Needs to specify that it throws ArithmeticException. c. Needs to specify that it throws InterruptedException. d. Needs to specify that it throws RuntimeException. e. Needs to specify that it throws both ArithmeticException and InterruptedException. |
| 5.23 | What, if anything, would cause the following code not to compile? class A { void f() throws ArithmeticException { //... } } public class MyClass extends A { public static void main(String[] args) { A obj = new MyClass(); try { obj.f(); } catch (ArithmeticException e) { return; } catch (Exception e) { System.out.println(e); throw new RuntimeException("Something wrong here"); } } // InterruptedException is a direct subclass of Exception. void f() throws InterruptedException { //... } } Select the one correct answer. a. The main() method must declare that it throws RuntimeException. b. The overriding f() method in MyClass must declare that it throws ArithmeticException, since the f() method in class A declares that it does. c. The overriding f() method in MyClass is not allowed to throw InterruptedException, since the f() method in class A does not throw this exception. d. The compiler will complain that the catch(ArithmeticException) block shadows the catch(Exception) block. e. You cannot throw exceptions from a catch block. f. Nothing is wrong with the code, it will compile without errors. |
| 5.24 | Assuming assertions are enabled, which of these assertion statements will throw an error? Select the two correct answers. a. assert true : true; b. assert true : false; c. assert false : true; d. assert false : false; |
| 5.25 | Which of the following are valid runtime options? Select the two correct answers. a. -ae b. -enableassertions c. -source 1.4 d. -disablesystemassertions e. -dea |
| 5.26 | What is the class name of the exception thrown by an assertion statement? Select the one correct answer. a. Depends on the assertion statement. b. FailedAssertion c. AssertionException d. RuntimeException e. AssertionError f. Error |
| 5.27 | What can cause an assertion statement to be ignored? Select the one correct answer. a. Nothing. b. Using appropriate compiler options. c. Using appropriate runtime options. d. Using both appropriate compiler and runtime options. |
| 5.28 | Given the following method, which statements will throw an exception, assuming assertions are enabled? static int inv(int value) { assert value > -50 : value < 100; return 100/value; } Select the two correct answers. a. inv(-50); b. inv(0); c. inv(50); d. inv(100); e. inv(150); |
| 5.29 | Which runtime options would cause assertions to be enabled for the class org.example.ttp.Bottle? Select the two correct answers. a. -ea b. -ea:Bottle c. -ea:org.example d. -ea:org... e. -enableexceptions:org.example.ttp.Bottle f. -ea:org.example.ttp |
| 5.30 | What will be the result of compiling and running the following code with assertions enabled? public class TernaryAssertion { public static void assertBounds(int low, int high, int value) { assert ( value > low ? value < high : false ) : (value < high ? "too low" : "too high" ); } public static void main(String[] args) { assertBounds(100, 200, 150); } } Select the one correct answer. a. The compilation fails because the method name assertBounds cannot begin with the keyword assert. b. The compilation fails because the assert statement is invalid. c. The compilation succeeds and the program runs without errors. d. The compilation succeeds and an AssertionError with the error message "too low" is thrown. e. The compilation succeeds and an AssertionError with the error message "too high" is thrown. |
| 5.31 | Which statements are true about the AssertionError class? Select the two correct answers. a. It is a checked exception. b. It has a method named toString(). c. It has a method named getErrorMessage(). d. It can be caught by a try-catch construct. |
| 5.32 | Which of these classes is the direct superclass of AssertionError? Select the one correct answer. a. Object b. Throwable c. Exception d. Error e. RuntimeError |
| 5.33 | Given the following command, which classes would have assertions enabled? java -ea -da:com... net.example.LaunchTranslator Select the two correct answers. a. com.example.Translator b. java.lang.String c. dot.com.Boom d. net.example.LaunchTranslator e. java.lang.AssertionError |
| 6.1 | Which statements are true? Select the two correct answers. a. In Java the extends clause is used to specify inheritance. b. The subclass of a non-abstract class can be declared abstract. c. All the members of the superclass are inherited by the subclass. d. A final class can be abstract. e. A class in which all the members are declared private, cannot be declared public. |
| 6.2 | Which statements are true? Select the two correct answers. a. Inheritance defines a has-a relationship between a superclass and its subclasses. b. Every Java object has a public method named equals. c. Every Java object has a public method named length. d. A class can extend any number of other classes. e. A non-final class can be extended by any number of classes. |
| 6.3 | Which statements are true? Select the two correct answers. a. A subclass must define all the methods from the superclass. b. It is possible for a subclass to define a method with the same name and parameters as a method defined by the superclass. c. It is possible for a subclass to define a field with the same name as a field defined by the superclass. d. It is possible for two classes to be the superclass of each other. |
| 6.4 | Given the following classes and declarations, which statements are true? // Classes class Foo { private int i; public void f() { /* ... */ } public void g() { /* ... */ } } class Bar extends Foo { public int j; public void g() { /* ... */ } } // Declarations: // ... Foo a = new Foo(); Bar b = new Bar(); // ... Select the three correct answers. a. The Bar class is a legal subclass of Foo. b. The statement b.f(); is legal. c. The statement a.j = 5; is legal. d. The statement a.g(); is legal. e. The statement b.i = 3; is legal. |
| 6.5 | Which statement is true? Select the one correct answer. a. Private methods cannot be overridden in subclasses. b. A subclass can override any method in a superclass. c. An overriding method can declare that it throws more exceptions than the method it is overriding. d. The parameter list of an overriding method must be a subset of the parameter list of the method that it is overriding. e. The overriding method can have a different return type than the overridden method. |
| 6.6 | Given classes A, B, and C, where B extends A, and C extends B, and where all classes implement the instance method void doIt(). How can the doIt() method in A be called from an instance method in C? Select the one correct answer. a. doIt(); b. super.doIt(); c. super.super.doIt(); d. this.super.doIt(); e. A.this.doIt(); f. ((A) this).doIt(); g. It is not possible. |
| 6.7 | What would be the result of attempting to compile and run the following code? // Filename: MyClass.java public class MyClass { public static void main(String[] args) { C c = new C(); System.out.println(c.max(13, 29)); } } class A { int max(int x, int y) { if (x>y) return x; else return y; } } class B extends A{ int max(int x, int y) { return super.max(y, x) - 10; } } class C extends B { int max(int x, int y) { return super.max(x+10, y+10); } } Select the one correct answer. a. The code will fail to compile because the max() method in B passes the arguments in the call super.max(y, x) in the wrong order. b. The code will fail to compile because a call to a max() method is ambiguous. c. The code will compile without errors and will print 13 when run. d. The code will compile without errors and will print 23 when run. e. The code will compile without errors and will print 29 when run. f. The code will compile without errors and will print 39 when run. |
| 6.8 | Given the following code, which is the simplest print statement that can be inserted into the print() method? // Filename: MyClass.java public class MyClass extends MySuperclass { public static void main(String[] args) { MyClass object = new MyClass(); object.print(); } public void print() { // INSERT CODE HERE THAT WILL PRINT // THE "Hello, world!" STRING FROM THE Message // CLASS. } } class MySuperclass { Message msg = new Message(); } class Message { // The message that should be printed: String text = "Hello, world!"; } Select the one correct answer. a. System.out.println(text); b. System.out.println(Message.text); c. System.out.println(msg.text); d. System.out.println(object.msg.text); e. System.out.println(super.msg.text); f. System.out.println(object.super.msg.text); |
| 6.9 | Given the following code, which of these constructors can be added to MySubclass without causing a compile-time error? class MySuper { int number; MySuper(int i) { number = i; } } class MySub extends MySuper { int count; MySub(int cnt, int num) { super(num); count=cnt; } // INSERT ADDITIONAL CONSTRUCTOR HERE } Select the one correct answer. a. MySub() {} b. MySub(int cnt) { count = cnt; } c. MySub(int cnt) { super(); count = cnt; } d. MySub(int cnt) { count = cnt; super(cnt); } e. MySub(int cnt) { this(cnt, cnt); } f. MySub(int cnt) { super(cnt); this(cnt, 0); } |
| 6.10 | Which statement is true? Select the one correct answer. a. A super() or this() call must always be provided explicitly as the first statement in the body of a constructor. b. If both a subclass and its superclass do not have any declared constructors, the implicit default constructor of the subclass will call super() when run. c. If neither super() nor this() is declared as the first statement in the body of a constructor, then this() will implicitly be inserted as the first statement. d. If super() is the first statement in the body of a constructor, then this() can be declared as the second statement. e. Calling super() as the first statement in the body of a constructor of a subclass will always work, since all superclasses have a default constructor. |
| 6.11 | What will the following program print when run? // Filename: MyClass.java public class MyClass { public static void main(String[] args) { B b = new B("Test"); } } class A { A() { this("1", "2"); } A(String s, String t) { this(s + t); } A(String s) { System.out.println(s); } } class B extends A { B(String s) { System.out.println(s); } B(String s, String t) { this(t + s + "3"); } B() { super("4"); }; } Select the one correct answer. a. It will simply print Test. b. It will print Test followed by Test. c. It will print 123 followed by Test. d. It will print 12 followed by Test. e. It will print 4 followed by Test. |
| 6.12 | Which statements are true about interfaces? Select the two correct answers. a. Interfaces allow multiple implementation inheritance. b. Interfaces can be extended by any number of other interfaces. c. Interfaces can extend any number of other interfaces. d. Members of an interface are never static. e. Members of an interface can always be declared static. |
| 6.13 | Which of these field declarations are legal within the body of an interface? Select the three correct answers. a. public static int answer = 42; b. int answer; c. final static int answer = 42; d. public int answer = 42; e. private final static int answer = 42; |
| 6.14 | Which statements are true about interfaces? Select the two correct answers. a. The keyword extends is used to specify that an interface inherits from another interface. b. The keyword extends is used to specify that a class inherits from an interface. c. The keyword implements is used to specify that an interface inherits from another interface. d. The keyword implements is used to specify that a class inherits from an interface. e. The keyword implements is used to specify that a class inherits from another class. |
| 6.15 | Which statement is true about the following code? // Filename: MyClass.java abstract class MyClass implements Interface1, Interface2 { public void f() { } public void g() { } } interface Interface1 { int VAL_A = 1; int VAL_B = 2; void f(); void g(); } interface Interface2 { int VAL_B = 3; int VAL_C = 4; void g(); void h(); } Select the one correct answer. a. Interface1 and Interface2 do not match, therefore, MyClass cannot implement them both. b. MyClass only implements Interface1. Implementation for void h() from Interface2 is missing. c. The declarations of void g() in the two interfaces conflict, therefore, the code will not compile. d. The declarations of int VAL_B in the two interfaces conflict, therefore, the code will not compile. e. Nothing is wrong with the code, it will compile without errors. |
| 6.16 | Given the following code, which declaration can be inserted at the indicated line without causing a compilation error? interface MyConstants { int r = 42; int s = 69; // INSERT CODE HERE } Select the two correct answers. a. final double circumference = 2*Math.PI*r; b. int total = total + r + s; c. int AREA = r*s; d. public static MAIN = 15; e. protected int CODE = 31337; |
| 6.17 | Given the following program, which statement is true? // Filename: MyClass.javapublic class MyClass { public static void main(String[] args) { A[] arrA; B[] arrB; arrA = new A[10]; arrB = new B[20]; arrA = arrB; // (1) arrB = (B[]) arrA; // (2) arrA = new A[10]; arrB = (B[]) arrA; // (3) }}class A {}class B extends A {} Select the one correct answer. a. The program will fail to compile because of the assignment at (1). b. The program will throw a java.lang.ClassCastException in the assignment at (2) when run. c. The program will throw a java.lang.ClassCastException in the assignment at (3) when run. d. The program will compile and run without errors, even if the (B[]) cast in the statements at (2) and (3) is removed. e. The program will compile and run without errors, but will not do so if the (B[]) cast in statements at (2) and (3) is removed. |
| 6.18 | Which is the first line that will cause compilation to fail in the following program? // Filename: MyClass.javaclass MyClass { public static void main(String[] args) { MyClass a; MySubclass b; a = new MyClass(); // (1) b = new MySubclass(); // (2) a = b; // (3) b = a; // (4) a = new MySubclass(); // (5) b = new MyClass(); // (6) }}class MySubclass extends MyClass {} Select the one correct answer. a. Line labeled (1). b. Line labeled (2). c. Line labeled (3). d. Line labeled (4). e. Line labeled (5). f. Line labeled (6). |
| 6.19 | Given the following definitions and reference declarations, which one of the following assignments is legal? // Definitions:interface I1 {}interface I2 {}class C1 implements I1 {}class C2 implements I2 {}class C3 extends C1 implements I2 {}// Reference declarations:// ... C1 obj1; C2 obj2; C3 obj3;// ... Select the one correct answer. a. obj2 = obj1; b. obj3 = obj1; c. obj3 = obj2; d. I1 a = obj2; e. I1 b = obj3; f. I2 c = obj1; |
| 6.20 | Given the following class definitions and the reference declarations, what can be said about the statement y = (Sub) x? // Class definitions:class Super {}class Sub extends Super {}// Reference declarations// ... Super x; Sub y;// ... Select the one correct answer. a. Illegal at compile time. b. Legal at compile time, but might be illegal at runtime. c. Definitely legal at runtime, but the (Sub) cast is not strictly needed. d. Definitely legal at runtime, and the (Sub) cast is needed. |
| 6.21 | Given the following class definitions and declaration statements, which one of these assignments is legal at compile time? // Definitions:interface A {}class B {}class C extends B implements A {}class D implements A {}// Declaration statements:// [...] B b = new B(); C c = new C(); D d = new D();// [...] Select the one correct answer. a. c = d; b. d = c; c. A a = d; d. d = (D) c; e. c = b; |
| 6.22 | Which letters will be printed when the following program is run? // Filename: MyClass.javapublic class MyClass { public static void main(String[] args) { B b = new C(); A a = b; if (a instanceof A) System.out.println("A"); if (a instanceof B) System.out.println("B"); if (a instanceof C) System.out.println("C"); if (a instanceof D) System.out.println("D"); }}class A {}class B extends A {}class C extends B {}class D extends C {} Select the three correct answers. a. A will be printed. b. B will be printed. c. C will be printed. d. D will be printed. |
| 6.23 | Given three classes A, B, and C, where B is a subclass of A and C is a subclass of B, which one of these boolean expressions is true when an object denoted by reference o has actually been instantiated from class B as opposed to from A or C? Select the one correct answer. a. (o instanceof B) && (!(o instanceof A)) b. (o instanceof B) && (!(o instanceof C)) c. !((o instanceof A) || (o instanceof B)) d. (o instanceof B) e. (o instanceof B) && !((o instanceof A) || (o instanceof C)) |
| 6.24 | When the following program is run, it will print all the letters I, J, C, and D. Is this statement true or false? public class MyClass { public static void main(String[] args) { I x = new D(); if (x instanceof I) System.out.println("I"); if (x instanceof J) System.out.println("J"); if (x instanceof C) System.out.println("C"); if (x instanceof D) System.out.println("D"); }}interface I{}interface J{}class C implements I {}class D extends C implements J {} Select the one correct answer. a. True. b. False. |
| 6.25 | What will be the result of attempting to compile and run the following program? public class Polymorphism { public static void main(String[] args) { A ref1 = new C(); B ref2 = (B) ref1; System.out.println(ref2.f()); }}class A { int f() { return 0; } }class B extends A { int f() { return 1; } }class C extends B { int f() { return 2; } } Select the one correct answer. a. The program will fail to compile. b. The program will compile without error, but will throw a ClassCastException when run. c. The program will compile without error and print 0 when run. d. The program will compile without error and print 1 when run. e. The program will compile without error and print 2 when run. |
| 6.26 | What will be the result of attempting to compile and run the following program? public class Polymorphism2 { public static void main(String[] args) { A ref1 = new C(); B ref2 = (B) ref1; System.out.println(ref2.g()); }}class A { private int f() { return 0; } public int g() { return 3; }}class B extends A { private int f() { return 1; } public int g() { return f(); }}class C extends B { public int f() { return 2; }} Select the one correct answer. a. The program will fail to compile. b. The program will compile without error and print 0 when run. c. The program will compile without error and print 1 when run. d. The program will compile without error and print 2 when run. e. The program will compile without error and print 3 when run. |
| 6.27 | Given the following code, which statements are true? public interface HeavenlyBody { String describe(); }class Star { String starName; public String describe() { return "star " + starName; }}class Planet extends Star { String name; public String describe() { return "planet " + name + " orbiting star " + starName; }} Select the two correct answers: a. The code will fail to compile. b. The use of inheritance is justified, since Planet is-a Star. c. The code will fail to compile if the name starName is replaced with the name bodyName throughout the declaration of the Star class. d. The code will fail to compile if the name starName is replaced with the name name throughout the declaration of the Star class. e. An instance of Planet is a valid instance of HeavenlyBody. |
| 6.28 | Given the following code, which statement is true? public interface HeavenlyBody { String describe(); }class Star implements HeavenlyBody { String starName; public String describe() { return "star " + starName; }}class Planet { String name; Star orbiting; public String describe() { return "planet " + name + " orbiting " + orbiting.describe(); }} Select the one correct answer: a. The code will fail to compile. b. The use of aggregation is justified, since Planet has-a Star. c. The code will fail to compile if the name starName is replaced with the name bodyName throughout the declaration of the Star class. d. The code will fail to compile if the name starName is replaced with the name name throughout the declaration of the Star class. e. An instance of Planet is a valid instance of a HeavenlyBody. |
| 7.1 | What will be the result of attempting to compile and run the following code? public class MyClass { public static void main(String[] args) { Outer objRef = new Outer(); System.out.println(objRef.createInner().getSecret()); }}class Outer { private int secret; Outer() { secret = 123; } class Inner { int getSecret() { return secret; } } Inner createInner() { return new Inner(); }} Select the one correct answer. a. The code will fail to compile because the class Inner cannot be declared within the class Outer. b. The code will fail to compile because the method createInner() cannot be allowed to pass objects of the class Inner to methods outside of the class Outer. c. The code will fail to compile because the secret field is not accessible from the method getSecret(). d. The code will fail to compile because the method getSecret() is not visible from the main() method in the class MyClass. e. The code will compile without error and will print 123 when run. |
| 7.2 | Which statements are true about nested classes? Select the two correct answers. a. An instance of a static member class has an inherent outer instance. b. A static member class can contain non-static fields. c. A static member interface can contain non-static fields. d. A static member interface has an inherent outer instance. e. For each instance of the outer class, there can exist many instances of a non-static member class. |
| 7.3 | What will be the result of attempting to compile and run the following code? public class MyClass { public static void main(String[] args) { State st = new State(); System.out.println(st.getValue()); State.Memento mem = st.memento(); st.alterValue(); System.out.println(st.getValue()); mem.restore(); System.out.println(st.getValue()); } public static class State { protected int val = 11; int getValue() { return val; } void alterValue() { val = (val + 7) % 31; } Memento memento() { return new Memento(); } class Memento { int val; Memento() { this.val = State.this.val; } void restore() { ((State) this).val = this.val; } } }} Select the one correct answer. a. The code will fail to compile since the static main() method tries to create a new instance of the static member class State. b. The code will fail to compile since the declaration of class State.Memento is not accessible from the main() method. c. The code will fail to compile since the non-static member class Memento declares a field with the same name as a field in the outer class State. d. The code will fail to compile since the State.this.val expression in the Memento constructor is invalid. e. The code will fail to compile since the ((State) this).val expression in the method restore() of the class Memento is invalid. f. The program compiles without error and prints 11, 18, and 11 when run. |
| 7.4 | What will be the result of attempting to compile and run the following program? public class Nesting { public static void main(String[] args) { B.C obj = new B().new C(); }}class A { int val; A(int v) { val = v; }}class B extends A { int val = 1; B() { super(2); } class C extends A { int val = 3; C() { super(4); System.out.println(B.this.val); System.out.println(C.this.val); System.out.println(super.val); } }} Select the one correct answer. a. The program will fail to compile. b. The program will compile without error and print 2, 3, and 4, in that order, when run. c. The program will compile without error and print 1, 4, and 2, in that order, when run. d. The program will compile without error and print 1, 3, and 4, in that order, when run. e. The program will compile without error and print 3, 2, and 1, in that order, when run. |
| 7.5 | Which statements are true about the following program? public class Outer { public void doIt() { } public class Inner { public void doIt() { } } public static void main(String[] args) { new Outer().new Inner().doIt(); }} Select the two correct answers. a. The doIt() method in the Inner class overrides the doIt() method in the Outer class. b. The doIt() method in the Inner class overloads the doIt() method in the Outer class. c. The doIt() method in the Inner class hides the doIt() method in the Outer class. d. The full name of the Inner class is Outer.Inner. e. The program will fail to compile. |
| 7.6 | Which statement is true? Select the one correct answer. a. Non-static member classes must have either default or public accessibility. b. All nested classes can declare static member classes. c. Methods in all nested classes can be declared static. d. All nested classes can be declared static. e. Static member classes can contain non-static methods. |
| 7.7 | Given the declaration interface IntHolder { int getInt(); } which of the following methods are valid? //----(1)---- IntHolder makeIntHolder(int i) { return new IntHolder() { public int getInt() { return i; } }; } //----(2)---- IntHolder makeIntHolder(final int i) { return new IntHolder { public int getInt() { return i; } }; } //----(3)---- IntHolder makeIntHolder(int i) { class MyIH implements IntHolder { public int getInt() { return i; } } return new MyIH(); } //----(4)---- IntHolder makeIntHolder(final int i) { class MyIH implements IntHolder { public int getInt() { return i; } } return new MyIH(); } //----(5)---- IntHolder makeIntHolder(int i) { return new MyIH(i); } static class MyIH implements IntHolder { final int j; MyIH(int i) { j = i; } public int getInt() { return j; } } Select the two correct answers. a. The method labeled (1). b. The method labeled (2). c. The method labeled (3). d. The method labeled (4). e. The method labeled (5). |
| 7.8 | Which statements are true? Select the two correct answers. a. No other static members, except final static fields, can be declared within a non-static member class. b. If a non-static member class is nested within a class named Outer, then methods within the non-static member class must use the prefix Outer.this to access the members of the class Outer. c. All fields in any nested class must be declared final. d. Anonymous classes cannot have constructors. e. If objRef is an instance of any nested class within the class Outer, then the expression (objRef instanceof Outer) will evaluate to true. |
| 7.9 | Which statement is true? Select the one correct answer. a. Top-level classes can be declared static. b. Classes declared as members of top-level classes can be declared static. c. Local classes can be declared static. d. Anonymous classes can be declared static. e. No classes can be declared static. |
| 8.1 | Which statement is true? Select the one correct answer. a. Objects can be explicitly destroyed using the keyword delete. b. An object will be garbage collected immediately after it becomes unreachable. c. If object obj1 is accessible from object obj2, and object obj2 is accessible from obj1, then obj1 and obj2 are not eligible for garbage collection. d. Once an object has become eligible for garbage collection, it will remain eligible until it is destroyed. e. If object obj1 can access object obj2 that is eligible for garbage collection, then obj1 is also eligible for garbage collection. |
| 8.2 | Identify the location in the following program where the object, initially referenced with arg1, is eligible for garbage collection. public class MyClass { public static void main(String[] args) { String msg; String pre = "This program was called with "; String post = " as first argument."; String arg1 = new String((args.length > 0) ? "'" + args[ 0 ] + "'" : "<no argument>"); msg = arg1; arg1 = null; // (1) msg = pre + msg + post; // (2) pre = null; // (3) System.out.println(msg); msg = null; // (4) post = null; // (5) args = null; // (6) }} Select the one correct answer. a. After the line labeled (1). b. After the line labeled (2). c. After the line labeled (3). d. After the line labeled (4). e. After the line labeled (5). f. After the line labeled (6). |
| 8.3 | Which statement is true? Select the one correct answer. a. If an exception is thrown during the execution of the finalize() method of an eligible object, then the exception is ignored and the object is destroyed. b. All objects have a finalize() method. c. Objects can be destroyed by explicitly calling the finalize() method. d. The finalize() method can be declared with any accessibility. e. The compiler will fail to compile code that defines an overriding finalize() method that does not explicitly call the overridden finalize() method from the superclass. |
| 8.4 | Which statement is true? Select the one correct answer. a. The compiler will fail to compile code that explicitly tries to call the finalize() method. b. The finalize() method must be declared with protected accessibility. c. An overriding finalize() method in any class can always throw checked exceptions. d. The finalize() method can be overloaded. e. The body of the finalize() method can only access other objects that are eligible for garbage collection. |
| 8.5 | Which statement describes guaranteed behavior of the garbage collection and finalization mechanisms? Select the one correct answer. a. Objects will not be destroyed until they have no references to them. b. The finalize() method will never be called more than once on an object. c. An object eligible for garbage collection will eventually be destroyed by the garbage collector. d. If object A became eligible for garbage collection before object B, then object A will be destroyed before object B. e. An object, once eligible for garbage collection, can never become accessible by a live thread. |
| 8.6 | Given the following class, which of these static initializer blocks can be inserted after the comment? public class MyClass { private static int count = 5; final static int STEP = 10; boolean alive; // INSERT STATIC INITIALIZER BLOCK HERE } Select the three correct answers. a. static { alive = true; count = 0; } b. static { STEP = count; } c. static { count += STEP; } d. static ; e. static {;} f. static { count = 1; } |
| 8.7 | What will be the result of attempting to compile and run the following code? public class MyClass { public static void main(String[] args) { MyClass obj = new MyClass(l); } static int i = 5; static int l; int j = 7; int k; public MyClass(int m) { System.out.println(i + ", " + j + ", " + k + ", " + l + ", " + m); } { j = 70; l = 20; } // Instance Initializer Block static { i = 50; } // Static Initializer Block } Select the one correct answer. a. The code will fail to compile, since the instance initializer block tries to assign a value to a static field. b. The code will fail to compile, since the field k will be uninitialized when it is used. c. The code will compile without error and will print 50, 70, 0, 20, 0 when run. d. The code will compile without error and will print 50, 70, 0, 20, 20 when run. e. The code will compile without error and will print 5, 70, 0, 20, 0 when run. f. The code will compile without error and will print 5, 7, 0, 20, 0 when run. |
| 8.8 | Given the following class, which instance initializer block inserted at the indicated location will allow the class to compile without errors? public class MyClass { static int gap = 10; double length; final boolean active; // INSERT CODE HERE } Select the one correct answer. a. instance { active = true; } b. MyClass { gap += 5; } c. { gap = 5; length = (active ? 100 : 200) + gap; } d. { ; } e. { length = 4.2; } f. { active = (gap > 5); length = 5.5 + gap;} |
| 8.9 | What will be the result of attempting to compile and run the following program? public class Initialization { private static String msg(String msg) { System.out.println(msg); return msg; } public Initialization() { m = msg("1"); } { m = msg("2"); } String m = msg("3"); public static void main(String[] args) { Object obj = new Initialization(); } } Select the one correct answer. a. The program will fail to compile. b. The program will compile without error and will print 1, 2, and 3 when run. c. The program will compile without error and will print 2, 3, and 1 when run. d. The program will compile without error and will print 3, 1, and 2 when run. e. The program will compile without error and will print 1, 3, and 2 when run. |
| 8.10 | What will be the result of attempting to compile and run the following program? public class Initialization { private static String msg(String msg) { System.out.println(msg); return msg; } static String m = msg("1"); { m = msg("2"); } static { m = msg("3"); } public static void main(String[] args) { Object obj = new Initialization(); }} Select the one correct answer. a. The program will fail to compile. b. The program will compile without error and will print 1, 2, and 3 when run. c. The program will compile without error and will print 2, 3, and 1 when run. d. The program will compile without error and will print 3, 1, and 2 when run. e. The program will compile without error and will print 1, 3, and 2 when run. |
| 8.11 | Which of the labeled lines in the following code can be uncommented by removing the // characters and still allow the code to compile correctly? class GeomInit { // int width = 14; /* Line A */ { // area = width * height; /* Line B */ } int width = 37; { // height = 11; /* Line C */ } int height, area; // area = width * height; /* Line D */ { // int width = 15; /* Line E */ area = 100; } }; Select the two correct answers. a. Line A b. Line B c. Line C d. Line D e. Line E |
| 9.1 | Which is the correct way to start a new thread? Select the one correct answer. a. Just create a new Thread object. The thread will start automatically. b. Create a new Thread object and call the method begin(). c. Create a new Thread object and call the method start(). d. Create a new Thread object and call the method run(). e. Create a new Thread object and call the method resume(). |
| 9.2 | When extending the Thread class to provide a thread's behavior, which method should be overridden? Select the one correct answer. a. begin() b. start() c. run() d. resume() e. behavior() |
| 9.3 | Which statements are true? Select the two correct answers. a. The class Thread is abstract. b. The class Thread implements Runnable. c. Classes implementing the Runnable interface must define a method named start. d. Calling the method run() on an object implementing Runnable will create a new thread. e. A program terminates when the last non-daemon thread ends. |
| 9.4 | What will be the result of attempting to compile and run the following program? public class MyClass extends Thread { public MyClass(String s) { msg = s; } String msg; public void run() { System.out.println(msg); } public static void main(String[] args) { new MyClass("Hello"); new MyClass("World"); }} Select the one correct answer. a. The program will fail to compile. b. The program will compile without errors and will print Hello and World, in that order, every time the program is run. c. The program will compile without errors and will print a never-ending stream of Hello and World. d. The program will compile without errors and will print Hello and World when run, but the order is unpredictable. e. The program will compile without errors and will simply terminate without any output when run. |
| 9.5 | Given the following program, which statements are guaranteed to be true? public class ThreadedPrint { static Thread makeThread(final String id, boolean daemon) { Thread t = new Thread(id) { public void run() { System.out.println(id); } }; t.setDaemon(daemon); t.start(); return t; } public static void main(String[] args) { Thread a = makeThread("A", false); Thread b = makeThread("B", true); System.out.print("End\n"); } } Select the two correct answers. a. The letter A is always printed. b. The letter B is always printed. c. The letter A is never printed after End. d. The letter B is never printed after End. e. The program might print B, End and A, in that order. |
| 9.6 | Which statement is true? Select the one correct answer. a. No two threads can concurrently execute synchronized methods on the same object. b. Methods declared synchronized should not be recursive, since the object lock will not allow new invocations of the method. c. Synchronized methods can only call other synchronized methods directly. d. Inside a synchronized method, one can assume that no other threads are currently executing any other methods in the same class. |
| 9.7 | Given the following program, which statement is true? public class MyClass extends Thread { static Object lock1 = new Object(); static Object lock2 = new Object(); static volatile int i1, i2, j1, j2, k1, k2; public void run() { while (true) { doit(); check(); } } void doit() { synchronized(lock1) { i1++; } j1++; synchronized(lock2) { k1++; k2++; } j2++; synchronized(lock1) { i2++; } } void check() { if (i1 != i2) System.out.println("i"); if (j1 != j2) System.out.println("j"); if (k1 != k2) System.out.println("k"); } public static void main(String[] args) { new MyClass().start(); new MyClass().start(); } } Select the one correct answer. a. The program will fail to compile. b. One cannot be certain whether any of the letters i, j, and k will be printed during execution. c. One can be certain that none of the letters i, j, and k will ever be printed during execution. d. One can be certain that the letters i and k will never be printed during execution. e. One can be certain that the letter k will never be printed during execution. |
| 9.8 | Which one of these events will cause a thread to die? Select the one correct answer. a. The method sleep() is called. b. The method wait() is called. c. Execution of the start() method ends. d. Execution of the run() method ends. e. Execution of the thread's constructor ends. |
| 9.9 | Which statements are true about the following code? public class Joining { static Thread createThread(final int i, final Thread t1) { Thread t2 = new Thread() { public void run() { System.out.println(i+1); try { t1.join(); } catch (InterruptedException e) { } System.out.println(i+2); } }; System.out.println(i+3); t2.start(); System.out.println(i+4); return t2; } public static void main(String[] args) { createThread(10, createThread(20, Thread.currentThread())); } } Select the two correct answers. a. The first number printed is 13. b. The number 14 is printed before the number 22. c. The number 24 is printed before the number 21. d. The last number printed is 12. e. The number 11 is printed before the number 23. |
| 9.10 | What can be guaranteed by calling the method yield()? Select the one correct answer. a. All lower priority threads will be granted CPU time. b. The current thread will sleep for some time while some other threads run. c. The current thread will not continue until other threads have terminated. d. The thread will wait until it is notified. e. None of the above. |
| 9.11 | Where is the notify() method defined? Select the one correct answer. a. Thread b. Object c. Applet d. Runnable |
| 9.12 | How can the priority of a thread be set? Select the one correct answer. a. By using the setPriority() method in the class Thread. b. By passing the priority as a parameter to the constructor of the thread. c. Both of the above. d. None of the above. |
| 9.13 | Which statements are true about locks? Select the two correct answers. a. A thread can hold more than one lock at a time. b. Invoking wait() on a Thread object will relinquish all locks held by the thread. c. Invoking wait() on an object whose lock is held by the current thread will relinquish the lock. d. Invoking notify() on a object whose lock is held by the current thread will relinquish the lock. e. Multiple threads can hold the same lock at the same time. |
| 9.14 | What will be the result of invoking the wait() method on an object without ensuring that the current thread holds the lock of the object? Select the one correct answer. a. The code will fail to compile. b. Nothing special will happen. c. An IllegalMonitorStateException will be thrown if the wait() method is called while the current thread does not hold the lock of the object. d. The thread will be blocked until it gains the lock of the object. |
| 9.15 | Which of these are plausible reasons why a thread might be alive, but still not be running? Select the four correct answers. a. The thread is waiting for some condition as a result of a wait() call. b. The execution has reached the end of the run() method. c. The thread is waiting to acquire the lock of an object in order to execute a certain method on that object. d. The thread does not have the highest priority and is currently not executing. e. The thread is sleeping as a result of a call to the sleep() method. |
| 10.1 | What is the return type of the hashCode() method in the Object class? Select the one correct answer. a. String b. int c. long d. Object e. Class |
| 10.2 | Which statement is true? Select the one correct answer. a. If the references x and y denote two different objects, then the expression x.equals(y) is always false. b. If the references x and y denote two different objects, then the expression (x.hashCode() == y.hashCode()) is always false. c. The hashCode() method in the Object class is declared final. d. The equals() method in the Object class is declared final. e. All arrays have a method named clone. |
| 10.3 | Which exception can the clone() method of the Object class throw? Select the one correct answer. a. CloneNotSupportedException b. NotCloneableException c. IllegalCloneException d. NoClonesAllowedException |
| 10.4 | Which of the following are wrapper classes? Select the three correct answers. a. java.lang.Void b. java.lang.Int c. java.lang.Boolean d. java.lang.Long e. java.lang.String |
| 10.5 | Which of the following classes do not extend the java.lang.Number class? Select the two correct answers. a. java.lang.Float b. java.lang.Byte c. java.lang.Character d. java.lang.Boolean e. java.lang.Short |
| 10.6 | Which of these classes define immutable objects? Select the three correct answers. a. Character b. Byte c. Thread d. Short e. Object |
| 10.7 | Which of these classes have a one-parameter constructor taking a string? Select the two correct answers. a. Void b. Integer c. Boolean d. Character e. Object |
| 10.8 | Which of the wrapper classes have a booleanValue() method? Select the one correct answer. a. All wrapper classes. b. All wrapper classes except Void. c. All wrapper classes that also implement the compareTo() method. d. All wrapper classes extending Number. e. Only the class Boolean. |
| 10.9 | Which statements are true about wrapper classes? Select the two correct answers. a. String is a wrapper class. b. Double has a compareTo() method. c. Character has a intValue() method. d. Byte extends Number. |
| 10.10 | Given the following program, which lines will print 11 exactly? class MyClass { public static void main(String[] args) { double v = 10.5; System.out.println(Math.ceil(v)); // (1) System.out.println(Math.round(v)); // (2) System.out.println(Math.floor(v)); // (3) System.out.println((int) Math.ceil(v)); // (4) System.out.println((int) Math.floor(v)); // (5) } } Select the two correct answers. a. The line labeled (1). b. The line labeled (2). c. The line labeled (3). d. The line labeled (4). e. The line labeled (5). |
| 10.11 | Which method is not defined in the Math class? Select the one correct answer. a. double tan2(double) b. double cos(double) c. int abs(int a) d. double ceil(double) e. float max(float, float) |
| 10.12 | What is the return type of the method round(float) from the Math class? Select the one correct answer. a. int b. float c. double d. Integer e. Float |
| 10.13 | What is the return type of the method ceil(double) from the Math class? Select the one correct answer. a. int b. float c. double d. Integer e. Double |
| 10.14 | What will the following program print when run? public class Round { public static void main(String[] args) { System.out.println(Math.round(-0.5) + " " + Math.round(0.5)); } }; Select the one correct answer. a. 0 0 b. 0 1 c. -1 0 d. -1 1 e. None of the above. |
| 10.15 | Which statements are true about the expression ((int)(Math.random()*4))? Select the three correct answers. a. It may evaluate to a negative number. b. It may evaluate to the number 0. c. The probability of it evaluating to the number 1 or the number 2 is the same. d. It may evaluate to the number 3. e. It may evaluate to the number 4. |
| 10.16 | Which of the following operators cannot be used in conjunction with a String object? Select the two correct answers. a. + b. - c. += d. . e. & |
| 10.17 | Which expression will extract the substring "kap" from a string defined by String str = "kakapo"? Select the one correct answer. a. str.substring(2, 2) b. str.substring(2, 3) c. str.substring(2, 4) d. str.substring(2, 5) e. str.substring(3, 3) |
| 10.18 | What will be the result of attempting to compile and run the following code? class MyClass { public static void main(String[] args) { String str1 = "str1"; String str2 = "str2"; String str3 = "str3"; str1.concat(str2); System.out.println(str3.concat(str1)); } } Select the one correct answer. a. The code will fail to compile since the expression str3.concat(str1) will not result in a valid argument for the println() method. b. The program will print str3str1str2 when run. c. The program will print str3 when run. d. The program will print str3str1 when run. e. The program will print str3str2 when run. |
| 10.19 | What function does the trim() method of the String class perform? Select the one correct answer. a. It returns a string where the leading white space of the original string has been removed. b. It returns a string where the trailing white space of the original string has been removed. c. It returns a string where both the leading and trailing white space of the original string has been removed. d. It returns a string where all the white space of the original string has been removed. e. None of the above. |
| 10.20 | Which statements are true? Select the two correct answers. a. String objects are immutable. b. Subclasses of the String class can be mutable. c. All wrapper classes are declared final. d. All objects have a public method named clone(). e. The expression ((new StringBuffer()) instanceof String) is always true. |
| 10.21 | Which of these expressions are legal? Select the four correct answers. a. "co".concat("ol") b. ("co" + "ol") c. ('c' + 'o' + 'o' + 'l') d. ("co" + new String('o' + 'l')) e. ("co" + new String("co")) |
| 10.22 | What will be the result of attempting to compile and run the following code? public class RefEq { public static void main(String[] args) { String s = "ab" + "12"; String t = "ab" + 12; String u = new String("ab12"); System.out.println((s==t) + " " + (s==u)); } } Select the one correct answer. a. The code will fail to compile. b. The program will print false false when run. c. The program will print false true when run. d. The program will print true false when run. e. The program will print true true when run. |
| 10.23 | Which of these parameter lists have a corresponding constructor in the String class? Select the three correct answers. a. () b. (int capacity) c. (char[] data) d. (String str) |
| 10.24 | Which method is not defined in the String class? Select the one correct answer. a. trim() b. length() c. concat(String) d. hashCode() e. reverse() |
| 10.25 | Which statement concerning the charAt() method of the String class is true? Select the one correct answer. a. The charAt() method takes a char value as an argument. b. The charAt() method returns a Character object. c. The expression ("abcdef").charAt(3) is illegal. d. The expression "abcdef".charAt(3) evaluates to the character 'd'. e. The index of the first character is 1. |
| 10.26 | Which expression will evaluate to true? Select the one correct answer. a. "hello: there!".equals("hello there") b. "HELLO THERE".equals("hello there") c. ("hello".concat("there")).equals("hello there") d. "Hello There".compareTo("hello there") == 0 e. "Hello there".toLowerCase().equals("hello there") |
| 10.27 | What will the following program print when run? public class Search { public static void main(String[] args) { String s = "Contentment!"; int middle = s.length()/2; String nt = s.substring(middle-1, middle+1); System.out.println(s.lastIndexOf(nt, middle)); } }; Select the one correct answer. a. 2 b. 4 c. 5 d. 7 e. 9 f. 11 |
| 10.28 | What will be the result of attempting to compile and run the following program? public class MyClass { public static void main(String[] args) { String s = "hello"; StringBuffer sb = new StringBuffer(s); sb.reverse(); if (s == sb) System.out.println("a"); if (s.equals(sb)) System.out.println("b"); if (sb.equals(s)) System.out.println("c"); } } Select the one correct answer. a. The code will fail to compile since the constructor of the String class is not called properly. b. The code will fail to compile since the expression (s == sb) is illegal. c. The code will fail to compile since the expression (s.equals(sb)) is illegal. d. The program will print c when run. e. The program will throw a ClassCastException when run. |
| 10.29 | What will be the result of attempting to compile and run the following program? public class MyClass { public static void main(String[] args) { StringBuffer sb = new StringBuffer("have a nice day"); sb.setLength(6); System.out.println(sb); } } Select the one correct answer. a. The code will fail to compile since there is no method named setLength in the StringBuffer class. b. The code will fail to compile since the StringBuffer reference sb is not a legal argument to the println() method. c. The program will throw a StringIndexOutOfBoundsException when run. d. The program will print have a nice day when run. e. The program will print have a when run. f. The program will print ce day when run. |
| 10.30 | Which of these parameter lists have a corresponding constructor in the StringBuffer class? Select the three correct answers. a. () b. (int capacity) c. (char[] data) d. (String str) |
| 10.31 | Which method is not defined in the StringBuffer class? Select the one correct answer. a. trim() b. length() c. append(String) d. reverse() e. setLength(int) |
| 10.32 | What will be the result of attempting to compile and run the following code? public class StringMethods { public static void main(String[] args) { String str = new String("eenny"); str.concat(" meeny"); StringBuffer strBuf = new StringBuffer(" miny"); strBuf.append(" mo"); System.out.println(str + strBuf); } } Select the one correct answer. a. The code will fail to compile. b. The program will print eenny meeny miny mo when run. c. The program will print meeny miny mo when run. d. The program will print eenny miny mo when run. e. The program will print eenny meeny miny when run. |
| 11.1 | Which of these are core interfaces in the collections framework? Select the three correct answers. a. Set b. Bag c. LinkedList d. Collection e. Map |
| 11.2 | Which of these implementations are provided by the java.util package? Select the two correct answers. a. HashList b. HashMap c. ArraySet d. ArrayMap e. TreeMap |
| 11.3 | What is the name of the interface used to represent collections that maintain non-unique elements in order? Select the one correct answer. a. Collection b. Set c. SortedSet d. List e. Sequence |
| 11.4 | Which statements are true about collections? Select the two correct answers. a. Some operations on a collection may throw an UnsupportedOperationException. b. Methods calling optional operations in a collection must either catch an UnsupportedOperationException or declare it in their throws clause. c. A List can have duplicate elements. d. An ArrayList can only accommodate a fixed number of elements. e. The Collection interface contains a method named get. |
| 11.5 | What will be the result of attempting to compile and run the following program? import java.util.*; public class Sets { public static void main(String[] args) { HashSet set1 = new HashSet(); addRange(set1, 1); ArrayList list1 = new ArrayList(); addRange(list1, 2); TreeSet set2 = new TreeSet(); addRange(set2, 3); LinkedList list2 = new LinkedList(); addRange(list2, 5); set1.removeAll(list1); list1.addAll(set2); list2.addAll(list1); set1.removeAll(list2); System.out.println(set1); } static void addRange(Collection col, int step) { for (int i = step*2; i<=25; i+=step) col.add(new Integer(i)); } } Select the one correct answer. a. The program will fail to compile since operations are performed on incompatible collection implementations. b. The program will fail to compile since the TreeSet denoted by set2 has not been given a Comparator to use when sorting its elements. c. The program will compile without error, but will throw an UnsupportedOperationException when run. d. The program will compile without error and will print all primes below 25 when run. e. The program will compile without error and will print some other sequence of numbers when run. |
| 11.6 | Which of these methods are defined in the Collection interface? Select the three correct answers. a. add(Object o) b. retainAll(Collection c) c. get(int index) d. iterator() e. indexOf(Object o) |
| 11.7 | What will be the output from the following program? import java.util.*; public class Iterate { public static void main(String[] args) { List l = new ArrayList(); l.add("A"); l.add("B"); l.add("C"); l.add("D"); l.add("E"); ListIterator i = l.listIterator(); i.next(); i.next(); i.next(); i.next(); i.remove(); i.previous(); i.previous(); i.remove(); System.out.println(l); }; }; Select the one correct answer. a. It will print [A, B, C, D, E]. b. It will print [A, C, E]. c. It will print [B, D, E]. d. It will print [A, B, D]. e. It will print [B, C, E]. f. It will print throw a NoSuchElementException. |
| 11.8 | Which of these methods from the Collection interface will return the value true if the collection was modified during the operation? Select the two correct answers. a. contains() b. add() c. containsAll() d. retainAll() e. clear() |
| 11.9 | Which of these methods can be called on objects implementing the Map interface?Select the two correct answers. a. contains(Object o) b. addAll(Collection c) c. remove(Object o) d. values() e. toArray() |
| 11.10 | Which statements are true about maps?Select the two correct answers. a. The return type of the values() method is Set. b. Changes made in the set view returned by keySet() will be reflected in the original map. c. The Map interface extends the Collection interface. d. All keys in a map are unique. e. All Map implementations keep the keys sorted. |
| 11.11 | Which sequence of digits will the following program print?import java.util.*;public class Lists { public static void main(String[] args) { List list = new ArrayList(); list.add("1"); list.add("2"); list.add(1, "3"); List list2 = new LinkedList(list); list.addAll(list2); list2 = list.subList(2, 5); list2.clear(); System.out.println(list); }}Select the one correct answer. a. [1, 3, 2] b. [1, 3, 3, 2] c. [1, 3, 2, 1, 3, 2] d. [3, 1, 2] e. [3, 1, 1, 2] f. None of the above. |
| 11.12 | Which of these classes have a comparator() method?Select the two correct answers. a. ArrayList b. HashMap c. TreeSet d. HashSet e. TreeMap |
| 11.13 | Which method prototypes are defined in the interface java.util.Map.Entry?Select the two correct answers. a. Object getKey() b. Object setKey(Object value) c. void remove() d. Object getValue() e. void setValue(Object value) |
| 11.14 | Given that the objects denoted by the parameters override the equals() and the hashCode() methods appropriately, which return values are possible from the following method?String func(Object x, Object y) { return (x == y) + " " + x.equals(y) + " " + (x.hashCode() == y.hashCode());}Select the two correct answers. a. "false false true" b. "false true false" c. "false true true" d. "true false false" e. "true false true" |
| 11.15 | Insert code into the equalsImpl() method in order to provide a correct implementation of the equals() method.public class Pair { int a, b; public Pair(int a, int b) { this.a = a; this.b = b; } public boolean equals(Object o) { return (this == o) || (o instanceof Pair) && equalsImpl((Pair) o); } private boolean equalsImpl(Pair o) { // ... PROVIDE IMPLEMENTATION HERE ... }}Select the three correct answers. a. return a == o.a || b == o.b; b. return false; c. return a >= o.a; d. return a == o.a; e. return a == o.a && b == o.b; |
| 11.16 | Which collection implementation is thread-safe?Select the one correct answer. a. ArrayList b. HashSet c. Vector d. TreeSet e. LinkedList |
| 11.17 | Which code provides a correct implementation of the hashCode() method in the following program?import java.util.*;public class Measurement { int count; int accumulated; public Measurement() {} public void record(int v) { count++; accumulated += v; } public int average() { return accumulated/count; } public boolean equals(Object other) { if (this == other) return true; if (!(other instanceof Measurement)) return false; Measurement o = (Measurement) other; if (count != 0 && o.count != 0) return average() == o.average(); return count == o.count; } public int hashCode() { // ... PROVIDE IMPLEMENTATION HERE ... }}Select the two correct answers. a. return 31337; b. return accumulated / count; c. return (count << 16) ^ accumulated; d. return ~accumulated; e. return count == 0 ? 0 : average(); |
|
rahulrastogi |
Latest page update: made by rahulrastogi
, Sep 14 2007, 1:32 AM EDT
(about this update
About This Update
358 words added 1936 words deleted view changes - complete history) |
|
Keyword tags:
None
More Info: links to this page
|