Chapter 7 of CMIS 109 - Study.com \\ Definition is the prompt
method Explanation A method is a procedure associated with a class and defines the behavior of the objects that are created from the class.
Chapter 7.2 - A _____ is a procedure associated with a class that describes an action an object is able to perform. a. function b. method c. routine d. procedure
Trees tree = new Trees("elm"); Explanation Because we created the parameter for the species in the constructor, we can create new instances by passing in values (or variables) to that constructor.
Chapter 7.6 - Examine the following code for a constructor. Which option will create a new instance of the Trees class and pass the parameter of elm to the constructor? public class Trees { String species; public Trees(String trees) { this.species = species; } } a. Trees tree = new Trees("elm"); b. Trees "elm" = new Tree; c. new Trees("elm") d. Trees = new "elm",
Data types Explanation As long as data types are different, you can have the same number of arguments in different methods of the same name. Java knows by the type
Chapter 7.7 - You can have overloaded methods of the same name and number of parameters, if only the _____ are different. a. Return values b. Nesting of brackets c. Data types d. Argument names
getTreeInfo Explanation The method in the child class shares the name of the method from the parent. Therefore, it is being overridden. It also returns a different value than the parent.
Chapter 7.8 - Examine the following code. What is the name of the method that is being overridden?
Polymorphism Explanation Polymorphism refers to being able to perform the same thing but differently. Root beer is brewed, but not in the same way as beer. Yet each can use the brew() method in different ways.
Chapter 7.1 - A parent class called SoftDrink has a brew() method. RootBeer and Beer classes inherit from SoftDrink. Each uses the brew() method but in different ways. Which aspect of object-oriented design is this? a. Polymorphism b. Methods c. Inheritance d. Encapsulation
superclass Explanation A parent is also a superclass: any children created from this class inherit variables and methods from it.
Chapter 7.10 - A parent class is also called a _____ a. superclass b. subclass c. masterclass d. majorclass
Overriding Explanation When you override a method, you basically use the core method from the parent class but add your own pieces to it. For example, a child class might add more values to a standard calculation.
Chapter 7.10 - The method of the parent class can be re-used and modified in a subclass inherited from the parent class. What is the term used to reference this behavior? a. Overriding b. Voiding c. Blending d. Extending
BlueChip is a Stock Explanation Remember in the concept of inheritance, the child is the parent! That is, it inherits from the parent. Another example is: A Magazine (subclass) is a Periodical (parent or superclass)
Chapter 7.10 - Which phrase accurately describes the relationship between the subclass BlueChip and the parent Stock? a. BlueChip is a Stock b. Stock is a subclass c. BlueChip overrides Stock d. Stock is a child class
Java loads classes dynamically. You can't know which method(s) are valid. Explanation Java loads classes dynamically. It brings us right back to the diamond problem: which method is the one you really need to use?
Chapter 7.11 - From a technical perspective, why is multiple inheritance difficult in Java? a. Oracle no longer supports the technology after buying Java. b. Java loads classes dynamically. You can't know which method(s) are valid. c. Classes are loaded sequentially. Updating the diamond is one-way. d. The developers missed a key step in creating Java.
Diamond Explanation Classes B and C inherit from A. They each have overridden methods from A. If you try to create class D that inherits from B and C, which method will be used? This is the diamond problem and increases complexity and ambiguity.
Chapter 7.11 - The _____ problem describes the complexity of multiple inheritance in Java. Star Diamond Polymorphism Dynamic
@Override Explanation This is an important compiler check. For example, if you have a toString and a tostring method, you want to make sure you call/create the right one! @Override tells the compiler to do a double check.
Chapter 7.11 - When using multiple interfaces to mimic multiple inheritance, what keyword is added before the method declaration? @Implement @Extend @Void @Override
System performance . Explanation Although it's a cool concept, multiple inheritance presents ambiguity and complexity. The diamond problem presents itself. If an overridden method exists in multiple class, which method does the sub-class use? Plus, it is not used as frequently.
Chapter 7.11 - Which of the following is not a reason that Java prevents the use of multiple inheritance? Diamond problem Low usage frequency Ambiguity System performance
public class Z implements A, B { } Explanation We cannot just extend (or inherit) from more than one class. Instead, by using interfaces we can have multiple implementations.
Chapter 7.11- Which of the following code snippets illustrates a method of multiple inheritance in Java? a. private void Z implements A, B { } b. public class Z extends A, B { } c. public class Z implements A, B { } d. private class Z extends A, B, implements C { }
No, Java doesn't support multiple inheritance Explanation You cannot inherit directly from more than one class.
Chapter 7.12- 4. Examine the following class declaration? Is this valid? a. Yes, but it will generate warnings b. It depends on the system running it c. Yes, multiple inheritance is allowed d. No, Java doesn't support multiple inheritance
Inherits Explanation And in turn Fruit could inherit from a Plant class: when the Banana class is created with the extends class, it now has access to all variables and methods within the parent class Fruit.
Chapter 7.12- Examine the following code. It can be said that the class Banana _____ attributes from the class Fruit Edits Inherits Copies Deletes
int I = 7; int j = 0; int k = I/j; Explanation This would result in dividing by zero. While it's easy to spot here, sometimes this bug lurks in your code. If there is even a remote possibility of a divide-by-zero error, you should add an exception handler (try and catch) to prevent the program from crashing.
Chapter 7.14 - 4. Which of the following code samples will create an exception in Java?
Inference Explanation Four core concepts of object-oriented programming are abstraction, encapsulation, inheritance and polymorphism. Note that inference is NOT a core concept.
Chapter 7.2 - Which of the following is NOT a core concept of object-oriented programming? a. Polymorphism b. Encapsulation c. Inference d. Abstraction
class Explanation A class must be declared before any objects of that class can be created, as a class declaration is a template describing how objects of that class are organized and behave.
Chapter 7.4 - A _____ is a description of a group of objects that have common properties and behavior. It's a blueprint from which specific objects are created. a. method b. class c. constructor d. variable
new Explanation The new operator allocates memory to contain the state variables for a new instance of the specified class. It then calls the class constructor to initialize its state variables. The new operator returns a reference to the object just constructed.
Chapter 7.4 - A specific instance of a class is created using the _____ operator. a. comparison b. resources c. new d. addition
Inheritance Explanation Just as you inherited traits from your forebears, child classes (sub-classes) inherit methods and fields from a parent class.
Chapter 7.1 - An object-oriented design consists of periodical and publication data. The hierarchy of classes, from top to bottom is as follows: Publication -> Book -> Novel. If the Novel class gains all methods and fields from the Book class, this is an example of which of the concepts of Object Oriented Design? a. Encapsulation b. Inheritance c. Polymorphism d. Interfaces
Encapsulation Explanation We need to hide the data or encapsulate it. You can still call the calculateTax() method in the Tax class, without having to know exactly how the tax is being calculated, or even what the employee pay rate is. It's good practice to keep things restricted/encapsulated. Start with the tightest controls.
Chapter 7.1 - You are designing a program for payroll. The Tax class has a method to calculate tax rates based on pay. You want to keep this method and its variables invisible to other classes. They can access the method but don't get to see its details. This is an example of _____. a. Interfaces b. Polymorphism c. Inheritance d. Encapsulation
instance; Semi class Explanation A new semi is an instance of the Semi class. It is an object. This is the basis of object-oriented programming, the collection of classes and objects.
Chapter 7.1 - You are designing a program for vehicles. Some of the class names include Semi, Compact, and SUV. If we create a new semi, it is said that the semi is a(n) _____ of a(n) _____. a. Semi; object b. object; class c. instance; Semi class d. class; semi object
Methods Explanation Methods are the functions or the actual tasks being carried out. A Journal class is great, but without corresponding actions, it won't really do anything.
Chapter 7.1 - You have designed a hierarchy of classes for accounting software. All parent/child class relationships are set. What will you need for the classes to actually perform tasks? a. Encapsulation b. Polymorphism c. Inheritance d. Methods
Both coverageOption and employeePremium cannot be used. Explanation You can inherit variables and methods to the subclass but not back up to the parent.
Chapter 7.10 - 1. Examine the following code. Which variable CANNOT be used in the parent class of Benefit? a. HIPAAcode b. Both coverageOption and employeePremium cannot be used. c. employeePremium d. coverageOption
public class Employee { private String empName; } public class JobCodes extends Employee { private long jobCode; } Explanation The child class must use the extends keyword in order to inherit the variables and methods from the parent.
Chapter 7.10 - 3. Which of the following correctly creates a subclass, or child class, from the main Employee class?
It is overridden Explanation The SpeedBoat class inherits everything from the Boat class, including the methods. But in this case it is overridden and will go faster.
Chapter 7.12- Examine the following code. What is true about the method increaseSpeed()? It is unacceptable It is overloaded It is overridden It is not allowed
Many forms Explanation Many shapes or many forms: You can override or overload methods, or even have instances of classes with the same object name.
Chapter 7.12- Polymorphism means _____ Data hiding Multiple inheritance Many forms Shape-bending
Overloading Explanation If we have three methods called assembleSandwich() but with different arguments, we've effectively overloaded the method. Java knows which one to use, based on the arguments we send it.
Chapter 7.12- Polymorphism supports _____, which is several methods with the same name but different arguments. Overriding Overclocking Overarching Overloading
Abstract Explanation Since Product is a fairly generic concept, it would make more sense if it were abstract. You wouldn't be able to create instances of Product classes; but it does make sense to create an instance of a Brillo Pad.
Chapter 7.13- 3. Of the following types, what type of class would Product be? Abstract Double Empty Null
Be instantiated Explanation You cannot create an instance of an abstract class/instantiate one. It is better used for hierarchies: Think of it as a model. You could have a Vehicle class that is abstract, with Truck, SUV, or Semi as classes under it.
Chapter 7.13- An abstract class cannot _____ Be updated Be instantiated Have methods Be empty
Multiple inheritance Explanation Also called polymorphism, multiple inheritance means one class can inherit from more than one parent class. This is not allowed in Java; instead, we can use interfaces to avoid this restriction.
Chapter 7.13- Java doesn't allow _____, meaning one class cannot inherit from more than one parent class. Instantiation Data hiding Encapsulation Multiple inheritance
Shape Explanation The other shapes are very specific and could have very different methods for determining area, volume, etc. A Shape, on the other hand, is quite generic, and can be used to setup the hierarchy of classes.
Chapter 7.13- Which of the following classes would be suitable as an abstract class? Shape Diamond Rhombus Oval
public class Oval implements Shape { } Explanation Remember that if a class uses an interface it IMPLEMENTS it; if an interface draws on another interface, it EXTENDS it.
Chapter 7.13- Which of the following correctly shows the Oval class using the Shape interface?
A message will be displayed Explanation The catch code block is executed if the file is not found. In that case, a nice message is displayed and the program continues on its way. This is an exception handler.
Chapter 7.14 - 3. Examine the following code. What will happen if the file myfile.txt is not found? a. The program will terminate abnormally b. A message will be displayed c. A file with that name will be created d. The program will continue without notice or warning
Exception handler Explanation When you write code to catch the exception, it is actually a handled exception: You've caught what Java threw at you and do something with it instead of letting the program crash.
Chapter 7.14 - Code that catches an exception and performs other code is called a(n) _____ try block Void class Exception handler unhandled exception
Exception class Explanation The Exception class, which is a subclass of the Throwable class, holds the classes for exceptions. That is, the exceptions for file not found, divide-by-zero, array index errors, etc., are subclasses of Exception.
Chapter 7.14 - Java keeps a list of exceptions and will know what exception has occurred. Where are these stored? FileNotFound class Applet class Exception class Error class
ArrayIndexOutOfBoundsException Explanation If you are working with arrays, and there is the possibility that a non-existent index could be referenced, write a handler using this exception.
Chapter 7.14 - Which of the following exceptions will occur if you try to reference an index of an array that doesn't exist? a. ClassCastException b. ArithmeticException c. FileNotFoundException d. ArrayIndexOutOfBoundsException
Functions are independent of classes, and methods are defined within a class. Explanation A function is independent and not associated with a class, while methods are defined within a class.
Chapter 7.2 - How do functions differ from methods? a. There is no difference between a function and a method. b. A method can be reused for several different classes. c. Functions are independent of classes, and methods are defined within a class. d. Methods are independent of classes, and functions are defined within a class.
objects Explanation An object is a component of a program that knows how to perform certain actions and how to interact with other elements of the program. Objects are the basic units of object-oriented programming. The items that fall under the class of vehicles (car, truck, and bus) are referred to as objects.
Chapter 7.2 - Sally is continuing work on her Object-Oriented Program and is writing code for a car, truck, and bus. These items under the class called vehicle are referred to as _____. a. attributes b. objects c. items d. methods
class Explanation Sally is creating a class which is a blueprint of an object. If the class is a 'vehicle', Sally will then provide the guidelines, or blueprints, about this class containing attributes and functions.
Chapter 7.2 - Sally is writing some Object-Oriented Program code for a vehicle which contains the attributes (manufacturer, color, and price), along with what she wants the vehicle to be able to do (start, drive, and park). This is known as a _____. a. class b. method c. template d. function
Instance Explanation The class is the blueprint for the object, e.g., Employee. Each time you use that class, you create an instance of the Employee object.
Chapter 7.3 - An object is a(n) _____ of a class a. Derivative b. Method c. Instance d. Unit
Encapsulated Explanation Information hiding, or encapsulation, refers to the concept that information is contained within the class. For example, a method from a Payroll program can access an Employee class and not need to know the inner workings of the class in order to retrieve pay rate.
Chapter 7.3 - Information can be _____ in a Java class, meaning available only to that class a. Public b. Deleted c. Void d. Encapsulated
Constructor Explanation A constructor creates an object, or instance, of the class.
Chapter 7.3 - The line of code that creates an object from the class is called a _____ a. Method b. Builder c. Void d. Constructor
Customer name = new Customer(); Explanation If the Customer class accepted parameters, such as name or order number, you can pass those in thus: Customer name = new Customer('Joe', 99978);
Chapter 7.3 - Which code snippet will create/instantiate a new instance of the Customer class? a. name = new Customer(); b. new Customer() = name; c. Customer name = new Customer(); d. Customer() = new name;
Inheritance Explanation A class can inherit properties, methods, and behaviors from other classes. For example, if there is a parent class of Publication, I can create two classes of Paperback and Magazine that inherit all properties from Publication. Then, we can adjusted those sub-classes as needed for specific purposes.
Chapter 7.3 - _____ means that classes can share the properties and methods from other classes a. Encapsulation b. Information Hiding c. Inheritance d. Instantiation
class declaration Explanation A class declaration is a description of the state variables and methods available to an object of that type. It must be specified before Java objects of that type can be created.
Chapter 7.4 - A {Blank} must be specified before a specific instance of a Java object can be created. a. subroutine b. class declaration c. exception handler d. comment
methods Explanation A method is a unit of programming which accepts parameters passed in and performs some action (possibly utilizing and/or modifying the object's internal state variables). A method can optionally return a value to the caller.
Chapter 7.4 - Actions are performed on an object by calling its _____. a. constructor b. methods c. parameters d. declaration
a reference to the object Explanation The new operator first allocates memory to hold the object's state variables, then calls the class constructor to initialize the state variables, then, finally, a reference to the object is returned so it can be stored in a variable and later used to access the object.
Chapter 7.4 - After constructing a specific object, the Java new operator returns _____. a. the constructor b. the object's size c. a reference to the object d. the class name
inherits Explanation Inheritance and instantiation go hand-in-hand: Each time you instantiate an instance of a class, the methods and variables come with it. They are inherited.
Chapter 7.5 - An instance of a class _____ methods and variables from the main class. a. inherits b. deletes c. ignores d. duplicates
Orders myOrder = new Orders(1234, 14); Explanation Since there's a constructor, you can't just use new Orders(10,52). Nor can you create a new Orders instance without arguments, since you have created a constructor for it. It requires two values, orderID and quantity on hand.
Chapter 7.5 - Examine the following code for the class Orders. Which of the following correctly instantiates a new instance of Orders? class Orders { long orderID; long qtyOnHand; public Orders(long orderID, long qtyOnHand) { } } a. Orders my Order = new Orders(14); b. Orders my Order = new Orders(); c. new Orders(10, 52) d. Order myOrder = new Orders(1234, 14);
The third parameter from the constructor Explanation The constructor has three arguments, but the code does not include all of them when it instantiates the instance of the class.
Chapter 7.5 - Examine the following code. What is missing from the instantiation of the instance of the DiskDrive class? class DiskDrive { double capacity; long modelNumber; double partitionSize; public DiskDrive (double capacity, double modelNumber, double partitionSize) { } DiskDrive Seagate = new DiskDrive(0, 15); a. The third parameter from the constructor b. The keyword public c. Nothing d. All parameters from the constructor
The keyword new is missing to create the instance Explanation In order to create a new instance of the Order class, you need the keyword new. It's perfectly OK to name the new instance order
Chapter 7.5 - Examine the following code. What is missing? Orders order = Order(); a. The keyword new is missing to create the instance b. Arguments between the parentheses c. Cannot name the new object the same as the class d. Nothing; this code will compile
constructor Explanation If you don't specify a constructor, Java uses a default. When you create code such as Rectangle = new Rectangle();, you could create a dozen constructors that start with Rectangle(); these can accept parameters, e.g., public Rectangle (int height, int width);
Chapter 7.5 - The _____ sets aside memory and initializes variables within the class a. object b. constructor c. operator d. main method
The name doesn't match the class Explanation The name of the constructor must match the name of the class! In this case, it won't compile since it's not sure if you are creating another class, or what the intentions are.
Chapter 7.6 - Examine the following code: the constructor is preventing it from compiling. What is wrong? class Cards{ String suit; public DeckofCards(String suit) { this.suit = suit; } } a. The this keyword is misused b. The name doesn't match the class c. Not enough arguments d. Cannot have arguments
Cards ace = new Cards("Diamonds"); Explanation Because the constructor specified an argument of String for the suit, you have to provide this when instantiating new objects of the class. The constructor takes only one argument.
Chapter 7.6 - Examine the following constructor. Which code snippet correctly creates a new instance of the Cards class? class Cards { String suit; public Cards (String suit) { this.suit = suit; } } a. Cards ace = new Cards(); b. Cards ace = new Cards("Hearts", 4); c. Cards ace = new Cards(15); d. Cards ace = new Cards("Diamonds");
Use the this keyword Explanation If you have a variable for payRate in the class and as an argument to the constructor, refer to payRate in the constructor as this.payRate
Chapter 7.6 - The same variable names can be used in the class and constructor. In the constructor, how do you differentiate them? a. Use the static keyword b. Use the void keyword c. Place in quotes d. Use the this keyword
Java creates a default constructor Explanation Even though you won't see it in your source code, the default constructor is there, with empty brackets. E.g., if you had a class called Fonts, a constructor exists: Fonts () { }
Chapter 7.6 - What happens if you don't create a constructor for a class? a. Nothing b. Java creates a default constructor c. Java won't compile the program d. Java displays a warning
public Employee(String e, double r, double h) { String empName = e; double pay = r; double hours = h; } Explanation To have a valid overload, we need to have a different number of arguments. We can't simply create another one with two.
Chapter 7.7 - Examine the following code for a class and its constructor. What is the correct method to overload the constructor for a third argument? public class Employee { String employeeName; double payRate; double hoursworked; public Employee(String e, double r) { String empName = e; double pay = r; } } a... b... c... d...
Both cannot be double Explanation You can have two methods with the same number of arguments, but they can't both be double values, since we already have two of them in this example.
Chapter 7.7 - Examine the following methods. We need to add another method with the same name (overload the methods), to add two values together. What must be true about these new arguments? public double calcValues (double rate, double percentage){ return rate * percentage; } public double calcValues(double rate, double percentage, double modifier) { return rate * percentage * modifier; } a. Both must be float b. They must be double c. Both cannot be double d. Both must be String
The program will not compile Explanation Overloading is fine, but you have to have the methods in the first place! Trying to call a method that does not exist cannot work.
Chapter 7.7 - If you call an overloaded method that doesn't exist, what will happen? a. The program will not compile b. Java will use a default method c. Nothing d. Java will display a warning
Cannot create two identical constructors Explanation Overloading doesn't mean cloning! You can't create two identical constructors. Even though the parameters are different, each constructor has one parameter.
Chapter 7.7 - The following code will not compile or run. Why? class Employee { public Employee (double r) { payRate = r; public Employee (double z){ payRate = z; } } a. Missing curly brackets b. Cannot create two identical constructors c. Pay rate should be float d. Constructors have wrong name(s)
public class ABonds extends Bonds { double adminfee = 1; public double calcFees( ) { return adminfee + (apr * load); } } Explanation In order to override the method, the child class must inherit from the parent (using the extends keyword). The method name must be the same, except you can do different calculations and return a different value.
Chapter 7.8 - Examine the following code that defines a class for Bonds. Which of the following is the correct way to override the method calcFees?
super Explanation If there are two methods for calculating exhange rates, and you want to use the parent method, the code would look something like: super.getExchangeRate(double currencyRate)
Chapter 7.8 - If you have overridden a method but still want to use the parent class method, what keyword is used? a. void b. super c. static d. main
Inheritance Explanation If we like a class, we can create a child class to inherit all the variables and methods from that parent class. We can then override any method(s) from the parent to do some other task. This all falls under the umbrella of inheritance, a key feature of Java and other object-oriented programming languages.
Chapter 7.8 - Overriding methods in Java is part of the overall concept of _____ a. Encapsulation b. Inheritance c. Polymorphism d. Data hiding
In the child class Explanation The child, or subclass, inherits methods from the parent; when you create a new method with the same name as a parent class method, that is an override.
Chapter 7.8 - When overriding a method, where is the new method defined? a. In the parent class b. Outside the main class c. In the child class d. In the main class
public long setQoh(long qty) { qty = qty + 5; return qty; } Explanation When we override, we have to have the same parameters and data types. We will be performing some other task (in this case, we'll assume the parent method doesn't add 5 to the quantity). This overridden method is included in the child class (subclass).
Chapter 7.9 - 1. Which of the following statements correctly overrides the setQoh method?
Overridden Explanation The method has the same name and parameters as the method in the parent class. You can always use the method from the parent class, but if you want to do something else with the method, you can override it.
Chapter 7.9 - 2. Examine the following code. The method getID is _____ in the subclass VipOrder. a. Deleted b. Overridden c. Copied d. Overloaded
getHours Explanation Both methods are defined with the same data type and parameters in both the parent and the child class. Therefore, the child class overrides the parent class's original method.
Chapter 7.9 - 5. Examine the following code. Which method is being overridden? a. getHours b. Employee c. getFees d. UnionEmployee
Child Explanation The child class inherits all variables and methods from the parent class. If you want to do something different with one or more of the parent's classes, you can override it in the child class.
Chapter 7.9 - In which class can you override a method? a. Public static b. Private c. Child d. Parent
public class Invoice { public double setCost(double cost) { return cost; } public double setCost(double cost, int cID) { return cost; } } Explanation The overloaded methods must all have the same name, but accept different parameters. Otherwise, they are just clones.
Chapter 7.9 - Which of the following shows an overloaded method for setCost?