Midterm Exam (NOW FINAL)
If a switch statement is written that contains no break statements whatsoever, 1) this is a syntax error and an appropriate error message will be generated 2) each of the case clauses will be executed every time the switch statement is encountered 3) this is equivalent to having the switch statement always take the default clause, if one is present 4) this is not an error, but nothing within the switch statement ever will be executed 5) none of the above
none of the above
Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates? for (int i=0;i<5;i++) x += i; 1)0 2)4 3)5 4)10 5)15
10
The following nested loop structure will execute the inner most statement (x++) how many times? for (int j = 0; j < 100; j++) for (int k = 100; k > 0; k--) x++; 1)100 2)200 3)10,000 4)20,000 5)1,000,000
10,000
What is output with the statement System.out.println(""+x+y); if x and y are int values where x=10 and y=5? 1) 15 2) 105 3) 10 5 4) x+y 5) An error since neither x nor y is a String
105
What is the efficiency of binary search? 1)n2 2)n 3)log2 n 4)n/2 5)none of the above
log2 n
Based on the following recursive method: public int question1_2(int x, int y) { if (x == y) return 0; else return question1_2(x-1, y) + 1; } Question: If the method is called as question1_2(8, 3), what is returned? 1)11 2)8 3)5 4)3 5)24
5
An int array stores the following values: 9, 4, 12, 2, 6, 8, 18. How many passes will it take in all for Selection Sort to sort this array? 1)2 2)4 3)5 4)6 5)7
6
For the question below, refer to the following recursive factorial method. public int factorial(int x) { if (x > 1) return x * factorial (x - 1); else return 1; } What is returned if factorial(3) is called? 1)0 2)1 3)3 4)6 5)9
6
In which phase of program development would you expect the programmer(s) to determine the classes and objects needed? 1)Software requirements 2)Software design 3)Software implementation 4)Software testing 5)Could occur in any of the above
Software design
The idea that program instructions execute in order (linearly) unless otherwise specified through a conditional statement is known as 1) boolean execution 2) conditional statements 3) try and catch 4) sequentiality 5) flow of control
flow of control
Which of the following will yield a pseudorandom number in the range [ -5, +5 ) given the following: Random gen = new Random( ); 1) gen.nextFloat( ) * 5 2) gen.nextFloat( ) * 10 - 5 3) gen.nextFloat( ) * 5 - 10 4) gen.nextInt( ) * 10 - 5 5) gen.nextInt(10) - 5
gen.nextFloat( ) * 10 - 5
Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls m3 and then m2 calls m4, m3 calls m5. If m4 has just terminated, what method will resume execution? 1) m1 2) m2 3) m3 4) m5 5) main
m2
Having multiple class methods of the same name where each method has a different number of or type of parameters is known as 1)encapsulation 2)information hiding 3)tokenizing 4)importing 5)method overloading
method overloading
The behavior of an object is defined by the object's 1)instance data 2)constructor 3)visibility modifiers 4)methods 5)all of the above
methods
Which of the following reserved words in Java is used to create an instance of a class? 1)class 2)public 3)public or private, either could be used 4)import 5)new
new
The term exception propagation means 1)an exception is caught by the first catch clause 2)an exception not caught by the first catch clause is caught by an outer (enclosing) catch clause 3)exceptions are caught, sequentially, by catch clauses in the current try block 4)exceptions always are caught by the outermost try block 5)none of the above
none of the above
Assume that Student, Employee and Retired are all extended classes of Person, and all four classes have different implementations of the method getMoney. Consider the following code where are the required parameters for the constructors: Person p = new Person( ); int m1 = p.getMoney( ); // assignment 1 p = new Student( ); int m2 = p.getMoney( ); // assignment 2 if (m2 < 100000) p = new Employee( ); else if (m1 > 50000) p = new Retired( ); int m3 = p.getMoney( ); // assignment 3 The reference to getMoney( ) in assignment 3 is to the class 1)Person 2)Student 3)Employee 4)Retired 5)none of the above, this cannot be determined by examining the code
none of the above, this cannot be determined by examining the code
In Java, arrays are 1)primitive data types 2)objects 3)interfaces 4)primitive data types if the type stored in the array is a primitive data type and objects if the type stored in the array is an object 5)Strings
objects
The relationship between a class and an object is best described as 1) classes are instances of objects 2) objects are instances of classes 3) objects and classes are the same thing 4) classes are programs while objects are variables 5) objects are the instance data of classes
objects are instances of classes
When a program terminates because a thrown exception is not handled, the program 1)starts up again automatically 2)opens a dialog box, which asks the user whether the program should start again, end, or enter a debugging mode 3)saves all output to a disk file called the "runStackTrace.txt" 4)opens a dialog box for the user to specify a disk file name, and all output is stored to that disk file 5)outputs a message indicating what and where the exception was thrown
outputs a message indicating what and where the exception was thrown
Polymorphism is achieved by 1)overloading 2)overriding 3)embedding 4)abstraction 5)encapsulation
overriding
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 } } Another method that might be desired is one that updates the Student's number of credit hours. This method will receive a number of credit hours and add these to the Student's current hours. Which of the following methods would accomplish this? public int updateHours( ) { return hours; } ------------------------------------- public void updateHours( ) { hours++; } ------------------------------------ public updateHours(int moreHours) { hours += moreHours; } ---------------------------------------- public void updateHours(int moreHours) { hours += moreHours; } ------------------------------------------- public int updateHours(int moreHours) { return hours + moreHours; }
public void updateHours(int moreHours) { hours += moreHours; }
Visibility modifiers include 1) public, private 2) public, private, protected 3) public, private, protected, final 4) public, protected, final, static 5) public, private, protected, static
public, private, protected
Which of the following is a legal Java identifier? 1) i 2) CS class 3) ilikeclass! 4) idon'tlikeclass 5) i-like-class
i
Which of the following characters does not need to have an associated "closing" character in a Java program? 1){ 2)( 3)[ 4)< 5)all of these require closing characters
<
A Java program can handle an exception in several different ways. Which of the following is not a way that a Java program could handle an exception? 1)ignore the exception 2)handle the exception where it arose using try and catch statements 3)propagate the exception to another method where it can be handled 4)throw the exception to a pre-defined Exception class to be handled 5)all of the above are ways that a Java program could handle an exception
throw the exception to a pre-defined Exception class to be handled
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 } } Which of the following patterns should be used in place of "xxxx" when instantiating df so that the gpa to be output in typical form (like 3.810)? 1) "#.###" 2) "#.0" 3) "0.# " 4) "0.000" 5) "0.0##"
"0.000"
Based on the following recursive method: public int question1_2(int x, int y) { if (x == y) return 0; else return question1_2(x-1, y) + 1; } Calling this method will result in infinite recursion if which condition below is initially true? 1)(x = = y) 2)(x != y) 3)(x > y) 4)(x < y) 5)(x = = 0 && y != 0)
(x < y)
Given the following assignment statement, which of the following answers is true regarding the order that the operators will be applied based on operator precedence? a = (b + c) * d / e - f; 1) *, /, +, - 2) *, +, /, - 3) +, *, /, - 4) +, /, *, - 5) +, -, *, /
+, *, /, -
Of the following if statements, which one correctly executes three instructions if the condition is true? 1) if (x<0) a = b * 2; y = x; z = a - y; 2) { if (x < 0) a = b * 2; y = x; z = a - y; } 3) if { (x < 0) a = b * 2; y = x; z = a - y; } 4) f (x < 0) { a = b * 2; y = x; z = a - y; } 5) b, c and d are all correct, but not a
4) f (x < 0) { a = b * 2; y = x; z = a - y; }
An int array stores the following values: 9, 4, 12, 2, 6, 8, 18. Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm? 1)9, 4, 12, 2, 6, 8, 18 2)4, 9, 12, 2, 6, 8, 18 3)2, 4, 12, 9, 6, 8, 18 4)2, 4, 6, 8, 9, 12, 18 5)2, 4, 9, 12, 6, 8, 18
2, 4, 12, 9, 6, 8, 18
An int array stores the following values: 9, 4, 12, 2, 6, 8, 18. Which of the following lists of numbers would accurately show the array after the second pass of the Selection Sort algorithm? 1)9, 4, 12, 2, 6, 8, 18 2)2, 4, 9, 6, 12, 8, 18 3)2, 4, 12, 9, 6, 8, 18 4)2, 4, 6, 8, 9, 12, 18 5)2, 4, 12, 6, 8, 9, 18
2, 4, 12, 9, 6, 8, 18
An int array stores the following values: 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? 1)9, 4, 12, 2, 6, 8, 18 2)2, 4, 6, 9, 12, 8, 18 3)2, 4, 6, 8, 9, 12, 18 4)2, 4, 6, 9, 8, 12, 18 5)2, 4, 6, 8, 12, 9, 18
2, 4, 6, 8, 12, 9, 18
Given the nested if-else structure below, answer the following question: if (a > 0) if (b < 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed? 1) 0 2) 2 3) 3 4) 4 5) 5
3
In Java a variable may contain 1) A value or a reference 2) A package 3) A method 4) A class 5) Any of the above
A value or a reference
Which of the following is true regarding Java classes? 1)All classes must have 1 parent but may have any number of children (derived or extended) classes. 2)All classes must have 1 child (derived or extended) class but may have any number of parent classes. 3)All classes must have 1 parent class and may have a single child (derived or extended) class. 4)All classes can have any number (0 or more) of parent classes and any number of children (derived or extended) classes. 5)All classes can have either 0 or 1 parent class and any number of children (derived or extended) classes.
All classes must have 1 parent but may have any number of children (derived or extended) classes.
If two variables contain aliases of the same object then 1) The object may be modified using either alias 2) The object cannot be modified unless there's but a single reference to it 3) A third alias is created if/when the object is modified 4) The object will become an "orphan" and subsequently collected by the garbage collector if both variables are set to null 5) Answers (a) and (d) are correct
Answers (a) and (d) are correct
In a development environment that fully supports JavaFX, which of the following is true? 1) The launch method of the scene class is called automatically. 2) The launch method of the Application class is called automatically. 3) Since the launch method is called automatically, you do not need to write the main method. 4) Since the launch method is called automatically, you do not need to call it in the main method. 5) Both B and C are true
Both B and C are true
Assume that x and y are int variables with x = 5, y = 3, and a and d are char variables with a = 'a' and d = 'A', and examine the following conditions: Condition 1: (x < y && x > 0) Condition 2: (a != d || x != 5) Condition 3: !(true && false) Condition 4: (x > y || a == 'A' || d != 'A') 1) All 4 Conditions are true 2) Only Condition 2 is true 3) Condition 2 and Condition 4 are true only 4) Conditions 2, 3 and 4 are all true, Condition 1 is not 5) All 4 Conditions are false
Conditions 2, 3 and 4 are all true, Condition 1 is not
In Java, "instantiation" means 1) Noticing the first time something is used 2) Creating a new object 3) Creating a new alias to an existing object 4) Launching a method 5) None of the above
Creating a new object
What are the main programming mechanisms that constitute object-oriented programming? 1)Encapsulation, inheritance, polymorphism 2)Encapsulation, abstraction, inheritance 3)Inheritance, polymorphism, recursion 4)Polymorphism, recursion, abstraction 5)None of the above
Encapsulation, inheritance, polymorphism
Java is a strongly typed language. What is meant by "strongly typed"? 1) Every variable must have an associated type before you can use it 2) Variables can be used without declaring their type 3) Every variable has a single type associated with it throughout its existence in the program, and the variable can only store values of that type 4) Variables are allowed to change type during their existence in the program as long as the value it currently stores is of the type it is currently declared to be 5) Variables are allowed to change types during their existence in the program but only if the change is to a narrower type
Every variable has a single type associated with it throughout its existence in the program, and the variable can only store values of that type
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( ); } } 1) Figure Rectangle Box 2) Rectangle Box 3) Figure Figure Figure 4) Syntax error. This code won't compile or execute 5) None of the above
Figure Rectangle Box
Which of the following statements is completely true? 1)If a class is declared to be abstract then every method in the class is abstract and must be overridden 2)If a class is declared to be abstract then some methods in the class may have their bodies omitted 3)If a class is declared to be abstract then all methods in the class must have their bodies omitted 4)If a class is declared to be abstract then all the instance variables must be overridden when a concrete class is derived from the abstract base class
If a class is declared to be abstract then some methods in the class may have their bodies omitted
Which of the following is true regarding the mod operator, %? 1) It can only be performed on int values and its result is a double 2) It can only be performed on int values and its result is an int 3) It can only be performed on float or double values and its result is an int 4) It can only be performed on float or double values and its result is a double 5) It can be performed on any numeric values, and the result always is numeric
It can be performed on any numeric values, and the result always is numeric
What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int initialized to 0. for (j=0; j < list.length; j++) if (list[j] < temp) c++; 1)It finds the smallest value and stores it in temp 2)It finds the largest value and stores it in temp 3)It counts the number of elements equal to the smallest value in list 4)It counts the number of elements in list that are less than temp 5)It sorts the values in list to be in ascending order
It counts the number of elements in list that are less than temp
Consider the array declaration and instantiation: int[ ] arr = new int[5]; Which of the following is true about arr? 1)It stores 5 elements with legal indices between 1 and 5 2)It stores 5 elements with legal indices between 0 and 4 3)It stores 4 elements with legal indices between 1 and 4 4)It stores 6 elements with legal indices between 0 and 5 5)It stores 5 elements with legal indices between 0 and 5
It stores 5 elements with legal indices between 0 and 4
Which statement is completely true? 1)Java upcasts automatically, but you must explicitly downcast 2)Java downcasts automatically, but you must explicitly upcast 3)Java expects the user to explicitly upcast and downcast 4)Java will both upcast and downcast automatically 5)The rules for upcasting and downcasting depend upon whether classes are declared public, protected, or private
Java upcasts automatically, but you must explicitly downcast
Which is now the preferred approach for developing Java programs that use graphics and GUIs? 1) Swing 2) AWT 3) JavaFX 4) API 5) All of these are preferred approaches
JavaFX
In order to preserve encapsulation of an object, we would do all of the following except for which one? 1) Make the instance data private 2) Define the methods in the class to access and manipulate the instance data 3) Make the methods of the class public 4) Make the class final 5) All of the above preserve encapsulation
Make the class final
Comparing the amount of memory required by selection sort and insertion sort, what can one say? 1)Selection sort requires more additional memory than insertion sort 2)Insertion sort requires more additional memory than selection sort 3)Both methods require about as much additional memory as the data they are sorting 4)Neither method requires additional memory 5) None of the above
Neither method requires additional memory
If x is an int where x = 0, what will x be after the following loop terminates? while (x < 100) x *= 2; 1)2 2)4 3)100 4)128 5)None of the above, this is an infinite loop
None of the above, this is an infinite loop
All classes in Java are directly or indirectly subclasses of the ________ class. 1)Wrapper 2)String 3)Reference 4)this 5)Object
Object
Which properties are true of String objects? 1) Their lengths never change 2) The shortest string has zero length 3) Individual characters within a String may be changed using the replace method 4) The index of the first character in a string is one 5) Only (a) and (b) are true
Only (a) and (b) are true
Assume that Student, Employee and Retired are all extended classes of Person, and all four classes have different implementations of the method getMoney. Consider the following code where are the required parameters for the constructors: Person p = new Person( ); int m1 = p.getMoney( ); // assignment 1 p = new Student( ); int m2 = p.getMoney( ); // assignment 2 if (m2 < 100000) p = new Employee( ); else if (m1 > 50000) p = new Retired( ); int m3 = p.getMoney( ); // assignment 3 The reference to getMoney( ) in assignment 1 is to the class 1)Person 2)Student 3)Employee 4)Retired 5)none of the above, this cannot be determined by examining the code
Person
Write a declaration for a Rectangle named squareShape that is 400 pixels wide, 400 pixels high, and its upper-left corner position is at point (50, 50). 1) Rectangle squareShape = new squareShape(50, 50, 400, 400); 2) squareShape = new Rectangle(50, 50, 400, 400); 3) Rectangle squareShape = new Rectangle(50, 400, 50, 400); 4) Rectangle squareShape = new Rectangle(50, 50, 400, 400); 5) Rectangle = new squareShape(400, 400, 50, 50);
Rectangle squareShape = new Rectangle(50, 50, 400, 400);
NullPointerException and ArithmeticException are both derived from which class? 1)Error 2)Exception 3)RuntimeException 4)IllegalAccessException 5)CheckedException
RuntimeException
Assume that Student, Employee and Retired are all extended classes of Person, and all four classes have different implementations of the method getMoney. Consider the following code where are the required parameters for the constructors: Person p = new Person( ); int m1 = p.getMoney( ); // assignment 1 p = new Student( ); int m2 = p.getMoney( ); // assignment 2 if (m2 < 100000) p = new Employee( ); else if (m1 > 50000) p = new Retired( ); int m3 = p.getMoney( ); // assignment 3 The reference to getMoney( ) in assignment 2 is to the class 1) Person 2) Student 3) Employee 4) Retired 5) none of the above, this cannot be determined by examing the code
Student
Use the following class definition to answer the question: 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 } } Which of the following could be used to instantiate a new Student s1? 1) Student s1 = new Student( ); 2) s1 = new Student( ); 3) Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33); 4) new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33); 5) new Student(s1);
Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33);
Assume that x is a double that stores 0.362491. To output this value as 36%, you could use the NumberFormat class with NumberFormat nf = NumberFormat.getPercentInstance( ); Which of the following statements then would output x as 36%? 1) System.out.println(x); 2) System.out.println(nf); 3) System.out.println(nf.x); 4) System.out.println(nf.format(x)); 5) System.out.println(format(x));
System.out.println(nf.format(x));
import java.util.Scanner; public class Questions { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(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); } } computes 1) The correct average of x, y and z as a double 2) The correct average of x, y and z as an int 3) The average of x, y, and z as a double, but the result may not be accurate 4) the sum of x, y and z 5) the remainder of the sum of x, y and z divided by 3
The average of x, y, and z as a double, but the result may not be accurate
What happens if you declare a class constructor to have a void return type? 1) You'll likely receive a syntax error 2) The program will compile with a warning, but you'll get a runtime error 3) There's nothing wrong with declaring a constructor to be void 4) The class' default constructor will be used instead of the one you're declaring 5) None of the above
The class' default constructor will be used instead of the one you're declaring
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; 1) The condition short circuits and the assignment statement is not executed 2) The condition short circuits and the assignment statement is executed without problem 3) The condition does not short circuit causing a division by zero error 4) 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 5) The condition will not compile because it uses improper syntax
The condition short circuits and the assignment statement is not executed
Comparing the performance of selection sort and insertion sort, what can one say? 1)Selection sort is more efficient than insertion sort 2)Insertion sort is more efficient than selection sort 3)The efficiencies of both sorts are about the same 4)The efficiencies of both sorts depend upon the data being sorted 5)none of the above
The efficiencies of both sorts are about the same
Consider the following enumeration: enum Speed { FAST, MEDIUM, SLOW }; 1) The ordinal value of MEDIUM is 2 2) The ordinal value of SLOW is 1 3) The name of the Speed enumeration whose ordinal value is zero is FAST 4) The name of the Speed enumeration whose ordinal value is one is SLOW 5) None of the above
The name of the Speed enumeration whose ordinal value is zero is FAST
What kind of performance can you expect if you perform linear search on a sorted array? 1)The performance will be about the same as on an unsorted array 2)The performance will be much better than on an unsorted array 3)The performance will be much worse than on an unsorted array 4)The performance will be worse than n2 in this case 5)none of the above
The performance will be about the same as on an unsorted array
A recursive algorithm is superior to an iterative algorithm along which of the following criteria? 1)The recursive algorithm is easier to debug 2)The recursive algorithm is computationally more efficient 3)The recursive algorithm is more elegant 4)The recursive algorithm requires less memory to execute 5)All of the above
The recursive algorithm is more elegant
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' ; 1) This code will work correctly in all cases 2) This code will work correctly only if score >= 60 3) This code will work correctly only if score < 60 4) This code will work correctly only if score < 70 5) This code will not work correctly under any circumstances
This code will work correctly only if score < 70
In order to determine the type that a polymorphic variable refers to, the decision is made 1)by the programmer at the time the program is written 2)by the compiler at compile time 3)by the operating system when the program is loaded into memory 4)by the Java run-time environment at run time 5)by the user at run time
by the Java run-time environment at run time
Which of the following is true regarding Java syntax and semantics? 1) a Java compiler can determine if you have followed proper syntax but not proper semantics 2) a Java compiler can determine if you have followed proper semantics but not proper syntax 3) a Java compiler can determine if you have followed both proper syntax and semantics 4) a Java compiler cannot determine if you have followed either proper syntax or semantics 5) a Java compiler can determine if you have followed proper syntax and can determine if you have followed proper semantics if you follow the Java naming convention rules
a Java compiler can determine if you have followed proper syntax but not proper semantics
A unique aspect of Java that allows code compiled on one machine to be executed on a machine of a different hardware platform is Java's 1) bytecode 2) syntax 3) use of objects 4) use of exception handling 5) all of the above
bytecode
In a UML diagram for a class: 1)classes are represented as rectangles, 2)there may be a section containing the name of the class 3)there may be a section containing the attributes (data) of the class 4)there may be a section containing the methods of the class 5)all of the above
all of the above
The difference between a checked and an unchecked exception is 1)checked exceptions need not be listed in a throws clause 2)unchecked exceptions must be listed in a throws clause 3)neither kind of exception follows the rules of exception propagation 4)an unchecked exception requires no throws clause 5)a checked exception always must be caught by a try block; an unchecked exception does not
an unchecked exception requires no throws clause
If a programmer writes a class wanting it to be extended by another programmer, then this programmer must 1)change private methods and instance data to be protected 2)change public methods and instance data to be protected 3)change all methods to be protected 4)change the class to be protected 5)none of the above, the programmer does not have to change anything
change private methods and instance data to be protected
Inheritance through an extended (derirved) class supports which of the following concepts? 1) interfaces 2) modulary 3) information hiding 4) code reuse 5) correctness
code reuse
In order to implement Comparable in a class, what method(s) must be defined in that class? 1)equals 2)compares 3)both lessThan and greaterThan 4)compareTo 5)both compares and equals
compareTo
Which of the following is not a method of the Object class? 1)clone 2)compareTo 3)equals 4)toString 5)all of the above are methods of the Object class
compareTo
Which of the following would be a good variable name for the current value of a stock? 1) curstoval 2) theCurrentValueOfThisStockIs 3) currentStockVal 4) csv 5) current
currentStockVal
What will the following line of Java code do: // System.out.println("Hello"); 1)do nothing 2)cause "Hello" to be output 3)cause a syntax error 4)cause "(Hello)" to be output 5)there is no way to know without executing this line of code
do nothing
A class' constructor usually defines 1)how an object is initialized 2)how an object is interfaced 3)the number of instance data in the class 4)the number of methods in the class 5)if the instance data are accessible outside of the object directly
how an object is initialized
A finally clause will execute 1)only if the try statement that precedes it does not throw an exception 2)only if the try statement that precedes it throws an exception that is caught 3)only if the try statement that precedes it throws an exception that is not caught 4)only if the try statement that precedes it throws an exception, whether it is caught or not 5)in any circumstance
in any circumstance
Abstract methods are used when defining 1)interface classes 2)derived classes 3)classes that have no constructor 4)arrays 5)classes that have no methods
interface classes
The relationship between a parent class and a child class is referred to as a(n) ____ relationship. 1)has-a 2)is-a 3)was-a 4)instance-of 5)alias
is-a
The relationship between a parent class and a child class is referred to as a(n) ________ relationship. 1) has-a 2) is-a 3) was-a 4) instance-of 5) alias
is-a
Consider the following skeletal code. public static void main(String[ ] args) { try { ExceptionThrowerCode etc = new ExceptionThrowerCode( ); etc.m1( ); etc.m2( ); } catch (ArithmeticException ae) { } } public class ExceptionThrowerCode { public void m1( ) { } public void m2( ) { try { m3( ); } catch(ArithmeticException ae) { } catch(NullPointerException npe) { } } public void m3( ) { try { } catch(ArithmeticException ae) { } } } If a NullPointerException arises in the try statement in m3, 1)it is caught in main 2)it is caught in m1 3)it is caught in m2 4)it is caught in m3 5)it is not caught leading to the program terminating
it is caught in m2
Consider the following skeletal code. public static void main(String[ ] args) { try { ExceptionThrowerCode etc = new ExceptionThrowerCode( ); etc.m1( ); etc.m2( ); } catch (ArithmeticException ae) { } } public class ExceptionThrowerCode { public void m1( ) { } public void m2( ) { try { m3( ); } catch(ArithmeticException ae) { } catch(NullPointerException npe) { } } public void m3( ) { try { } catch(ArithmeticException ae) { } } } If an ArithmeticException arises in the try statement in m3, 1)it is caught in main 2)it is caught in m1 3)it is caught in m2 4)it is caught in m3 5)it is not caught leading to the program terminating
it is caught in m3
Consider the following skeletal code. public static void main(String[ ] args) { try { ExceptionThrowerCode etc = new ExceptionThrowerCode( ); etc.m1( ); etc.m2( ); } catch (ArithmeticException ae) { } } public class ExceptionThrowerCode { public void m1( ) { } public void m2( ) { try { m3( ); } catch(ArithmeticException ae) { } catch(NullPointerException npe) { } } public void m3( ) { try { } catch(ArithmeticException ae) { } } } If a NullPointerException arises in the try statement in m1, 1)it is caught in main 2)it is caught in m1 3)it is caught in m2 4)it is caught in m3 5)it is not caught leading to the program terminating
it is not caught leading to the program terminating
Given that s is a String, what does the following loop do? for (int j = s.length( ); j > 0; j--) System.out.print(s.charAt(j-1)); 1) it prints s out backwards 2) it prints s out forwards 3) it prints s out backwards after skipping the last character 4) it prints s out backwards but does not print the 0th character 5) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0
it prints s out backwards
Which of the following packages includes classes that represent shapes in JavaFX? 1) javafx.scene.shape 2) javafx.shape.scene 3) javafx.shapes 4) javafx.stage.shape 5) javafx.stage.scene
javafx.scene.shape
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? 1) s1 = getClassRank( ); 2) s1.toString( ); 3) s1.getHours( ); 4) s1.getClassRank( ); 5) getClassRank(s1);
s1.getClassRank( );
In addition to their usage providing a mechanism to convert (to box) primitive data into objects, what else do the wrapper classes provide? 1) enumerations 2) static constants 3) arrays to contain the data 4) exceptions 5) none of the above
static constants
A cast is required in which of the following situations? 1) using charAt to take an element of a String and store it in a char 2) storing an int in a float 3) storing a float in a double 4) storing a float in an int 5) all of the above require casts
storing a float in an int
Which of the following messages passed to the String str could throw a StringIndexOutOfBoundsException? 1)str.length( ) 2)str.charAt(2); 3)str.replace('a', 'A'); 4)str.equals(str); 5)any of the above could throw a StringIndexOutOfBoundsException
str.charAt(2);
An exception can produce a "call stack trace" which lists 1)the active methods in the order that they were invoked 2)the active methods in the opposite order that they were invoked 3)the values of all instance data of the object where the exception was raised 4)the values of all instance data of the object where the exception was raised and all local variables and parameters of the method where the exception was raised 5)the name of the exception thrown
the active methods in the opposite order that they were invoked
We compare sorting algorithms by examining 1)the number of instructions executed by the sorting algorithm 2)the number of instructions in the algorithm itself (its length) 3)the types of loops used in the sorting algorithm 4)the amount of memory space required by the algorithm 5)whether the resulting array is completely sorted or only partially sorted
the number of instructions executed by the sorting algorithm
An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship? 1)inner 2)i 3)private 4)this 5)static
this
For the question below, refer to the following recursive factorial method. public int factorial(int x) { if (x > 1) return x * factorial (x - 1); else return 1; } What condition defines the base case for this method? 1)(x > 1) 2)(x = = 1) 3)(x = = 0) 4)(x <= 0) 5)(x <= 1)
x <= 1
Which of the following would return the last character of the String x? 1) x.charAt(0); 2) x.charAt(last); 3) x.charAt(length(x)); 4) x.charAt(x.length( )-1); 5) x.charAt(x.length( ));
x.charAt(x.length( )-1);
What value will z have if we execute the following assignment statement? float z = 5 / 10; 1) z will equal 0.0 2) z will equal 0.5 3) z will equal 5.0 4) z will equal 0.05 5) none of the above, a run-time error arises because z is a float and 5 / 10 is an int
z will equal 0.0