Computer Science 109 : Introduction to Programming

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

The same variable names can be used in the class and constructor. In the constructor, how do you differentiate them?

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

After constructing a specific object, the Java new operator returns _____.

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.

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.

class

Sally is writing some Object-Oriented Program code for a vehicle that contains the attributes manufacturer, color, and price, along with what she wants the vehicle to be able to do which includes start, drive, and park. To do this, she needs to create a _____ for vehicles.

class

In a linear function, the time taken is ______ if the size of the output is doubled.

doubled

A programming algorithm is like:

A recipe.

Of the following types, what type of class would Product be?

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.

What is space complexity?

Amount of storage an algorithm needs to work. Explanation: Space complexity is a measure of the amount of storage space needed by an algorithm to work.

_____ means that classes can share the properties and methods from other classes.

Inheritance Explanation: A class can inherit properties, methods, and behaviors from other classes.

If we declare a two-dimensional array of size 5 by 5 and the data type sorted is 4 bytes long, how much memory will be allocated.

192 bytes. Explanation: The correct answer is '192 bytes'. We have the array header of 12 bytes plus 5 elements times 4 bytes which equals 32 and since it is a multiple of 8 we do not need padding. Then each element has an array of its own, also of size 5, so we have 5 * 32 bytes which equals 160. Lastly we add 32 to 160 and we have 192 bytes.

When we declare a two-dimensional array 10 by 5 of data type 4 bytes long, how much memory will be allocated?

336 bytes. Explanation: The correct answer is 336 bytes. We have an array header 12 bytes plus 10 elements times 4 bytes. (10 x 4 ) + 12 = 52. We need to pad it up to a multiple of 8, adding 4 bytes (52 + 4 = 56). That's the outer array. Now, the each of the other 5 elements is its own entity, so you can multiply 5 x 56 (our outer array was 56, and the inner ones will be as well). Therefore, 56 + (5 x 56) = 336.

If we declare an array of 12 elements and data type of 2 bytes each, how much memory will be allocated?

40 bytes. Explanation: The correct answer is '40 bytes'. The array header is 12 plus 12 elements times 2 bytes plus 4 byte padding to make it a multiple of 8 bytes.

When using multiple interfaces to mimic multiple inheritance, what keyword is added before the method declaration?

@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.

An algorithm is written down as:

A list of steps.

Examine the following code. What will happen if the file myfile.txt is not found?

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.

Which of the following exceptions will occur if you try to reference an index of an array that doesn't exist?

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.

An abstract class cannot _____

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.

If f(n) is the function then O(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. } is true for which of the following symptotic notations ?

Big Oh. Explanation: The Big Oh (O): 1. Measures the upper bound (worst case or maximum time) 2. For an algorithm represented by f(n) where n is the input size, O(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }, where g(n) is the upper bound calculated.

Which phrase accurately describes the relationship between the subclass BlueChip and the parent Stock?

BlueChip is a Stock.

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?

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.

Examine the following code. Which variable CANNOT be used in the parent class of Benefit?

Both coverageOption and employeePremium cannot be used.

The following code will not compile or run. Why?

Cannot create two identical constructors.

Examine the following constructor. Which code snippet correctly creates a new instance of the Cards class?

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.

In which class can you override a method?

Child.

Which code snippet will create/instantiate a new instance of the Customer class?

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);

Which is the Java library that will help you access more tools to manipulate and sort arrays?

java.util.Arrays

You can have overloaded methods of the same name and number of parameters, if only the _____ are different.

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.

The _____ problem describes the complexity of multiple inheritance in 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.

Information can be _____ in a Java class, meaning available only to that class

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.

Java keeps a list of exceptions and will know what exception has occurred. Where are these stored?

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.

Code that catches an exception and performs other code is called a (n) _____

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.

Which of the following correctly shows the Oval class using the Shape interface?

Explanation: Remember that if a class uses an interface it IMPLEMENTS it; if an interface draws on another interface, it EXTENDS it.

Which of the following correctly creates a subclass, or child class, from the main Employee class?

Explanation: The child class must use the extends keyword in order to inherit the variables and methods from the parent.

Which of the following shows an overloaded method for setCost?

Explanation: The overloaded methods must all have the same name, but accept different parameters. Otherwise, they are just clones.

Which of the following code samples will create an exception in Java?

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.

What does the binarySearch method allow us to do?

Find the position of the item we're looking for in the array.

How does Insertion-Sort work?

Finding if each item is in the right position of the array according to the criteria.

Instead of a list of steps, algorithms can be written using:

Flowcharts with arrows to illustrate the journey. Explanation: Instead of a list of steps, algorithms can be written using flowcharts with arrows to illustrate the journey. Text is not the only way. We wouldn't want to use a modern computer language, because those are not simple English, and we don't want to use ambiguous language (unclear language) because otherwise neither we nor the computer will be sure what to do.

How do functions differ from methods?

Functions are independent of classes, and methods are defined within a class.

What is one major drawback of experimental analysis?

Implementation of programs.

When overriding a method, where is the new method defined?

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.

All the operations on the array have good performance except?

Increasing array size. Explanation: The correct answer is 'Increasing an array size'. Since array size is fixed, to increase an array one must create a new array with the desired size and copy the element's values over.

Which of the following is NOT a core concept of object-oriented programming?

Inference. Explanation: Four core concepts of object-oriented programming are abstraction, encapsulation, inheritance and polymorphism. Note that inference is NOT a core concept.

Overriding methods in Java is part of the overall concept of _____.

Inheritance

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?

Inheritance. Explanation: Just as you inherited traits from your forebears, child classes (sub-classes) inherit methods and fields from a parent class.

Examine the following code. It can be said that the class Banana _____ attributes from the class Fruit.

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.

The ingredients in an algorithm are called:

Inputs. Explanation: The ingredients in an algorithm are called inputs. Outputs are the results, a procedure is another name for the how-to portion (the algorithm steps), and pseudocode is an advanced way to write down an algorithm.

Examine the following code. What is true about the method increaseSpeed()?

It is overriden. Explanation: The SpeedBoat class inherits everything from the Boat class, including the methods. But in this case it is overridden and will go faster.

What is Range sorting?

It's used when you only need some part of an array sorted.

What happens if you don't create a constructor for a class?

Java creates a default constructor.

From a technical perspective, why is multiple inheritance difficult in Java?

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?

An algorithm tests the air pressure on a slope at different altitudes. Given that the pressure depends on the altitude, what is the optimal algorithmic function to use?

Linear. Explanation: This is a linear function because you are studying the relationship between pressure and height where the function is a straight line and there is a relationship between pressure and height, and the slope changes in a linear way in relation to the height or distance.

Kara's code includes a binary search operation. Which of the following functions would be optimal to test the execution time of this function?

Log n. Explanation: Logarithmic Function f(n) = log n, or O log (n) function is used for binary search algorithms where the input n is divided into 2 and and the search process continued till the element is found. The time to run the algorithm is equal to the log of the input size n.

Polymorphism means ______

Many forms.

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?

Methods. Explanation: Methods are the functions or the actual tasks being carried out.

Java doesn't allow _____, meaning one class cannot inherit from more than one parent class.

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.

Examine the following class declaration? Is this valid?

No, Java doesn't support multiple inheritance.

If you use sorting methods contained in the Java library, do you have to code the sorting algorithm?

No, the library does all the sorting according to the set criteria.

What are asymptotic notations?

Notations that allow us to analyze an algorithm's running time by identifying its behavior as the input size for the algorithm increases.

If one for loop runs with size N and the other with size M and the loops follow a linear function, what is the expected time complexity?

O(M+N) Explanation: Here, since the detailed steps of the loops are not mentioned so we assume that one loop runs with time complexity O(N) and O(M). This is because the loops follow a linear progression. Therefore, the combined time complexity of the loops will be a combined complexity of the two loops which is O(N+M).

What is the time complexity of the following? i=0; while(i<n) { j=i; while(j<n && j<i+5) { /* code */ j++; } i=j; }

O(N) Explanation: In this case i goes from 0 to N, but j never goes back to starting from 0, as it always starts from i. This means that the loop always moves forward and doesn't form a quadratic function. The function formed is linear and the time complexity is linear, hence O(N).

For a linear function, what would be its time complexity?

O(N) Explanation: The time complexity of a linear function is O(N)

If an algorithm runs in quadratic time, what is its time complexity using Big-O?

O(N^2)

What is Bubble sort performance?

O(n2)

Examine the following code for the class Orders. Which of the following correctly instantiates a new instance of Orders?

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.

Polymorphism supports _____, which is several methods with the same name but different arguments.

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.

Examine the following code. The method getID is _____ in the subclass VipOrder.

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.

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?

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.

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?

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.

Out of the four possibilities listed, which of the following class names would be suitable as an abstract class?

Shape. Explanation: Three of the names are specific shapes and could have very different methods for determining their characteristics, such as area or volume. A shape, on the other hand, is quite generic and can be used to set up the hierarchy of classes.

An algorithm always leads to a(n):

Solution.

If you have overridden a method but still want to use the parent class method, what keyword is used?

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)

Which of the following is not a reason that Java prevents the use of multiple inheritance?

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.

Examine the following code. What is missing? Orders order = Orders();

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.

Examine the following code: the constructor is preventing it from compiling. What is wrong?

The name doesn't match the class.

If you call an overloaded method that doesn't exist, what will happen?

The program will not compile.

Examine the following code. What is missing from the instantiation of the instance of the DiskDrive class?

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.

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?

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.

A two-dimensional Java array is:

an array of arrays Explanation: A Java two-dimensional array is an array of arrays so the 'row' array is one block and each element's array is a block.

A _____ must be specified before a specific instance of a Java object can be created.

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.

The _____ sets aside memory and initializes variables within the class.

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);

The line of code that creates an object from the class is called a _____

constructor. Explanation: A constructor creates an object, or instance, of the class.

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 _____.

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.

Brian has an algorithm to read through an array that contains numbers 1 through 26 and convert each number to an English alphabet. He runs the algorithm a number of times with no changes to the size of the array and no changes to the task done. The function to use is: _____

f( n) = c Explanation: The constant function f(n) = c or O(1) will help determine how much time it will take to complete a task when the size n is known and the task is the same. Here the size n is 26. The task is the same (converting numbers to alphabets) and does not change, irrespective of the number of times the task is performed.

Examine the following code. Which method is being overridden?

getHours.

Examine the following code. What is the name of the method that is being overridden?

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.

An instance of a class _____ methods and variables from the main class.

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.

An object is a(n) _____ of a class

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.

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) _____.

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.

A _____ is a procedure associated with a class that describes an action an object is able to perform.

method Explanation: A method is a procedure associated with a class and defines the behavior of the objects that are created from the class.

Actions are performed on an object by calling its _____.

methods

A specific instance of a class is created using the _____ operator.

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.

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 _____.

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.

An experimental analysis depends on the _____ results, so an algorithm cannot be measured unless an _____ is implemented.

output; equivalent program Explanation: As an experimental analysis depends on the output results, an algorithm cannot be measured unless an equivalent program is implemented.

Examine the following code for a class and its constructor. What is the correct method to overload the constructor for a third argument?

public Employee(String e, double r, double h) { String empName = e; double pay = r; double hours = h; }

Examine the following code that defines a class for Bonds. Which of the following is the correct way to override the method calcFees?

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.

Which of the following code snippets illustrates a method of multiple inheritance in java?

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.

Which of the following statements correctly overrides the setQoh method?

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).

A parent class is also called a _____.

superclass.

What is NOT an asymptotic notation?

β Explanation: β is not a type of asymptotic notation.


Kaugnay na mga set ng pag-aaral

Chapter 02: Theory, Research, and Evidence-Informed Practice

View Set

Immuno Ch. 8: T-cell mediated immunity

View Set

The 5 steps in the Entrepreneurial Process

View Set

VATI RN Leadership and Management 2019

View Set

Chapter 3 Review Questions AP Psychology

View Set

CP Computer Science Chapters 1-2 Test

View Set