SWE211 Final Revision Fall 2024 PDF

Summary

This document contains a set of multiple choice questions and short answer questions related to Java Programming. The questions cover topics such as static methods, instance methods, ArrayList, and other aspects of programming. The document appears to be a revision document for a Java programming course.

Full Transcript

Object Oriented Programming SWE211 Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 2 Suppose...

Object Oriented Programming SWE211 Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 2 Suppose the xMethod() is invoked from a main method in a MCQ class as follows, public static void main(String[] args) { xMethod(); } xMethod() is _________ in the class. a. a static method b. an instance method c. a static method or an instance method d. None of them Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 3 If a variable is declared static in a particular class, what does that MCQ mean? a) There is only one copy of it that all instances of the class can access or share b) It cannot be changed c) It can only be changed by the instances of the class in which it is declared d) Each instance of the class has its own copy of the variable Declare and construct an ArrayList with an initial capacity of 20 references to Object a) ArrayList list = new ArrayList(20) ; b) ArrayList[Object] list = new ArrayList(20) ; c) ArrayList list = new ArrayList() ; d) Object list(20) = new ArrayList() ; Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 4 MCQ Which of these cannot be declared static? a) Object b) Constants c) Method d) variable Which of the following statements are true? i. A subclass is a subset of a superclass. ii. A subclass is usually extended to contain more functions and more detailed information than its superclass. iii. "class A extends B" means A is a subclass of B. iv. "class A extends B" means B is a subclass of A. a) ii and iii only b) i only c) iii only d) i, ii and iii only e) iv only Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 5 Given a class named Student, which of the following is a valid constructor declaration for the class? MCQ a) Student (Student s) { } b) Student Student ( ) { } c) Static void Student(){ } d) Void Student ( ) { } Which option can we use in the try block to stop the execution of the “finally” block? a) return b) break c) continue d) No correct answer Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 6 What is printed as a result of executing the following code segment? MCQ a) [7, 2, 1, 2] b) [7, 1] c) [7, 2, 1] d) None of the mentioned What is printed as a result of executing the following code segment? a) [1, 4, 5] b) [1, 2, 3, 4, 5] c) [2, 4, 5] d) [1, 4, 3, 5] Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 7 Examine the following code: MCQ Which of the following will change the list so that it looks like? a) list.remove( list.size()-1 ); b) list.remove( list.size() ); c) list.remove( 5 ); d) list.clear( "Elmo" ); What is the output of the following java code? a) [A,B,A,null] b) [A, B,A] c) [A,B,null] d) Compiler error Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 8 Examine the following code: MCQ What is the output of this java code? a) Nothing is printed b) 5 c) 0 d) True What is the output of the following java code? a) [7,1,5] b) [7,5,1] c) [7, 1, 5, 1] d) [7, 1, 5] Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 9 Analyze the following code. MCQ a) The program has a compile error because the NClass class has a private constructor. b) The program does not compile because the parameter list of the main method is wrong. c) The program compiles, but has a runtime error because t has no initial value. d) The program compiles and runs fine. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 10 Analyze the following code. MCQ The code has a compile error because asList(array) requires that the array A. elements are objects. The code has a compile error because an integer such as 1 is automatically B. converted into an Integer object, but the array element type is Double. C. The code is correct and displays [1, 2, 3]. D. The code is correct and displays [1.0, 2.0, 3.0]. E. None of the mentioned Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 11 Analyze the following code. MCQ a) The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B. b) The program has a compile error, because m is overridden with a different signature in B. c) The program has a compile error, because b.m(5) cannot be invoked since the method m(int) is hidden in B. d) The program has a runtime error on b.i, because i is not accessible from b. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 12 Every JavaFX main class __________. a. b. overrides start(Stage s) method implements javafx.application.Application MCQ c. overrides run() method d. None of them Which of the following statements is true? a. A Node can be placed in a Pane. b. A Node can be placed in a Scene. c. A Pane can be placed in a Control. d. A Shape can be placed in a Control. To place a node in the left of a BorderPane p, use ___________. a p.setLeft(node); b p.setEast(node); c p.placeLeft(node); d p.left(node); e p.setWest(node); Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 13 MCQ Which of the following methods is NOT defined in the Animation class? a. resume() b. pause() c. play() d. stop() To remove a node from the pane, use ______. a. pane.remove(node); b. pane.removeAll(node); c. pane.getChildren().remove(node); d. Cannot remove a node from a pane Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 14 MCQ What is the output of the following JavaFX program? launch application. a. Test constructor is invoked. start method is invoked. launch application. b. start method is invoked. Test constructor is invoked. c. Test constructor is invoked. start method is invoked. d. start method is invoked. Test constructor is invoked. e. launch application. start method is invoked. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 15 MCQ Which of the following statements is correct to create a DataOutputStream to write to a file named out.dat? A. DataOutputStream outfile = new DataOutputStream(new File("out.dat")); B. DataOutputStream outfile = new DataOutputStream(new FileOutputStream("out.dat")); C. DataOutputStream outfile = new DataOutputStream(FileOutputStream("out.dat")); D. DataOutputStream outfile = new DataOutputStream("out.dat"); Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 16 After the following program is finished, how many bytes are written to the file t.dat? MCQ import java.io.*; public class Test { public static void main(String[] args) throws IOException { DataOutputStream output = new DataOutputStream( new FileOutputStream("t.dat")); output.writeShort(1234); output.writeShort(5678); output.close(); } } A. 2 bytes. B. 4 bytes. C. 8 bytes. D. 16 bytes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 17 T/F  Interfaces can be instantiated f  The modifiers public and static can be written in either order "public static" or "static public". t  Abstraction is supported by method overriding in java f  You can override a static method defined in a superclass f  You can write a constructor for an abstract class t  If AbsClass is an abstract class, the expression x instanceof AbsClass is always false. f  Classes, methods, and instance variables can all be declared final t Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 18 instanceof class ParentClass { // Class members o instanceof evaluates } class ChildClass extends ParentClass { to true if the left-hand- // Class members side object is a child } of the right-hand-side public class JavaTest { class or is the same public static void main(String[] args) { ChildClass childObj = new ChildClass(); type as the class System.out.println( provided. childObj instanceof ParentClass); System.out.println( childObj instanceof Object); System.out.println( childObj instanceof ChildClass); } } Output: true true true Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 19 instanceof import java.io.Serializable; o instanceof evaluates to class ChildClass implements Serializable{ true if the left-hand-side // Class members object is a child of the } right-hand-side class or public class JavaTest { is the same type as the public static void main(String[] args) { class provided. ChildClass childObj = new ChildClass(); System.out.println( o It also works with childObj instanceof Serializable); interfaces. } } Output: true Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 20 instanceof import java.io.Serializable; import java.util.Scanner; class ChildClass implements Serializable{ // Class members } o However, it gives a public class JavaTest { compile time error if it public static void main(String[] args) { will be always false. ChildClass childObj = new ChildClass(); System.out.println( childObj instanceof Serializable); System.out.println( childObj instanceof Scanner); } } Output: Compile time Error Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 21 instanceof class ParentClass { // Class members } class ChildClass extends ParentClass { // Class members } o However, it gives a public class JavaTest { compile time error if it public static void main(String[] args) { will be always false. ChildClass childObj = null; o A null reference is not System.out.println( childObj instanceof ParentClass); an instanceof any class. System.out.println( childObj instanceof Object); System.out.println( childObj instanceof ChildClass); } } Output: false false false Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 22 instanceof class ParentClass { // Class members } class ChildClass extends ParentClass { // Class members } o However, it gives a public class JavaTest { compile time error if it public static void main(String[] args) { will be always false. ParentClass childObj = new ChildClass(); o A null reference is not an System.out.println( childObj instanceof ParentClass); instanceof any class. System.out.println( o Usually, it is used with childObj instanceof Object); different actual and System.out.println( declared types. childObj instanceof ChildClass); } } Output: true true true Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 23 instanceof class ParentClass { // Class members } class ChildClass extends ParentClass { // Class members } o However, it gives a class OtherClass extends ParentClass{ compile time error if it // Class members will be always false. } o A null reference is not an public class JavaTest { public static void main(String[] args) { instanceof any class. ParentClass obj = new ChildClass(); o Usually, it is used with System.out.println( different actual and obj instanceof OtherClass); declared types. // No compile time error } } Output: false Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 24 o Write a program to count the number of lines in a text file. import java.io.File; o How to count the number import java.io.FileNotFoundException; of words? import java.util.Scanner; public class JavaTest { public static void main(String[] args) { File f = new File("file.txt"); try { Scanner file = new Scanner(f); int c = 0; while(file.hasNext()){ c++; file.nextLine(); } System.out.print("Number of lines = "); System.out.println(c + " lines"); file.close(); } catch (FileNotFoundException e) { Output: System.out.println(e); } Number of lines = 2 lines } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 25 o How to count the number of words? o How to get file name as import java.io.File; user input? import java.io.FileNotFoundException; import java.util.Scanner; public class JavaTest { public static void main(String[] args) { System.out.print("Enter file name: "); Scanner s = new Scanner(System.in); File f = new File("file.txt"); try { Scanner file = new Scanner(f); int c = 0; while(file.hasNext()){ c++; file.next(); } System.out.print("Number of words = "); System.out.println(c + " words"); file.close(); } catch (FileNotFoundException e) { Output: System.out.println(e); } Number of words = 4 words } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 26 o Write a program to count the number of lines in a text file. import java.io.File; o How to count the number import java.io.FileNotFoundException; of words? import java.util.Scanner; public class JavaTest { o How to get file name as public static void main(String[] args) { user input? System.out.print("Enter file name: "); Scanner s = new Scanner(System.in); File f = new File(s.nextLine()); try { Scanner file = new Scanner(f); int c = 0; while(file.hasNext()){ c++; file.next(); } System.out.print("Number of words = "); System.out.println(c + " words"); file.close(); } catch (FileNotFoundException e) { Output: System.out.println(e); Enter file name: file.txt } Number of words = 4 words } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 27 Code import java.util.ArrayList; import java.util.Arrays; Write a java method that public class JavaTest { takes an array of different public static int[] integers(Object[] in) { ArrayList ints=new ArrayList(); elements’ type and return an for(Object o : in) array of only the ints. if(o instanceof Integer) ints.add((Integer)o); int[] out = new int[ints.size()]; For example: for(int i=0; i

Use Quizgecko on...
Browser
Browser