11-13 JAVA

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

All true

A. A method can be overloaded in the same class. 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.

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

D. s, o, and d reference the same String object.

Composition means ______________

D. that a class contains a data field that references another object.

Invoking _________ returns the number of the elements in an ArrayList x,

D. x.size()

Given two reference variables t1 and t2, if t1.equals(t2) is true, t1 == t2 ___________.

may be true or false

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?

protected

Analyze the following code: ArrayList<String> list = new ArrayList<String>(); 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. C. If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine.

What is displayed on the console when running the following program? class Test { public static void main(String[] args) { try { method(); System.out.println("After the method call"); } catch (NumberFormatException ex) { System.out.println("NumberFormatException"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } } static void method() { 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.

All true

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

Analyze the following code: 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.

Assume Calendar calendar = new GregorianCalendar(). __________ returns the month of the year.

A. calendar.get(Calendar.MONTH)

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

Which of the following statements are true?

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.

Which of the following is not an advantage of Java exception handling?

B. Exception handling improves performance.

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 }

B. In the class B, an instance method can only access j, k, m

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

B. Program 1 displays false and Program 2 displays true

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); } public void setI(int i) { this.i = 3 * i; } }

B. The constructor of class A is called and it displays "i from A is 7".

What is displayed on the console when running the following program? 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"); } } }

B. The program displays Welcome to Java two times followed by End of the block.

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

B. The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally.

The java.util.Calendar and java.util.GregorianCalendar classes are introduced in Chapter 11. Analyze the following code. 1. import java.util.*; 2. public class Test { 3. public static void main(String[] args) { 4. Calendar[] calendars = new Calendar[10]; 5. calendars[0] = new Calendar(); 6. calendars[1] = new GregorianCalendar(); 7. } 8. }

B. The program has a compile error on Line 5 because java.util.Calendar is an abstract class.

What is displayed on the console when running the following program? 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"); } } }

B. Welcome to Java followed by The finally clause is executed in the next line, then an error message.

What is wrong in the following program? class Test { public static void main (String[] args) { try { System.out.println("Welcome to Java"); } } }

B. You cannot have a try block without a catch block or a finally block.

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

B. You will get a runtime error because you cannot cast objects from sibling classes.

What is output of the following code: ArrayList<String> list = new ArrayList<String>(); 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)));

B. false true

The relationship between an interface and the class that implements it is

C. Inheritance

What is the best suitable relationship between Employee and Faculty?

C. Inheritance

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

C. Program 1 displays true and Program 2 displays false

Analyze the following code: Cylinder cy = new Cylinder(1, 1); Circle c = cy;

C. The code is fine.

What is displayed on the console when running the following program? 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"); } } }

C. The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.

Analyze the following code. public class Test { public static void main(String[] args) { Number x = new Integer(3); System.out.println(x.intValue()); System.out.println(x.compareTo(new Integer(4))); } }

C. The program has a compile error because x does not have the compareTo method.

Analyze the following code: 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; public String toString() { return "A's x is " + x; } }

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.

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

C. [New York, Atlanta, Dallas]

Which of the following class definitions defines a legal abstract class?

C. abstract class A { abstract void unfinished(); }

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

C. false true

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 { public String toString() { return "Student"; } } class Person extends Object { public String toString() { return "Person"; } }

C. m(new Person()) causes an error D. m(new Object()) causes an error

You can create an ArrayList using _________.

C. new ArrayList()

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]?

C. x.add(1, "Chicago")

Suppose an ArrayList list contains {"red", "red", "green"}. What is 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--; }

C. {"green"}

Suppose an ArrayList list contains {"red", "red", "green"}. What is 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);

C. {"green"}

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

ClassCastException

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); } public void setI(int i) { this.i = 3 * i; } }

D. The constructor of class A is called and it displays "i from A is 60".

Analyze the following code: 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"); } } }

D. The program has a compilation error.

Analyze the following code. public class Test { public static void main(String[] args) { Number x = new Integer(3); System.out.println(x.intValue()); System.out.println((Integer)x.compareTo(new Integer(4))); } }

D. The program has a compile error because the member access operator (.) is executed before the casting operator.

Analyze the following code. public class Test { public static void main(String[] args) { java.util.Date x = new java.util.Date(); java.util.Date y = x.clone(); System.out.println(x = y); } }

D. The program has a compile error because the return type of the clone() method is java.lang.Object.

The Rational class in this chapter extends java.lang.Number and implements java.lang.Comparable. Analyze the following code. 1. public class Test { 2. public static void main(String[] args) { 3. Number[] numbers = {new Rational(1, 2), new Integer(4), new Double(5.6)}; 4. java.util.Arrays.sort(numbers); 5. } 6. }

D. The program has a runtime error because the compareTo methods in Rational, Integer, and Double classes do not compare the value of one type with a value of another type.

Analyze the following code. 1. public class Test { 2. public static void main(String[] args) { 3. Fruit[] fruits = {new Fruit(2), new Fruit(3), new Fruit(1)}; 4. java.util.Arrays.sort(fruits); 5. } 6. } class Fruit { private double weight; public Fruit(double weight) { this.weight = weight; } }

D. The program has a runtime error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.

What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it?

D. Use the default modifier.

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 = (java.util.ArrayList<String>)(list.clone()); list.add("Atlanta"); list1.add("Dallas"); System.out.println(list1);

D. [New York, Dallas]

Show the output of running the class Test in the following code lines: interface A { } class C { } class B extends D implements A { } public class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b is an instance of A"); if (b instanceof C) System.out.println("b is an instance of C"); } } class D extends C { }

D. b is an instance of A followed by b is an instance of C.

Given the following code: 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(); Which of the following expressions evaluates to false?

D. c4 instanceof C2

Assume Calendar calendar = new GregorianCalendar(). __________ returns the week of the year.

D. calendar.get(Calendar.WEEK_OF_YEAR)

Which of the following is a correct interface?

D. interface A { void print();}

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) { } } D. The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in 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"; } }

II

An instance of _________ are unchecked exceptions.

RuntimeException Error NumberFormatException

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

StringIndexOutOfBoundsException

What is the output of the following code? public class Test1 { public static void main(String[] args) { ChildClass c = new ChildClass(); c.print(); } } class ParentClass { int id = 1; void print() { System.out.println(id); } } class ChildClass extends ParentClass { int id = 2; }

1

_______ is a reference type.

A. A class type B. An interface type C. An array type

Analyze the following program. 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);

Which statements are most accurate regarding the following classes? class A { private int i; protected int j; public int getI() { return i; } public int getJ() { return j; } } class B extends A { private int k; protected int m; public int getK() { return k; } public int getM() { return m; } }

A. An object of B contains data fields i, j, k, m

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

Analyze the following code: Circle c = new Circle (5); Cylinder c = cy;

A. The code has a compile error.

What is true about the super keyword

A. You can use super to invoke a super class constructor. B. You can use super to invoke a super class method. D. You cannot invoke a method in superclass's parent class.

What are the reasons to create an instance of the File class?

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.

Encapsulation means ______________.

A. that data fields should be declared private.

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to become [Beijing]?

A. x.remove("Singapore")

What exception type does the following program throw? public class Test { public static void main(String[] args) { System.out.println(1 / 0); } }

ArithmeticException

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

ArrayIndexOutOfBoundsException

Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a no-arg constructor. Which of the following is correct?

B. A a = new B(); D. B b = new B();

A constructor may...

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.

Correct code

B. ArrayList<Double> list = new ArrayList<>(); list.add(3.4); D. ArrayList<Integer> list = new ArrayList<>(); list.add(3);

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 { public String getInfo() { return "Student"; } } class Person { public String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } }

B. Person Student

Analyze the following code: public class A extends B { } class B { public B(String s) { } }

B. The program has a compilation error because the default constructor of A invokes the default constructor of B, but B does not have a default constructor. D. The program would compile fine if you add the following constructor into A: A(String s) { super(s); }

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

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.

What is displayed on the console when running the following program? 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"); } } }

B. Welcome to Java followed by The finally clause is executed in the next line

What is displayed on the console when running the following program? class Test { public static void main (String[] args) { try { System.out.println("Welcome to Java"); } finally { System.out.println("The finally clause is executed"); } } }

B. Welcome to Java followed by The finally clause is executed in the next line

The visibility of these modifiers increases in this order:

B. private, none (if no modifier is used), protected, and public.

Which of the following declares an abstract method in an abstract Java class?

B. public abstract void method();

The equals method is defined in the Object class. Which of the following is correct to override it in the String class?

B. public boolean equals(Object other)

Inheritance means ______________.

B. that a class can extend another class.

Invoking _________ returns the first element in an ArrayList x.

B. x.get(0)

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors?

B. x.set(2, "New York"); C. x.get(2) D. x.remove(2)

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

B. {"green", "red", "green"}

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

C. "The default constructor of A is invoked""The default constructor of B is invoked"

The java.lang.Number and its subclasses are introduced in Chapter 11. Analyze the following code. Number numberRef = new Integer(0); Double doubleRef = (Double)numberRef;

C. A runtime class casting exception occurs, since numberRef is not an instance of Double.

What is displayed on the console when running the following program? 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 (RuntimeException ex) { System.out.println("RuntimeException"); } catch (Exception ex) { System.out.println("Exception"); } } }

C. The program displays RuntimeException followed by After the method call.

What is displayed on the console when running the following program? 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"); } }

C. The program displays Welcome to Java two times followed by End of the block two times.

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?

C. The variable should be marked protected.

Polymorphism means ______________.

C. that a variable of supertype can refer to a subtype object

What is output of the following code: ArrayList<java.util.Date> list = new ArrayList<java.util.Date>(); 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)));

C. true true

Analyze the following code. Number[] numberArray = new Integer[2]; numberArray[0] = new Double(1.5);

D. At runtime, new Integer[2] is assigned to numberArray. This makes each element of numberArray an Integer object. So you cannot assign a Double object to it.

Assume an employee can work for only one company. What is the best suitable relationship between Company and Employee?

D. Composition

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) { } }

D. The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

What is the output of running class Test? public class Test { public static void main(String[] args) { new Circle9(); } } public abstract class GeometricObject { protected GeometricObject() { System.out.print("A"); } protected GeometricObject(String color, boolean filled) { System.out.print("B"); } } public class Circle9 extends GeometricObject { /** Default constructor */ public Circle9() { this(1.0); System.out.print("C"); } /** Construct circle with a specified radius */ public Circle9(double radius) { this(radius, "white", false); System.out.print("D"); } /** Construct a circle with specified radius, filled, and color */ public Circle9(double radius, String color, boolean filled) { super(color, filled); System.out.print("E"); } }

E. BEDC

Assume Calendar calendar = new GregorianCalendar(). __________ returns the number of days in a month.

E. calendar.getActualMaximum(Calendar.DAY_OF_MONTH)

Invoking _________ removes all elements in an ArrayList x.

E. x.clear()

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.

Error

An instance of _________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program.

Exception

The following code causes Java to throw _________. int number = Integer.MAX_VALUE + 1;

No exceptions

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

NullPointerException

An instance of _________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors..

RuntimeException

A Java exception is an instance of __________.

Throwable

A method must declare to throw ________.

checked exceptions

Object-oriented programming allows you to derive new classes from existing classes. This is called ____________.

inheritance

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

true


Kaugnay na mga set ng pag-aaral

Chapter 16 Anatomy of the Female Reproductive System

View Set

Fluid, Electrolyte, and Acid-Base Regulation

View Set

LI.FE.P.A.C high school health (spiritual, mental, and emotional health)

View Set

Unit 6 MC (not sure this is on it)

View Set

EC1 U3 ASD3 Actividades y lugares para las actividades

View Set

GEOCHEMICAL CYCLES: CYCLES OF EARTH MATERIALS

View Set