CS-145 Final Exam Study (Quiz 1-8)

¡Supera tus tareas y exámenes ahora con Quizwiz!

A polymorphic reference can refer to different types of objects over time. A) True B) False

A) True

Accessors and mutators provide mechanisms for controlled access to a well-encapsulated class. A) True B) False

A) True

Assume that the class Bird has a static method fly( ). If b is a Bird, then to invoke fly, you could do Bird.fly( );. A) True B) False

A) True

These two ways of setting up a String yield identical results:a) String string = new String("123.45");b) String string = "" + 123.45; A) True B) False

A) True

While the Exception class is part of java.lang, IOException is part of java.io. A) True B) False

A) True

Which of the following is a legal Java identifier? A) i B) class C) ilikeclass! D) idon'tlikeclass E) i-like-class

A) i

In addition to their usage providing a mechanism to convert (to box) primitive data into objects, what else do the wrapper classes provide? A) static constants B) arrays to contain the data C) enumerations D) exceptions E) none of the above

A) static constants

The do loop differs from the while loop in that A) the do loop will always execute the body of the loop at least once B) the while loop will always execute the body of the loop at least once C) the while loop will continue to loop while condition in the while statement is false and the do loop will continue to loop while the condition in the while statement is true D) the do loop will continue to loop while condition in the while statement is false and the while loop will continue to loop while the condition in the while statement is true E) none of the above, there is absolutely no difference between the two types of loops

A) the do loop will always execute the body of the loop at least once

Given the following code fragment String strA = "aBcDeFg"; String strB = strA.toLowerCase( ); strB = strB.toUpperCase( ); String strC = strA.toUpperCase( ); A) strA.equals(strC) would be true B) strB.compareTo(strC) would yield 0 C) strA.compareTo(strC) would yield 0 D) strB.equals(strC) would be true E) none of the above

B) strB.compareTo(strC) would yield 0

All Java classes must contain a main method which is the first method executed when the Java class is called upon. A) True B) False

B) False

Arrays have a built in toString method that returns all of the elements in the array as one String with "\n" inserted between each element. A) True B) False

B) False

Consider the following class hierarchy and answer these questions. A is a derived class of X. A) True B) False

B) False

Every class definition must include a constructor. A) True B) False

B) False

Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header. A) True B) False

B) False

In Java, it is possible to create an infinite loop out of while and do loops, but not for-loops. A) True B) False

B) False

In order to compare int, float and double variables, you can use <, >, ==, !=, <=, >=, but to compare char and String variables, you must use compareTo( ), equals( ) and equalsIgnoreCase( ). A) True B) False

B) False

It is not possible to test out any single method or class of a system until the entire system has been developed, and so all testing is postponed until after the implementation phase. A) True B) False

B) False

Java methods can return more than one item if they are modified with the reserved word continue, as in public continue int foo( ) { ... } A) True B) False

B) False

Only difficult programming problems require a pseudocode solution before the programmer creates the implementation (program) itself. A) True B) False

B) False

Reserved words in Java can be redefined by the programmer to mean something other than their original intentions. A) True B) False

B) False

System.out.print is used in a program to denote that a documentation comment follows. A) True B) False

B) False

The type of the reference, not the type of the object, is use to determine which version of a method is invoked in a polymorphic reference. A) True B) False

B) False

These two ways of setting up a String yield identical results:a) String string = "12345"b) String string = 12345; A) True B) False

B) False

To swap the 3rd and 4th elements in the int array values, you would do: values[3] = values[4]; values[4] = values[3]; A) True B) False

B) False

You may use the super reserved word to access a parent class'private members. A) True B) False

B) False

For the following questions, refer to the class defined below: import java.util.Scanner; public class Questions33_34 { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan=Scanner.create(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } Questions33_34 computes A) he sum of x, y and z B) The average of x, y, and z as a double, but the result may not be accurate C) the remainder of the sum of x, y and z divided by 3 D) The correct average of x, y and z as an int E) The correct average of x, y and z as a double

B) The average of x, y, and z as a double, but the result may not be accurate

Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on a student's test score. if (score >= 90) grade = 'A'; if (score >= 80) grade = 'B' ;if (score >= 70) grade = 'C'; if (score >= 60) grade = 'D'; else grade = 'F'; A) This code will work correctly only if grade >= 60 B) This code will work correctly only if grade < 70 C) This code will work correctly in all cases D) This code will work correctly only if grade < 60 E) This code will not work correctly under any circumstances

B) This code will work correctly only if grade < 70

Can a program exhibit polymorphism if it only implements early binding? A) No, because without late binding polymorphism cannot be supported B) Yes, because one form of polymorphism is overloading C) Yes, because early binding has nothing to do with polymorphism D) Yes, because so long as the programs uses inheritance and/or interfaces it supports polymorphism E) none of the above

B) Yes, because one form of polymorphism is overloading

Abstract methods are used when defining A) lasses that have no constructor B) derived classes C) arrays D) interface classes E) classes that have no methods

B) derived classes

Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0? A) if (x > 0) x++; else x--; B) if (x > 0) x++; else if (x < 0) x--; C)if (x > 0) x++; f (x < 0) x--; else x = 0; D) if (x == 0) x = 0; else x++; x--; E) x++; x--;

B) if (x > 0) x++; else if (x < 0) x--;

Consider the following two lines of code. What can you say about s1 and s2? String s1 = "testing" + "123"; String s2 = new String("testing 123"); A) s1 and s2 will compare "equal" B) s1 and s2 are both references to different String objects C) s1 and s2 are both references to the same String object D) the line declaring s2 is legal Java; the line declaring s1 will produce a syntax error E) none of the above

B) s1 and s2 are both references to different String objects

The String class' compareTo method A) compares two string in a case-independent manner B) yields 0 if the two strings are identical C) returns 1 if the first string comes lexically before the second string D) yields true or false E) none of the above

B) yields 0 if the two strings are identical

For the questions below, assume values is an int array that is currently filled to capacity, with the following values: | 9 | 4 | 12 | 2 | 6 | 8 | 18 | What is returned by values[3]? A) 12 B) 6 C) 3 D) 2 E) 9

D) 2

What is printed by the following code? public class Inherit { abstract class Speaker { abstract public void speak( ); } class Cat extends Speaker { public void speak( ) { System.out.println("Woof!"); } } class Dog extends Speaker { public void speak( ) { System.out.println("Meow!"); } } Inherit( ) { Speaker d = new Dog( ); Speaker c = new Cat( ); d.speak( ); c.speak( ); } public static void main(String[ ] args) { new Inherit( ); } } A) Woof! Meow! B) Meow! Meow! C) Meow! Woof! D) Woof! Woof! E) none of the above

C) Meow! Woof!

What is printed? public class Inherit { abstract class Figure { void display( ) { System.out.println("Figure"); } } abstract class Rectangle extends Figure { } class Box extends Rectangle { void display( ){ System.out.println("Rectangle"); } } Inherit( ) { Figure f = (Figure) new Box( ); f.display( ); Rectangle r = (Rectangle) f; r.display( ); } public static void main(String[ ] args) { new Inherit( ); } } A) Figure Rectangle B) Rectangle Figure C) Rectangle Rectangle D) Figure Figure E) none of the above

C) Rectangle Rectangle

Comparing the performance of selection sort and insertion sort, what can one say? A) The efficiencies of both sorts depend upon the data being sorted B) Insertion sort is more efficient than selection sort C) The efficiencies of both sorts are about the same D) Selection sort is more efficient than insertion sort E) none of the above

C) The efficiencies of both sorts are about the same

Which of the following statements are true about Java loops? A) loops may be replaced by an appropriate combination of if-else and switch statements B) while loops and do loops are essentially the same; but while loops always execute at least once C) all three loop statements are functionally equivalent D) if you know the number of times that a loop is to be performed, the best loop statement to use is a while loop E) none of the above

C) all three loop statements are functionally equivalent

In order for a computer to be accessible over a computer network, the computer needs its own A) router B) packet C) network address D) MODEM E) communication line

C) network address

An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship? A) inner B) i C) this D) static E) private

C) this

An int array stores the following values. Use the array to answer the questions below. | 9 | 4 | 12 | 2 | 6 | 8 | 18 | Which of the following lists of numbers would accurately show the array after the fourth pass of the Selection Sort algorithm? A) 9, 4, 12, 2, 6, 8, 18 B) 2, 4, 6, 9, 8, 12, 18 C) 2, 4, 6, 9, 12, 8, 18 D) 2, 4, 6, 8, 12, 9, 18 E) 2, 4, 6, 8, 9, 12, 18

D) 2, 4, 6, 8, 12, 9, 18

What is printed by the following code? Consider the polymorphic invocation. public class Inherit { class Figure { void display( ) { System.out.println("Figure"); } } class Rectangle extends Figure { void display( ) { System.out.println("Rectangle"); } } class Box extends Figure { void display( ) { System.out.println("Box"); } } Inherit( ) { Figure f = new Figure( ); Rectangle r = new Rectangle( ); Box b = new Box( ); f.display( ) f = r;f.display( ); f = b; f.display( ); } public static void main(String[ ] args) { new Inherit( ); } } A) Figure Figure Figure B) Rectangle Box C) Syntax error. This code won't compile or execute. D) Figure Rectangle Box E) none of the above

D) Figure Rectangle Box

How many times will the following loop iterate? int x = 10; while (x > 0) { System.out.println(x); x--; } A) times B) 1 time C) 11 times D) 10 times E) 9 times

D) 10 times

Consider the array declaration and instantiation: int[ ] arr = new int[5]; Which of the following is true about arr? A) It stores 4 elements with legal indices between 1 and 4 B) It stores 5 elements with legal indices between 0 and 5 C) It stores 5 elements with legal indices between 1 and 5 D) It stores 5 elements with legal indices between 0 and 4 E) It stores 6 elements with legal indices between 0 and 5

D) It stores 5 elements with legal indices between 0 and 4

Comparing the amount of memory required by selection sort and insertion sort, what can one say? A) Insertion sort requires more additional memory than selection sort B) Selection sort requires more additional memory than insertion sort C) Both methods require about as much additional memory as the data they are sorting D) Neither method requires additional memory E) None of the above

D) Neither method requires additional memory

Assume that count is 0, total is 20 and max is 1. The following statement will do which of the following? if (count != 0 && total / count > max) max = total / count; A) The condition does not short circuit causing a division by zero error B) The condition short circuits so that there is no division by zero error when evaluating the condition, but the assignment statement causes a division by zero error C)The condition will not compile because it uses improper syntax D) The condition short circuits and the assignment statement is not executed E) The condition short circuits and the assignment statement is executed without problem

D) The condition short circuits and the assignment statement is not executed

In order to implement Comparable in a class, what method(s) must be defined in that class? A) both compares and equals B) both lessThan and greaterThan C) equals D) compareTo E) compares

D) compareTo

Use the code below to answer the following questions. Note that the catch statements in the code are not implemented, but you will not need those details. Assume filename is a String, x is an int, a is a double array and i is an int. Use the comments i1, i2, i3, e1, e2, e3, e4, e5 to answer the questions (i for instruction, e for exception handler). try { BufferedReader infile = new BufferedReader(new FileReader(filename)); // i1 int x = Integer.parseInt(infile.readLine( )); // i2 a[++i] = (double) (1 / x); // i3 } catch (FileNotFoundException ex) {...} // e1 catch (NumberFormatException ex) {...} // e2 catch (ArithmeticException ex) {...} // e3 catch (ArrayIndexOutOfBounds ex) {...} // e4 catch (IOException ex) {...} // e5 An exception raised by the instruction in i1 would be caught by the catch statement labeled A) e5 B) e1 C) e2 D) either e1 or e5 E) either e1, e4, or e5

D) either e1 or e5

In general, spending more time in development to ensure better software will A) slightly reduce maintenance efforts B) shorten testing time C) slightly increase maintenance efforts D) greatly reduce maintenance efforts E) not alter the time it takes for any other stage whatsoever

D) greatly reduce maintenance efforts

For the questions below, consider a class called ChessPiece. This class has two instance data, String type and int player. The variable type will store "King", "Queen", "Bishop", etc and the int player will store 0 or 1 depending on whose piece it is. We wish to implement Comparable for the ChessPiece class. Assume that, the current ChessPiece is compared to a ChessPiece passed as a parameter. Pieces are ordered as follows: "Pawn" is a lesser piece to a "Knight" and a "Bishop", "Knight" and "Bishop" are equivalent for this example, both are lesser pieces to a "Rook" which is a lesser piece to a "Queen" which is a lesser piece to a "King." Which of the following method headers would properly define the method needed to make this class Comparable? A) public int comparable(Object cp) B) public boolean comparable(Object cp) C) public boolean compareTo(Object cp) D) public int compareTo(Object cp) E) public int compareTo( )

D) public int compareTo(Object cp)

For the questions below, use the following class definition. import java.text.DecimalFormat; public class Student { private String name; private String major; private double gpa; private int hours; public Student(String newName, String newMajor, double newGPA, int newHours) { name = newName; major = newMajor; gpa = newGPA; hours = newHours; } public String toString( ) { DecimalFormat df = new DecimalFormat("xxxx"); // xxxx needs to be replaced return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours } } Assume that another method has been defined that will compute and return the student's class rank (Freshman, Sophomore, etc). It is defined as: public String getClassRank( ) Given that s1 is a student, which of the following would properly be used to get s1's class rank? A) s1 = getClassRank( ); B) s1.toString( ); C) s1.getHours( ); D) s1.getClassRank( ); E) getClassRank(s1);

D) s1.getClassRank( );

A bad programming habit is to build an initial program and then spend a great deal of time modifying the code until it is acceptable. This is known as A) the prototyping approach B) the waterfall model C) the recursive approach D) the build-and-fix approach E) iterative development

D) the build-and-fix approach

In order to have some code throw an exception, you would use which of the following reserved words? A) throws B) try C) goto D) throw E) Throwable

D) throw

Which memory capacity is the largest? A) 1,500,000,000,000 bytes B) 100 gigabytes C) 3,500,000 kilobytes D) 10 terabyte E) 12,000,000 megabytes

E) 12,000,000 megabytes

The Scanner class provides an abstraction for input operations by A) performing conversion operations from String to the appropriate type as specified in the Scanner message B) using try and catch statements to catch any IOException instead of throwing the Exception elsewhere C) parsing input lines into individual tokens D) inputting from the standard input stream if create is called using System.in E) all of the above

E) all of the above

For the questions below, assume values is an int array that is currently filled to capacity, with the following values: | 9 | 4 | 12 | 2 | 6 | 8 | 18 | The statement System.out.println(values[7]); will A) cause a syntax error B) output 18 C) output 7 D) output nothing E) cause an ArrayOutOfBoundsException to be thrown

E) cause an ArrayOutOfBoundsException to be thrown

For the questions below, assume values is an int array that is currently filled to capacity, with the following values: | 9 | 4 | 12 | 2 | 6 | 8 | 18 | The statement System.out.println(values[7]); will A) output nothing B) cause a syntax error C) output 18 D) output 7 E) cause an ArrayOutOfBoundsException to be thrown

E) cause an ArrayOutOfBoundsException to be thrown

Which phase of the fetch-decode-execute cycle might use a circuit in the arithmetic-logic unit? A) decode B) during fetch or execute, but not decode C) fetch D) could be used in fetch, decode or execute phase E) execute

E) execute

The goal of testing is to A) give out-of-work programmers something to do B) ensure that the software has no errors C) find syntax errors D) evaluate how well the software meets the original requirements E) find logical and run-time errors

E) find logical and run-time errors

If a switch statement is written that contains no break statements whatsoever, A) this is not an error, but nothing within the switch statement ever will be executed B) this is a syntax error and an appropriate error message will be generated C) this is equivalent to having the switch statement always take the default clause, if one is present D) each of the case clauses will be executed every time the switch statement is encountered E) none of the above

E) none of the above

If x is an int where x = 0, what will x be after the following loop terminates? while (x < 100) x *= 2; A) 100 B) 2 C) 128 D) 64 E) none of the above, this is an infinite loop

E) none of the above, this is an infinite loop

StringTokenizer is a class in the java.util library that can divide a String based on some delimiter String (a delimiter is a separator). If the instruction StringTokener st = new StringTokenizer(str, "&&"); is executed, where str is some String, then st divides up str into separate Strings whenever A) a single ampersand ("&") is found B) a blank space is found C) two ampersands or the substring "and" or "AND" is found D) two blank spaces are found E) two ampersands are found

E) two ampersands are found

"class Aggregate" is incorrect. Choose the correct line so that this program prints Granite: weight=25.0 value=4 numKind=7 public class Inherit { abstract class Stone { protected float weight = 13; protected int value = 4; abstract public String toString( ); } class Aggregate { protected int numKind; } class Granite extends Aggregate { Granite( ) { weight = 25; numKind = 7; } public String toString( ) { return "Granite: weight=" + weight + " value=" + value + " numKind=" + numKind; } } Inherit( ) { Granite g = new Granite(); System.out.println(g); } public static void main(String[ ] args) { new Inherit( ); } } a) abstract class Aggregate extends Granite { b) class Aggregate extends Stone { c) abstract class Aggregate { d) abstract class Aggregate extends Stone e) none of the above

d) abstract class Aggregate extends Stone


Conjuntos de estudio relacionados

Descriptive and Inferential Statistics 90%

View Set

A+ Chapter 3 - Peripherals and Expansion

View Set

arguments for and against protection

View Set

Internet Giants: Law and Economics of Media Platforms by Randal Picker at the University of Chicago

View Set

Chapter 17 Small Business Protection: Risk Management and Insurance

View Set