Java 10-15
Which of the following statements will convert a string s into i of int type? (choose all that apply) A) i = Integer.parseInt(s); B) i = (int)(Double.parseDouble(s)); C) i = Integer.valueOf(s).intValue(); D) i = (new Integer(s)).intValue(); E) i = Integer.valueOf(s);
A) i = Integer.parseInt(s); B) i = (int)(Double.parseDouble(s)); C) i = Integer.valueOf(s).intValue(); D) i = (new Integer(s)).intValue();
The UML uses ________ before a member name to indicate that the member is protected. A) # B) - C) + D) None of the above.
A) #
Comprehensive ________ is a reference type. (choose all that apply) A) A class type B) An interface type C) An array type D) A primitive type
A) A class type B) An interface type C) An array type
Which of the following is incorrect? A) A constructor may be static. B) A constructor may invoke a static method. C) A constructor may be private. 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 superclasses constructor.
A) A constructor may be static.
Which of the following statements registers a panel object p as a listener for a button variable jbt? A) jbt.addActionListener(p); B) addActionListener(p); C) jbt.addActionEventListener(p); D) jbt.addEventListener(p);
A) jbt.addActionListener(p);
Which of the following statements are correct? (choose all that apply) A) new java.math.BigInteger("343"); B) new java.math.BigDecimal(343.445); C) new java.math.BigInteger(343); D) new java.math.BigDecimal("343.445");
A) new java.math.BigInteger("343"); D) new java.math.BigDecimal("343.445");
The method in the ActionEvent ________ returns the action command of the button. A) getActionCommand() B) getID() C) getModifiers() D) paramString()
A) getActionCommand()
Encapsulation means ________. A) that data fields should be declared private B) that a class can extend another class C) that a class can contain another class D) that a variable of supertype can refer to a subtype object
A) that data fields should be declared private
"Students are comparable" is a weak inheritance relationship, which can be implemented using interfaces. A) true B) false
A) true
A button's background color can be changed, as well as its foreground color. A) true B) false
A) true
A panel can be placed inside a panel. A) true B) false
A) true
A source object and a listener object can be the same. A) true B) false
A) true
Aggregation is a special form of association that represents an ownership relationship between two classes. A) true B) false
A) true
All the numeric wrapper classes implement the Comparable interface. A) true B) false
A) true
An abstract class can be extended. A) true B) false
A) true
Composition is a special type of aggregation relationship with exclusive ownership. A) true B) false
A) true
If a data field is declared in the superclass, you may hide it by redeclaring it in the subclass. A) true B) false
A) true
If a method is declared protected in the superclass, you may declare the method public in the subclass. A) true B) false
A) true
If a parameter is of the java.lang.Object type, you can pass any object to it. This is known as generic programming. A) true B) false
A) true
The methods parseInt, parseDouble, parseFloat, parseLong are in the classes Integer, Double, Float, and Long to convert a numeric string into an integer, double, float, or long. A) true B) false
A) true
The name of abstract class and interface is in italic in the UML. A) true B) false
A) true
You can create an Integer object from an integer by using the Integer constructor with the int argument. A) true B) false
A) true
You can draw things on an applet. A) true B) false
A) true
You cannot create an instance of an abstract class using the new operator. A) true B) false
A) true
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) private B) protected C) public D) Use the default modifier
B) protected
Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to become [Beijing, Chicago, Singapore]? A) x.add("Chicago") B) x.add(1, "Chicago") C) x.add(0, "Chicago") D) x.add(2, "Chicago")
B) x.add(1, "Chicago")
Invoking ________ returns the first element in an ArrayList x. A) x.first() B) x.get(0) C) x.get(1) D) x.get()
B) x.get(0)
Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause runtime errors? (choose all that apply) A) x.get(1) B) x.remove(2) C) x.set(2, "New York"); D) x.get(2) E) x.size()
B) x.remove(2) C) x.set(2, "New York"); D) x.get(2)
The header for the paintComponent method is ________. A) public void paintComponent(Graphics g) B) protected void paintComponent() C) protected void paintComponent(Graphics g) D) private void paintComponent(Graphics g)
C) protected void paintComponent(Graphics g)
Which of the following methods override the toString method in the Object class? A) public void toString(String s) B) public static String toString() C) public String toString() D) public String toString(String s)
C) public String toString()
Inheritance means ________. A) that data fields should be declared private B) that a variable of supertype can refer to a subtype object C) that a class can extend another class D) that a class can contain another class
C) that a class can extend another class
The component that processes the listener is called ________. A) the source object B) the adapter object C) the listener object D) the adaptee object
C) the listener object
Show the output of the following code? import javax.swing.*; public class Test { public static void main(String[] args) { JButton jbtOK = new JButton("OK"); System.out.print(jbtOK.isVisible() + ", "); JFrame frame = new JFrame(); System.out.println(frame.isVisible()); } } A) false, false B) false, true C) true, false D) true, true
C) true, false
Which of the following statements are true? (choose all that apply) A) Invoking repaint() causes paintComponent to be invoked by the JVM. B) The paintComponent method is automatically invoked by the JVM. You should never invoke it directly. C) Whenever a GUI component is displayed, its Graphics object is automatically created. D) You may create a Graphics object using new Graphics().
A) Invoking repaint() causes paintComponent to be invoked by the JVM. B) The paintComponent method is automatically invoked by the JVM. You should never invoke it directly. C) Whenever a GUI component is displayed, its Graphics object is automatically created.
Can you use the setBorder method to set a border for ________? (choose all that apply) A) JComponent B) Container C) JButton D) Component E) JLabel
A) JComponent C) JButton E) JLabel
Which of the following is poor design? (choose all that apply) A) A method is an instance method, but it does not reference any instance data fields or invoke instance methods. B) A method must be invoked after/before invoking another method in the same class. C) A parameter is passed from a constructor to initialize a static data field. D) A data field is derived from other data fields in the same class.
A) A method is an instance method, but it does not reference any instance data fields or invoke instance methods. B) A method must be invoked after/before invoking another method in the same class. C) A parameter is passed from a constructor to initialize a static data field. D) A data field is derived from other data fields in the same class.
Which of the following statements are true? (choose all that apply) A) A source may have multiple listeners. B) The listener object's class must implement the corresponding event-listener interface. C) The listener object must be registered by the source object. D) A listener may listen for multiple sources. E) Each event class has a corresponding listener interface.
A) A source may have multiple listeners. B) The listener object's class must implement the corresponding event-listener interface. C) The listener object must be registered by the source object. D) A listener may listen for multiple sources. E) Each event class has a corresponding listener interface.
Which of the following statements regarding abstract methods are true? (choose all that apply) A) A subclass of a non-abstract superclass can be abstract. B) An abstract class can be used as a data type. C) A subclass can override a concrete method in a superclass to declare it abstract. D) An abstract class can have instances created using the constructor of the abstract class. E) An abstract class can be extended.
A) A subclass of a non-abstract superclass can be abstract. B) An abstract class can be used as a data type. C) A subclass can override a concrete method in a superclass to declare it abstract. E) An abstract class can be extended.
________ describes dependent relationships between two classes. (choose all that apply) A) Aggregation B) Composition C) Dependency D) Association
A) Aggregation B) Composition C) Dependency D) Association
Which of the following statements are true? (choose all that apply) A) An anonymous inner class is an inner class without a name. B) An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause. C) An anonymous inner class must implement all the abstract methods in the superclass or in the interface. D) An anonymous inner class is compiled into a class named OuterClassName$n.class. E) An anonymous inner class always uses the no-arg constructor from its superclass to create an instance. If an anonymous inner class implements an interface, the constructor is Object().
A) An anonymous inner class is an inner class without a name. B) An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause. C) An anonymous inner class must implement all the abstract methods in the superclass or in the interface. D) An anonymous inner class is compiled into a class named OuterClassName$n.class. E) An anonymous inner class always uses the no-arg constructor from its superclass to create an instance. If an anonymous inner class implements an interface, the constructor is Object().
________ can be viewed as a special case of ________. A) Association/dependency B) Dependency/association
A) Association/dependency
Which of the following statements are true? (choose all that apply) A) Each GUI component contains a Graphics object that can be obtained using getGraphics() method. B) Once a GUI component is visible, getGraphics() returns the object. C) If a GUI component is not visible, getGraphics() returns null. D) The Graphics object is automatically created for each visible GUI component.
A) Each GUI component contains a Graphics object that can be obtained using getGraphics() method. B) Once a GUI component is visible, getGraphics() returns the object. C) If a GUI component is not visible, getGraphics() returns null. D) The Graphics object is automatically created for each visible GUI component.
The relationship between an interface and the class that implements it is A) Inheritance B) Aggregation C) Association D) None of the above.
A) Inheritance
What is the best suitable relationship between Employee and Faculty? A) Inheritance B) Aggregation C) Association D) None of the above.
A) Inheritance
Which of the following statements are true? (choose all that apply) A) Inheritance models the is-a relationship between two classes. B) A weak is-a relationship describes that a class has certain properties. C) A weak is-a relationship can be represented using interfaces. D) A strong is-a relationship can be represented using class inheritance. E) A strong is-a relationship describes a direct inheritance relationship between two classes.
A) Inheritance models the is-a relationship between two classes. B) A weak is-a relationship describes that a class has certain properties. C) A weak is-a relationship can be represented using interfaces. D) A strong is-a relationship can be represented using class inheritance. E) A strong is-a relationship describes a direct inheritance relationship between two classes.
Which of the following statements are true? (choose all that apply) A) Inner classes can make programs simple and concise. B) An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class. C) An inner class can be declared public or private subject to the same visibility rules applied to a member of the class. D) An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class.
A) Inner classes can make programs simple and concise. B) An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class. C) An inner class can be declared public or private subject to the same visibility rules applied to a member of the class. D) An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class.
Which of the following statements is correct? (choose all that apply) A) Integer.parseInt("345", 8); B) Integer.parseInt(100, 16); C) Integer.parseInt(100); D) Integer.parseInt("12", 2); E) Integer.parseInt("100");
A) Integer.parseInt("345", 8); E) Integer.parseInt("100");
Analyze the following code: Circle c = new Circle (5); Cylinder c = cy; A) The code has a syntax error. B) The code is fine. C) The code has a runtime error
A) The code has a syntax error.
Analyze the following code: ArrayList list = new ArrayList(); list.add("Beijing"); list.add("Tokyo"); list.add("Shanghai"); list.set(3, "Hong Kong"); (choose all that apply) 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(4, "Hong Kong"), the code will compile and run fine. D) If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine.
A) The last line in the code causes a runtime error because there is no element at index 3 in the array list. D) If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine.
Analyze the following code: import javax.swing.*; public class Test extends JFrame { private JButton jbtOK = new JButton("OK"); public static void main(String[] args) { // Create a frame and set its properties JFrame frame = new Test(); frame.setTitle("Logic Error"); frame.setSize(200, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public Test() { jbtOK.setToolTipText("This is a button"); add(new JButton("OK")); } } A) The tool tip text will be displayed if you replace add(new JButton("OK")) with add(jbtOK). B) The tool tip text will be displayed if you swap the two lines in the Test constructor. C) The tool tip text will be displayed if you replace add(new JButton("OK")) with add(jbtOK= new JButton("OK")). D) The tool tip text is displayed when you move the mouse on the button.
A) The tool tip text will be displayed if you replace add(new JButton("OK")) with add(jbtOK).
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 protected. B) The variable should have no special access modifier. C) The variable should be marked private and an accessor method provided. D) The variable should be marked private. E) The variable should be marked public.
A) The variable should be marked protected.
The GeometricObject and Circle classes are defined in Chapter 11. Analyze the following code. public class Test { public static void main(String[] args) { GeometricObject x = new Circle(3); GeometricObject y = (Circle)(x.clone()); System.out.println(x); System.out.println(y); } } (choose all that apply) A) To enable a Circle object to be cloned, the Circle class has to override the clone() method and implement the java.lang.Cloneable interface. B) If GeometricObject implements Cloneable and Circle overrides the clone() method, the clone() method will work fine to clone Circle objects. C) The program has a syntax error because the clone() method is protected in the Object class. D) After you override the clone() method and make it public in the Circle class, the problem can compile and run just fine, but y is null if Circle does not implement the Cloneable interface.
A) To enable a Circle object to be cloned, the Circle class has to override the clone() method and implement the java.lang.Cloneable interface. B) If GeometricObject implements Cloneable and Circle overrides the clone() method, the clone() method will work fine to clone Circle objects. C) The program has a syntax error because the clone() method is protected in the Object class. D) After you override the clone() method and make it public in the Circle class, the problem can compile and run just fine, but y is null if Circle does not implement the Cloneable interface.
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; } } (choose all that apply) A) When executing System.out.println(a1), the toString() method in the A class is invoked. B) When executing System.out.println(a2), the toString() method in the Object class is invoked. C) The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString()); D) When executing System.out.println(a1), the toString() method in the Object class is invoked.
A) When executing System.out.println(a1), the toString() method in the A class is invoked. B) When executing System.out.println(a2), the toString() method in the Object class is invoked.
Which of the following statements are true? (choose all that apply) A) You can set a background color on any instance of Component. B) You can set a border on any instance of JComponent. C) You can set a tooltip text on any instance of JComponent. D) You can set a foreground color on any instance of Component.
A) You can set a background color on any instance of Component. B) You can set a border on any instance of JComponent. C) You can set a tooltip text on any instance of JComponent. D) You can set a foreground color on any instance of Component.
Which of the following statements are true? (choose all that apply) A) You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods. B) Override the methods equals and toString defined in the Object class whenever possible. C) Override the hashCode method whenever the equals method is overridden. By contract, two equal objects must have the same hash code. D) A public default no-arg constructor is assumed if no constructors are defined explicitly.
A) You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods. B) Override the methods equals and toString defined in the Object class whenever possible. C) Override the hashCode method whenever the equals method is overridden. By contract, two equal objects must have the same hash code. D) A public default no-arg constructor is assumed if no constructors are defined explicitly.
In a CRC index card, (choose all that apply) A) each row has two columns. B) write it in the corresponding right column if the responsibility involves a collaborator class. C) write the class name at the top of the card. D) write a responsibility on the left column.
A) each row has two columns. B) write it in the corresponding right column if the responsibility involves a collaborator class. C) write the class name at the top of the card. D) write a responsibility on the left column.
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) false true C) true true D) true false
A) false false
Invoking ________ returns the width of the string in a FontMetrics object fm. A) fm.stringWidth(s) B) fm.getWidth(s) C) fm.getHeight(s) D) getLength(s)
A) fm.stringWidth(s)
________ is a general binary relationship that describes an activity between two objects. A) Composition B) Association C) Inheritance D) Aggregation
B) Association
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"); } } A) ABCD B) BEDC C) CBAE D) AEDC E) BACD
B) BEDC
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. } A) The program has a syntax error on Line 4 because java.util.Calendar is an abstract class. B) The program has a syntax error on Line 6 because Calendar[1] is not of a GregorianCalendar type. C) The program has a syntax error on Line 5 because java.util.Calendar is an abstract class. D) The program has no syntax errors.
B) The program has a syntax error on Line 6 because Calendar[1] is not of a GregorianCalendar type.
Clicking the closing button on the upper-right corner of a frame generates a(n) ________ event. A) ComponentEvent B) WindowEvent C) ContainerEvent D) MouseMotionEvent E) ItemEvent
B) WindowEvent
An aggregation relationship is usually represented as ________ in ________. A) a method/the aggregating class B) a data field/the aggregating class C) a data field/the aggregated class D) a method/the aggregated class
B) a data field/the aggregating class
The size of ________ can grow and shrink at runtime. A) an array B) an ArrayList
B) an ArrayList
Assume Calendar calendar = new GregorianCalendar(). ________ returns the number of days in a month. A) calendar.get(Calendar.MONTH) B) calendar.getActualMaximum(Calendar.DAY_OF_MONTH) C) calendar.get(Calendar.WEEK_OF_MONTH) D) calendar.get(Calendar.WEEK_OF_YEAR) E) calendar.get(Calendar.MONTH_OF_YEAR)
B) calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
A frame can be placed inside a frame. A) true B) false
B) false
An interface cannot extend more than one interface. A) true B) false
B) false
Suppose you add the same button ten times into the container, the button will appear ten times in the container. A) true B) false
B) false
The Number class is an instance of Comparable. A) true B) false
B) false
The order of the components added into a container of FlowLayout is immaterial. A) true B) false
B) false
The visible property of a JFrame object is ________ be default. A) true B) false
B) false
You can always successfully cast a superclass to a subclass. A) true B) false
B) false
Given a Graphics object g, to draw a line from the upper left corner to the bottom right corner, you use ________. A) g.drawLine(0, 0, getWidth(), getWidth()) B) g.drawLine(0, 0, getWidth(), getHeight()) C) g.drawLine(0, 0, getHeight(), getHeight()) D) g.drawLine(0, 0, 100, 100)
B) g.drawLine(0, 0, getWidth(), getHeight())
Suppose a button jbt is placed in a frame, the coordinate of the button within the content pane of the frame is ________. A) cannot be obtained B) (0, 0) C) (jbt.getX(), jbt.getY()) D) (jbt.x, jbt.y)
C) (jbt.getX(), jbt.getY())
A faculty teaches a course. What is the best suitable relationship between Faculty and Course? A) None B) Inheritance C) Association D) Aggregation
C) Association
________ represents an exclusive ownership relationship between two objects. A) Aggregation B) Inheritance C) Composition D) Association
C) Composition
Which of the following are subclasses of java.awt.Component? (choose all that apply) A) Layout managers B) Helper classes such as Color and Font C) Container classes D) Swing user interface classes
C) Container classes D) Swing user interface classes
To get the x coordinate of the mouse pointer for the MouseEvent evt, you use ________. A) evt.getPoint().x B) evt.getX() C) Either A or B D) Neither A nor B
C) Either A or B
The default layout out of a JPanel is ________. A) GridLayout B) None C) FlowLayout D) BorderLayout
C) FlowLayout
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. } A) The program has a syntax error because numbers is declared as Number[], so you cannot pass it to Arrays.sort(Object[]). B) The program has a syntax error because numbers is declared as Number[], so you cannot assign {new Rational(1, 2), new Integer(4), new Double(5.6)} to it. C) 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. D) The program has a runtime error because numbers is declared as Number[], so you cannot assign {new Rational(1, 2), new Integer(4), new Double(5.6)} to it.
C) 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.
To divide BigDecimal b1 by b2 and assign the result to b1, you write ________. A) b2.divide(b1); B) b1 = b2.divide(b1); C) b1 = b1.divide(b2); D) b1 = b2.divide(b1); E) b1.divide(b2);
C) b1 = b1.divide(b2);
Which of the following classes cannot be extended? A) class A { private A();} B) class A { } C) final class A { } D) class A { protected A();}
C) final class A { }
Given a Graphics object g, to draw a filled oval with width 20 and height 30 centered at (50, 50), you use ________. A) g.fillOval(50, 50, 20, 30) B) g.fillOval(30, 30, 40, 30) C) g.fillOval(40, 35, 20, 30) D) g.fillOval(30, 30, 20, 30) E) g.fillOval(50, 50, 40, 30)
C) g.fillOval(40, 35, 20, 30)
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 j, k, m. B) An object of B contains data fields k, m. C) An object of B contains data fields j, m. D) An object of B contains data fields i, j, k, m.
D) An object of B contains data fields i, j, k, m.
Analyze the following code. Number[] numberArray = new Integer[2]; numberArray[0] = new Double(1.5); A) Since each element of numberArray is of the Number type, you cannot assign a Double object to it. B) You cannot use Number as a data type since it is an abstract class. C) Since each element of numberArray is of the Number type, you cannot assign an Integer object to it. 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.
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.
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 false and Program 2 displays false B) Program 1 displays true and Program 2 displays false C) Program 1 displays true and Program 2 displays true D) Program 1 displays false and Program 2 displays true
D) Program 1 displays false and Program 2 displays true
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 runtime error on b.i, because i is not accessible from b. B) The program has a compilation error because m is overridden with a different signature in B. C) The program has a compilation error because b.m(5) cannot be invoked since the method m(int) is hidden in B. D) The method m is not overridden in B. B inherits the method m from A and defines an overloaded method in B.
D) The method m is not overridden in B. B inherits the method m from A and defines an overloaded method in B.
Analyze the following code. import java.awt.*; import javax.swing.*; public class Test extends JFrame { public Test() { add(new MyDrawing("Welcome to Java!")); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class MyDrawing extends JPanel { String message; public MyDrawing(String message) { this.message = message; } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString(message, 20 ,20); } } A) The program would display Welcome to Java! if new JFrame() is replaced by new Test("My Frame"). B) The program would display Welcome to Java! if new JFrame() is replaced by Test(). C) The program runs fine and displays Welcome to Java! D) The program would display Welcome to Java! if new JFrame() is replaced by new Test().
D) The program would display Welcome to Java! if new JFrame() is replaced by new Test().
Given a Graphics object g, to draw an circle with radius 20 centered at (50, 50), you use ________. A) g.drawOval(50, 50, 40, 40) B) g.drawOval(30, 30, 20, 20) C) g.drawOval(50, 50, 20, 20) D) g.drawOval(30, 30, 40, 40)
D) g.drawOval(30, 30, 40, 40)
Given a Graphics object g, to draw a polygon to connect points (3, 3), (4, 10), (10, 20), (2, 100), you use ________. A) g.drawPolyline(new int[]{3, 4, 10, 2}, new int[]{3, 10, 20, 100}, 4) B) g.drawPolygon({3, 4, 10, 2}, {3, 10, 20, 100}, 4) C) g.drawPolyline({3, 4, 10, 2}, {3, 10, 20, 100}, 4) D) g.drawPolygon(new int[]{3, 4, 10, 2}, new int[]{3, 10, 20, 100}, 4)
D) g.drawPolygon(new int[]{3, 4, 10, 2}, new int[]{3, 10, 20, 100}, 4)
Given a Graphics object g, to draw a polyline to connect points (3, 3), (4, 10), (10, 20), (2, 100), you use ________. A) g.drawPolyline({3, 4, 10, 2}, {3, 10, 20, 100}, 4) B) g.drawPolygon({3, 4, 10, 2}, {3, 10, 20, 100}, 4) C) g.drawPolygon(new int[]{3, 4, 10, 2}, new int[]{3, 10, 20, 100}, 4) D) g.drawPolyline(new int[]{3, 4, 10, 2}, new int[]{3, 10, 20, 100}, 4)
D) g.drawPolyline(new int[]{3, 4, 10, 2}, new int[]{3, 10, 20, 100}, 4)
Swing components that don't rely on native GUI are referred to as ________. A) non-GUI components B) GUI components C) heavyweight components D) lightweight components
D) lightweight components
To a create an instance of BigDecimal for 454.45, use A) new BigInteger(454.45); B) BigInteger("454.45"); C) BigInteger(454.45); D) new BigInteger("454.45");
D) new BigInteger("454.45");
You can assign ________ to a variable of Object[] type. (choose all that apply) A) new double[100] B) new char[100] C) new int[100] D) new String[100] E) new java.util.Date[100]
D) new String[100] E) new java.util.Date[100]
You should override the ________ method to draw things on a Swing component. A) update() B) init() C) repaint() D) paintComponent()
D) paintComponent()
Which of the following methods override the equals method in the Object class? A) public static boolean equals(Object o) B) public void equals(Object o) C) public boolean equals(SomeType o) D) public boolean equals(Object o)
D) public boolean equals(Object o)
The method ________ sets the background color to yellow in JFrame f. A) f.setBackground(Color.yellow) B) f.setBackground(Color.YELLOW) C) f.setBackGround(Color.yellow) D) setBackground(Color.YELLOW) E) setBackground(Color.yellow)
D) setBackground(Color.YELLOW)
You use the keyword ________ to reference a method in the superclass from a subclass. A) that B) this C) superObject D) super
D) super
Analyze the following code. import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test extends A { public static void main(String[] args) { A frame = new Test(); frame.setSize(200, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } JButton jbtOK = new JButton("OK"); public Test() { getContentPane().add(jbtOK); jbtOK.addActionListener(this); } public void actionPerformed(ActionEvent e) { super.actionPerformed(e); if (e.getSource() == jbtOK) System.out.println("OK button is clicked"); } } class A extends JFrame implements ActionListener { JButton jbtCancel = new JButton("Cancel"); public A() { getContentPane().setLayout(new FlowLayout()); getContentPane().add(jbtCancel); jbtCancel.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == jbtCancel) System.out.println("Cancel button is clicked"); } } A) The program displays Cancel button on the left of the OK button. B) If the super.actionPerformed(e) statement in the actionPerformed method in the Test class is omitted, no message is displayed if you click the Cancel button. C) When you click the Cancel button the message "Cancel button is clicked" is displayed. D) When you click the OK button the message "OK button is clicked" is displayed. E) All of the above.
E) All of the above.
) Pressing a button generates a(n) ________ event. A) ContainerEvent B) ItemEvent C) MouseMotionEvent D) ActionEvent E) MouseEvent
E) MouseEvent