All Quizzes

Ace your homework & exams now with Quizwiz!

What is a method? A member of a class that describes a data item that describes an object. A member of a class that defines an operation on an object. An operation that is not associated with a specific object. An operation that is not associated with a specific object.

A member of a class that defines an operation on an object.

A class can only implement one interface.

False

All methods must return a value.

False

Which of the following is allowed in a Java interface? Headings for constructors. Headings for public methods. Instance variables. Method bodies.

Headings for public methods.

What happens if an no exceptions are thrown inside a try block? The code in the the try block is skipped and then the code in the catch block is executed. The code in the try block is executed and then the code in the catch block is executed. The code in the try block is skipped and then the code in the catch block is skipped. The code in the try block is executed and then the code in the catch block is skipped.

The code in the try block is executed and then the code in the catch block is skipped.

What happens if an exception is thrown by a statement inside a try block? The remainder of the try block is skipped and then the code in the catch block is executed. The remainder of the try block is executed and then the code in the catch block is executed. The remainder of the try block is skipped and then the code in the catch block is skipped. The remainder of the try block is executed and then the code in the catch block is skipped.

The remainder of the try block is skipped and then the code in the catch block is executed.

You cannot create an object of an abstract class.

True

Which of the following is the correct way to open a text file called "myfile.txt" for reading? inputStream = new File("myfile.txt"); inputStream = new Scanner("myfile.txt"); inputStream = new File(new Scanner("myfile.txt")); inputStream = new Scanner(new File("myfile.txt"));

inputStream = new Scanner(new File("myfile.txt"));

Which of the following is the correct way to declare an array of 12 integers in Java? int numbers = new int[12]; int[] numbers = new int[12]; [int numbers] = new int[12]; numbers = new int[12];

int[] numbers = new int[12];

Given the following initial array: {'P', 'A', 'U', 'S'} Which of the the following correctly illustrates the sequence of passes performed by selection sort? {'P', 'A', 'U', 'S'} {'A', 'P', 'U', 'S'} {'A', 'P', 'U', 'S'} {'A', 'P', 'S', 'U'} {'P', 'A', 'U', 'S'} {'A', 'P', 'U', 'S'} {'A', 'P', 'S', 'U'} {'A', 'P', 'S', 'U'} {'P', 'A', 'U', 'S'} {'A', 'U', 'S', 'P'} {'A', 'P', 'U', 'S'} {'A', 'P', 'S', 'U'} {'P', 'A', 'U', 'S'} {'P', 'A', 'S', 'U'} {'A', 'P', 'S', 'S'} {'A', 'P', 'S', 'U'}

{'P', 'A', 'U', 'S'} {'A', 'P', 'U', 'S'} {'A', 'P', 'U', 'S'} {'A', 'P', 'S', 'U'}

Given an sorted array containing the following numbers: 1 3 7 10 12 17 19, what numbers are compared with the search item by a binary search for the number 12? 1, 3, 7, 10, 12 10, 17, 12 19, 17, 12 10, 12

10, 17, 12

Which of the following best describes the steps taken by the merge sort algorithm when sorting the array containing the following numbers: 4 3 2 1 4 3 2 1 3 4 2 1 3 4 1 2 1 2 3 4 4 3 2 1 1 3 2 4 1 2 3 4 1 2 3 4 4 3 2 1 3 4 2 1 2 3 4 1 1 2 3 4 4 3 2 1 3 4 2 1 3 2 4 1 3 2 1 4 2 3 1 4 2 1 3 4 1 2 3 4 1 2 3 4

4 3 2 1 3 4 2 1 3 4 1 2 1 2 3 4

What is the output of the following code? public static void main(String[] args){ System.out.println(getMysteryValue(3)); } pubic static int getMysteryValue(int n) { if (n <= 1) return 1; else return getMysteryValue(n - 1) + n; } 1 3 6 9

6

Which of the following best describes the concept of inheritance? A base class inherits public methods from a derived class. A derived class inherits public methods from a base class. A base class inherits private instance variables from a derived class. A derived class inherits private instance members from a base class.

A derived class inherits public methods from a base class.

What is decomposition? A form of testing that starts by testing a method before testing any other method that calls it. A design strategy that breaks a task down into several subtasks. The use of classes, inheritance, and polymorphism to make a program more modular. The combining of code from several smaller methods into one larger method.

A design strategy that breaks a task down into several subtasks.

What is the difference between a full path name and a relative path name? A full path name starts at the root directory while a relative path name starts at the directory in which the program is running. A relative path name starts at the root directory while a full path name starts at the directory in which the program is running. A full path name is used by UNIX operating systems while a relative pathname is used by Windows operating systems. A relative path name is used by UNIX operating systems while a full pathname is used by Windows operating systems.

A full path name starts at the root directory while a relative path name starts at the directory in which the program is running.

What is the difference between overriding a method and overloading a method? A method in a derived class overloads a method with a different signature but same name in the base class. A new method with the same signature overrides an existing method in the same class. A method in a derived class overloads a method with the same signature in the base class. A new method with a different signature but same name overrides an existing method in the same class. A method in a derived class overrides a method with a different signature but same name in the base class. A new method with the same signature overloads an existing method in the same class. A method in a derived class overrides a method with the same signature in the base class. A new method with a different signature but same name overloads an existing method in the same class.

A method in a derived class overrides a method with the same signature in the base class. A new method with a different signature but same name overloads an existing method in the same class.

What is a recursive method? A method that can not be called outside of a class. A method that contains a loop. A method that calls itself. A method that calls another method.

A method that calls itself.

Which of the following best describes the binary search algorithm? A sorted array is searched starting at the beginning, then moving towards the end until the search item is found or the end is reached. A sorted array is searched starting at the end, then moving towards the beginning until the search item is found or the beginning is reached. A sorted array is searched starting in the middle. If the middle element is smaller than the search item, the left half is searched recursively. If the middle element is larger than the search item, the right half is searched recursively. The search continues until the search item is found or no items are left. A sorted array is searched starting in the middle. If the middle element is smaller than the search item, the right half is searched recursively. If the middle element is larger than the search item, the left half is searched recursively. The search continues until the search item is found or no items are left.

A sorted array is searched starting in the middle. If the middle element is smaller than the search item, the right half is searched recursively. If the middle element is larger than the search item, the left half is searched recursively. The search continues until the search item is found or no items are left.

What is the difference between a static variable and an instance variable? A static variable can not be modified after it is initialized. An instance variable can be modified. A static variable can be modified after it is initialized. An instance variable can not. Every object has its own version of a static variable. An instance variable is shared by all objects of a class. A static variable is shared by all objects of a class. Every object has its own version of an instance variable.

A static variable is shared by all objects of a class. Every object has its own version of an instance variable.

What is the difference between a text file and a binary file? A text file stores a sequence of characters while a binary file uses some other encoding of data. A binary file stores a sequence of characters while a text file uses some other encoding of data. A text file can be used as an input file while a binary file can be used as an output file. A binary file can be used as an input file while a text file can be used as an output file.

A text file stores a sequence of characters while a binary file uses some other encoding of data.

What is the difference between an abstract class and an interface? An interface can contain method definitions while and abstract can only contain method headers. An abstract class can contain method definitions while an interface can only contain method headers. An abstract class can contain constructors, while an interface can not. An interface can contain constructors while an abstract class can not.

An abstract class can contain method definitions while an interface can only contain method headers.

What is the difference between an accessor method (or getter) and a mutator method (or setter)? An accessor method returns data contained in an instance variable. A mutator method modifies data contained in an instance variable. An accessor method can be called from outside a class definition. A mutator method can only be called inside a class definition. An accessor method must be a void method. A mutator method must return a value. An accessor method is used to access local variables. A mutator method is used to access instance variables.

An accessor method returns data contained in an instance variable. A mutator method modifies data contained in an instance variable.

What is the difference between an input stream and an output stream? An input stream processes file data while an output stream processes console data. An input stream processes console data while an output stream processes file data. An input stream reads data from a file or console and sends it to a program. An output stream takes data from a program and writes it to a file or console. An input stream takes data from a program and writes it to a file or console. An output stream reads data from a file or console and sends it to a program.

An input stream reads data from a file or console and sends it to a program. An output stream takes data from a program and writes it to a file or console.

What is an exception? A method that is called when an unusual event occurs. A statement that is executed after the keyword else. An object that signals the occurrence of an unusual event. An error that is detected when code is compiled.

An object that signals the occurrence of an unusual event.

When is a finally block executed? If an exception has been thrown in the try block. If no exceptions have been thrown in the try block. Both of the above. None of the above.

Both of the above.

Where should the keyword abstract be used in a class definition for an abstract class? In the declaration of the class. In the declarations of all abstract methods. Both of the above. None of the above.

Both of the above.

How is the inheritance relationship represented in a UML diagram? By an arrow pointing from the derived class to the base class. By an arrow pointing from the base class to the derived class. By drawing the box for the derived class inside of the box for the base class. By drawing the box for the base class inside of the box for the derived class.

By an arrow pointing from the derived class to the base class.

A private method in a base class is inherited by a derived class.

False

Arrays can be used as parameters for methods, but a method can not return an array.

False

If an array is sorted, the fastest way to search it is by using a sequential search.

False

If you are going to insert a throw statement in your code it is probably best to use the built-in Exception class rather than building your own.

False

Java treats a recursive method call differently from an ordinary method call.

False

Java uses static binding to determine which version of a method is called on an object.

False

Only one constructor may be defined per class.

False

Regular methods may be overloaded, but a constructor may not be overloaded.

False

Sequential search needs to be sorted before use

False

The close method of the PrintWriter class must be called inside of a try block.

False

The merge sort algorithm contains only one recursive call.

False

Given the following class definitions: public class Carnivore { public Carnivore() {} public void speak() { System.out.println("Grrr"); } } public class Cat extends Carnivore { public Car() {} public void speak() { System.out.println("Meow"); } Carnivore rumTumTugger = new Carnivore(); Carnivore bombalurina = new Cat(); Cat munkustrap = new Cat(); rumTumTugger.speak(); bombalurina.speak(); munkustrap.speak(); What output will the following statements generate? Grrr Meow Meow Grrr Grrr Meow Grrr Grrr Grrr Meow Meow Meow

Grrr Meow Meow

What is the difference between inheritance and polymorphism? Inheritance means that methods in a base class can be overridden in a derived class. Polymorphism means that methods in a derived class can be overridden in the base class. Inheritance means that methods in a derived class can be overridden in the base class. Polymorphism means that methods in a base class can be overridden in a derived class. Inheritance means that methods written in a derived class can be applied to objects of the base class. Polymorphism means that methods written in a base class can be applied to objects of a derived class. Inheritance means that methods written in a base class can be applied to objects of a derived class. Polymorphism means that methods written in a derived class can be applied to objects of the base class.

Inheritance means that methods written in a base class can be applied to objects of a derived class. Polymorphism means that methods written in a derived class can be applied to objects of the base class.

Given the following code: public class Larry { private Curly curly; /* additional instance variables and methods */ } public class Moe extends Larry{ /* instance variables and methods */ } Which of the following best describes the relationship between Larry, Curly, and Moe? Larry is-a Curly, Moe is-a Larry. Larry has-a Curly. Moe is-a Larry. Larry is-a Curly, Moe has-a Larry. Larry has-a Curly, Moe has-a Larry.

Larry has-a Curly. Moe is-a Larry.

Which of the following is not required for the correct implementation of a recursive method? There must be a branching statement that leads to different cases. Only one branch may contain a recursive call. Any recursive call must be given arguments that are "smaller" than the arguments to the method. One or more branches must include no recursive call.

Only one branch may contain a recursive call.

Which Java class is the preferred class to use for writing to a text file? FileOutputStream PrintWriter Scanner File

PrintWriter

Given a class called Rectangle with a constructor that takes two doubles as arguments representing the dimensions, which of the following is the correct way to invoke that constructor? Rectangle rect = Rectangle(8.2, 3.5); Rectangle rect = new Rectangle(8.2, 3.5); Rectangle rect;Rectangle(rect, 8.2, 3.5); Rectangle rect;new Rectangle(rect, 8.2, 3.5);

Rectangle rect = new Rectangle(8.2, 3.5);

Which of the following classes is for an exception that does not need to be caught in a catch block or declared in a throws clause? Throwable Error Exception RuntimeException

RuntimeException

Which Java class is the preferred class to use for reading from a text file? FileOutputStream PrintWriter Scanner File

Scanner

Which of the following best describes the merge sort algorithm? Consecutive items in the wrong order are repeatedly swapped until the array is sorted. The smallest unsorted item is repeatedly swapped with the first unsorted item. A pivot item is chosen, the array is divided into items smaller than the pivot and items larger than the pivot, and the two parts are sorted recursively. The array is split in half, the two halves are sorted recursively, then the two halves are combined to put all items in the correct order.

The array is split in half, the two halves are sorted recursively, then the two halves are combined to put all items in the correct order.

Why is it a good idea to override the toString and equals methods when creating a new class? If they are not overridden, the code for the class will not compile. If they are not overridden, the code for the class will generate a runtime error. The inherited versions will have the wrong signature. The inherited versions will not handle any private instance variables of the new class.

The inherited versions will not handle any private instance variables of the new class.

What is the result if the final modifier is used in the heading of a method definition? The method can not be overridden. The method can not be overloaded. The method must return a constant value. The method must return no value.

The method can not be overridden.

Assume there is a class called Trombone with a default constructor. Which of the following is the correct way to create and initialize an array of 76 Trombone objects? Trombone[] marchingBand = new Trombone[101]; Trombone[] marchingBand = new Trombone[101]; for(int i = 0; i < marchingBand.length; i++) marchingBand[i] = new Trombone(); for(int i = 0; i < 101; i++) Trombone marchingBand[i] = new Trombone(); Trombone marchingBand[101] = new Trombone();

Trombone[] marchingBand = new Trombone[101]; for(int i = 0; i < marchingBand.length; i++) marchingBand[i] = new Trombone();

A class can only extend one base class

True

A comma-separated values or CSV file is a simple text format used to store a list of records.

True

A method in a derived class with the same signature overrides the method from the base class.

True

A method that does not handle an exception that it throws must have a throws clause in its heading.

True

A recursive method must contain a base case, otherwise it will not terminate.

True

A statement opening a text file for reading or writing may generate a FileNotFoundException.

True

A try block must have a corresponding catch block.

True

All of the items stored in an array must be of the same type.

True

An array can be used as an instance variable in a class.

True

An interface can contain public named constants.

True

Binary search is faster than Sequential search

True

Binary search needs to be sorted before used

True

Data in a file remains after program execution ends.

True

For large arrays, binary search is much faster than sequential search.

True

Java provides several predefined exception classes.

True

The Object class is the ultimate ancestor of every other class.

True

The keyword super can be used to call an overridden base class method in a method of the derived class.

True

When calling a method from another method in the same class, the form methodName() is equivalent to this.methodName().

True

Given two arrays array1 and array2, when does the expression array1 == array2 evaluate to true? When array1 and array2 have the same length and contain the same items. When array1 and array2 have the same length and contain different items. When array1 and array2 have the different lengths. When array1 and array2 refer to the same array object.

When array1 and array2 refer to the same array object.

When does a stack overflow occur? When the data structure used to keep track of recursive calls becomes to large to be stored in available memory. When the data structure used to keep track of new objects becomes to large to be stored in available memory. When a numerical value becomes to large to be stored in available memory. When a program is too large to be stored in available memory.

When the data structure used to keep track of recursive calls becomes to large to be stored in available memory.

Which of the following is not a method of the File class? exists() close() canRead() canWrite()

close()

Assume that Mammal is a subclass of Animal and Dog is a subclass of Mammal. Given the following declarations: Animal animal; Mammal mammal; Dog dog; Which of the follow statements is not legal? animal = mammal; mammal = dog; dog = animal; animal = dog;

dog = animal;

Given the following header for a method in the class Demo: public static double smallest(double[] data) Which of the following is the correct way to call this method, given an array of doubles called values? double min = Demo.smallest(values); double min = Demo.smallest(values[]); double min = Demo.smallest(values[0]); for (int i = 0; i < values.length; i++)double min = Demo.smallest(values[i]);

double min = Demo.smallest(values);

Consider the following code: Employee emp1 = new Employee(); emp1.setName("Anna Karenina"); Employee emp2 = new Employee(); emp2.setName("David Copperfield"); emp2 = emp1; Which of the following best describes the contents of the two variables emp1 and emp2 after this code has executed? emp1 refers to the object with the name David Copperfield. emp2 refers to a different object with the same data. emp1 refers to the object with the name David Copperfield. emp2 refers to the same object. emp1 refers to the object with the name Anna Karenina. emp2 refers to a different object with the same data. emp1 refers to the object with the name Anna Karenina. emp2 refers to the same object.

emp1 refers to the object with the name Anna Karenina. emp2 refers to the same object.

Which of the following is the correct way to initialize a two-dimensional array of integers with 3 columns and 5 rows? int[][] table = new int[3][5]; int[][] table = new int[5][3]; int[][] table = new int[3, 5]; int[][] table = new int[5, 3];

int[][] table = new int[5][3];

Given an array called items, what is the correct way of determining the number of elements in the array? items.size() items.length() items.size items.length

items.length

Which of the following statements opens a text file called "myfile.txt" so that new text can be appended to the existing contents of the file? outputStream = new PrintWriter("myfile.txt"); outputStream = new PrintWriter(new FileOutputStream("myfile.txt")); outputStream = new PrintWriter(new FileOutputStream("myfile.txt", false)); outputStream = new PrintWriter(new FileOutputStream("myfile.txt", true));

outputStream = new PrintWriter(new FileOutputStream("myfile.txt", true));

A class called Printer has a method called printDocument. This calls a void method in the Printer class called printPage that takes an integer representing the page number as an argument. How should printDocument call printPage with an argument of 4? printPage(4); Printer.printPage(4); Printer printer; printer.printPage(4); Printer printer.printPage(4);

printPage(4);

Given the following class declaration: public class MyList { private String[] list; private int numItems; /** Creates an empty list with the given capacity */ public MyList(int capacity) { list = new String[capacity]; numItems = 0; } /**Attempts to add an item to the next open spot in the list. Returns false if the list is full. */ public boolean addItem(String item) { // YOUR CODE HERE } } Which of the the following is a correct implementation of the addItem method? public boolean addItem(String item) { list[numItems] = item; numItems++; return true; } public boolean addItem(String item) { numItems++; list[numItems] = item; return true; } public boolean addItem(String item) { if(numItems == list.length) return false; else { list[numItems] = item; numItems++; return true; } } public boolean addItem(String item) { if(numItems == list.length) return false; else { numItems++; list[numItems] = item; return true; } }

public boolean addItem(String item) { if(numItems == list.length) return false; else { list[numItems] = item; numItems++; return true; } }

Which of the following is the correct way to declare a new exception class called BadStuffHappenedException? public class BadStuffHappenedException { // Constructors } public class BadStuffHappenedException extends Exception { // Constructors } public class BadStuffHappenedException implements Exception { // Constructors } public class BadStuffHappenedException { private Exception e; // Constructors }

public class BadStuffHappenedException extends Exception { // Constructors }

Given an interface called Pourable, how should an interface called Drinkable be declared so it inherits all of the methods headers from Pourable and adds some additional method headers? public class Pourable implements Drinkable public class Drinkable implements Pourable public class Pourable extends Drinkable public class Drinkable extends Pourable

public class Drinkable extends Pourable

How should a class called Philosopher be declared so that it is derived from the base class Person and implements the interface Reasonable? public class Philosopher extends Reasonable implements Person public class Philosopher extends Reasonable, Person public class Philosopher extends Person implements Reasonable public class Philosopher implements Reasonable, Person

public class Philosopher extends Person implements Reasonable

Given the following method header: public double computeArea(double length, double width) Which of the following would be a legal overloaded method in the same class? public double computeArea(double length) public double computeArea(double side1, double side2) public int computeArea(double length, double width) private double computeArea(double length, double width)

public double computeArea(double length)

Which of the following method headers must be implemented in any class that implements the Comparable interface? public int compareTo(Object other); public String toString(); public boolean equals(Object other); public boolean lessThan(Object other);

public int compareTo(Object other);

Which of the following is the correct method heading for a method called doSomething that throws an exception called SomethingWentWrongException? public void doSomething() public void doSomething(SomethingWentWrongException e) public void doSomething() throws Exception public void doSomething() throws SomethingWentWrongException

public void doSomething() throws SomethingWentWrongException

Given the following base case constructor definition: public QuizQuestion(int initialPoints) { points = initialPoints; } How would this constructor be called from a derived class constructor? base(initialPoints); QuizQuestion(initialPoints); super(initialPoints); this(initialPoints);

super(initialPoints);

If the statement data.saveToFile() throws a FileNotFoundException, which of the following is the correct way to handle this? try{ // some code data.saveToFile(); // more code } catch (Exception e) { // code to handle FileNotFoundException } try{ // some code data.saveToFile(); // more code } catch (FileNotFoundException e) { // code to handle FileNotFoundException }

try{ // some code data.saveToFile(); // more code } catch (FileNotFoundException e) { // code to handle FileNotFoundException }


Related study sets

Scientific Revolution and Enlightenment Key terms and Questions

View Set

Humanities and Social Sciences Key Concepts

View Set

Academic Team Full Practice Set 13

View Set

Differences between Plant Cells and Animal Cells-final

View Set