Quiz 1 Sectiunea 7 1. Following good programming guidelines, what access modifier should be used for the class fields in the following situation? A car insurance company wants to create a class named Customer that stores all data for a specified customer including the fields: vehicle information, policy information, and a credit card number. Public Protected Private (*) Default All of the above 2. A team is working on a coding project. They desire that all portions of their code should have access to the classes that they write. What access modifier should be used for each class? Public (*) Protected Private Default All of the above 3. Which of the following could be a reason to need to pass an object into a method? Easier access to the information contained within the object. The ability to make changes to an object inside of the method. Comparing two objects. All of the above. (*) 4. Which of the following shows the correct way to initialize a method DolphinTalk that takes in 2 integers, dol1 and dol2, and returns the greater int between the two? int DolphinTalk(dol1, dol2){ if(dol1 > dol2) return dol1; else return dol2;} int DolphinTalk(int,int){ if(dol1 > dol2) return dol1; else return dol2;} int DolphinTalk(int dol1,int dol2){ if(dol1 > dol2) return dol1; else return dol2;} (*) int DolphinTalk, int dol1,int dol2 { if(dol1 > dol2) return dol1; else return dol2;} All of the above 5. Cameron wishes to write a method that takes in two objects and returns the one with the greatest value. Is this possible? Yes, but he will have to use two different methods, one to take in the objects and the other to return an object. Yes, methods can take objects in as parameters and can also return objects all within the same method. (*) No, it is not possible to return objects. No, it is not possible to have objects as parameters or to return objects. 6. You are assigned to write a method that compares two objects of type Career. One requirement of your assignment is to have your method compare the "greatestPossibleSalary" instance data of Career objects. The "greatestPossibleSalary" field is data type int. What would be the best return type from your compare method? Career, because if it returns the highest paying Career object it will be able to use the same method later to compare other aspects of Career objects. (*) Integer, because it is the easiest to code with. Sectiunea 7 1 String, because is should return a string of the name of the career that is highest paying because none of the other information of the career matters. Array, because it can store the most information. 7. Consider the following: There is a method A that calls method B. Method B is a variable argument method. With this, which of the following are true? Method A can invoke method B twice, each time with a different number of arguments. (*) A compliler error will result since method B does not know how large an array to create when it is invoked by method A. When invoked, method B creates an array to store some or all of the arguments passed to it from method A. (*) All of the above. 8. What type(s) would work for a variable argument method? Integers, Strings, and Booleans (*) Constructors Arrays (*) Objects (*) All of the above 9. It is possible to have more than one constructor with the same name in a class, but they must have different parameters. True or false? True (*) False 10. Which of the following is a possible way to overload constructors? (*) Sectiunea 7 2 Quiz 2 Sectiunea 7 1. Static variables can't use which of the following specifiers? Public Protected Friendly (*) Default Private 2. You can assign new values to static variables by prefacing them with the this keyword and a dot or period. True or false? True (*) False 3. Which of the following statements about static methods is true? They exist once per class. (*) They exist once in each instance. They can be overridden by a subclass. They can access any instance variable. They cannot access static variables declared outside the method. 4. You can create static class methods inside any Java class. True or false? True (*) False 5. You can return an instance of a private class through a static method of a different class. True or false? True False (*) 6. You can create static classes as independent classes. True or false? True False (*) 7. You can use an inner static class to return an instance of its outer class container. True or false? True (*) False 8. A linear recursive method can call how many copies of itself? 1 (*) 2 or more None 9. Which case does a recursive method call last? Recursive Case Convergence Case Basic Case Base Case (*) None of the above 10. A non-linear recursive method can call how many copies of itself? 1 2 or more (*) None Quiz 3 Sectiunea 7 1. What is a UML? Unidentified Molding Level, the level of access permitted by the default access specifier. Unified Modeling Language, a standardized language for modeling systems and structures in programming. (*) Universal Model Light, a program that reads the brightness of any given lightbulb. None of the above. Sectiunea 7 3 2. What does it mean to inherit a class? The subclass (or child class) gains access to any non-private methods and variables of the superclass (or parent class). (*) The access specifier has been set to private. A way of organizing the hierarchy of classes. Extending a method from a superclass. 3. Which of the following correctly defines a superclass (or parent class)? A class that inherits methods and fields from a more general class. The most specific class of a hierarchy system of classes. A class that passes down its methods to more specialized classes. (*) A keyword that allows or restricts access to data and methods. 4. Which of the following correctly defines a subclass (or child class)? A class that inherits methods and fields from a more general class. (*) A keyword that allows or restricts access to data and methods. A class that passes down its methods to more specialized classes. The most general class of a hierarchy system. 5. Methods are generally declared as public so other classes may use them. True or false? True (*) False 6. Which is the most accurate description of the code reuse philosiphy? A programming philosophy that promotes stealing your classmates' code. A programming philosophy that promotes having no concern about the security of code. A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods. A programming philosophy that promotes simpler, more efficient coding by using existing code for new applications. (*) 7. Which of the following correctly describes the use of the keyword super? A keyword that restricts access to only inside the same class. A keyword that allows subclasses to access methods, data, and constructors from their parent class. (*) A keyword that signals the end of a program. A keyword that allows access from anywhere. 8. Which of the following is the proper way to set the public variable length of the super class equal to 5 from inside the subclass? super.length() = 5 super.length(5) super.length = 5 (*) super(length = 5) 9. What is a hierarchy? A programming philosophy that promotes simpler, more efficient coding by using existing code for new applications. A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods. A keyword that allows subclasses to access methods, data, and constructors from their parent class. A structure that categorizes and organizes relationships among ideas and concepts of things with the most general at the top and the most specific at the bottom. (*) Sectiunea 7 4 10. It is possible to extend a class that already exists in Java, such as the Applet class. True or false? True (*) False Quiz 4 Sectiunea 7 1. Why would a programmer use polymorphism rather than sticking to a standard array? Because arrays only work using the same object type and polymorphism provides a way around this. (*) Because it is easier to add or remove objects using polymorphism even when all of the objects are of the same type. Because arrays are more complex and polymorphism simplifies them by restricting them to only contain the same type objects. A programmer wouldn't use polymorphism over a standard array. 2. It is possible to override methods such as equals() and toString() in a subclass of Object to fit the needs of the objects of the subclass. True or false? True (*) False 3. Is there a difference between overriding a method and overloading a method? Yes. Overriding is done within a single class and overloading is done through a series of superclasses and their subclasses. Yes. Overriding allows for the creation of an array of different object types and overloading restricts an array to only contain the same object types. Yes. Overriding is done in the subclass and allows for redefining a method inherited from the superclass and overloading is done within a class and allows for multiple methods with the same name. (*) No, they are the same. 4. Which of the following is a goal of the object model? Providing modular code that can be reused by other programs or classes. (*) Concealing implementation. (*) Data abstraction. (*) Protecting information and limiting other classes' ability to change or corrupt data. (*) 5. If Sandal extends Shoe, it is possible to declare an object such that Sandal s = new Shoe(); True False (*) 6. What allows Java to correctly and automatically determine which method to invoke based on the type of object being referred to at the time the method is called? Abstract classes Polymorphism Inheritance Dynamic Method Dispatch (*) 7. Which of the following are true about an abstract class? It is possible to create objects of this type. The Java Virtual Machine does not differentiate abstract classes from concrete classes. It is possible to create references of this type. (*) It is identified by the Java keyword abstract. (*) Sectiunea 7 5