Final Exam Practice
What is the natural order of the array below? String[] pets = {"dog", "cat, "bird", "hamster"}; a. "cat, "dog", "bird", "hamster" b. "hamster", "dog", "cat, "bird" c. "dog", "cat, "bird", "hamster" d. "bird", "cat, "dog", "hamster" e. "bird", "dog", "cat, "hamster"
"bird", "cat, "dog", "hamster"
Suppose myArray is declared in the main method and then passed into method myMethod as shown below. After myMethod completes and returns to main, what would be the result of printing each element in the array separated by a space? public static void main(String[] args) { int[] myArray = new int[5]; myMethod(myArray); for (int i : myArray) { System.out.print(i + " "); } } public static void myMethod (int[] integerArray) { integerArray[3] += 1; } a. 0 0 0 0 0 b. 1 0 0 0 0 c. 0 1 0 0 0 d. 0 0 1 0 0 e. 0 0 0 1 0
0 0 0 1 0
What is the natural ordering of the array below? Integer[] integerArray = {5, 3, 4, 2, 1}; 1, 2, 3, 4, 5 5, 4, 3, 2, 1 5, 3, 4, 2, 1 1, 2, 4, 3, 5 None of the above
1, 2, 3, 4, 5
What is an input/output (I/O) stream? A statement that evaluates a condition before reading in or writing out data. A block of code to handle exceptions that may occur when reading in or writing out data. One of three different clauses (e.g. throws or catch). A sequence of bytes that flow from a source to a destination. None of the above
A sequence of bytes that flow from a source to a destination.
Which of the following are inherited by a subclass? (circle all that apply) a. public methods b. public variables c. protected variables d. protected methods e. constructors
All but constructor
The signature of a method is determined by _____________________. a. name b. number of parameters c. type of parameters d. order of parameters e. All of the above
All of the above
What does the underlined portion of the code below represent? public static void main(String[] args) An array of null values An array of String objects which are null An array of String objects which are command line arguments An array of String objects which are command line arguments with null values An array of String objects which are command line arguments with numeric values
An array of String objects which are command line arguments
Suppose s1 and s2 are objects of the String class, which implements the Comparable interface. If the values of s1 and s2 are "sun" and "son" respectively, what is the expected return value when s1.compareTo(s2) is called? [also: "sun".compareTo("son") ] a. false b. true c. An int value greater than zero d. An int value less than zero
An int value greater than zero
Suppose a variable has been declared with the protected modifier. Which classes can access the variable? Any class Any class within the same package and all subclasses Any class within the same package Only subclasses Only the class it is declared in
Any class within the same package and all subclasses
For class C1 and class C2, suppose class C1 has an instance variable of type C2. An aggregation dependency between class C1 and class C2 is which of the following relationships? a. C1 is-a C2 b. C2 is-a C1 c. C1 uses C2 d. C1 has-a C2 e. C2 has-a C1
C1 has-a C2
Suppose a method in class C1 creates an instance of class C2. A general dependency between class C1 and class C2 is which of the following relationships? a. C1 is-a C2 b. C2 is-a C1 c. C1 uses C2 d. C1 has-a C2 e. C2 has-a C1
C1 uses C2
Suppose class C2 extends class C1. An inheritance dependency is which of the following relationships? a. C1 is-a C2 b. C2 is-a C1 c. C1 uses C2 d. C1 has-a C2 e. C2 has-a C1
C2 is-a C1
Which interface in the Java API should be used to define how objects can be compared for their natural ordering? a. Equality b. Compare c. Logical d. Iterator e. Comparable
Comparable
Assume that each class implements the Comparable interface and creates its own compareTo method. Which one of the following is possible? Staff s = new Comparable(); Doctor d = new Comparable(); MaternityPatient p = new Patient(); Comparable c = new Staff(); Comparable c = new Object();
Comparable c = new Staff();
If you want to define an alternate ordering, what interface should you implement? Orderable AlternateOrderable Comparable Comparator None of the above
Comparator
Java supports multiple inheritance. True False
False
The size of an array object can be changed only within the method in which it was declared. True False
False
True/False: A reference variable of type C can refer to an object of class C or to an object of any subclass of C, as well as any object the parent of C.
False
True/False: A static variable is unique in that the value does not change.
False
True/False: After an array is created, its length can be increased but not decreased.
False
True/False: An abstract method in a super class can never be overridden in any immediate subclass.
False
True/False: An overridden method has the same body but different signature.
False
True/False: Having multiple methods in a class with the same name but difference signatures is called method decompositions.
False
True/False: If a checked exception is not caught or listed in the throws clause and you attempt to compile and/or execute the program, a run-time error occurs?
False
True/False: In Java, abstract methods always end with {}.
False
True/False: In object-oriented programming, a class can be considered as an encapsulation of first-generation and second-generation languages.
False
True/False: The parameter of the main method in Java typically contains the compiler instructions for the program and/or the number of times to run the program.
False
True/False: The protected modifier is used to indicate that a method cannot be overridden
False
True/False: The unchecked exceptions in Java are objects of type IOException or any of its descendants.
False
True/False: When a method creates a Scanner object to read from a file (i.e., a Scanner object created with a File object as the parameter), the potential exception (e.g., FileNotFoundException or IOException) must be handled with try-catch blocks; there is no other alternative.
False
True/False: When a method is called, if an exception occurs and it is not caught and handled in the method, then the exception is printed immediately from the calling method rather being propagated to the calling method.
False
True/False: When a method uses a PrintWriter object to write to a file, simply invoking the println method on the PrintWriter object will ensure that the data is written to the file.
False
True/False: When a try block is executed (and System.exit is not called), the associated finally block (if it has one) will execute only if the try block does not throw an exception.
False
True/False: When an interface contains abstract methods, the methods are considered private.
False
True/False: When the constructor of a subclass is called, the parameterless constructor in the super class is called only when the instance is created by a constructor with parameters.
False
True/False: With respect to inheritance, a subclass inherits instance variables and instance methods defined in its parent but not those defined in the ancestors of the parent.
False
Assume that only the Doctor class has a method called performSurgery(). Will the code below compile? Staff s = new Doctor(); s.performSurgery(); Yes No
No
In Java, a parent class can have a maximum of ______ child classes? a. 0 b. 1 c. 2 d. 3 e. None of the above.
None of the above
What is printed after executing the following loop? public static void forEachPrint() { String[] stringArray = new String[5]; stringArray[1] = " COMP1210 "; for (String element : stringArray) { System.out.print(element.trim() + " "); } } a. COMP1210 b. COMP1210 c. COMP1210 null null null null d. COMP1210 and then a NullPointerException occurs e. NullPointerException occurs
NullPointerException occurs
What term describes having two or more methods in a class with the same name but different signatures? aliasing redundancy hiding refining overloading
Overloading
Which one of the following is using polymorphism correctly? ICUPatient p = new Patient(); Doctor d = new MaternityPatient(); Doctor d = new Staff(); Patient p = new Person(); Person p = new ICUPatient();
Person p = new ICUPatient();
Which of the following streams is typically associated with a program printing to the screen using the println method? a. System.err b. System.in c. System.out d. b & c above e. All of the above (a, b, & c)
System.out
Which of the standard I/O streams is used to print to the console (e.g., Run I/O tab in jGRASP)? System.in System.out System.err
System.out
Assume that each class in the diagram above has its own toString method. What will occur after the following lines are executed? Staff s = new Doctor(); System.out.print(s.toString()); The Patient class toString method is called The ICUPatient class toString method is called The Person class toString method is called. The Staff class toString method is called. The Doctor class toString method is called.
The Staff class toString method is called.
Every Java object inherits from the Object class. True False
True
In a variable length parameter list, the actual parameters are automatically put into an array with the variable name specified in the formal parameter. True False
True
True/False: A public static method can be referenced from another class by using the class name and the dot notation.
True
True/False: A static method in class can never reference an instance variable of the class.
True
True/False: A static variable can be referenced by all methods in the class.
True
True/False: An Exception is an object that describes an unusual or erroneous situation.
True
True/False: An input/output (I/O) stream is a sequence of bytes that flow from a source to a destination.
True
True/False: If an expression that includes integer division by zero (e.g., 10 / 0) is evaluated, an exception is thrown (i.e., run-time error occurs).
True
True/False: If you wanted to sort items in an order other than their natural ordering, the Comparator interface in the Java API should be implemented.
True
True/False: Suppose parent class P has a child class C (i.e., C extends P), and we have the following assignment of a child C object to a reference of the parent type P: P p = new C(); Then any method defined in the parent class P can be called on p without casting.
True
True/False: Testing and debugging are not the same activity.
True
True/False: The protected access modifier can be applied to the fields of a class to allow subclasses and classes in the same package direct access to the fields.
True
True/False: When a method is declared as private, it can be accessed within the class, but it cannot be accessed from outside the class.
True
True/False: When an exception occurs the call stack trace that shows the methods that were invoked along the path to the where the exception occurred.
True
When a class implements the Comparable interface, the compareTo method defines the natural ordering for objects of the class. True False
True
The difference between a checked and an unchecked exception is that _________________. (Select all that apply.) a checked exception must be caught or listed in the throws clause of any method that may throw or propagate it an unchecked exception inherits from RuntimeException and its descendants an unchecked exception must be caught or propagated an checked exception inherits from RuntimeException and its descendants
a checked exception must be caught or listed in the throws clause of any method that may throw or propagate it an unchecked exception inherits from RuntimeException and its descendants
Assume that obj1 and obj2 are instances of a class that has implemented the Comparable interface, and imagine that obj1 is greater than obj2. What will the following method call return? obj1.compareTo(obj2) negative value a positive value zero true false
a positive value
What is unique about an abstract method is that ____________. a class containing an abstract method must be abstract an abstract method has no body a class containing an abstract method cannot be instantiated a and b a, b, and c
a, b, and c
Which of the following is/are true regarding the concepts of overloading and overriding? when overriding, methods have the same signature when overloading, methods have a different signature overriding involves an inherited method and one in the derived class a and b a, b, and c
a, b, and c
A(n) ________________ is a step-by-step process for solving a problem. requirements unit test class test class algorithm
algorithm
What are the benefits of inheritance? avoiding redundancy code reuse testing maintainability all of the above
all of the above
The Comparable interface includes which of the following abstract methods? compare equals compareAll compareTo compareToIgnoreCase
compareTo
Regarding a step-by-step process for solving a problem, breaking a "step" into smaller steps is called ___________________. composition decomposition synthesis resynthesize summarizing
decomposition
In Java, what reserved word is used to establish an inheritance relationship? protected super extends abstract final
extends
import org.junit.Assert; import org.junit.Test; public class PracticeExamJUnitTest { @Test public void firstTest() { String s = "hello"; String s2 = "Hello"; Assert.assertTrue("Strings did not match. ", s.equals(s2)); } @Test public void secondTest() { double closeToZero = 0.00001; double zero = 0; Assert.assertEquals("Second test failed. ", zero, closeToZero, .0001); } @Test public void thirdTest() { int[] listOfInts = {1, 2, 3, 4, 5}; int[] listOfOtherInts = {1, 2, 3}; boolean isPassing = listOfInts[4] % 4 == 0; Assert.assertTrue("There was a problem in third test. ", isPassing); } @Test public void fourthTest() { int[] listOfInts = {1, 2, 3, 4, 5}; Assert.assertTrue("Something went wrong. ", listOfInts[3] / 4 == 1); } } What is the result of firstTest?
fail
import org.junit.Assert; import org.junit.Test; public class PracticeExamJUnitTest { @Test public void firstTest() { String s = "hello"; String s2 = "Hello"; Assert.assertTrue("Strings did not match. ", s.equals(s2)); } @Test public void secondTest() { double closeToZero = 0.00001; double zero = 0; Assert.assertEquals("Second test failed. ", zero, closeToZero, .0001); } @Test public void thirdTest() { int[] listOfInts = {1, 2, 3, 4, 5}; int[] listOfOtherInts = {1, 2, 3}; boolean isPassing = listOfInts[4] % 4 == 0; Assert.assertTrue("There was a problem in third test. ", isPassing); } @Test public void fourthTest() { int[] listOfInts = {1, 2, 3, 4, 5}; Assert.assertTrue("Something went wrong. ", listOfInts[3] / 4 == 1); } } What is the result of thirdTest?
fail
Suppose that you want to restrict inheritance for class A (i.e., class A can have no subclasses). What Java reserved word would you use? protected super extends abstract final
final
Which one of the following is a correct declaration and instantiation of an array? a. int[] scores; b. int[] scores = new int[1.5]; c. int[] scores = {"low", "high"}; d. int[] scores = new int[15]; e. All of the above
int[] scores = new int[15];
Which one of the choices below initializes an array? int[4] units = {147, 323, 89, 933}; int[] units = {147 323 89 933}; int[] units = 147, 323, 89, 933; int[4] units = 147 323 89 933; int[] units = {147, 323, 89, 933};
int[] units = {147, 323, 89, 933};
18-21 see study guide
junit questions
After the line of code below is executed what will be the value of names[7]? String[] names = new String[10]; a. 0 b. " " c. true d. 7 e. null
null
Imagine there is an array called numbers. Which of the expressions below evaluates to the number of positions in the array? numbers.size numbers.length numbers.elements numbers.slots numbers.numberOfElements
numbers.length
After the following two statements are executed, which of the expressions below will return true? Select all that apply. Doctor d = new Doctor(); Patient p = new Patient(); d instanceof Patient instanceof Person d instanceof Doctor d instanceof Staff p instanceof Staff
p instanceof Person d instanceof Doctor d instanceof Staff
22. For the code below, you desire to print the last parameter of the variable length parameter. Fill in the blank below to accomplish that. public static void printTheLastParameter(int ... parameterList) { System.out.println(__21__); } a. parameterList b. parameterList[end] c. parameterList[length] d. parameterList[parameterList.length - 1] e. parameterList.get(last)
parameterList[parameterList.length - 1]
import org.junit.Assert; import org.junit.Test; public class PracticeExamJUnitTest { @Test public void firstTest() { String s = "hello"; String s2 = "Hello"; Assert.assertTrue("Strings did not match. ", s.equals(s2)); } @Test public void secondTest() { double closeToZero = 0.00001; double zero = 0; Assert.assertEquals("Second test failed. ", zero, closeToZero, .0001); } @Test public void thirdTest() { int[] listOfInts = {1, 2, 3, 4, 5}; int[] listOfOtherInts = {1, 2, 3}; boolean isPassing = listOfInts[4] % 4 == 0; Assert.assertTrue("There was a problem in third test. ", isPassing); } @Test public void fourthTest() { int[] listOfInts = {1, 2, 3, 4, 5}; Assert.assertTrue("Something went wrong. ", listOfInts[3] / 4 == 1); } } What is the result of secondTest?
pass
import org.junit.Assert; import org.junit.Test; public class PracticeExamJUnitTest { @Test public void firstTest() { String s = "hello"; String s2 = "Hello"; Assert.assertTrue("Strings did not match. ", s.equals(s2)); } @Test public void secondTest() { double closeToZero = 0.00001; double zero = 0; Assert.assertEquals("Second test failed. ", zero, closeToZero, .0001); } @Test public void thirdTest() { int[] listOfInts = {1, 2, 3, 4, 5}; int[] listOfOtherInts = {1, 2, 3}; boolean isPassing = listOfInts[4] % 4 == 0; Assert.assertTrue("There was a problem in third test. ", isPassing); } @Test public void fourthTest() { int[] listOfInts = {1, 2, 3, 4, 5}; Assert.assertTrue("Something went wrong. ", listOfInts[3] / 4 == 1); } } What is the result of the fourthTest?
pass
What is the result of running the code below? int[] scores = new int[5]; for (int i = 0; i <= scores.length; i++) { System.out.println( scores[i]); } prints five zeros prints five ones prints four zeros prints six ones and then a runtime error occurs prints five zeros and then a runtime error occurs
prints five zeros and then a runtime error occurs
In a class, a support method should be declared as _______________? public static void private None of the above
private
In a class, a service method should be declared as _______________. public static viod private None of the above
public
In Java, interfaces may not contain _____________________. Check all that apply. a. abstract methods b. constants c. public instance variables d. private instance variables e. private methods
public instance variables private instance variables private methods
A method's signature does not include which of the following? return type name of method type of each parameter order of parameters number of parameters
return type
Static methods and instance methods are different in that _______________________. (Select all that apply). static methods can access instance fields and static fields. static methods can access static fields, but not instance fields. instance methods can access instance fields but not static fields. instance methods can access instance fields and static fields.
static methods can access static fields, but not instance fields. instance methods can access instance fields and static fields.
Static variables and instance variables are different in that static variables have the same value across every instance. static variables cannot be changed once initialized. instance variables have the same value across every instance. instance variables cannot be changed once initialized. you are limited in the number of static variables you can declare.
static variables have the same value across every instance.
To call a parent method that has been overridden, what Java reserved word should you use? protected super extends abstract final
super
When a class implements an interface with an abstract method, the class must include the header of the method the class must include the header and body of the method the class must include at least one other method that calls the method in the interface All of the above None of the above
the class must include the header and body of the method
An abstract method in an interface includes ___________________. the method header but no body the method header and body the method header and an empty body All of the above None of the above
the method header but no body
What Java reserved word allows an object to refer to itself within a method? itself class new goto this
this
An exception can be explicitly thrown in a method by using the reserved word _________ along with the specific Exception object to be thrown. throw throws catch try and catch synchronized
throw
The code below is an example of what kind of conversion? Staff s = new Doctor(); broadening narrowing constricting widening None of the above
widening