exam study

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code? String element = "red"; for (int i = 0; i < list.size(); i++) if (list.get(i).equals(element)) { list.remove(element); i--; } A. {"red", "red", "green"} B. {"red", "green"} C. {"green"} D. {}

C

Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code? String element = "red"; for (int i = list.size() - 1; i >= 0; i--) if (list.get(i).equals(element)) list.remove(element); A. {"red", "red", "green"} B. {"red", "green"} C. {"green"} D. {}

C

Suppose an ArrayList list contains {"red", "green", "red", "green"}. What is the list after the following code? list.remove("red"); A. {"red", "green", "red", "green"} B. {"green", "red", "green"} C. {"green", "green"} D. {"red", "green", "green"}

B

Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code? String element = "red"; for (int i = 0; i < list.size(); i++) if (list.get(i).equals(element)) list.remove(element); A. {"red", "red", "green"} B. {"red", "green"} C. {"green"} D. {}

B

Show the output of the following code: String[] array = {"red", "green", "blue"}; ArrayList<String> list = new ArrayList<>(Arrays.asList(array)); list.add(0, "red"); System.out.println(list); A. ["red", "green", "blue", "red"] B. ["red", "green", "blue"] C. ["red", "red", "green", "blue"] D. ["red", "green", "red", "blue"]

C

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]? A. x.add("Chicago") B. x.add(0, "Chicago") C. x.add(1, "Chicago") D. x.add(2, "Chicago")

C

Invoking _________ returns the first element in an ArrayList x. A. x.first() B. x.get(0) C. x.get(1) D. x.get()

B

Object-oriented programming allows you to derive new classes from existing classes. This is called ____________. A. encapsulation B. inheritance C. abstraction D. generalization

B

An instance of _________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. A. RuntimeException B. Exception C. Error D. Throwable E. NumberFormatException

A

Analyze the following code. // Program 1 public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(((A)a1).equals((A)a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } // Program 2 public class Test { public static void main(String[] args) { A a1 = new A(); A a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } A. Program 1 displays true and Program 2 displays true B. Program 1 displays false and Program 2 displays true C. Program 1 displays true and Program 2 displays false D. Program 1 displays false and Program 2 displays false

A

Analyze the following code: public class Test { public static void main(String[] args) throws MyException { System.out.println("Welcome to Java"); } } class MyException extends Error { } A. You should not declare a class that extends Error, because Error raises a fatal error that terminates the program. B. You cannot declare an exception in the main method. C. You declared an exception in the main method, but you did not throw it. D. The program has a compile error.

A

Analyze the following program. public class Test { public static void main(String[] args) { try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (Exception ex) { System.out.println(ex); } } } A. An exception is raised due to Integer.parseInt(s); B. An exception is raised due to 2 / i; C. The program has a compile error. D. The program compiles and runs without exceptions.

A

Assume Cylinder is a subtype of Circle. Analyze the following code: Circle c = new Circle (5); Cylinder c = cy; A. The code has a compile error. B. The code has a runtime error. C. The code is fine.

A

Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ___________. A. true B. false

A

What exception type does the following program throw? public class Test { public static void main(String[] args) { System.out.println(1 / 0); } } A. ArithmeticException B. ArrayIndexOutOfBoundsException C. StringIndexOutOfBoundsException D. ClassCastException E. No exception

A

What is displayed on the console when running the following program? public class Test { public static void main(String[] args) { try { p(); System.out.println("After the method call"); } catch (NumberFormatException ex) { System.out.println("NumberFormatException"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } } static void p() { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } } A. The program displays NumberFormatException. B. The program displays NumberFormatException followed by After the method call. C. The program displays NumberFormatException followed by RuntimeException. D. The program has a compile error. E. The program displays RuntimeException.

A

What is the output of the following code? public class Test { public static void main(String[] args) { Object o1 = new Object(); Object o2 = new Object(); System.out.print((o1 == o2) + " " + (o1.equals(o2))); } } A. false false B. true true C. false true D. true false

A

What is the output of the following code? public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson(); } } class Student extends Person { private String getInfo() { return "Student"; } } class Person { private String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } } A. Person Person B. Person Student C. Student Student D. Student Person

A

Which of the following is incorrect? A. A constructor may be static. B. A constructor may be private. C. A constructor may invoke a static method. D. A constructor may invoke an overloaded constructor. E. A constructor invokes its superclass no-arg constructor by default if a constructor does not invoke an overloaded constructor or its superclass's constructor.

A

Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; } A. An object of B contains data fields i, j, k, m. B. An object of B contains data fields j, k, m. C. An object of B contains data fields j, m. D. An object of B contains data fields k, m.

A

What are the reasons to create an instance of the File class? Please select all that apply. A. To determine whether the file exists. B. To obtain the properties of the file such as whether the file can be read, written, or is hidden. C. To rename the file. D. To delete the file. E. To read/write data from/to a file

ABCD

Which of the following statements are true? Please select all that apply. A. Override the equals(Object) method in the Object class whenever possible. B. Override the toString() method in the Object class whenever possible. C. A public default no-arg constructor is assumed if no constructors are defined explicitly. D. You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods.

ABCD

Which of the following statements are true? Please select all that apply. A. You use the keyword throws to declare exceptions in the method heading. B. A method may declare to throw multiple exceptions. C. To throw an exception, use the key word throw. D. If a checked exception occurs in a method, it must be either caught or declared to be thrown from the method.

ABCD

Which of the following statements are true? Please select all that apply. A. To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. B. Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. C. It is a compile error if two methods differ only in return type in the same class. D. A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. E. A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.

ABCDE

Analyze the following code. Please select all that apply. ArrayList<String> list = new ArrayList<>(); list.add("Beijing"); list.add("Tokyo"); list.add("Shanghai"); list.set(3, "Hong Kong"); A. The last line in the code causes a runtime error because there is no element at index 3 in the array list. B. The last line in the code has a compile error because there is no element at index 3 in the array list. C. If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine. D. If you replace the last line by list.add(4, "Hong Kong"), the code will compile and run fine.

AC

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to become [Beijing]? Please select all that apply. A. x.remove("Singapore") B. x.remove(0) C. x.remove(1) D. x.remove(2)

AC

Instances of _________ are unchecked exceptions. Please select all that apply. A. RuntimeException B. Exception C. Error D. Throwable E. NumberFormatException

ACE

A method must declare to throw ________. A. unchecked exceptions B. checked exceptions C. Error D. RuntimeException

B

An instance of _________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program. A. RuntimeException B. Exception C. Error D. Throwable E. NumberFormatException

B

Analyze the following code. // Program 1: public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } // Program 2: public class Test { public static void main(String[] args) { A a1 = new A(); A a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } A. Program 1 displays true and Program 2 displays true B. Program 1 displays false and Program 2 displays true C. Program 1 displays true and Program 2 displays false D. Program 1 displays false and Program 2 displays false

B

Analyze the following code: public class Test { public static void main(String[] args) { new B(); } } class A { int i = 7; public A() { System.out.println("i from A is " + i); } public void setI(int i) { this.i = 2 * i; } } class B extends A { public B() { setI(20); // System.out.println("i from B is " + i); } @Override public void setI(int i) { this.i = 3 * i; } } A. The constructor of class A is not called. B. The constructor of class A is called and it displays "i from A is 7". C. The constructor of class A is called and it displays "i from A is 40". D. The constructor of class A is called and it displays "i from A is 60".

B

Analyze the following code: public class Test { public static void main(String[] args) { try { int zero = 0; int y = 2 / zero; try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException } catch(Exception e) { } } catch(RuntimeException e) { System.out.println(e); } } } A. A try-catch block cannot be embedded inside another try-catch block. B. A good programming practice is to avoid nesting try-catch blocks, because nesting makes programs difficult to read. You can rewrite the program using only one try-catch block. C. The program has a compile error because Exception appears before RuntimeException. D. None of the above.

B

Given the following classes and their objects: class C1 {}; class C2 extends C1 {}; class C3 extends C1 {}; C2 c2 = new C2(); C3 c3 = new C3(); Analyze the following statement: c2 = (C2)((C1)c3); A. c3 is cast into c2 successfully. B. You will get a runtime error because you cannot cast objects from sibling classes. C. You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form. D. The statement is correct.

B

Inheritance means ______________. A. that data fields should be declared private B. that a class can extend another class C. that a variable of supertype can refer to a subtype object D. that a class can contain another class

B

Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code: class Square extends GeometricObject { double length; Square(double length) { GeometricObject(length); } } A. The program compiles fine, but you cannot create an instance of Square because the constructor does not specify the length of the Square. B. The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally. C. The program compiles fine, but it has a runtime error because of invoking the Square class's constructor illegally.

B

The equals method is defined in the Object class. Which of the following is correct to override it in the String class? A. public boolean equals(String other) B. public boolean equals(Object other) C. public static boolean equals(String other) D. public static boolean equals(Object other)

B

The getValue() method is overridden in two ways. Which one is correct? I: public class Test { public static void main(String[] args) { A a = new A(); System.out.println(a.getValue()); } } class B { public String getValue() { return "Any object"; } } class A extends B { public Object getValue() { return "A string"; } } II: public class Test { public static void main(String[] args) { A a = new A(); System.out.println(a.getValue()); } } class B { public Object getValue() { return "Any object"; } } class A extends B { public String getValue() { return "A string"; } } A. I B. II C. Both I and II D. Neither

B

The visibility of these modifiers increases in this order: A. private, protected, none (if no modifier is used), and public. B. private, none (if no modifier is used), protected, and public. C. none (if no modifier is used), private, protected, and public. D. none (if no modifier is used), protected, private, and public.

B

What exception type does the following program throw? public class Test { public static void main(String[] args) { int[] list = new int[5]; System.out.println(list[5]); } } A. ArithmeticException B. ArrayIndexOutOfBoundsException C. StringIndexOutOfBoundsException D. ClassCastException E. No exception

B

What is displayed on the console when running the following program? public class Test { public static void main (String[] args) { try { System.out.println("Welcome to Java"); return; } finally { System.out.println("The finally clause is executed"); } } } A. Welcome to Java B. Welcome to Java followed by The finally clause is executed in the next line C. The finally clause is executed D. None of the above

B

What is displayed on the console when running the following program? public class Test { public static void main (String[] args) { try { System.out.println("Welcome to Java"); } finally { System.out.println("The finally clause is executed"); } } } A. Welcome to Java B. Welcome to Java followed by The finally clause is executed in the next line C. The finally clause is executed D. None of the above

B

What is displayed on the console when running the following program? public class Test { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to HTML"); } finally { System.out.println("The finally clause is executed"); } } } A. Welcome to Java, then an error message. B. Welcome to Java followed by The finally clause is executed in the next line, then an error message. C. The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed, then an error message. D. None of the above.

B

What is displayed on the console when running the following program? public class Test { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } A. The program displays Welcome to Java three times followed by End of the block. B. The program displays Welcome to Java two times followed by End of the block. C. The program displays Welcome to Java three times. D. The program displays Welcome to Java two times.

B

What is the output of the following code? ArrayList<String> list = new ArrayList<>(); String s1 = new String("Java"); String s2 = new String("Java"); list.add(s1); list.add(s2); System.out.println((list.get(0) == list.get(1)) + " " + (list.get(0)).equals(list.get(1))); A. true false B. false true C. true true D. false false

B

What is the output of the following code? public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson(); } } class Student extends Person { @Override public String getInfo() { return "Student"; } } class Person { public String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } } A. Person Person B. Person Student C. Student Student D. Student Person

B

What is wrong in the following program? public class Test { public static void main (String[] args) { try { System.out.println("Welcome to Java"); } } } A. You cannot have a try block without a catch block. B. You cannot have a try block without a catch block or a finally block. C. A method call that does not declare exceptions cannot be placed inside a try block. D. Nothing is wrong.

B

Which class do you use to write data into a text file? A. File B. PrintWriter C. Scanner D. System

B

Which of the following are Java keywords? A. instanceOf B. instanceof C. cast D. casting

B

Which of the following is not an advantage of Java exception handling? A. Java separates exception handling from normal processing tasks. B. Exception handling improves performance. C. Exception handling makes it possible for the caller's caller to handle the exception. D. Exception handling simplifies programming because the error-reporting and error-handling code can be placed at the catch block.

B

Which of the following is used to test if a file c:\temp\test.txt exists? A. new File("c:\temp\test.txt").exists() B. new File("c:\\temp\\test.txt").exists() C. new File("c:\temp\test.txt").exist() D. new File("c:\\temp\\test.txt").exist() E. None of the above.

B

Which of the following statements creates an instance of File on Window for the file c:\temp.txt? A. new File("c:\temp.txt") B. new File("c:\\temp.txt") C. new File("c:/temp.txt") D. new File("c://temp.txt")

B

Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; // some methods omitted } A. In the class B, an instance method can only access i, j, k, m. B. In the class B, an instance method can only access j, k, m. C. In the class B, an instance method can only access j, m. D. In the class B, an instance method can only access k, m.

B

Analyze the following code: public class Test extends A { public static void main(String[] args) { Test t = new Test(); t.print(); } } class A { String s; A(String s) { this.s = s; } public void print() { System.out.println(s); } } A. The program does not compile because Test does not have a default constructor Test(). B. The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed. C. The program would compile if a default constructor A(){ } is added to class A explicitly. D. The program compiles, but it has a runtime error due to the conflict on the method name print.

B C

Which of the following statements are true? Please select all that apply. A. A subclass is a subset of a superclass. B. A subclass is usually extended to contain more functions and more detailed information than its superclass. C. "class A extends B" means A is a subclass of B. D. "class A extends B" means B is a subclass of A.

B C

Analyze the following code. Please select all that apply. public class A extends B { } class B { public B(String s) { } } A. The program has a compile error because A does not have a default constructor. B. The program has a compile error because the default constructor of A invokes the default constructor of B, but B does not have a default constructor. C. The program would compile fine if you add the following constructor into A: A(String s) { } D. The program would compile fine if you add the following constructor into A: A(String s) { super(s); }

B D

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors? Please select all that apply. A. x.get(1) B. x.set(2, "New York"); C. x.get(2) D. x.remove(2) E. x.size()

BCD

// Program 1: public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(Object a) { return this.x == ((A)a).x; } } // Program 2: public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } A. Program 1 displays true and Program 2 displays true B. Program 1 displays false and Program 2 displays true C. Program 1 displays true and Program 2 displays false D. Program 1 displays false and Program 2 displays false

C

A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this? A. The variable should be marked public. B. The variable should be marked private. C. The variable should be marked protected. D. The variable should have no special access modifier. E. The variable should be marked private and an accessor method provided.

C

An instance of _________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully. A. RuntimeException B. Exception C. Error D. Throwable E. NumberFormatException

C

Analyze the following code: Double[] array = {1, 2, 3}; ArrayList<Double> list = new ArrayList<>(Arrays.asList(array)); System.out.println(list); A. The code is correct and displays [1, 2, 3]. B. The code is correct and displays [1.0, 2.0, 3.0]. C. The code has a compile error because an integer such as 1 is automatically converted into an Integer object, but the array element type is Double. D. The code has a compile error because asList(array) requires that the array elements are objects.

C

Analyze the following code: Integer[] c = {3, 5}; java.util.Collections.shuffle(c); System.out.println(java.util.Arrays.toString(c)); A. The code is correct and displays [3, 5]. B. The code is correct and displays [5, 3]. C. The code has a compile error on Collections.shuffle(c). c cannot be an array. D. The code has a compile error on Integer[] c = {3, 5}.

C

Analyze the following code: double[] c = {1, 2, 3}; System.out.println(java.util.Collections.max(c)); A. The code is correct and displays 3. B. The code is correct and displays 3.0. C. The code has a compile error on Collections.max(c). c cannot be an array. D. The code has a compile error on double[] c = {1, 2, 3}.

C

Assume Cylinder is a subtype of Circle. Analyze the following code: Cylinder cy = new Cylinder(1, 1); Circle c = cy; A. The code has a compile error. B. The code has a runtime error. C. The code is fine.

C

Given two reference variables t1 and t2, if t1.equals(t2) is true, t1 == t2 ___________. A. is always true B. is always false C. may be true or false

C

Polymorphism means ______________. A. that data fields should be declared private B. that a class can extend another class C. that a variable of supertype can refer to a subtype object D. that a class can contain another class

C

The output from the following code is __________. java.util.ArrayList<String> list = new java.util.ArrayList<>(); list.add("New York"); java.util.ArrayList<String> list1 = list; list.add("Atlanta"); list1.add("Dallas"); System.out.println(list1); A. [New York] B. [New York, Atlanta] C. [New York, Atlanta, Dallas] D. [New York, Dallas]

C

What exception type does the following program throw? public class Test { public static void main(String[] args) { String s = "abc"; System.out.println(s.charAt(3)); } } A. ArithmeticException B. ArrayIndexOutOfBoundsException C. StringIndexOutOfBoundsException D. ClassCastException E. No exception

C

What is displayed on the console when running the following program? public class Test { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; double y = 2.0 / i; System.out.println("Welcome to HTML"); } finally { System.out.println("The finally clause is executed"); } } } A. Welcome to Java. B. Welcome to Java followed by The finally clause is executed in the next line. C. The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed. D. None of the above.

C

What is displayed on the console when running the following program? public class Test { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } System.out.println("End of the block"); } } A. The program displays Welcome to Java three times followed by End of the block. B. The program displays Welcome to Java two times followed by End of the block. C. The program displays Welcome to Java two times followed by End of the block two times. D. You cannot catch RuntimeException errors.

C

What is displayed on the console when running the following program? public class Test { public static void main(String[] args) { try { method(); System.out.println("After the method call"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } catch (Exception ex) { System.out.println("Exception"); } } static void method() throws Exception { try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (NumberFormatException ex) { System.out.println("NumberFormatException"); throw ex; } catch (RuntimeException ex) { System.out.println("RuntimeException"); } } } A. The program displays NumberFormatException twice. B. The program displays NumberFormatException followed by After the method call. C. The program displays NumberFormatException followed by RuntimeException. D. The program has a compile error.

C

What is displayed on the console when running the following program? public class Test { public static void main(String[] args) { try { p(); System.out.println("After the method call"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } catch (Exception ex) { System.out.println("Exception"); } } static void p() throws Exception { try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } catch (Exception ex) { System.out.println("Exception"); } } } A. The program displays RuntimeException twice. B. The program displays Exception twice. C. The program displays RuntimeException followed by After the method call. D. The program displays Exception followed by RuntimeException. E. The program has a compile error.

C

What is the output of running class C? class A { public A() { System.out.println( "The default constructor of A is invoked"); } } class B extends A { public B() { System.out.println( "The default constructor of B is invoked"); } } public class C { public static void main(String[] args) { B b = new B(); } } A. Nothing displayed B. "The default constructor of B is invoked" C. "The default constructor of A is invoked" followed by "The default constructor of B is invoked" D. "The default constructor of B is invoked" followed by "The default constructor of A is invoked" E. "The default constructor of A is invoked"

C

What is the output of the following code? ArrayList<java.util.Date> list = new ArrayList<>(); java.util.Date d = new java.util.Date(); list.add(d); list.add(d); System.out.println((list.get(0) == list.get(1)) + " " + (list.get(0)).equals(list.get(1))); A. true false B. false true C. true true D. false false

C

What is the output of the following code? public class Test { public static void main(String[] args) { String s1 = new String("Java"); String s2 = new String("Java"); System.out.print((s1 == s2) + " " + (s1.equals(s2))); } } A. false false B. true true C. false true D. true false

C

What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are accessible to any subclasses in any package? A. public B. private C. protected D. Use the default modifier.

C

Which of the following classes cannot be extended? A. class A { } B. class A { private A() { }} C. final class A { } D. class A { protected A() { }}

C

Which of the statements regarding the super keyword is incorrect? A. You can use super to invoke a super class constructor. B. You can use super to invoke a super class method. C. You can use super.super.p to invoke a method in superclass's parent class. D. You cannot invoke a method in superclass's parent class.

C

You can create an ArrayList using _________. A. new ArrayList[] B. new ArrayList[100] C. new ArrayList<>() D. ArrayList()

C

Analyze the following code. Please select all that apply. public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new Object(); System.out.println(a1); System.out.println(a2); } } class A { int x; @Override public String toString() { return "A's x is " + x; } } A. The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString()); B. When executing System.out.println(a1), the toString() method in the Object class is invoked. C. When executing System.out.println(a2), the toString() method in the Object class is invoked. D. When executing System.out.println(a1), the toString() method in the A class is invoked.

CD

Given the following code, find the compile error. Please select all that apply. public class Test { public static void main(String[] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); } public static void m(Student x) { System.out.println(x.toString()); } } class GraduateStudent extends Student { } class Student extends Person { @Override public String toString() { return "Student"; } } class Person extends Object { @Override public String toString() { return "Person"; } } A. m(new GraduateStudent()) causes an error B. m(new Student()) causes an error C. m(new Person()) causes an error D. m(new Object()) causes an error

CD

A Java exception is an instance of __________. A. RuntimeException B. Exception C. Error D. Throwable E. NumberFormatException

D

Analyze the following code: double[] array = {1, 2, 3}; ArrayList<Double> list = new ArrayList<>(Arrays.asList(array)); System.out.println(list); A. The code is correct and displays [1, 2, 3]. B. The code is correct and displays [1.0, 2.0, 3.0]. C. The code has a compile error because an integer such as 1 is automatically converted into an Integer object, but the array element type is Double. D. The code has a compile error because asList(array) requires that the array elements are objects.

D

Analyze the following code: public class Test { public static void main(String[] args) { B b = new B(); b.m(5); System.out.println("i is " + b.i); } } class A { int i; public void m(int i) { this.i = i; } } class B extends A { public void m(String s) { } } A. The program has a compile error, because m is overridden with a different signature in B. B. The program has a compile error, because b.m(5) cannot be invoked since the method m(int) is hidden in B. C. The program has a runtime error on b.i, because i is not accessible from b. D. The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

D

Analyze the following code: public class Test { public static void main(String[] args) { String s = new String("Welcome to Java"); Object o = s; String d = (String)o; } } A. When assigning s to o in Object o = s, a new object is created. B. When casting o to s in String d = (String)o, a new object is created. C. When casting o to s in String d = (String)o, the contents of o is changed. D. s, o, and d reference the same String object.

D

Analyze the following code: public class Test { public static void main(String[] args) { new B(); } } class A { int i = 7; public A() { setI(20); System.out.println("i from A is " + i); } public void setI(int i) { this.i = 2 * i; } } class B extends A { public B() { // System.out.println("i from B is " + i); } @Override public void setI(int i) { this.i = 3 * i; } } A. The constructor of class A is not called. B. The constructor of class A is called and it displays "i from A is 7". C. The constructor of class A is called and it displays "i from A is 40". D. The constructor of class A is called and it displays "i from A is 60".

D

Analyze the following code: public class Test { public static void main(String[] args) { try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; } catch (Exception ex) { System.out.println("NumberFormatException"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } } } A. The program displays NumberFormatException. B. The program displays RuntimeException. C. The program displays NumberFormatException followed by RuntimeException. D. The program has a compile error.

D

Given the following code, which of the following expressions evaluates to false? class C1 {} class C2 extends C1 { } class C3 extends C2 { } class C4 extends C1 {} C1 c1 = new C1(); C2 c2 = new C2(); C3 c3 = new C3(); C4 c4 = new C4(); A. c1 instanceof C1 B. c2 instanceof C1 C. c3 instanceof C1 D. c4 instanceof C2

D

Invoking _________ returns the number of the elements in an ArrayList x. A. x.getSize() B. x.getLength(0) C. x.length(1) D. x.size()

D

What exception type does the following program throw? public class Test { public static void main(String[] args) { Object o = new Object(); String d = (String)o; } } A. ArithmeticException B. ArrayIndexOutOfBoundsException C. StringIndexOutOfBoundsException D. ClassCastException E. No exception

D

What exception type does the following program throw? public class Test { public static void main(String[] args) { Object o = null; System.out.println(o); } } A. ArithmeticException B. ArrayIndexOutOfBoundsException C. StringIndexOutOfBoundsException D. No exception E. NullPointerException

D

What is displayed on the console when running the following program? public class Test { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } System.out.println("End of the block"); } } A. The program displays Welcome to Java three times followed by End of the block. B. The program displays Welcome to Java two times followed by End of the block. C. The program displays Welcome to Java two times followed by End of the block two times. D. The program displays Welcome to Java and End of the block, and then terminates because of an unhandled exception.

D

What modifier should you use on a class so that a class in the same package can access it but a class (including a subclass) in a different package cannot access it? A. public B. private C. protected D. Use the default modifier.

D

Which of the following statements is false? A. A public class can be accessed by a class from a different package. B. A private method cannot be accessed by a class in a different package. C. A protected method can be accessed by a subclass in a different package. D. A method with no visibility modifier can be accessed by a class in a different package.

D

Which of the following statements is false? A. You can always pass an instance of a subclass to a parameter of its superclass type. This feature is known as polymorphism. B. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compile time. C. A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime. D. Dynamic binding can apply to static methods. E. Dynamic binding can apply to instance methods.

D

You can assign _________ to a variable of Object[] type. Please select all that apply. A. new char[100] B. new int[100] C. new double[100] D. new String[100] E. new java.util.Date[100]

DE

Which of the following statements are true? Please select all that apply. A. A method can be overloaded in the same class. B. A method can be overridden in the same class. C. If a method overloads another method, these two methods must have the same signature. D. If a method overrides another method, these two methods must have the same signature. E. A method in a subclass can overload a method in the superclass.

DEA

Invoking _________ removes all elements in an ArrayList x. A. x.remove() B. x.clean() C. x.delete() D. x.empty() E. x.clear()

E

The following code causes Java to throw _________. int number = Integer.MAX_VALUE + 1; A. RuntimeException B. Exception C. Error D. Throwable E. no exceptions

E

What exception type does the following program throw? public class Test { public static void main(String[] args) { Object o = null; System.out.println(o.toString()); } } A. ArithmeticException B. ArrayIndexOutOfBoundsException C. StringIndexOutOfBoundsException D. ClassCastException E. NullPointerException

E

Which of the following statements are true? A. If a file (e.g., c:\temp.txt) does not exist, new File("c:\\temp.txt") returns null. B. If a directory (e.g., c:\liang) does not exist, new File("c:\liang") returns null. C. If a file (e.g., c:\temp.txt) does not exist, new File("c:\\temp.txt") creates a new file named c:\temp.txt. D. If a directory (e.g., c:\liang) does not exist, new File("c:\liang") creates a new directory named c:\liang. E. None of the above.

E


Kaugnay na mga set ng pag-aaral

Local Anesthetics Evolve Quizzes

View Set

Disasters Lec 8 Atmosphere and Severe Weather

View Set

AP Human Geography/European History Hints and Answers

View Set