COSC 2436 - Exam 1

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

The __________ package is automatically imported into all Java programs. A) java.java B) java.default C) java.util D) java.lang

D) java.lang

The values variable references a two-dimensional double array with 10 rows and 20 columns. Write code (only the code block) that sums all the elements in the array and stores the sum in the variable total.

double total = 0.0; // Accumulator // Sum the values in the array. for (int row = 0; row < 10; row++) { for (int col = 0; col < 20; col++) total += values[row][col]; }

Write the first line of the definition for a stereo class. The class should extend the SoundSyatcm class, and it should implement the CDplayable, TunerPlayable, and CassettePlayable interfaces.

public class Stereo extends SoundSyatcm implements CDplayable, TunerPlayable, CassettePlayable

Write an interface named Nameable that specifies the following methods: public void setName(String n) public String getName()

public interface Nameable { void setName(String n); String getName(); }

Which of the following is true about protected access? A) Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package. B) Protected members may be accessed by methods in the same package or in a subclass, but only if the subclass is in the same package. C) Protected members cannot be accessed by methods in any other classes. D) Protected members are actually named constants.

A) Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package.

For the following code, what would be the value of str[2]? String[] str = {"abc", "def", "ghi", "jkl"}; A) a reference to the String object containing "ghi" B) "ghi" C) a reference to the String object containing "def" D) "def"

A) a reference to the String object containing "ghi"

A(n) __________ method is a method that appears in a superclass but expects to be overridden in a subclass. A) abstract B) protected C) static D) overloaded

A) abstract

When recursive methods directly call themselves, it is known as __________. A) direct recursion B) indirect recursion C) basic recursion D) static recursion

A) direct recursion

When an object is created, the attributes associated with the object are called __________. A) instance fields B) class instances C) instance methods D) fixed attributes

A) instance fields

You cannot use the == operator to compare the contents of __________. A) objects B) strings C) integers D) Boolean values

A) objects

In order to do a binary search on an array __________. A) the array must first be sorted B) you must first do a sequential search to be sure the element you are looking for is there C) the values of the array must be numeric D) no requirements are necessary

A) the array must first be sorted

If object1 and object2 are objects of the same class, to make object2 a copy of object1 __________. A) write a method for the class that will make a field by field copy of object1 data members into object2 data members B) use the copy method that is a part of the Java language C) use the default constructor to create object2 with object1 data members D) use an assignment statement to make object2 a copy of object1

A) write a method for the class that will make a field by field copy of object1 data members into object2 data members

What will be displayed after the following code is executed? StringBuilder strb = new StringBuilder(12); strb.append("The cow "); strb.append("jumped over the "); strb.append("moon."); System.out.println(strb); A) The cow jumped over the moon. B) The cow jumped over the moon. C) The cow jump D) 12The cow jumped over the moon.

B) The cow jumped over the moon.

The try statement may have an optional __________ clause which must appear after all of the catch clauses. A) try-again B) finally C) default D) abort

B) finally

Methods that operate on an object's fields are called __________. A) instance variables B) instance methods C) public methods D) private methods

B) instance methods

All methods specified by an interface are __________. A) private B) public C) protected D) static

B) public

In an inheritance relationship __________. A) the subclass constructor always executes before the superclass constructor B) the superclass constructor always executes before the subclass constructor C) the constructor with the lowest overhead always executes first regardless of inheritance in subclasses D) the unified constructor always executes first regardless of inheritance

B) the superclass constructor always executes before the subclass constructor

Two or more methods in a class may have the same name as long as __________. A) they have different return types B) they have different parameter lists C) they have different return types but the same parameter list D) You cannot have two methods with the same name.

B) they have different parameter lists

The binary search algorithm __________. A) is less efficient than the sequential search algorithm B) will cut the portion of the array being searched in half each time it fails to locate the search value C) will have a maximum number of comparisons equal to the number of elements in the array D) will, normally, have the number of comparisons that is half the number of elements in the array

B) will cut the portion of the array being searched in half each time it fails to locate the search value

What will be displayed after the following code is executed? String str = "abc456"; for (int i = 0; i < str.length(); i++) { char chr = str.CharAt(i); if (!Character.isLetter(chr)) System.out.print (Character.toUpperCase(chr)); } A) ABC B) ABC456 C) 456 D) abc456

C) 456

Given the following code that uses recursion to find the factorial of a number, how many times will the else clause be executed if n = 5? private static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } A) 3 B) 4 C) 5 D) 6

C) 5

Which of the following is an example of a lambda expression? A) int x = x * factor; B) IntCalculator = new divider(x, 2); C) IntCalculator multiplier = x -> x * factor; D) Any of these are examples of a lambda expression.

C) IntCalculator multiplier = x -> x * factor;

In a catch statement, what does the following code do? System.out.println(e.getMessage()); A) It prints the code that caused the exception. B) It prints the stack trace. C) It prints the error message for an exception. D) It overrides the toString method.

C) It prints the error message for an exception.

Given the following two-dimensional array declaration, which statement is true? int[][] numbers = new int[6][9]; A) The numbers array has 54 rows. B) The numbers array has 15 rows. C) The numbers array has 6 rows and 9 columns. D) The numbers array has 6 columns and 9 rows.

C) The numbers array has 6 rows and 9 columns.

Which of the following is not true about static methods? A) It is not necessary for an instance of the class to be created to execute a static method. B) They are called by placing the key word static after the access specifier in the method header. C) They are called from an instance of the class. D) They are often used to create utility classes that perform operations on data but have no need to collect and store data.

C) They are called from an instance of the class.

The __________ is at least one case in which a problem can be solved without recursion. A) recursive case B) termination point C) base case D) point of absolution

C) base case

Each array in Java has a public field named __________ that contains the number of elements in the array.. A) size B) capacity C) length D) limit

C) length

When a field is declared static there will be __________. A) a copy of the field for each method in the class B) a copy of the field in each class object C) only one copy of the field in memory D) two reference copies of the field for each method in the class

C) only one copy of the field in memory

When an exception is thrown by code in its try block, the JVM begins searching the try statement for a catch clause that can handle it and passes control of the program to __________. A) the last catch clause that can handle the exception B) each catch clause that can handle the exception C) the first catch clause that can handle the exception D) the statement that appears immediately after the catch block

C) the first catch clause that can handle the exception

What will be the value of matches after the following code is executed? boolean matches; String[] productCodes = {"456HI345", "3456hj"}; matches = productCodes[0].regionMatches(true, 1, productCodes[1], 2, 3); A) 56H B) 56h C) true D) false

C) true

To compare two objects in a class, __________. A) use the == operator (for example, object1 == object2) B) write a method to do a byte-by-byte compare of the two objects C) write an equals method that will make a field by field compare of the two objects D) This cannot be done since objects consist of several fields.

C) write an equals method that will make a field by field compare of the two objects

In a multi-catch (introduced in Java 7) the exception types are separated in the catch clause by the __________ symbol. A) * B) ? C) | D) &

C) |

The StringBuilder class's insert method allows you to insert a(n) __________ into the calling object's string. A) char array B) primitive type C) String object D) All of these

D) All of these

Given the following code, what will be the value of finalAmount when it is displayed? public class Order { private int orderNum; private double orderAmount; private double orderDiscount; public Order(int orderNumber, double orderAmt, double orderDisc) { orderNum = orderNumber; orderAmount = orderAmt; orderDiscount = orderDisc; } public int getOrderAmount() { return orderAmount; } public int getOrderDisc() { return orderDisc; } } public class CustomerOrder { public static void main(String[] args) { int ordNum = 1234; double ordAmount = 580.00; double discountPer = .1; Order order; double finalAmount = order.getOrderAmount() — order.getOrderAmount() * order.getOrderDisc(); System.out.printf("Final order amount = $%,.2f\n", finalAmount); } } A) 528.00 B) 580.00 C) There is no value because the constructor has an error. D) There is no value because the object, order, has not been created.

D) There is no value because the object, order, has not been created.

If a method does not handle a possible checked exception, what must the method have? A) a try clause in its header B) a catch clause in its header C) a finally clause in its header D) a throws clause in its header

D) a throws clause in its header

In a recursive program, the number of times a method calls itself is known as the __________. A) call count B) cyclic identity C) method heap D) depth of recursion

D) depth of recursion

_________ occurs when method A calls method B which in turn calls method A. A) dynamic recursion B) linear recursion C) direct recursion D) indirect recursion

D) indirect recursion

What would be the results of executing the following code? StringBuilder str = new StringBuilder("Little Jack Horner "); str.append("sat on the "); str.append("corner"); A) The program would crash. B) str would reference "Little Jack Horner ". C) str would reference "Little Jac Horner sat on the ". D) str would reference "Little Jack Horner sat on the corner".

D) str would reference "Little Jack Horner sat on the corner".

What is the difference between overriding a superclass method and overloading a superclass method?

Overloading is when a method has the same name as one or more other methods, but a different parameter list. Although overloaded methods have the same name, they have different signatures. When a method overrides another method, however, they both have the same signature.

Following method that accepts a String as an argument. The method use recursion to display each individual character in the String. public static void main(String [] args) { String str = "test string"; display(str, 0); } public static void display(String str, int pos) { if (pos < str.length()) { System.out.print(str.charAt(pos)); display(str, pos + 1); } } Modify the method so that it displays the String backwards.

public static void display(String str, int pos) { if (pos < str.length()) { display(str, pos + 1); System.out.print(str.charAt(pos)); } }

Convert the following iterative method to one that uses recursion: public static void sign(int n) { while (n > 0) { System.out.println("No Parking"); n--; } }

public static void sign(int n) { if (n > 0) { System.out.println("No Parking"); sign(n - 1); } }


Kaugnay na mga set ng pag-aaral

Geo - chapter 14 (Internal Processes)

View Set

RCD330: DAW Editing and Processing Final Exam Study Guide

View Set

alpha, beta and power busmgt2320

View Set

Psych Ch.9 Thinking, Language, and Intelligence

View Set

Database Management Systems (CSE 4503) Chapter 1

View Set