CS3330 Object Oriented Programming (Java) Professor Wergeles Midterm Fall 2018 Mizzou Practice Exam

¡Supera tus tareas y exámenes ahora con Quizwiz!

30. To declare a class named A with a single generic type, which of the following should be used? A. public class A<E> { } B. public class A<E, F> { } C. public class A(E) { } D. public class A(E, F) { }

Answer: A <E> is a way of telling the class it follows to use a single generic type. A is correct. B is incorrect because it specifies the use of two generic types. C and D are complete garbage.

24. True or False: An abstract class can have non-abstract methods. A. True B. False

Answer: A An abstract class can have abstract methods, but it can also have non-abstract (defined) methods. If a method of a class is abstract, then the class must also be abstract. But if a class is abstract it can have zero or more abstract methods and can have non-abstract methods.

23. Which of the following always needs to start with a Capital letter? A. class names B. objects and class names C. fields D. data types and fields

Answer: A Class names start with a capital letter. (Interface and enumeration names also start with a capital letter.) Variables that refer to objects (instances) and fields are to start with lowercase letters. Data types that are classes start with uppercase letters and data types that are primitive types begin with lower case letters. Constants are typically all uppercase.

6. What is the minimum number of methods that must be defined in classD for it to compile with no errors? public interface InterfaceC { void methodC(); } public interface InterfaceD extends InterfaceC { void methodD(); } public class ClassC implements InterfaceC { public void methodC() {} public void methodD() {} } public class ClassD extends ClassC implements InterfaceD { public ClassD() {} } A. No particular methods are required B. methodC C. methodD D. methodC and methodD E. methodC, methodD, and toString

Answer: A InterfaceC and InterfaceD declare abstract methods that must be implemented for any class that implements them to be concrete (not abstract). InterfaceC requires methodC(). InterfaceD requires methodD(). ClassC implements InterfaceC and it defines methodC() and methodD(). ClassC therefore does everything it needs to do as required by InterfaceC that requires methodC() to be defined. ClassC does define methodC(). ClassD extends ClassC and therefore gets methodC() and methodD() as a result of that inheritance. So, even though ClassD implements InterfaceD is doesn't need to define methodD() because it got the definition from ClassC. Therefore for ClassD everything is defined that needs to be defined.

32. Suppose: ArrayList<String> list = new ArrayList<>(); Which of the following operations are correct? A. list.add("Mizzou"); B. list.add(new Integer(100)); C. list.add(new java.util.Date()); D. list.add(new ArrayList<String>());

Answer: A The <String> indicates the use of generics. See: https://docs.oracle.com/javase/tutorial/java/generics/why.html http://www.tutorialspoint.com/java/java_generics.htm The <String> after ArrayList indicates that the ArrayList is to hold String objects. "Mizzou" is a String so adding it to the list makes sense. "new Integer(100)" creates an Integer object, not a String. So, it can't be added to the list because it is the wrong type. "new java.util.Date()" creates a Date object, not a String. So, it can't be added to the list because it is the wrong type. "new ArrayList<String>()" creates an ArrayList object, not a String. So, it can't be added to the list because it is the wrong type. This: ArrayList<String> list = new ArrayList<>(); Could be written like this: ArrayList<String> list = new ArrayList<String>(); But, this is preferred: ArrayList<String> list = new ArrayList<>(); The "new ArrayList<>();" knows what to use for the generic type based on the variable it is being assigned to: ArrayList<String> list. So, putting <String> both placed is redundant, but not incorrect.

27. What is the process of defining more than one method in a class differentiated by method signature? A. Function overriding B. Function overloading C. Function doubling D. None of the above

Answer: B "defining more than one method in a class differentiated by method signature" is the definition of overloading. Overriding happens when the method signatures are the same and one method replaces (overrides another). "Function doubling" is meaningless.

34. Is ArrayList<Integer> a subclass of ArrayList<Object>? A. Yes B. No

Answer: B <Integer> and <Object> indicate the generic types for the ArrayList class. ArrayList<Integer> is an ArrayList. ArrayList<Object> is an ArrayList. They are the same type: ArrayList. There is no subclassing going on here. The difference between the two ArrayLists is that one has been told to hold Integers and the other has been told to hold Objects.

44. Which of the following classes cannot be extended? A. public class A { } B. final public class A { } C. class A { } D. public class A { private int i = 0; }

Answer: B A class that is final cannot be extended because final means it cannot be change. A final field cannot have its value changed. A final method cannot be overridden. A final class cannot be extended. The class in answer A is a normal public class and can be extended. The class in answer C has a default access level and can be extended if in the same package. The class in answer D is a normal public class and can be extended. That is contains a private field has no effect on the ability to extend the class. The private field simply will not be accessible to the subclass.

11. Which of the following is true? A. Abstract and final can be declared on a class B. Abstract and final are contradictory and cannot be declared on a class C. Interfaces can be declared as private D. Interface methods can be declared private

Answer: B A is not true because final means a class can't be extended and abstract means a class must be extended to create a concrete class. A class can't be both abstract and final because an abstract class only has value if it can be extended and that can't happen if it is final. If an interface is private then it can't be accessed by any classes. The whole purpose of an interface is to implement it and that can't happen if it is private. Therefore, C is not true. D is not true because interface methods are public by default and cannot be declared as private. And, if you could declare them as private it would make no sense since they are abstract and need to be defined. If they were private a class that implemented the interface couldn't access the method declarations if they were private.

25. Choose the best definition of an object in the context of this course. A. a thing B. an instance of a class C. something you wear

Answer: B An object is an instance of a class. Instances of a class are objects. Object instances are created using the new keyword.

47. Which of the following declares an abstract method in an abstract Java class? A. public abstract method(); B. public abstract void method(); C. public void abstract Method(); D. public void method() {} E. public abstract void method() {}

Answer: B Answer A is missing the return type on the method. Answer C has the return type (void) at the wrong place. "void" should be immediately to the left of the method. In addition M in Method() is the wrong case for a method which should begin with a lower case letter. Answer D is missing "abstract" which is needed to make the method abstract in a class. Answer E has { } after the method which means the method is defined. An abstract method is declared but not defined. { } after the method() defines it by creating a code block. Even though the code block is empty it is a code block and therefore defines it! This is not allowed with an abstract method.

22. Given the declaration : int [ ] ar = {5,4,3,2,1}; What is the value of ar[2]? A. 2 B. 3 C. 4 D. 5

Answer: B Arrays are zero-based. The first item, which is 5, in the array is at index 0. An index of 2 points to the third item in the array. Therefore, the correct answer is 3 since it is the third item in the array.

3. Given: C and D are classes E and F are interfaces Which of the following cannot be true? A. D extends C B. C implements D C. C implements E D. D implements E, F

Answer: B C cannot implement D because D is a class and not an interface. You can only implement interfaces. D can extend C because D and C are classes. C can implement E because C is a class and E is an interface. D can implement E and F because D is a class and both E and F are interfaces.

49. Suppose C is an abstract class, D is a concrete subclass of C, and both C and D have a no-arguments constructor. Which of the following is correct? A. C c = new C(); B. C c = new D(); C. D d = new C();

Answer: B D is a subclass of C therefore it can be treated like a D or a C. C is abstract so no instance of it can be created. A is incorrect because an instance of C cannot be created because C is abstract. B is correct because D objects can be treated like C objects since D inherits from C. C is incorrect because an instance of C cannot be created because C is abstract. And, even if an C could be created it could not be treated like a D object. (From class example: A Dog can be treated like a Pet because Dog inherits from Pet. If you create Pet object it cannot be treated like a Dog.)

5. What is the size of a variable of type float in Java? A. 2 bytes B. 4 bytes C. 8 bytes D. It depends on the compiler setting E. It depends on the operating system

Answer: B Google: Java double number of bytes https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html "float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform." There are 8 bits in a byte. 32 bits is therefore 4 bytes (8 x 4 = 32). A float takes 4 bytes.

26. If class A implements an interface does it need to implement all methods of that interface? A. Yes, always. B. No, not when A is abstract

Answer: B If class A is concrete all methods must be defined. If class A implements an interface and it is concrete it must define (implement) all of the methods of the interface. BUT, if class A is itself abstract then it does not have to define all of the methods that are abstract. So if class A implements an interface it does not need to implement all of the methods of the interface if it is itself abstract.

18. With inheritance, a derived subclass object can directly access any A. public or private superclass member B. private superclass member C. public or protected superclass member D. protected, public or private superclass member

Answer: C A is wrong because private does not allow access by a subclass. B is wrong because private does not allow access by a subclass. D is wrong because private does not allow access by a subclass. C is correct because both public and protected allow a subclass to access a superclass's members.

17. If none of the private/protected/public is specified for a member, that member ... A. is accessible publicly B. is only accessible by other classes of the same package C. is only accessible from within the class D. is accessible by the class and its subclasses

Answer: B If private, protected, or public is not specified then it is the default modifier. The default modifier provides "package access" that allows classes in the same package to access the member. The default modifier is not the same as public access, so A is not correct. Public access allows access from everywhere. In C "is only accessible from within the class" refers to private access, so it is not correct. In D "is accessible by the class and its subclasses" refers to protected access, so it is not correct.

45. The signature of a method in Java consists of ____________________ A. method name B. method name and parameter list C. return type, method name, and parameter list D. parameter list

Answer: B In the Java programming language, a method signature is the method name and the number and type of its parameters. Return types and thrown exceptions are not considered to be a part of the method signature.

7. Which of the following statements causes a syntax error? public interface InterfaceA { void methodA(); } public interface InterfaceB extends InterfaceA { void methodB(); } public class ClassA implements InterfaceA { public void methodA() {} public void methodB() {} } public class ClassB extends ClassA implements InterfaceB { public ClassB() {} ... <methods not shown> } A InterfaceA obj = new ClassA(); B. InterfaceB obj = new ClassA(); C. InterfaceA obj = new ClassB(); D. InterfaceB obj = new ClassB(); E. ClassA obj = new ClassB();

Answer: B InterfaceB obj = new ClassA(); causes a syntax error because a ClassA object cannot be treated like an InterfaceB object. ClassA implements InterfaceA. A ClassA object can be treated like an InterfaceA object. ClassA has no relationship with InterfaceB. The code in A is fine. A ClassA object can be treated like an InterfaceA object because ClassA implements InterfaceA. The code in C is fine. A ClassB object can be treated like an InterfaceA object because ClassB extends ClassA and ClassA implements InterfaceA. A ClassB object can be treated like a ClassA object or an InterfaceA object. The code in D is fine. A ClassB object can be treated like an InterfaceB object because ClassB implements InterfaceB. The code in E is fine. A ClassB object can be treated like a ClassA object because ClassB extends ClassA. Always look at the ancestry of a class to see what it can become. Anything the class extends or implements up through the hierarchy it can become.

19. What is the keyword used in Java to create an object? A. this B. new C. sync D. new() E. self

Answer: B The "new" keyword is used to create objects in Java. The "this" keyword is used to access the instance from within the class. "sync" is meaningless here. "synchronized" is used in threaded applications. "new()" is not a keyword. Parentheses don't go after new. "self" is what some other OO languages used to access the instance from within the class.

35. Which of the following methods is available on every object instance in Java? A. println() B. equals() C. thread() D. start() E. getPackage() F. implode()

Answer: B The documentation for the Object class lists the methods available to it. Since all objects inherit from the Object class in Java they all have these methods available to them. http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html equals() is available on every object instance in Java.

40. Which of the following code is correct? A. ArrayList<Double> list = new ArrayList<>(); list.add(4); B. ArrayList<Double> list = new ArrayList<>(); list.add(4.6); C. ArrayList<Integer> list = new ArrayList<>(); list.add(4.6); D. ArrayList<Integer> list = new ArrayList<>(); list.add("4");

Answer: B This is a generics example. In each case generics is used to indicate the type of objects held by the ArrayList. In A it is Doubles. In B it is Doubles. In C is is Integers. In D it is Integers. A is incorrect because 4 is a Integer and it can't be added to a list of Doubles. B is correct because 4.6 is a Double and it can be added to a list of Doubles. C is incorrect because 4.6 is a Double and it can't be added to a list of Integers. D is incorrect because "4" is a String and it can't be added to a list of Integers.

33. To declare an interface named A with two generic types, use A. public interface A<E> { } B. public interface A<E, F> { } C. public interface A(E) { } D. public interface A(E, F) { }

Answer: B public interface A<E, F> { } is the correct syntax for indicating that interface A should use two generic types. A is wrong because it specifies only one generic type. C and D are garbage.

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

Answer: C "accessible to any subclasses in any package" is the purpose of the protected access level. A is incorrect because public provides no restrictions. B is incorrect because it restricts to within the class itself. D is incorrect because the default modifier restricts the package.

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

Answer: C "that data fields should be declared private" Is not polymorphism. Making data fields private is often a good idea so you can control access to them through getters and setters. "that a class can extend another class" is inheritance and is fundamental to object oriented programming. Inheritance is required to get polymorphism. But, polymorphism is something more... it is the ability to use the inheritance in a way that allows a subtype to be treated like a supertype. (From example in class: a Dog can be treated like a Pet because Dog extends Pet.) A class can contain another class. That is called an inner class. The use of inner classes is not related to polymorphism. It is a way to control access.

16. The Size enum is defined as: public enum Gender { SMALL, MEDIUM, LARGE } Which of the following is correct for assigning a gender to a variable s of type Size? A. Size s = "MEDIUM"; B. Size s = new Size(MEDIUM); C. Size s = Size.MEDIUM; D. Size s = Size->MEDIUM;

Answer: C A is not correct mainly because "MEDIUM" is a String object...not an element of an enumeration. B. is garbage. C is correct because Size is an enumeration. MEDIUM is a member of that enumeration and Size.MEDIUM is how you access the member of an enumeration. D is garbage.

28. Class B inherits from Class A, what cannot be said: A. B is a sub-class of A B. A is a super-class of B C. B has access to private members of A D. B has access to protected members of A

Answer: C A subclass can access the private members of a superclass. Private restricts access to within the class itself. So, C is correct because B is a subclass of A (B inherits from A). If B inherits from A, then: • B is a sub-class of A • A is a super-class of B The protected access level allows a subclass to access the protected members of the superclass. B extends A (B inherits from A) and therefore can access protected members of A.

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

Answer: C ArrayList documentation (Google: Java ArrayList): https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html "Chicago" needs to be added as index position 1 to put it after "Atlanta" as position 0.

43. Which statements are most accurate regarding the following classes? class C { private int s; protected int t; } class D extends C { private int x; protected int y; // some methods omitted } A. In the class D, an instance method can only access x, y. B. In the class D, an instance method can only access t, y. C. In the class D, an instance method can only access t, x, y. D. In the class D, an instance method can access s, t, x, y.

Answer: C D extends C, and C has one private field (s) and one protected field (t). A private field is not accessible to a subclass. A protected field is accessible to a subclass. This means from D the field t can be accessed but s cannot be accessed. D has access to all of its own members regardless of access level. So, x and y can be access by D. This means D can access x, y, and t but cannot access s. A is wrong because more than x and y can be access by D. t can be accessed. B is wrong because x can be accessed by D because it is a field in D. That x is private has no effect on D's ability to access it because it is defined in D. D is wrong because s cannot be accessed. In C, which D extends, s is private and therefore cannot be accessed D.

21. Which of the following prototypes shows that a Coffee subclass is derived from a superclass called Drink? A. class Drink extends Coffee B. class Coffee derived Drink C. class Coffee extends Drink D. class Drink derived Coffee E. class Coffee implements Drink

Answer: C If Coffee is the subclass and Drink is the superclass, then Coffee extends Drink. Answer A is backwards. Answers B and D are garbage. E is indicating that Drink is an interface that Coffee implements which is not what was stated.

13. How can you force a non-abstract (concrete) class to implement a set of methods? A. Make the class inherit from an abstract class. B. Make the class implement an interface. C. Both of the above. D. You can not.

Answer: C If a class is concrete (non-abstract) then it must implement any abstract methods as a result of inheritance (extends) or through application of an interface (implements). So, both A and B are ways to force a non-abstract (concrete) class to implement a set of methods. An abstract class that inherits from an abstract class or implements an interface (which has all abstract methods) is not forced to implement the abstract methods. This is only the case because it is abstract itself. A concrete class must be fully defined because it can be used to create objects and they have to function!

1. Which of the following is a Java keyword? A. current B. last C. finally D. protect

Answer: C Java keywords (I googled: Java keywords): http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html current is not in the list of keywords last is not in the list of keywords finally IS IN the list of keywords protect is not in the list of keywords (protected is in the list) Keywords are reserved for use by the language. Language keywords are not to be used as the names for variables or classes. You should know or have ready access to the keywords so you don't use them incorrectly. Many IDEs will warn you if you try to use a keyword incorrectly, but you can't always count on that.

20. The last value in an array called names can be found at index: A. 0 B. 1 C. names.length - 1 D. names.length

Answer: C Since arrays are zero-based...that is, the first item in the array is at index 0, the last value is at the length of the array minus 1.

14. If classes Student, Staff, and Faculty extend class Person, which one makes sense: A. Faculty[] faculties={new Person(), new Staff(), new Student()}; B. Staff[] staff={new Person(), new Faculty(), new Student()}; C. Person[] persons={new Faculty(), new Staff(), new Student()};

Answer: C Student, Staff, and Faculty all inherit from (extend) Person, so they can all be treated like (cast as) Person objects. A and B are wrong because the objects being created can't be cast as the type indicated for the array they are to be assigned to. A Person object, if created directly, cannot be treated like a Faculty, Staff or Student object. A Staff object and a Student object cannot be cast as Faculty objects. A Faculty object and a Student object cannot be cast as Staff Objects. Faculty, Staff, and Student objects are Person objects. This is like a Dog and a Cat are Pet objects, from the example in class, because Dog and Cat inherit from Pet. A Dog can't be cast as a Cat. A Cat can't be cast as a Dog.

15. Choose the appropriate data type for this value: 0 A. float B. boolean C. int D. double

Answer: C The 0 is an int. It is not a float because a float has to have a decimal part. A boolean is a true or false value (not a 1 or 0). It is not a double because a double has to have a decimal part.

10. What can the 'super' keyword be used to do? A. Call the super class's constructor. B. Access the super class's members. C. Both of the above. D. None of the above.

Answer: C The super keyword in Java can be used to access the superclass's constructor. The super keywords can also be used to access fields or methods of a superclass if the access levels on the fields or methods allow it. Answer C is correct because A and B can both be done using super. https://docs.oracle.com/javase/tutorial/java/IandI/super.html

31. To create a list to store integers, use A. ArrayList<Object> list = new ArrayList<int>(); B. ArrayList<int> list = new ArrayList<int>(); C. ArrayList<Integer> list = new ArrayList<>(); D. ArrayList<Integer> list = new ArrayList<Number>();

Answer: C This is a generics question. This is correct: ArrayList<Integer> list = new ArrayList<>(); This would also work but is not listed: ArrayList<Integer> list = new ArrayList<Integer>(); This is preferred: ArrayList<Integer> list = new ArrayList<>(); A is not correct mostly because "int" is not an object type. This is not allowed because int is not a class: ArrayList<int> B is not correct for the same reason as A. D is not correct because the Number type cannot be treated like an Integer type. Integer inherits from Number, but so do other numerical types. An Integer can be a Number, but a Number is not necessarily an Integer. Answer C very specifically is the way one declares an Integer ArrayList and assigns it an instance.

4. Which of the following is not a method of java.util.ArrayList? A. add(Object x); B. remove(Object x); C. insert(int i, Object x); D. contains(Object x); E. set(int i, Object x);

Answer: C java.util.ArrayList documentation (I googled: java.util.ArrayList): https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html add() is in documentation remove() is in documentation insert() is NOT in the documentation contains() is in the documentation set() is in the documentation I used the browser search feature to search for each method in the documentation.

12. Which of the following statements is true? A. A static variable cannot have its value set in a constructor B. An instance variable can't be declared final C. A static variable must be declared final D. A static method can't access an instance variable E. Only a static method can access a static variable

Answer: D A static method can't access an instance variable because it is a method on the class rather than an instance. A static method doesn't have an instance to access. "this" is not defined in a static method. A static variable can have its value set in a constructor. A static variable is a variable set on the class rather than an instance. As long the access level on the static variable allows it to be accessed its value can be set anywhere the access level allows including in a constructor. An instance variable can be declared final. It just means that the value or reference it is set to cannot be changed once set. A static variable does not have to be declared as final. It can be declared as final (not changeable) but it doesn't have to be declared final. A static variable can be accessed anywhere its access level allows. A static variable can be accessed in an instance method or a static method. A static variable is a class variable and is referenced from the class name.

29. In JavaFX which of the following can be set as the root UI element for a Scene? A. AnchorPane B. VBox C. GridPane D. All of the above

Answer: D An AnchorPane, a VBox, and a GridPane are all containers for other UI elements. A root UI element is the top UI element of a tree (directed graph) of UI elements. The container UI elements are often used as root UI elements to start the layout of the UI. Other container elements we used in class are StackPane and HBox.

9. What is the super class for all Exception and Error classes? A. Catch B. Exception C. Reportable D. Throwable

Answer: D Exception class documentation ( I googled: Java SE 8 Exception Class): https://docs.oracle.com/javase/8/docs/api/java/lang/Exception.html Error class documentation (I googled: Java SE 8 Error Class): https://docs.oracle.com/javase/8/docs/api/java/lang/Error.html Both Exception and Error classes inherit from Throwable.

48. Which of these keywords can be used to prevent method overriding? A. static B. constant C. protected D. final

Answer: D If a method is declared as final, it cannot be changed. Method overriding is a way to change the method. Therefore, making a method final is a way to prevent it being overridden. A static method is a method that belongs to a class. It does not have the meaning of "not changing" or "do not change"...that is what "final" means. The static modifier means that the thing belongs to the class (rather than an instance). "constant" is not a Java keyword. See: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html "protected" is the access level and does not limit the ability of a subclass to override a method. "protected" makes is possible for a subclass to access the member of the superclass.

50. Which keyword is used by a method to refer to the object that invoked it? A. import B. catch C. abstract D. this

Answer: D In a class "this" refers to the instance. In some OO languages other than Java "self" is used instead of "this".

38. You can assign _________ to a variable of Object[] type. A. new char[50] B. new int[50] C. new double[50] D. new String[50]

Answer: D Object is a supertype of all classes. String ultimately inherits from Object. Therefore, a String[] (String array) can be treated like an Object[] (Object array) because a String[] can be cast as an Object[]. A char, an int, and a double are not object types. They are primitive data types. Primitive data types cannot be treated like objects. Since they are not object types they don't not inherit from Object.

41. 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. public B. private C. protected D. Use the default modifier.

Answer: D The default modifier provides "package access." The public access level allows access from everywhere. It doesn't restrict access so A cannot be correct. The private access level restricts access to within the class itself. So, another class in the same package cannot access something that is private. Therefore, B cannot be correct. The protected access level allows subclasses to access members of its superclass regardless of whether or not they are in the same package. A class in a different package that is a subclass could access protected members of its superclass therefore you couldn't say for protected "but a class in a different package cannot access it". This makes C incorrect. See: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html Make sure you have the access levels table memorized for the in-class midterm exam.

2. What is displayed by: System.out.println("2" + new Integer(3) + 4); A. The statement has a syntax error and won't compile B. 9 C. 27 D. 234 E. ClassCastException

Answer: D The println() method presents a String context because it accepts a String as its parameter. The "+" in a String context means concatenation of Strings. The Integer created by "new Integer(3)" is converted to a String by its toString() method in the String context. And, in a String context the 4 is also converted to a String. So: "2" concatenated with "3" concatenated with "4" is "234".

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

Answer: D This creates a String object that is referred to by s: String s = new String("Hello World!"); This makes o refer to the same object as s but treats it like its superclass of Object (from which all objects inherit): Object o = s; This takes o and casts it back to what is really is... a String...and makes d refer to it: String d = (String)o; All three references are to the same object instance. "Object o = s;" does not create a new object. The same object is referred to by o and s. The difference is that o is treated like an Object and s is treated like a String. String inherits from Object so a String can be treated like an Object. "String d = (String)o;" also does not create a new object. The same object is referred to by o and d. This line casts o as a String (which it really is) and gets d to refer to it as a String. None of the reference assignments change the contents of the object.

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

Answer: E ArrayList documentation (Google: Java ArrayList): https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

8. What is the output of the following code segment? ArrayList names = new ArrayList(); names.add("Sam"); names.add("Martha"); for (int i = 1; i < names.size(); i++) { names.add(i, "+"); } System.out.println(names); A. [Sam, Martha] B. [Sam, Martha, +] C. [Sam, +, Martha] D. [Sam, +, Martha, +] E. No output because the program goes into an infinite loop

Answer: E Each time through the loop the size of the array list, names, is obtained. Each time through the loop one more item is added to the array list. Therefore, the number of items in the array list will always stay larger than the value of i. The array list is growing while attempting to increment through the array list. This yields an infinite loop. The best way to see this is write a program and print the value of i and the size of the array list, names, in the loop.


Conjuntos de estudio relacionados

INSY 3305- Chapter 6,7,9(Comprehensive Exam 1 & 2)

View Set

Accounting 2121 Exam 1 Part 5: What is the normal balance for the following account types?

View Set

Evolve: Urinary/Reproductive System

View Set

Data Collection, Behavior, & Decisions

View Set

Concentration Mastering Chemistry

View Set

Ch. 12 Dealing with Union and Employee - Management Issues

View Set

Pharm Ch 57 Drugs Affecting Gastrointestinal Secretions

View Set

Module 5 SmartBook International Business

View Set