CSDS 132: Quizzes 8-15

Ace your homework & exams now with Quizwiz!

Give the appropriate method header ofr a method sort whose input is an array and we want the elemetns of the array to implement the Comparable interface. A. public <T extends Comparable <? super T>> void sort (T [] array} { B. public void sort (Comparable [] array) { C. public void sort (Comparable <?> [] array) D. public <T extends Comparable<T>> void sort (T [] array) {

A

Given the best method header for a static method that will return the maximum element of a List of elements where List <E> is a type of the API, we want to restrict the elements to be all the same class and that class must extend the abstract class Number. A. public static <T extends Number> max (List <T> list) { B. public static Number max (List <E> list) C. public static Number max (List <Number> list) { D. public static ? max (List <? extends Number> list) {

A

Here is a loop that shifts all the elements of an array down. (You had to do something like that in lab.) while (i < list.length - 1) { list[i] = list[i + 1]; i = i + 1; } Now, let us write it again, but this time use shortcuts! What must go inside the brackets so that the loops works correctly? while (i < list.length - 1) list[i] = list[???]; A. ++i B. --i C. i-- D. i++

A

If a program is using event driven programming paradigm, how does a program handle events? A. An event is something that happens on the computer outside the program. The event is handled by registering with the operating system to receive the event, indicating what code should be run when the event occurs, and then waiting for the event. B. An event is any exception that happens while the code is running. The event is handled using the try/catch statement. C. An event is any form of user input. Any class that extends Reader (such as FileReader, BufferedStreamReader) can be used to input the data from the user. D. An event is a really bad occurrence to happen to the machine running the program. to properly follow the event driven programming paradigm is to have multiple versions of the program running so that if one crashes, another is available to be used.

A

If we wish to explicitly deal with an exception, what statement should we use? A. try/catch B. if C. switch D. break

A

What are the rules for creating a nested class? A. A nested class is a class placed inside another class. There are no other rules. B. A nested class is not allowed in Java. C. A nested class is a class placed inside another class. A nested class must be static because it goes in the class and not in the instance. D. A nested class is a class placed inside another class. A nested class must be static and private because it belongs to the class and should not be used outside of this class.

A

What can an abstract class contain? A. Anything a regular class can contain plus abstract methods B. Only methods, fields, and abstract methods. Because an abstract lass cannot be instantiated, it cannot contain a constructor. C. Only abstract methods (also called method stubs). D. Nothing because it is abstract.

A

What does the Java keyword volatile indicate? A. It indicates that a particular's variable value could be updated by multiple threads, and the Java compiler's optimizer should not assume that the current thread is the only one changing the value. B. It indicates that running the program could cause the computer to catch fire or even explode. C. It indicates that a block of code could throw an exception. D. It indicates that a block of code needs to have a lock to prevent multiple threads from accessing code at the same time.

A

What is an anonymous class? A. A class defined with the new operator. As a result, the class has no name. B. A private class. Code outside the file the class is in cannot access the class and does not know the class's name. C. A class that likes to troll message boards. D. A class created without a name: public anonymous class {

A

What is reflection in Java? A. The part of the Java language that lets you view and manipulate the structure of a Java class or object. B. The methods that let you reverse the content of a structure such as an array or a linked list. C. The inelastic effect of an object striking a solid surface. D. The part of the Java language that lets you override methods to produce polymorphic results.

A

What is the correct method reference code that can replace the following? canvas.setOnMouseDragged (new ActionHandler <MouseEvent> () { public void handle (MouseEvent e) { Painter.this.draw(e); } } A. canvas.setOnMouseDragged (this::draw); B. canvas.setOnMouseDragged(e -> this.draw(e)); C. canvas.setOnMouseDragged(Painter.this.draw(e)); D. canvas.setOnMouseDragged(new ActionHandler <MouseEvenet> -> this.draw);

A

What is the difference between Comparable and Comparator? A. Both are interfaces used to indicate the values of the type can be compared and sorted. Comparable is used to define the default ordering on values, and Comparator is used to define any other ordering. B. Both are used to indicate that values of the type can be compared and sorted. Comparable is an interface while Comparator is an abstract class. C. Both are used to indicate that values of a type can be compared and sorted. Comparable's method parameter has Object as its input while Comparator uses a generic type. D. One has "ble" and other has "tor". There is no other difference. They are both interface that have a generic and same abstract method.

A

What rule must you following when placing a non-static method in an interface? A. All non-static methods in an interface must be public, non-static method stubs. B. There is no restriction. an interface can contain anything a class can contain. C. You must place a comment above any method in the interface body. D. All non-static methods in an interface must be private.

A

You have two values in variables a and b. The variables are the same type that implements the Comparable interface. How do you write the conditional "if a is less that b"? A. if (a.compareTo(b) < 0) B. if (a < b) C. if (a.lessThan(b)) D. if (a.compareTo(b) == true)

A

Which of the following are legal automatic typecasts in Java? A. Object o = new LLNode <String> ("Hi", null); B. LLNode <Object> node = new LLNode <String> ("Hi", null) C. LLNode <String> node = new LLNode <String> ("Hi", null) D. LLNode <JFrame> node = new LLNode <String> ("Hi", null) E. LLNode <T> node = new LLNode <S> (null, null)

A & C

Which of the following can be used to state that "a document is an array list of character sequecnes" where you are using the Java API's class ArrayList and interface CharSequence and ArrayList takes a generic type? A. public class Document extends ArrayList <CharSequence> { B. public class Documnet extends ArrayList <T extends CharSequence> { C. public class Document <T extends CharSequence> extends ArrayList <T> D. public class Document extends Arraylist <T implements CharSequence> { E. public class Document <T extends ArrayList implements CharSequence> {

A & C

Suppose we have a variable declared as LinkedList <? extends Employee> eList Indicate whether each of the following tasks is legal or illegal to do on variable eList. A. Add an Employee instance to eList B. Retrieve the name from an Employee instance in eList C. Change the salary of an Employee instance in eList D. Sort the eList so it stores Employee instances in order by last name

A. illegal B. legal C. legal D. illegal

How long does a variable exist inside a method body? A. Until there are no more references to the object it is declared in because then the Java garbage collector de-allocates the variable from the memory heap. B. Until execution leaves the compound statement it is declared in because then the stack frame holding the variable is de-allocated from the stack. C. As long as the program is being executed. ONce the program terminates, the operating system frees the memory so it can be used by another program. D. A variable declared inside a method, no matter where in the method it is declared, exists until the end of the execution of the method. When the return statement of the method is reached, all variable declarations are destroyed.

B

Suppose we have the following class definition: public class A implements B { and supposed we have two variable declarations A a; B b; Which of the following two assignment statements are legal? a = b; b = a; A. Both. Since class A implements interface B, the two types are interchangeable without explicit typecasts. B. Only b = a; Every object of type A is also type B, but not every object of type B is a type A. The other assignment requires an explicit typecast. C. Only a = b; Every object of type B is a type A, but not every object of type A is a type B. The other assignment requires an explicit typecast. D. Neither. Explicit typecasts are needed when converting between interfaces and class because we are not going up the object hierarchy.

B

What are threads in a program? A. A thread is the name for a bug fix used to patch up errors in your code. B. A thread is a piece of a program that can run concurrently with the rest of the program. C. A thread is a trace of the execution of your code. A thread is used to debug your code. D. A thread is the return path of your code. It si named after the thread used by Ariadne is the labyrinth of the Minotaur.

B

What is an abstract data type? A. A data type with no discernable form B. A data type in which the implementation details are hidden from other parts of the program that use the type C. A data type that can't be instantiated D. A data type with abstract methods (also known as method stubs)

B

What is the Java keyword synchronized used for? A. It is used to time the execution of a piece of code with the system clock. For example, we used synchronized to determine how long insertion sort would take on a list of n elements. B. It locks a piece of code so that only one thread at a time can enter any code associated with the same synchronized lock. C. It is used to create code that two or more threads will run at exactly the same time. D. It is used to synchronize threads so that they start at exactly the same time.

B

What is the correct lambda shortcut for the following code: ChangeListener <Number> handler = new ChangeListener <Number> () { public void changed (ObservableValue <? extends Number> observable, Number oldValue, Number newValue) { Painter.this.getTextField().setText(newValue.toString()); } } A. ChangeListener<Number> handler = new ChangeListener <Number> () -> (ObservableValue <? extends Number> observable, Number oldValue, Number newValue) { Painter.this.getTextField().setText(newValue.toString()); } B. ChangeListener <Number> handler = (observable, oldValue, newValue) -> {this.getText().setText(newValue.toString());}; C. ChangeListener <Number> hnadler = new lambda (ObservableValue <? extends Number> observable, Number oldValue, Number newValue) { Painter.this.getTextField().setText(newValue.toString()); }; D. ChangeListener <Number> handler = e -> {setText(newValue.toString());};

B

In the method header below, what is the type of parameter a? A. Integer B. Integer... C. Integer [] D. Object <Integer>

C

Suppose we have a variable z of type Integer that stores 10. What is the result of: z = z + 1 A. an error because + is not allowed on the Integer type B. the int value 11 C. a new Integer object storing the int value 11 D. the Integer object referenced by z is updated to now store the int value 10

C

What design paradigm should you use if you do not know how the program you are writing should be structured but you want something that you can test right away? A. JavaDoc documentation B. JUnit testing C. bottom-up design D. top-down design

C

What interface is used to indicate there exists a way to iterate through the elements of this data type? A. List B. ArrayList C. Iterable D. Iterator

C

What is a breakpoint? A. A location in the code where an error occurs breaking your program. B. A location in the loop where you place a break or continue statement to jump out of the loop body. C. A location in the code where the execution will stop when you are using a debugger. D. A place where you can ride the best waves.

C

What is an abstract class? A. A class that cannot be instantiated B. A class that does not have a class body C. A class that cannot be directly instantiated D. Another name for an interface: a class that permits multiple inheritance

C

What is the purpose of the T in the following class header? public class LLNode <T> { A. T is a class of the Java API. It is specified in the java.util.package. B. T is used as a place holder for an interface because Java does not allows true multiple inheritance. Interfaces are used to enforce certain behavior on a class by requiring the class to have specific methods. C. It lets us use T as a type in the class so we do not have to specify the type when writing the class. Instead, the type that T represents will be determined each time an object of the class is created. D. T is used as shorthand for a type that is previously defined, in this case LLNode. This allows us to shorten the amount of typing in the class because we can now declare variables with "T x" instead of "LLNode x".

C

What is the purpose of the main method in Java? A. It is called by the new operator to initialize an instance of a class. B. It is used for running automatic test cases for the class. C. It is the first method that is executed when a stand-alone program is run. D. Anything the programmer wants. It is just a method with the name "main".

C

When an anonymous class accesses a local variable from the method that the anonymous class is created inside, why must the variable be marked as final? A. Anonymous classes, because they do not have a name, are static classes. Therefore, all variables that the class accesses must be either static or final. B. Until the variable is marked as final, the Java compiler will assume that the programmer is not finished editing the code. It is a compiler error to compile incomplete code. C. All local variables are stored on the stack, but the anonymous class instance is in the heap. The stack variable may not exist when the anonymous class needs it. By indicating that the variable is final, the compiler knows that the value will not change and so it can make a copy for the anonymous class. D. It is an error for an anonymous class instance to change the value of a variable that is not inside the class. If the variable is not marked final, the instance may be able to change it and cause the method to not run properly.

C

Where can you declare a generic type in your program? A. Anywhere in the code, but the declaration must be placed immediately after a class name B. Anywhere in the code C. In the class header immediately after the class type or in a method header immediately before the return type D. Only in the class header and immediately after the class name in the class header

C

Given the appropriate class header for a class that uses a generic type T, but we want T to be restricted to only classes that implement the Comparable<T> interface. A. public class MyClass <T> extends Comparable <T> { B. public class MyClass <? extends Comparable <?>> { C. public class MyClass <Comparable<T>> { D. public class MyClass <T extends Comparable <? super T>> {

D

How long does a variable declared as a non-static field exist in your program? A. The field exists until execution leaves the body of the class. Once the execution leaves the class, the stack frame for the class is deallocated from the stack. B. The field exists until you shutdown and reboot your computer. C. The field exists for the duration of your program. Once the program ends, the operating system frees up the allocated memory so it can be used by another program. D. The field exists from the moment "new" creates the object containing the field until that object no longer exists in your program.

D

The Java API has a class called ArrayList and the Java API specifies the class as Class ArrayList <E> The class has a constructor that takes as input the number of elements that will be stored in the ArrayList. How would you create an instance of ArrayList that you are going to use to store 100 objects of type JFrame? A. ArrayList <E> alist = new ArrayList <E> (100); B. ArrayList <E> alist = new ArrayList <E> (JFrame, 100); C. JFrame [] alist = new JFrame [100]; D. ArrayList <JFrame> alist = new ArrayList <JFrame> (100);

D

What is a JavaFX property? A. The property is space on the screen used by JavaFX. The more pixels the JavaFX application uses and the more centrally located the application is on the screen, the valuable the "property" is that the application uses. B. A property is an attribute of a JavaFX application. For example, the background color of a button is a property. By making these properties, we can set the values using either Java API calls or CSS files. C. A property is another name for a method. In building a JavaFX application, we decide what properties each class should have, and then we move the properties as high up the hierarchy as possible, and create methods to implement them. D. A property is a private field along with public (or protected) getter/setter methods named appropriately. The property lets us bind the setter for one field to a getter of another field in a way that even works across threads.

D

What is a jar file? A. A file that is the JavaFX application thread. Al code that displays JavaFX gadgets must use the application thread. B. A file that contains food and can be opened by twisting off the lid. C. A file that contains related classes. You have to import the jar file at the top of your class file if you want to use any of those classes in your code. D. A file that contains multiple Java class files. It is used to combine all the files of a program into a single file so that file that can be used in other programs or executed by itself.

D

What is the benefit of using linked lists instead of arrays? A. Linked lists can store anything while arrays are restricted to store the same type. B. Linked lists are easier to program than arrays. C. Linked lists are not stored in contiguous memory so that they have very fast access to each element stored in the list. D. Linked lists are not stored in contiguous memory so increasing or decreasing the number of elements stored in the linked list is fast.

D

Which is the correct foreach loop that will sum up all the values in a LinkedList <Integer>, assuming LinkedList implements the Iterable interface? A. int sum = 0; for (LLNode <Integer> node = list.getFront(); node != null; node = node.getNext()) { sum += node.getElement(); } B. int sum = 0; for (Integer i : list) { sum += i.getElement(); } C. int sum = 0; for (LLNode <Integer> node : list) { sum += node.getElement(); } D. int sum = 0; for (Integer i : list) { sum += i; }

D

You have a class that declares a generic type. You are writing a nested class for this class, and the nested class will use the generic type. Does your choice of whether or not you make the nested class affect how the nested class uses the generic type? A. No. The generic type exists in all nested classes. B. Yes. The generic type exists in the static nested class, but a non-static nested class must declares its own generic type. C. No. The generic type does not exist in any nested class. Both the static and non-static nested classes must declare their own generic type. D. Yes. The generic type exists in the non-static nested class, but a static nested class must declare its own generic type.

D

Use the options given to write Java code that does exactly the same as the following: Optional<Filter<T>> of = Optional.ofNullable(filter); x = of.map (f -> f.passFiler(v)).orElse(false);

if (filter == null) { x = false; } else { x = filter.passFilter(v); }

For each kind of variable, indicate where Java allocates space for that variable. - instance field - class field - method parameter - local variable - the special variable "this"

instance field- the heap class field- the heap method parameter- the stack local variable- the stack the special variable "this"- the stack

Main method header

public static void main (String [] args) {


Related study sets

Absolutism/Constitutionalism Test Q?'s

View Set

Module 4: Random Variables and Introduction to Distributions

View Set

Memmlers The human body chapter 15 circulation

View Set

Chapter 26 Coagulation Modifier Drugs

View Set

Chapter 14- Retailing and direct marketing

View Set

Simulation Lab 2.2: Module 02 Install and Use Wireshark

View Set

Gr 8 Tegnologie Ratte en meganiese voordeel

View Set

Ch. 15 PrepU questions, NUR444 PrepU Ch.15, Chapter 15: Obsessive-Compulsive and Related Disorders, NSG 211 - CH 15 OCD, Ch 15 Anxiety and Obsessive-Compulsive Related Disorders, Chapter 15: Obsessive-Compulsive and Related Disorders

View Set

NWM Federal Tax Considerations for LIfe Insurance and Annuities

View Set