MIS 307 Final Review ISU

Ace your homework & exams now with Quizwiz!

What is the output of the given code snippet? int[] mynum = new int[5]; for (int i = 1; i < 5; i++) { mynum[i] = i + 1; System.out.print(mynum[i] + " "); } A. 2 3 4 5 B. 1 2 3 4 C. 1 3 4 5 D. 1 1 1 1

A. 2 3 4 5

If you increase the size of a dataset by two times, how much longer does it take to sort it with the merge sort algorithm? A. A little more than 2 times longer B. Approximately 4 times longer C. Approximately the same amount of time D. Approximately 100 times longer

A. A little more than 2 times longer

The HTTP command GET ____________. A. Asks the server to return the requested item. B. Sends an email message. C. Downloads an email message. D. Supplies form input to a server program.

A. Asks the server to return the requested item.

If a computer wants to request information from gateway.com, it must first ask a(n) ____ server to translate the domain name into a numeric Internet address. A. DNS B. Socket C. URL D. IP

A. DNS

What statement(s) are true about a recursive solution to a problem? I. Each recursive call must be able to simplify the problem somehow. II. Recursion must stop with a simple problem that is easily solved. III. Recursion can continue forever. A. I and II B. II and III C. I and III D. None of the statements are true of recursion.

A. I and II

An example of a fatal error that rarely occurs and is beyond your control as a programmer is the ____. A. OutOfMemoryError B. IndexOutOfBoundsException C. FileNotFoundException D. NumberFormatException

A. OutOfMemoryError

Consider the following code snippet: throw new IllegalArgumentException("This operation is not allowed!"); Which of the following statements about this code is correct? A. This code constructs an object of type IllegalArgumentException and throws the object. B. This code throws an existing IllegalArgumentException object. C. This code constructs an object of type IllegalArgumentException and reserves it for future use. D. This code will not compile.

A. This code constructs an object of type IllegalArgumentException and throws the object.

Consider the following code snippet. Scanner inputFile = new Scanner("dataIn.txt"); Which of the following statements is correct? A. This code will not open a file named "dataIn.txt", but will treat the string "dataIn.txt" as an input value. B. This code will create a new file named "dataIn.txt". C. This code will open an existing file named "dataIn.txt" for reading. D. This code will open a file named "dataIn.txt" for writing.

A. This code will not open a file named "dataIn.txt", but will treat the string "dataIn.txt" as an input value.

Consider the following code snippet. File inputFile = new File("dataIn.txt"); Scanner in = new Scanner(inputFile); while (in.hasNext()) { String input = in.next(); } Which of the following statements about this code is correct? A. This code will read in a word at a time from the input file. B. This code will read in the entire input file in one operation. C. This code will read in a line at a time from the input file. D. This code will read in a character at a time from the input file.

A. This code will read in a word at a time from the input file.

A(n) ____ is a string that identifies an information resource on the World Wide Web. A. URL B. Socket C. IP address D. Domain name

A. URL

Can the following array be searched using binary search? int[] A = {-17, -1, 0, 1, 2, 4, 5, 6}; A. Yes. Binary search can be applied to a sorted array. B. No. Binary search can be used only if the elements are in descending order. C. No, negative numbers are not allowed because they indicate that a value is not present.

A. Yes. Binary search can be applied to a sorted array.

What will be printed by the statements below? ArrayList<String> names = new ArrayList<String>(); names.add("Annie"); names.add("Bob"); names.add("Charles"); for (int i = 0; i < 3; i++) { String extra = names.get(i); names.add (extra); } System.out.print (names); A. [Annie, Bob, Charles, Annie, Bob, Charles] B. [Annie, Bob, Charles, Charles, Bob, Annie] C. [Annie, Annie, Bob, Bob, Charles, Charles] D. [Annie, Bob, Charles, Bob, Charles]

A. [Annie, Bob, Charles, Annie, Bob, Charles]

Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; Using Java 6 or later, which statement replaces the data array with a new array twice its size? A. data = Arrays.copyOf(data, 2 * data.length); B. data = Arrays.copyOf(data, 2); C. data = Arrays.copyOf(data, 2 * data); D. data = Arrays.copyOf(data, 2 * data.sizeOfArray());

A. data = Arrays.copyOf(data, 2 * data.length);

Complete the following code snippet with the correct enhanced for loop so it iterates over the array of Strings without using an index variable. String[] arr = { "abc", "def", "ghi", "jkl" }; ___________________ { System.out.print(str); } A. for (String str : arr) B. for (int str : String arr) C. for (str[] : arr) D. for (arr[] : str)

A. for (String str : arr)

Which of the following completes the selection sort method minimumPosition()? private static int minimumPosition(int[] a, int from) { int minPos = from; for (int i = from + 1; i < a.length; i++) { ________________ } return minPos; } A. if (a[i] > a[minPos]) { minPos = i; } B. if (a[i] < a[minPos]) { minPos = i; } C. if (a[i] < a[j]) { minPos = i; } D. if (a[i] < a[minPos]) { i = minPos; }

A. if (a[i] > a[minPos]) { minPos = i; }

Which one of the following statements is a valid initialization of an array named somearray of ten elements? A. int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; B. int somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; C. int[10] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; D. int somearray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

A. int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

The enhanced for loop A. is convenient for traversing all elements in an array B. can be used for traversing elements in a partially filled array C. is only used for arrays of integers D. is used to initialize all array elements to a common value

A. is convenient for traversing all elements in an array

If an element is present in an array of length n, how many comparisons, on average, are necessary to find it using a linear search? A. n / 2 B. n3 C. 2n D. n2

A. n / 2

Consider the recursive method myPrint: public void myPrint(int n) { if (n < 10) { System.out.print(n); } else { int m = n % 10; System.out.print(m); myPrint(n / 10); } } What is printed for the call myPrint(8)? A. 10 B. 8 C. 4 D. 21

B. 8

Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt and write to an output file named dataOut.txt. public static void main(String[] args) throws FileNotFoundException { String inputFileName = "dataIn.txt"; String outputFileName = "dataOut.txt"; File inputFile = _________________; Scanner in = new Scanner(inputFile) . . . } A. new File(inputFileName) B. new File(outputFileName) C. new File(inputFile) D.new File(System.in)

A. new File(inputFileName)

Insert the missing code in the following code fragment. This fragment is intended to read a file and write to a file. public static void main(String[] args) throws FileNotFoundException { String inputFileName = "dataIn.txt"; String outputFileName = "dataOut.txt"; File inputFile = new File(inputFileName); Scanner in = new Scanner(inputFile); PrintWriter out = _____________; . . . } A. new PrintWriter(outputFileName) B. new Scanner(outputFileName) C. new PrintWriter(outputFile) D. new Scanner(outputFile)

A. new PrintWriter(outputFileName)

What type of algorithm places elements in order? A. sorting B. searching C. insertion D. deletion

A. sorting

It may be necessary to "grow" an array when reading inputs because A. the number of inputs may not be known in advance. B. arrays in Java must be resized after every 100 elements. C. arrays are based on powers of two. D. the only alternative is a bounds exception.

A. the number of inputs may not be known in advance.

The HTTP response code 200 means _______________. A. the response is successful. B. the page is in the wrong language. C. the page was not found. D. the connection is lost.

A. the response is successful.

The following statement gets an element from position 4 in an array: x = a[4]; What is the equivalent operation using an ArrayList? A. x = a.get(4); B. x = a.get(); C. x = a.get[4]; D. x = a[4];

A. x = a.get(4);

Your program needs to store an integer sequence of unknown length. Disregarding performance impacts, which of the following is most suitable to use? A. An array declared as int[] marks; B. A array list declared as ArrayList<Integer> marks = new C. ArrayList<Integer>(); D. An array declared as int marks[0];

B. A array list declared as ArrayList<Integer> marks = new

Which of the following are considered members of a class? A. Private instance variables and methods. B. All instance variables and methods. C. Non-static instance variables and methods. D. Public instance variables and methods.

B. All instance variables and methods.

Which of the following strings is a palindrome? A. "Test" B. "Radar" C. "canal" D."salami"

B. "Radar"

Consider the following code snippet: ArrayList<Double> somedata = new ArrayList<Double>(); somedata.add(10.5); What does size() return for the ArrayList somedata after the given code snippet is executed? A. 0 B. 1 C. 10 D. 2

B. 1

Given an ordered array with 16 elements, how many elements must be visited (compared) in the worst case of binary search? A. 8 B. 4 C. 3 D. 2

B. 4

Given the following code snippet for searching an array: int[] arr = {3, 8, 12, 14, 17}; int newVal = 15; int pos = Arrays.binarySearch(arr, newVal); What value will pos have when this code is executed? A. 5 B. -5 C. 6 D. -6

B. 5

Suppose you use this code to get a web page from a server that does not exist, such as http://wombat.java/index.html. What will happen? URL u = new URL("http://wombat.java/index.html"); URLConnection connection = u.openConnection(); // Check if response code is HTTP_OK (200) HttpURLConnection httpConnection = (HttpURLConnection) connection; int code = httpConnection.getResponseCode(); String message = httpConnection.getResponseMessag(); System.out.println(code + " " + message); if (code != HttpURLConnection.HTTP_OK) return; // Read server response InputStream instream = connection.getInputStream(); Scanner in = new Scanner(instream); while (in.hasNextLine()) { String input = in.nextLine(); System.out.println(input); } A. The code will print "try again". B. An UnknownHostException will be thrown. C. The code will print the error code 404 - File Not Found D. The program will print a fake web page.

B. An UnknownHostException will be thrown.

If you increase the size of a dataset by two times, how much longer does it take to sort it with the selection sort algorithm? A. Approximately 2 times longer B. Approximately 4 times longer C. Approximately 10 times longer D. Approximately 100 times longer

B. Approximately 4 times longer

Which of the following statements about a PrintWriter object is true? A. A PrintWriter will be automatically closed when the program exits. B. Data loss may occur if a program fails to close a PrintWriter object before exiting. C. No data loss will occur if the program fails to close a PrintWriter before exiting. D. An exception will occur if the program fails to close a PrintWriter before exiting.

B. Data loss may occur if a program fails to close a PrintWriter object before exiting.

Which of the following is considered by the text to be the most important consideration when designing a class? A. Each class should represent an appropriate mathematical concept. B. Each class should represent a single concept or object from the problem domain. C. Each class should represent no more than three specific concepts. D. Each class should represent multiple concepts only if they are closely related.

B. Each class should represent a single concept or object from the problem domain.

Which statement(s) about recursion are true? I Recursion is always faster than iteration II Recursion is often easier to understand than iteration III Recursion is always significantly slower than iteration A. I B. II C. Both I and II D. Both I and III

B. II

The numbers 130.65.86.66 denote a(n) ____________________. A. TCP address B. IP address (version 4) C. URL address D. FTP address

B. IP address (version 4)

What is wrong in the given code snippet? int[] number = new int[3]; // Line 1 number[3] = 5; // Line 2 A. Line 1 cannot declare and initialize an array of three elements. B. Line 2 causes an ArrayIndexOutOfBoundsException error because 3 is an invalid index number. C. Line 2 successfully sets the third element of the array with value 5. D. Line 2 sets all the elements of the array with value 5.

B. Line 2 causes an ArrayIndexOutOfBoundsException error because 3 is an invalid index number.

Can the following array be searched using binary search? int[] A = {6, 5, 4, 2, 0, 1, -1, -17}; A. Yes. Binary search can be applied to any array. B. No. Binary search can be applied to a sorted array only. C. Yes, but the algorithm runs slower because the array is in descending order. D. No, negative numbers are not allowed because they indicate that a value is not present.

B. No. Binary search can be applied to a sorted array only.

After one iteration of selection sort working on an array of 10 elements, what must hold true? A. The array cannot be sorted. B. One element (the first) must be correctly placed. C. At least two elements are correctly placed. D. The largest element is correctly placed.

B. One element (the first) must be correctly placed.

What does this code show if called using mystery(new File("C:\\Users\\ghelmer\\Documents")); assuming the directory C:\Users\ghelmer\Documents exists and contains files and/or subdirectories? public static void mystery(File f) { File[] files = f.listFiles(); for (File file : files) { if (file.isDirectory()) { mystery(file); } else { System.out.println(file.getPath()); } } } A. Prints the name of each subdirectory in C:\Users\ghelmer\Documents, if any. B. Prints the name of each file, if any, in C:\Users\ghelmer\Documents and each subdirectory. C. Prints the name of each file in C:\Users\ghelmer\Documents but not in any subdirectories. D. Prints the number of files found in C:\Users\ghelmer\Documents and each subdirectory.

B. Prints the name of each file, if any, in C:\Users\ghelmer\Documents and each subdirectory.

Write a code fragment that connects to a server running on gorge.divms.uiowa.edu at port 2345 and reads one line of text sent by the server. A. Socket sock = new Socket(gorge.divms.uiowa.edu, 2345); Scanner scan = new Scanner(sock.getOutputStream()); String line = scan.nextLine(); B. Socket sock = new Socket(gorge.divms.uiowa.edu, 2345); Scanner scan = new Scanner(sock.getInputStream()); String line = scan.nextLine(); C. Scanner scan = new Scanner(sock.getInputStream()); Socket sock = new Socket(gorge.divms.uiowa.edu, 2345); String line = scan.nextLine();

B. Socket sock = new Socket(gorge.divms.uiowa.edu, 2345); Scanner scan = new Scanner(sock.getInputStream()); String line = scan.nextLine();

Suppose you use the URLGet program (which uses the URL, URLConnection, and HttpURLConnection classes) in Section 22.5 to try to get a web page that does not exist on a server (which does exist). Assuming that the server is otherwise working properly, what will happen? A. The call to openConnection will throw an exception. B. The call to getResponseCode will return the error code 404. C. The call to getResponseMessage will return null. D. The call to getInputStream will throw an exception.

B. The call to getResponseCode will return the error code 404.

When a program throws an exception within a method that has no try-catch block, which of the following statements about exception handling is true? A. Execution will continue with the next statement in the method. B. The current method terminates immediately. C.The current method must decide whether to continue or terminate. D. The user must decide whether to continue or terminate the program.

B. The current method terminates immediately.

Consider the following code snippet: try { PrintWriter outputFile = new PrintWriter(filename); try { writeData(outputFile); } finally { outputFile.close(); } } catch (IOException exception) { . . . } Which of the following statements about this code is correct? A. The file will be closed regardless of when the exception occurs. B. The file will be closed only if the writeData() statement does not throw an exception. C. The file will be closed even if the writeData() statement throws an exception. D. It is not possible to determine whether the file will be closed if an exception occurs.

B. The file will be closed only if the writeData() statement does not throw an exception.

Why did the recursive Fibonacci program get so slow when computing large Fibonacci numbers? A. Java can't compute large numbers very efficiently B. The recursive Fibonacci program repeats a lot of work C. Recursion is always extremely slow compared to iteration

B. The recursive Fibonacci program repeats a lot of work

Which of the following most likely indicates that you have chosen a good name for your class? A. The name consists of a single word. B. You can tell by the name what an object of the class is supposed to represent. C. You can easily determine by the name how many concepts the class represents. D. The name describes what task the class will perform.

B. You can tell by the name what an object of the class is supposed to represent.

Insert the missing code in the following code fragment. This fragment is intended to read all words from a text file, and finish the while loop when there are no more words to read. File inputFile = new File("dataIn.txt"); Scanner in = new Scanner(dataIn.txt); while (____________) { S tring input = in.next(); S ystem.out.println(input); } A. in.peek() B. in.hasNext() C. in.nextWord() D. in.getNext()

B. in.hasNext()

Identify the correct statement for defining and allocating an integer array named numarray that will hold ten elements. A. int[] numarray = new int[9]; B. int[] numarray = new int[10]; C. int[10] numarray; D. int numarray[10];

B. int[] numarray = new int[10];

When an array myArray is only partially filled, how can the programmer keep track of the current number of elements that are in use? A. access myArray.length B. maintain a companion variable, such as currentSize, that stores the current number of elements in use C. access myArray.currentElements() D. access myArray.length - 1

B. maintain a companion variable, such as currentSize, that stores the current number of elements in use

General Java variable naming conventions would suggest that a variable named NICKEL_VALUE would most probably be declared using which of the following combinations of modifiers? A. public void final B. public static final double C. private static double D.private static

B. public static final double

Insert the missing code in the following code fragment. This fragment is intended to read an input file and terminate the method if a FileNotFoundException occurs. public static void main(String[] args) __________________ { String inputFileName = "dataIn.txt"; File inputFile = new File(inputFileName); Scanner in = new Scanner(inputFile); . . . } A. extends FileNotFoundException B. throws FileNotFoundException C. inherits FileNotFoundException D. catches FileNotFoundException

B. throws FileNotFoundException

To retrieve the response code from a URLConnection object, you need __________________. A. to cast the HttpURLConnection object to the URLConnect subclass. B. to cast the URLConnection object to the HttpURLConnect subclass and then we can use the GetResponseCode() method. C. to use the getInputStream() method. D. to use the getCode() method.

B. to cast the URLConnection object to the HttpURLConnect subclass and then we can use the GetResponseCode() method.

Given the following class definition, which of the following is considered part of the class's public interface? public class CashRegister { private static int objectCounter; public void updateDimes(int dimes) {...} private boolean updateCounter(int counter) {...} } A. objectCounter B. updateDimes C. updateCounter D. None of these

B. updateDimes

Why can't Java methods change parameters of primitive type, as in falseSwap(double x, double y) in the homework assignment? A. Java methods can have no actual impact on parameters of any type. B.Parameters of primitive type are passed by value, not reference, so they are essentially local variables for a Java method. C. Parameters of primitive type are immutable. D. Java methods cannot accept parameters of primitive type.

B.Parameters of primitive type are passed by value, not reference, so they are essentially local variables for a Java method.

For this array: Index Value 0 5 1 10 2 15 3 20 4 25 5 30 6 35 7 40 8 45 What would be the first element (value) checked when searching for 15 using binary search? A. 5 B. 15 C. 25 D. 35

C. 25

What is the output of the following code snippet? int[] myarray = { 10, 20, 30, 40, 50 }; System.out.print(myarray[2] + " "); System.out.print(myarray[3]); A. 10 50 B. 20 30 C. 30 40 D. 40 50

C. 30 40

Which of the following describes an immutable class? A. A class that has no accessor or mutator methods. B. A class that has no accessor methods. C. A class that has no mutator methods. D. A class that has both accessor and mutator methods.

C. A class that has no mutator methods.

Under which of the following conditions would the public interface of a class be considered cohesive? A. All of its features are public and none of its features are static. B. The quality of the public interface is rated as moderate to high. C. All of the methods are related to the central concept that the class represents. D. It is obvious that the public interface refers to multiple concepts.

C. All of the methods are related to the central concept that the class represents.

The ____________ protocol translates domain names into Internet addresses. A. Directory Stage Network (DSN) B. Destination Network Internet Protocol (DNIP) C. Domain Name Service (DNS) D. Transmission Control Protocol (TCP)

C. Domain Name Service (DNS)

Why is it generally considered good practice to minimize coupling between classes? A. Low coupling increases the operational efficiency of a program. B. High coupling implies less interface cohesion. C. High coupling increases program maintenance, hinders code reuse and makes unit testing more difficult. D. Low coupling decreases the probability of code redundancy.

C. High coupling increases program maintenance, hinders code reuse and makes unit testing more difficult.

Consider the following code snippet: PrintWriter out = new PrintWriter("output.txt"); Which of the following statements about the PrintWriter object is correct? A. If a file named "output.txt" already exists, an exception will occur. B. If a file named "output.txt" already exists, data will be added at the end of the file. C. If a file named "output.txt" already exists, existing data will be deleted before data are added to the file. D. If a file named "output.txt" already exists, a new file named "output_1.txt" will be created and used.

C. If a file named "output.txt" already exists, existing data will be deleted before data are added to the file.

Which solution that finds the area of a triangle is the most efficient? (Note that each of these methods is valid for the right triangle considered in chapter 13) A. Recursive: sum the area of a triangle 1 unit smaller and the area of the bottom row of the triangle) B. Iterative: sum the area added to the triangle by each row of the triangle C. Mathematical: area = width * (width + 1) / 2

C. Mathematical: area = width * (width + 1) / 2

Binary search is an ____ algorithm. Selected Answer: A. O(n) B. O(n2) C. O(log n) D.O(n log n)

C. O(log n)

Which of the following application protocols is used to retrieve email messages from a mail server? A. DNS (Domain Name Service) B. IP (Internet Protocol) C. POP (Post Office Protocol) D. TCP (Transmission Control Protocol)

C. POP (Post Office Protocol)

To connect to the HTTP port (80) of the server yahoo.com, you use: A. Socket s = new Socket(80, yahoo.com); B. Socket s = connect("yahoo.com", 80); C. Socket s = new Socket("yahoo.com", 80); D. java yahoo.com 80

C. Socket s = new Socket("yahoo.com", 80);

If recursion does not have a special terminating case and instead continues infinitely, what error will occur? A. ArrayIndexOutOfBoundsException B. IllegalArgumentException C. StackOverflowError D. OutOfMemoryError

C. StackOverflowError

Which of the following classes has a static variable that is commonly used when printing output using println()? A. Scanner. B.String. C. System. D. Object.

C. System.

The ____ program is a useful tool for establishing test TCP connections with servers. A. DNS B. HTML C. Telnet (or Putty) D. TCP

C. Telnet (or Putty)

What is the result of executing this code snippet? int[] marks = { 90, 45, 67 }; for (int i = 0; i <= 3; i++) { System.out.println(marks[i]); } A. The code snippet does not give any output. B. The code snippet displays all the elements in the marks array: 90 45 67 and finishes normally. C. The code snippet prints: 90 45 67 and then dies with ArrayIndexOutOfBoundsException D. The code snippet executes an infinite loop.

C. The code snippet prints: 90 45 67 and then dies with ArrayIndexOutOfBoundsException

Which of the following statements about checked and unchecked exceptions is true? A. Checked exceptions are handled by the Java runtime. B. The compiler requires that the program is handling unchecked exceptions. C. The compiler requires that the program handles checked exceptions by either using "throws" declarations or by catching exceptions. D. All exceptions that are descendants of RunTimeException are checked exceptions.

C. The compiler requires that the program handles checked exceptions by either using "throws" declarations or by catching exceptions.

Consider the following code snippet: public static void main(String[] args) throws FileNotFoundException Which of the following statements about this code is correct? A. The main method is designed to catch and handle all types of exceptions. B. The main method is designed to catch and handle the FileNotFoundException. C. The main method should simply terminate if the FileNotFoundException occurs. D. The main method will not terminate if any exception occurs.

C. The main method should simply terminate if the FileNotFoundException occurs.

Two kinds of data are transmitted between programs: ______________ refers to the data that one program actually wants to send to another. Network protocol data are the data that describe how to reach the intended recipient and how to check for errors and data loss in the transmission. A. packets B. transmission data C. application data D. socket data

C. application data

Which code snippet prints out the elements in a partially filled array of integers where int currLength is the number of elements used in the array? A. for (int i = 0; i < myArray.lengthOfArray(); i++) { System.out.print(myArray[i]); } B. for (int i = 0; i < myArray.currLength(); i++) { System.out.print(myArray[i]); } C. for (int i = 0; i < currLength; i++) { System.out.print(myArray[i]); } D. for (int i = 0; i < myArray.length(); i++) { System.out.print(myArray[currLength]); }

C. for (int i = 0; i < currLength; i++) { System.out.print(myArray[i]); }

Insert the missing code in the following code fragment. This fragment is intended to read floating-point numbers from a text file. Scanner in = new Scanner(. . .); while (____________) { double hoursWorked = in.nextDouble(); System.out.println(hoursWorked); } A. in.getNextDouble() B. in.peek() C. in.hasNextDouble() D. in.readNextWord()

C. in.hasNextDouble()

Your program wishes to use a file named C:\java\myProg\input.txt on a Windows system. Which of the following is the correct code to do this? A. inputFile = new File("c:\java\myProg\input.txt"); B. inputFile = new File.open("c:\java\myProg\input.txt"); C. inputFile = new File("c:\\java\\myProg\\input.txt"); D. inputFile = new File.open("c:\\java\\myProg\\input.txt");

C. inputFile = new File("c:\\java\\myProg\\input.txt");

The following code is an example of a ___ search. public static int search(int[] a, int v) { for (int i = 0; i < a.length; i++) { if (a[i] == v) { return i; } } return -1; } A. sorted B. binary C. linear D.random

C. linear

Consider the getArea method from the textbook shown below. public int getArea() { if (width <= 0) { return 0; } // line #1 else if (width == 1) { return 1; } // line #2 else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } } Where is/are the terminating condition(s)? A. line #1 B. line #2 C. lines #1 and #2 D. line #4

C. lines #1 and #2

Which sort algorithm starts by cutting the array in half and then recursively sorts each half? A. insertion sort B. selection sort C. merge sort D. junk sort

C. merge sort

Insert the missing code in the following code fragment. This fragment is intended to read an input file. public static void main(String[] args) throws FileNotFoundException { String inputFileName = "dataIn.txt"; String outputFileName = "dataOut.txt"; File inputFile = new File(inputFileName); Scanner in = _______________; . . . } A. new Scanner(inputFileName) B. new Scanner(outputFileName) C. new Scanner(inputFile) D.new Scanner(System.in)

C. new Scanner(inputFile)

Suppose you wish to write a method that returns the sum of the elements in the partially filled array myArray. Which is the best and appropriate method header to pass the required information about the partially filled array into the method? A. public static int sum(int[] values) B. public static int sum() C. public static int sum(int[] values, int currSize) D. public static int sum(int[] values, int size, int currSize)

C. public static int sum(int[] values, int currSize)

Complete the code for the recursive method printSum shown in this code snippet, which is intended to return the sum of numbers from 1 to n: Complete the code for the recursive method printSum shown in this code snippet, which is intended to return the sum of numbers from 1 to n: public static int printSum(int n) { i f (n == 0) { r eturn 0; } e lse { _ _____________________________ } } A. return (printSum(n - 1)); B. return (n + printSum(n + 1)); C. return (n + printSum(n - 1)); D. return (n - printSum(n - 1));

C. return (n + printSum(n - 1));

The HTTP response code 404 means _______________. A. the response is successful. B. the page is found successfully. C. the page was not found. D. the connection is lost.

C. the page was not found.

Which sort algorithm is implemented by this method? public static void sort(int[] a) { if (a.length <= 1) { return; } int[] first = new int[a.length / 2]; int[] second = new int[a.length - first.length]; // Copy the first half of a into first, the second half into second . . . sort(first); sort(second); merge(first, second, a); } A. Selection Sort B. Insertion Sort C. Quick Sort D. Merge Sort

D. Merge Sort

Which of the following is true regarding side effects of methods? A. Modification of implicit parameters should be restricted to primitive data types. B. Modification of explicit parameters should never be allowed. C. Side effects involving standard output should be limited to String data. D. Minimize side effects that go beyond modification of implicit parameters.

D. Minimize side effects that go beyond modification of implicit parameters.

Which type of method modifies the object on which it is invoked? A. Constructor method. B. Static method. C. Accessor method. D. Mutator method.

D. Mutator method.

Which of the following statements about palindromes is correct? A. The empty string is not a palindrome. B. The string "I" is not a palindrome. C. The string "rascal" is a palindrome. D. All strings of length 0 or 1 are palindromes.

D. All strings of length 0 or 1 are palindromes.

When reading words with a Scanner object using the next() method, by default a word is defined as ____. A. Any sequence of characters consisting of letters only. B. Any sequence of characters consisting of letters, numbers, and punctuation symbols only. C. Any sequence of characters consisting of letters and numbers only. D. Any sequence of characters that is not white space.

D. Any sequence of characters that is not white space.

Judging by the name of the method, which of the following methods would most likely be a mutator method? A. getListOfDeposits. B. printAccountBalance. C. isOverdrawn. D. Deposit.

D. Deposit.

Which of the following describes the first thing you should do when beginning a new object-oriented programming activity? A. Consider how many concepts each class should represent. B. Determine which categories of classes may be needed for the project. C. Decide what combination of functions and methods are appropriate for the project. D. Identify the objects and the classes, based on the nouns in the problem statement, to which they belong.

D. Identify the objects and the classes, based on the nouns in the problem statement, to which they belong.

Why does the Scanner class belong to the category of classes known as actors? A. It acts as an interface between your program and the command line. B. It serves as an abstract entity that represents a user of your program. C. It plays an important part in streamlining the operation of your system. D. It performs a task, such as scanning a stream for numbers and characters.

D. It performs a task, such as scanning a stream for numbers and characters.

Consider the following code snippet: PrintWriter outputFile = new PrintWriter(filename); writeData(outputFile); outputFile.close(); How can the program ensure that the file will be closed if an exception occurs on the writeData call? A. The program does not need to take any action, because the output file will be automatically closed when the exception occurs. B. The program should place the outputFile.close() statement within a try block to ensure that the file will be closed. C. It is not possible to ensure that the file will be closed when the exception occurs. D. The program should place the outputFile.close() statement within a finally clause of a try block to ensure that the file is closed.

D. The program should place the outputFile.close() statement within a finally clause of a try block to ensure that the file is closed.

Consider the following code snippet: Scanner in = new Scanner(. . .); // Process the expected input. . . . // Should be finished with input. if (in.hasNext()) { throw new IOException("End of file expected"); } Which of the following statements about this code is correct? A. The program will display the message "End of file expected" if there is no data. B. The program will throw an exception if there is no data. C. The program will display the message "End of file expected" if there is data left in the input when the if statement is executed. D. The program will throw an exception if there is data left in the input when the if statement is executed.

D. The program will throw an exception if there is data left in the input when the if statement is executed.

What is the valid range of index values for an array of size 10? A. [0] to [10] B. [1] to [9] C. [1] to [10] D. [0] to [9]

D. [0] to [9]

A binary search is generally ____ a linear search. A. slower than B. equal to C. less efficient than D. faster than

D. faster than

Used on Socket's output stream, the PrintWriter's ______ method empties the buffer and forwards all waiting characters to the destination. A. print B. send C. scan D. flush

D. flush

Insert the missing code in the following code fragment. This code is intended to open a file and properly close the file even if an exception occurs while processing the file. public void String readFile() throws IOException { File inputFile = new File(. . .); Scanner in = new Scanner(inputFile); try { while (in.hasNext()) { . . . } } finally { ___________________ } } A. catch (IOException exception) B. catch (FileNotFound exception) C. catch (IllegalArgumentException exception) D. in.close();

D. in.close();

Which code snippet calculates the sum of all the elements in even positions in an array? A. int sum = 0; for (int i = 1; i < values.length; i = i + 2) { sum++; } B. int sum = 0; for (int i = 0; i < values.length; i++) { sum++; } C. int sum = 0; for (int i = 0; i < values.length; i++) { sum += values[i]; } D. int sum = 0; for (int i = 0; i < values.length; i = i + 2) { sum += values[i]; }

D. int sum = 0; for (int i = 0; i < values.length; i = i + 2) { sum += values[i]; }

Consider the getArea method from the textbook shown below. public int getArea() { if (width <= 0) { return 0; } // line #1 else if (width == 1) { return 1; } // line #2 else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } } Where is/are the recursive call(s)? A. line #1 B. line #2 C. lines #1 and #2 D. line #4

D. line #4

The _________ class makes it easy to fetch a file from a web server with just a URL. A. FileConnection B. ServerConnection C. ClientConnection D.URLConnection

D.URLConnection


Related study sets

Wk 5 - Practice: Fiscal Policy [due Day 5]

View Set

Discrimination II - Equal Pay and the Sex Equality Clause

View Set

Mastering Biology Chp 4 and 5 quiz

View Set

Basics of Material Science and Engineering #1

View Set

Business Law Chapters 10-12,19, & 43

View Set

Peds neuro, Ch. 48 Musculoskeletal or Articular Dysfunction, Chapter 49: Neuromuscular or Muscular Dysfunction, Chapter 45: Cerebral Dysfunction

View Set

Chapter 9, Development psychology

View Set

Review - Solving 2x2 Systems of Linear Equations

View Set