Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Reserved word wikipedia , lookup
Java syntax wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Structured programming wikipedia , lookup
Java (programming language) wikipedia , lookup
Class (computer programming) wikipedia , lookup
Name mangling wikipedia , lookup
Java ConcurrentMap wikipedia , lookup
Java performance wikipedia , lookup
Object-oriented programming wikipedia , lookup
CC316: Object Oriented Programming Lecture 14: Final Revision Dr. Manal Helal, Spring 2016. http://moodle.manalhelal.com 1 Q1) Object-oriented programming allows you to derive new classes from existing classes. This is called ________. A) inheritance B) encapsulation C) generalization D) abstraction A1) A Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 2 Q2) Suppose you create a class Cylinder to be a subclass of Circle. Analyse the following code: class Cylinder extends Circle { double length; Cylinder(double radius) { Circle(radius); } } A) The program compiles fine, but you cannot create an instance of Cylinder because the constructor does not specify the length of the cylinder. B) The program compiles fine, but it has a runtime error because of invoking the Circle class's constructor illegally. C) The program has a compile error because you attempted to invoke the Circle class's constructor illegally. A2) C Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Q3) Analyse the following code: (Choose all that apply.) public class Test extends A { public static void main(String[ ] args) { Test t = new Test(); t.print(); } A) The program does not compile because Test does not } have a default constructor Test(). class A { String s; B) The program would compile if a default constructor A() { } is added to class A explicitly. A(String s) { this.s = s; } C) The program compiles, but it has a runtime error due to the conflict on the method name print. public void print() { System.out.println(s); } } D) 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. A3) B, D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 4 Q4) Which of the statements regarding the super keyword is incorrect? A) You can use super to invoke a super class method. B) You cannot invoke a method in superclass's parent class. C) You can use super to invoke a super class constructor. D) You can use super.super.p to invoke a method in superclass's parent class. A4) D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 5 Q5) Which of the following statements are true? (Choose 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) 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. C) A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. D) Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. E) It is a compilation error if two methods differ only in return type in the same class. A5) A, B, C, D, E Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 6 Q6) Analyze the following code: (Choose 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); A) The program cannot be compiled, because } System.out.println(a1) is wrong and it should be replaced } by System.out.println(a1.toString()); class A { B) When executing System.out.println(a1), the toString() int x; method in the Object class is invoked. public String toString() { return "A's x is " + x; C) When executing System.out.println(a1), the toString() } method in the A class is invoked. } D) When executing System.out.println(a2), the toString() method in the Object class is invoked. A6) C, D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 7 Q7) Analyse the following code: Circle c = new Circle (5); Cylinder c = cy; A) The code is fine. B) The code has a runtime error. C) The code has a compile error. A7) C Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 8 Q8) Given the following code: 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? A) c3 instanceof C1 B) c1 instanceof C1 C) c4 instanceof C2 D) c2 instanceof C1 A8) C Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 9 Q9) You can assign ________ to a variable of Object[ ] type. (Choose all that apply.) A) new int[100] B) new String[100] C) new char[100] D) new java.util.Date[100] E) new double[100] A9) B, D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 10 Q10) You can create an ArrayList using ________. A) new ArrayList[ ] B) ArrayList() C) new ArrayList[100] D) new ArrayList() A10) D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 11 Q11) 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? A) protected B) public C) private D) Use the default modifier. A11) D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 12 Modifier on members in a class Accessed from the same class Accessed from the same package Accessed from a subclass Accessed from a different package public protected - default private - - - - - Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 13 Q12) Polymorphism means ________. A) that a class can extend another class B) that a variable of supertype can refer to a subtype object C) that data fields should be declared private D) that a class can contain another class A12) B Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 14 Q13) Composition means ________. A) that a variable of supertype can refer to a subtype object B) that a class can contain another class C) that a class can extend another class D) that data fields should be declared private A13) B Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 15 Q14) Every JavaFX main class ________. A) overrides start(Stage s) method B) implements javafx.application.Application C) extends javafx.application.Application D) overrides start() method A14) A, C Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 16 Q15) Which of the following statements are true? A) A scene is placed in the stage using the addScene method. B) A primary stage is automatically created when a JavaFX main class is launched. C) A scene is placed in the stage using the setScene method. D) You can have multiple stages displayed in a JavaFX program. E) A stage is displayed by invoking the show() method on the staged. A15) B, C, D, E Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 17 Q16) What is the output of the following JavaFX program? import javafx.application.Application; import javafx.stage.Stage; public class Test extends Application { public Test() { System.out.println("Test constructor is invoked."); } @Override // Override the start method in the Application class public void start(Stage primaryStage) { System.out.println("start method is invoked."); } public static void main(String[] args) { System.out.println("launch application."); Application.launch(args); } } A) Test constructor is invoked. start method is invoked. B) launch application. Test constructor is invoked. start method is invoked. C) start method is invoked. Test constructor is invoked. D) launch application. start method is invoked. Test constructor is invoked. E) launch application. start method is invoked. A16) B Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 18 Panes, UI Controls, and Shapes Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 19 Q17) Which of the following statements are true? A) A Control is a Node. B) A Pane is a Node. C) A Shape is a Node. D) A Stage is a Node. E) A Scene is a Node. A17) A, B, C Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 20 Q18) Which of the following statements are correct? A) Scene is a subclass on Node. B) Button is a subclass of Node. C) Every subclass of Node has a no-arg constructor. D) Pane is a subclass of Node. E) Circle is a subclass of Node. A18) B, C, D, E Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 21 Q19) Which of the following are binding properties? A) String B) IntegerProperty C) DoubleProperty D) Integer E) Double A19) B, C Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 22 Q20) What is the output of the following code? import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; public class Test { public static void main(String[] args) { IntegerProperty d1 = new SimpleIntegerProperty(1); IntegerProperty d2 = new SimpleIntegerProperty(2); d1.bind(d2); System.out.print("d1 is " + d1.getValue() + " and d2 is " + d2.getValue()); d2.setValue(3); System.out.println(", d1 is " + d1.getValue() + " and d2 is " + d2.getValue()); } } A) d1 is 2 and d2 is 2, d1 is 2 and d2 is 3 B) B) d1 is 2 and d2 is 2, d1 is 3 and d2 is 3 C) d1 is 1 and d2 is 2, d1 is 3 and d2 is 3 D) d1 is 1 and d2 is 2, d1 is 1 and d2 is 3 A20) B Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 23 The Color Class Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 24 Q21) Which of the following statements correctly creates a Color object? A) Color.color(0.3, 0.5, 0.5, 0.1); B) Color.color(0.3, 0.5, 0.5); C) new Color(0.3, 0.5, 0.5, 0.1); D) new Color(3, 5, 5, 1); E) new Color(0.3, 0.5, 0.5); A21) A, B, C Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 25 The Font Class FontDemo Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 26 Q22) Which of the following statements correctly creates a Color object? A) Font.font("Times", FontWeight.NORMAL, 34); B) Font.font("Times", 34); C) Font.font("Times", FontWeight.NORMAL, FontPosture.ITALIC, 34); D) new Font(34); E) new Font("Times", 34); A22) A, B, C, D, E Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 27 FlowPane ShowFlowPane Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 28 Q23) Which of the following statements are correct to create a FlowPane? A) new FlowPane(4, 5, Orientation.VERTICAL); B) new FlowPane(Orientation.VERTICAL); C) new FlowPane() D) new FlowPane(4, 5) A23) A, B, C, D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 29 The Delegation Model Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 30 Q24) A JavaFX action event handler is an instance of ________. A) ActionEvent B) EventHandler<ActionEvent> C) Action D) EventHandler A24) B Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 31 Q25) To register a source for an action event with a handler, use ________. A) source.setOnAction(handler) B) source.addAction(handler) C) source.addOnAction(handler) D) source.setActionHandler(handler) A25) A Example: btOK.setOnAction(handler1); Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 32 Selected User Actions and Handlers Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 33 Q26) Which of the following statements are true? A) A Button can fire a MouseEvent. B) A TextField can fire an ActionEvent. C) A Button can fire an ActionEvent. D) A Button can fire a KeyEvent. A26) A, B, C, D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 34 Q27) Which of the following statements are true? A) A Circle is a Shape. B) A Shape can fire an ActionEvent. C) A Text is a Shape. D) A Shape can fire a MouseEvent. E) A Shape can fire a KeyEvent. A27) A, C, D, E Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 35 Q28) Which statement is true about a non-static inner class? A) It is accessible from any other class. B) It must be final if it is declared in a method scope. C) It can access private instance variables in the enclosing object. D) It can only be instantiated in the enclosing class. E) It must implement an interface. A28) C Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 36 Q29) Which of the following statements are true? A) An anonymous inner class is an inner class without a name. B) 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(). C) An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause. D) An anonymous inner class is compiled into a class named OuterClassName$n.class. E) An anonymous inner class must implement all the abstract methods in the superclass or in the interface. A29) A, B, C, D, E Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 37 Q30) Which of the following statements are true? (Choose all that apply.) A) An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class. B) An inner class can be declared public or private subject to the same visibility rules applied to a member of the class. C) Inner classes can make programs simple and concise. D) 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. A30) A, B, C, D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 38 Q32) Suppose A is an anonymous inner class in Test. A is compiled into a file named ________. A) Test&1.class B) Test$A.class C) A.class D) Test$1.class E) A$Test.class A32) D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 39 A33) A Q33) Analyze the following code. import import import import import import public } A) The message "The OK button is clicked" is displayed when you click the OK button. B) The program has a runtime error because no handlers are registered with btOK. C) The program has a compile error because no handlers are registered with btOK. D) The handle method is not executed when you click the OK button, because no handler is registered with btOK. javafx.application.Application; javafx.event.ActionEvent; javafx.event.EventHandler; javafx.scene.Scene; javafx.scene.control.Button; javafx.stage.Stage; class Test extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { Button btOK = new Button("OK"); btOK.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { System.out.println("The OK button is clicked"); } }); Scene scene = new Scene(btOK, 200, 250); primaryStage.setTitle("MyJavaFX"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } public static void main(String[] args) { launch(args); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 40 Q34) Which of the following code correctly registers a handler with a button btOK? A) btOK.setOnAction(e -> {System.out.println("Handle the event");}); B) btOK.setOnAction((e) -> System.out.println("Handle the event");); C) btOK.setOnAction((ActionEvent e) -> System.out.println("Handle the event")); D) btOK.setOnAction(e -> System.out.println("Handle the event")); A34) A, B, C, D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 41 Q35) To handle the mouse click event on a pane p, register the handler with p using ________. A) p.setOnMousePressed(handler); B) p.setOnMouseDragged(handler); C) p.setOnMouseClicked(handler); D) p.setOnMouseReleased(handler); A35) C Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 42 Q36) To handle the key pressed event on a pane p, register the handler with p using ________. A) p.setOnKeyPressed(handler); B) p.setOnKeyClicked(handler); C) p.setOnKeyTyped(handler); D) p.setOnKeyReleased(handler); A36) A Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 43 Q37) Which of the following statements is correct? A) Generics can make programs easy to read. B) Generics can avoid cumbersome castings. C) Generics can make programs run faster. D) Generics can help detect type errors at compile time, thus make programs more robust. A37) A, B, D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 44 Q38) Which of the following statements is correct? A) Comparable<String> c = new Date(); B) Comparable<String> c = new String("abc"); C) Comparable<String> c = "abc"; D) Comparable<Object> c = new Date(); A38) B, C Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 45 Q39) Suppose List list = new ArrayList(). Which of the following operations are correct? A) list.add(new Integer(100)); B) list.add(new ArrayList()); C) list.add("Red"); D) list.add(new java.util.Date()); A39) A, B, C, D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 46 Q40) To declare a class named A with a generic type, use ________. A) public class A(E, F) { ... } B) public class A(E) { ... } C) public class A<E, F> { ... } D) public class A<E> { ... } A40) D Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 47 Q41) The method header is left blank in the following code. Fill in the header. public class GenericMethodDemo { public static void main(String[] args ) { Integer[] integers = {1, 2, 3, 4, 5}; String[] strings = {"London", "Paris", "New York", "Austin"}; print(integers); print(strings); } ________ { for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); System.out.println(); } } A) public static void print(Object[] list) B) public static <E> void print(E[] list) C) public static void print(int[] list) D) public static void print(Integer[] list) E) public static void print(String[] list) A41) A, B Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 48