CS test 3

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

E temp = new E();

(not valid - cannot instantiate generics)

E[] arr = new E[10];

(not valid - cannot instantiate generics)

temp instanceof E;

(not valid - cannot perform instanceof generics due to not being able to instantiate them)

how many of the following classes are checked? a. IllegalArgumentException b. FileNotFoundException c. NullPointerException d. Exception

2 b & d

What of the following can you throw in the code? a. FileNotFoundException b. IllegalArgumentException c. A and B d. None of the above

A and B (any exception can be thrown, as the Exception class extends Throwable)

Interface or Abstract Class Directly or indirectly extends Object

Abstract

Interface or Abstract Class can "extends" a class

Abstract

Interface or Abstract Class can have constructors

Abstract

Interface or Abstract Class can have instance variables?

Abstract

Interface or Abstract Class can have protected methods

Abstract

What are all the types of methods we can have in interfaces?

Abstract, static, and default

Why is binary search considered "faster" than linear search?

Binary search is considered "faster" than linear search as its worst-case runtime is less than that of linear search as the input size increases

Interface or Abstract Class can be instantiated

Both

Interface or Abstract Class can have abstract instance methods

Both

Interface or Abstract Class can have concrete instance methods

Both

Interface or Abstract Class can have concrete static methods

Both

Interface or Abstract Class can have constants

Both

Interface or Abstract Class can have private methods

Both

Interface or Abstract Class can have public methods

Both

What are the benefits of writing exceptions? Is it possible to write programs without exceptions?

By writing exceptions we separate our error handling from our program's core logic. It is possible to write programs without explicit exceptions, but implicitly every time we declare an object we are creating the potential for an unchecked null pointer exception during runtime.

What's the difference between checked exceptions and unchecked exceptions?

Checked exceptions will cause a compiler error if they are not handled or specified. Unchecked exceptions will not create a compiler error, as they are usually more common and can occur so often (for instance, every time we create a reference to an object) that handling them would be a massive hassle. Unchecked exceptions are all subclasses of RuntimeException (or subclasses of subclasses of RuntimeException... and so on).

What is the significance of default methods?

Default methods define a default implementation for the method, meaning a class may or may not override it

Say we have an Athlete class with only a method practice(). We also have a class FootballPlayer which extends athlete. The FootballPlayer class has one method: liftWeights(). What happens during compile time and runtime when these lines are executed: Athlete b; FootballPlayer c = new FootballPlayer(); b = c; b.practice(); b.liftWeights();

During compile time, the compiler checks if the declared type of b has the method practice() and also liftWeights(). Since liftWeights() is not a function of the Athlete class, the code will not compile.

Say we have an Athlete class with only a method practice(). We also have a class FootballPlayer which extends athlete. The FootballPlayer class has one method: liftWeights(). What happens during compile time and runtime when these lines are executed: Athlete a = new FootballPlayer(); a.practice();

During compile time, the compiler performs an is-a check to see if the FootballPlayer object is an Athlete. Since such an assignment is valid, the compiler then checks if the declared type has the function practice(). Since it does, the code successfully compiles. During runtime, the JVM will successfully run, since FootballPlayer inherits the practice() method from Athlete.

Explain all the reasons merge sort is different from selection sort.

For starters, merge sort is significantly faster than selection sort for large sets of data, as it runs in linearithmic time whereas selection sort runs in quadratic time. Merge sort works by recursively splitting the data into halves until it can't be split, then sorting those halves. Selection sort works by finding the smallest item in an unsorted section and adding that to the end of a sorted section, doing that repetitively until the whole data set is sorted.

Why are generic classes important? When do we use them?

Generic classes are important as they allow us to specify the type of input we want to work with within a certain class. For instance, the Comparable interface uses a generic to specify what type of object will be considered as a valid argument to the compareTo function. (implements Comparable<T>)

Interface or Abstract Class Supports multiple inheritance/implementation

Interface

Why do we use interfaces?

Interfaces allow us to write generic code that interacts with objects of different type that have no meaningful inheritance relationship but have common behaviors

Interface or Abstract Class can be a static type (another term for object type. both can be used as a declared type)

Neither

Interface or Abstract Class can be used on the right hand side of instanceof

Neither

Interface or Abstract Class can have abstract static method

Neither

Interface or Abstract Class can implements/extends interfaces

Neither

What is the object type in this example: Food a = new Pizza();

Pizza

What are 5 differences and similarities between interfaces vs. abstract classes?

Similarities: both can have abstract methods, static variables, final variables (constants), both can inherit others of the same type (interfaces can extend other interfaces, abstract classes can extend other abstract classes), and both can have public methods. Differences: multiple interfaces can be inherited, while multiple abstract classes cannot. Abstract classes can have instance variables (interfaces cannot). Abstract classes can have a constructor (interfaces cannot). Abstract classes can have protected methods (interfaces cannot). Abstract classes can extend other classes, interfaces can only extend other interfaces (class "implements" an interface, while interfaces "extends" other interfaces)

If you have Person[] myArray = new Person[5], what is the precondition if you want Arrays.sort(myArray) to work?

The Person class must implement the Comparable interface

What is the requirement in order to use binary search?

The input must already be sorted in increasing order

Does the following make sense logically? Explain your answer. int a = 5; int b = 0; try { int c = a/b; int d = c * 8; } catch (Exception e) { System.out.println("you made a mistake"); } catch(ArithmeticException ae) { System.out.println("AHHHHHH YOU DIVIDED BY 0"); }

This does not make sense and will raise a compiler error as ArithmeticException is caught after Exception, meaning the code will never be run (since Exception is a parent class of all exceptions)

What is the purpose of generics?

To specify the type of input we expect for a particular implementation of a general interface/class

When would we use default methods over static methods and vice versa?

Usually we would use default methods if we wanted to add more methods to an interface but don't want to force the writing of an implementation for that new method for every class already using the interface (this assists with backwards compatibility). In cases where we want to add the exact same functionality shared over the entire interface, we would likely use a static method.

Which is true about an abstract class? a. Abstract class can have non-static variables b. Abstract class can only have abstract methods c. Abstract class cannot contain non-final variables d. None of the above

a. Abstract class can have non-static variables

Which of the following are unchecked exceptions: a. ArithmeticException b. IllegalArgumentException c. NullPointerException d. IOException e. ClassNotFoundException

a. ArithmeticException b. IllegalArgumentException c. NullPointerException

When is an unchecked exception thrown? a. Runtime b. Compile time c. Both times d. It depends on the class

a. Runtime

If NotEnoughTimeException directly extends Exception, what type of Exception will NotEnoughTimeException be? a. Error b. Checked Exception c. Unchecked Exception d. B and C e. Cannot determine

b. Checked Exception

The ability for a method to be dynamically bound at runtime is which of the following principles? a. Inheritance b. Polymorphism c. Encapsulation d. Abstraction

b. Polymorphism

When is a checked exception thrown? a. It depends b. Runtime c. Compile-time d. Never e. None of the above

b. Runtime (Exceptions, of any kind, are thrown at runtime. Checked exceptions can result in compiler errors, which are messages indicating the errors. )

Which of the following is true about checked exceptions? a. They can be handled using try-catch blocks b. They can be handled by using the throws keyword (specifies the exception, doesn't handle) c. Both a and b d. Every program must have a checked exception

b. They can be handled by using the throws keyword (specifies the exception, doesn't handle)

If TooMuchTimeException directly extends RunTimeException, what type of Exception will TooMuchTimeException be? a. Error b. Checked Exception c. Unchecked Exception d. B and C e. Cannot determine

c. Unchecked Exception

What of the following can you catch in the code? a. ArithmeticException b. FileNotFoundException c. IllegalArgumentException d. B and C e. All of the above

e. All of the above

Every class inherits a default interface

false

Inheriting the "Compare" interface allows for a single Arrays.sort() method that is capable of ordering the elements in an array of any Java class

false

Unchecked exceptions can be handled by the keyword "throws"

false

Comparable's compareTo method compares two objects based on their natural ordering, assuming both objects are instances of a class that extends Comparable.

false (A class cannot extend comparable)

Comparable's compareTo method will return 1 if the object is greater than the other one comparing to, -1 if it's less.

false (Return value is not constrained to 1 and -1 when an object is greater than or less than another)

We can't downcast in the inheritance tree.

false (Unlike upcasting, downcasting can't be done implicitly, but it can still be performed)

A finally block is not executed if an exception occurs

false (finally block is ALWAYS executed)

All exceptions must be caught or thrown

false (not unchecked exceptions)

An interface must define both the headings and definition of each declared method

false (only has to define header for abstract methods)

Which of the following about Exceptions is/are true? a. If a method has a possibility of throwing a checked exception and it is not caught, then the method header has to declare a throws statement. b. If a method has a possibility of throwing an unchecked exception and it is not caught, then the method header has to declare a throws statement. c. When an exception is thrown, your code is always going to crash. d. The keyword, "throws" is used when you want to manually generate an exception e. Exceptions can be stored in an Array.

ii. A, E A: (True. Checked exceptions must be caught or specified to be thrown in header) d. false The keyword, "throws" is used when you want to manually generate an exception (False. Correct answer is "throw" rather than throws is correct for throwing an exception in a method body.) E. True. Exceptions are objects

When a class inherits an interface, the keyword ______ must be in the class header.

implements

Which package is the Comparable interface in?

java.lang

If a class implements Comparable<Student>. What method do you have to override? Write down the whole method header.

public int compareTo(Student s)

What is the keyword used to access the visible fields and data in a parent class?

super

Arrays.sort() utilizes the natural ordering provided by Comparable's method compareTo.

true

Comparable has exactly one abstract method

true

The "is a" check happens during runtime.

true (Can occur during compile time and runtime)

java supports multiple inheritance

true (true for interfaces)

(E) temp;

valid

E temp;

valid


संबंधित स्टडी सेट्स

TMC110 Module 3: Entrepreneurship

View Set

Chapter 11 Developing and Managing Products

View Set

PEDS ATI A+B practice qs for exam2

View Set

Exploration, Conquest, and Interaction Unit test review

View Set

Neurons and synaptic transmission- Biopsychology

View Set

One Solution, No Solution, Infinite Solutions

View Set

Catcher In the Rye Character List

View Set