CS180 Final Review: MC Quiz ?'s
When reading from a file, the file must exist.
True
Evaluate the following RPN Expression: 11 22 + 33+
66
When using a try-with-resources statement, what block is implied?
Finally Block
Why in the MyArrayList class does the method reallocate() have a private access modifier?
It is not to be run by someone who instantiates a MyArrayList object. --> A private method is hidden from someone who instantiates a MyArrayList object.
JVM is the acronym for
Java Virtual Machine
Which is correct about a HashMap<K,V> variable named map?
K is like an array subscript and V is like the value at that subscript location.
When reading from a file what do you have to do before reading?
Make sure the file exists
Which of these exception(s) is unchecked, almost always?
NullPointerException
In which of the following classes is the clone() method defined?
Object
All the following are allowed overloads of method... void magnify (int x, String y, double z)... except which one?
int magnify (int a, String b, double c) differs only in return type, not a valid overloading
What is the return type of the method? static int[][] makeMatrix(int[] arr1, int[] arr2) throws ArrayIndexOutOfBoundsException { //some rather interesting code goes here... }
int[][]
What is the name of the node with a parent and a child?
interior node
Which of the following is considered to be part of the method signature?
method name
A class that implements an Interface must have...
methods with the same signature and the same return type as the methods declared in the Interface
Where is the best place to run the JFrame?
on the Event Dispatch thread
What is the default accessor modifier for a method?
package-private
Suppose you have two constructors, one that takes an integer, and one that takes an integer and a String. How can you call the two parameter constructor from the one parameter constructor? Assume the integer is called num, and the String, str.
this(num, str)
If you have a binary tree that you must traverse, which is the most efficient way of traversal?
All traversal methods are just as efficient as the others.
What is another name for Hashmaps?
Associative Arrays
Which of these statements about Sockets and ServerSockets is correct?
Both clients and servers use Sockets. A server uses a ServerSocket to wait for a client to appear.
Evaluate the following RPN Expression: 30 x 400 1560 +
Cannot compute RPN expression (operands are out of order)
What do you have to do to ensure that data is written to a file every time?
Ensure that you flush after every write statement
What is the name of the thread on which all GUI based interactions between the user and the program should occur? (HINT, the abbreviation is EDT).
Event Dispatch Thread
Every class in Java possesses the toString() method (even if it is not explicitly defined) due to which of the following reasons?
Every class extends the Object class.
In Java an abstract class cannot extend another abstract class.
False
The sum of two positive integers that is computed using Java will always be greater than both of the integers.
False
True or False: Arrays can only be a maximum of 3 dimensions.
False
What is a difference between an abstract class and an interface?
Fields in an interface are public final static (i.e., constants). Fields in an an abstract class can be private.
What does the <> brackets in a definition of an ArrayList mean?
Generic brackets defining data type
Which of the following statement(s) is/are incorrect? I. PrintWriter will always overwrite the contents of the file. II. BufferedReader can be used to write across a network stream. III. BufferedWriter can only be used with files.
I, II, III
How many times does the following loop iterate? for (double i = 1/8; i < 1; i *= 2) { //Do something }
Infinite loop
What exception needs to be caught when using the method sleep() from a Thread object?
InterruptedException
Which of the following statements best describe(s) encapsulation?
It involves hiding implementation details from other programmers. It prevents other programmers from modifying certain fields or calling certain methods.
An algorithm must be "complete". What does that mean?
It must handle anything that comes up. --> This means that it is complete.
When writing a method with the same name and same signature as that of the super class in a child class, what is this concept called?
Method Overriding
What is the name of the exception that is thrown if the file cannot be found, when WRITING data?
No exceptions are thrown.
Which of these interfaces require implementation of 0 methods?
Serializable
Wheel wagon = new Wheel (17.5); What does the variable wagon contain?
The address in memory where the Wheel object is stored
Which of the following follows from the rule "Catches must be ordered from the lowest subclass to the highest superclass"?
The catch handler for the Exception class should be the last catch handler after the try block. -> The Exception class catch handler will work for any exceptions. Put it last!
What is the primary benefit of using buffers for File I/O?
The program runs faster. --> Performing fewer physical reads speeds up the program.
A class can have multiple methods with the same name.
True
How can you ensure that resources are shut off before program end?
Use the reader's close() method, or the equivalent. Use a try-with-resources statement
When should you use recursion?
When the solution is going to take a lot of loops or you cannot determine how many loops you will need.
Can you have two methods with the same name in the same class?
Yes, provided that each methods signatures are different
Which one of the statements about Java GUIs is true?
You can change the JFrame's layout manager to be GridLayout.
After evaluating the following statements, which option is correct? boolean a = true && false; boolean b = true || false;
a will be false
If no access modifier is specified on a class, field or method, it defaults to public.
false
The following loop will never terminate. for(int i = 0; i < 10; i--) { // Do something }
false
The following statement is valid in Java: LinkedList<double> linkedList = new LinkedList<>();
false
When writing to a file, the file must exist.
false
If one wanted to compile a file called fileA.java into a Java executable using the command line, which of the following commands would they execute? Assume that the user is already inside the src folder.
javac fileA.java
What is the name of the node with no children?
leaf node
Which of these methods do you use for making a Thread wait until another one finishes? Assume your thread object is called t.
t.join()
A class can implement multiple interfaces.
true
What is the output after printing the variable num? double num = 3 / 4;
0.0
Consider the following interface: public interface Foo { void bar(); // A public void barBar(); // B abstract void barBarBar(); // C public abstract void barBarBarBar(); // D } Which method declarations shown above are valid?
All of them
What is the most common behavioral interaction between two computers performing I/O Tasks over a network?
Client-Server interaction
In class Robot is a method that begins... public static void main(String[] args) { Which best describes method main()?
Method main() is where program execution begins. --> Without a main method, your class is not a program.
When performing an append operation on a file during the writing process, which are valid ways of doing this?
Read all the data into an ArrayList, and append the data into the list, which you would then overwrite the file with. Write to the file using the append operation of a writer that supports this Do a check on whether the file already has data in it - if it does, read that in, and then write new data to the file, including the old data.
How do you use a sentinel (flag) boolean variable to convert a do...while loop to a while loop?
Start the sentinel boolean variable as true and change it to false later.
If you have a binary tree with a root node that is null, what can you infer from this?
The node has no children
What is a class?
a blueprint for an object
What value does this expression return? (double) (1/2) * 8
0.0 Remember that what is in parentheses will be done first. (1/2) is integer division and (because of truncation) is 0. The (double) cast will change that to 0.0 and, of course, 0.0 * 8 is still 0.0.
Which of the following assertions about method overriding is true?
A derived class can override a method of the base class
Which one of these is correct about return statements?
A method does not need a return statement if it is a method with a void return type, but it may have one or more. In this case the return statements are used to leave the method from different points... like early if something goes wrong.
If you don't read data in the same order it was written in, what will happen?
OptionalDataException thrown
What is the method used for removing an item from the top of a Stack? Assume your stack object is called stack.
stack.pop()
Examine the following code snippet: String str = "text to substring from"; str.substring(18); System.out.println(str); What will the code snippet print?
text to substring from
What is the value of this expression? Integer.MAX_VALUE + 1 == Integer.MIN_VALUE
True
Given a binary tree like the following, which is the correct inorder traversal of the tree?
4-2-5-1-3
Evaluate the following RPN Expression: 42 42 + 42 + 180 + 180 3 * + 10 +
856
What is a proxy method?
A non-recursive method that calls the recursive method.
If you needed an object to read from different text files, which classes could you use? PrintWriter PrintReader BufferedReader FileReader FileInputStream
BufferedReader FileReader FileInputStream
Which of the following will not result in a StackOverflowException being thrown?
Calling a method from inside itself with a given base case.
Which of the following is NOT considered a primitive type in the Java Programming Language? boolean Double Boolean double Character
Double, Boolean, Character
What is the concept called for Java deciding which method to use when incorporating a list of an abstract class?
Dynamic Binding
The following statement is valid in Java: HashMap<Integer, String> hashmap = new HashMap<>();
True
True or False: Arrays have set sizes.
True
When working with super classes, what is the modifier used to indicate that a method must be written in a child class?
abstract
Which of the following is the loop where at least one execution is guaranteed?
do-while
Which of the following is the modifier to determine an unchangeable, final, value?
final
Which of the following loops is best for handling large data sets with unknown size?
for-each
What is the name of the node with no parents?
root node
Which of these methods do you use for making a thread wait for a specified amount of time before resuming?
t.sleep(...)
Suppose that r is an object of the Random class and that the species array was declared as follows... String[ ] species = { "pine", "elm", "spruce", "oak", "walnut", "cedar", "maple" }; What are all the possible values that r.nextInt(species.length) can return?
0, 1, 2, 3, 4, 5, 6 r.nextInt(int n) returns a value in the range [0-n) ... that is ... 0, 1, 2, ..., n-1.species.length is 7.So, r.nextInt(species.length) can return any of the values 0, 1, 2, 3, 4, 5, 6.
Assume you have this code in your project: String eclipse = "Not Intellij!" What does eclipse.length() return?
13
Consider the following code snippet: public static int euclid(int a, int b) { if (b == 0) { return a; } return euclid(b, a % b); } What will the above code result in, given the method call euclid(105, 30)?
15
Stack or queue: What is the best data structure (and why) for keeping threads waiting to get a core (CPU) to run?
A queue so that eventually each thread will move from back to front and get a core. --> We want threads to get a fair chance at running.
When making a thread halt execution for a specified amount of time, what do you have to do to ensure your program doesn't crash after the thread "reawakens"?
Catch an InterruptedException
Which of these is an example of Overriding?
Class Person has a method changeAddress() and Class Student (a subclass of Person) also has a method changeAddress() with the same signature --> Overriding is when there is a method with the same signature in a subclass
Static binding occurs at
Compilation time
Searching for a Purdue student in one of four files simultaneously is an example of...?
Domain Decomposition The only two types of Decomposition discussed were: Task Decomposition -- Split the task into multiple subtasks. Each subtask runs different code. Domain Decomposition -- Each subtask runs the same code, but on different input. (Searching for a student in several files simultaneously would be done by using the same code -- run() method -- for different files.)
True or False: If no access modifier is specified on a class, field, or method, it defaults to public
False if not specified... special access modifier called "package private" (not denotable, can't write it out); classes in the same package/interface/etc. can access the member it is "package private", so other classes/interfaces within same package can access member
What is the name of the exception that is thrown if the file does not exist, when READING data from the file?
FileNotFoundException
When using a standard try-catch clause, what is guaranteed to run after every try-catch statement?
Finally Block
Strings are...
Immutable Objects Not Primitive Types Made up of letters
ArrayList<String> list1 = new ArrayList<String>(); declares an ArrayList of Strings named list1. String[ ] list2 = new String[10]; declares an array of Strings named list2. Assume that both list1 and list2 contain the same five strings.Which statement below does the same thing as...?list1.add(3,"Tricky");
None of the above. In an ArrayList add(i,e) adds e at index i, pushing other values down. So, list1.add(3,"Tricky"); adds "Tricky" at index 3, pushing what had been at 3 to 4 and what had been at 4 to 5. The ArrayList now has six strings. The Java array has no add method or anything else that does this.
1 int x = 0; 2 System.out.println(x++); 3 System.out.println(++x); What does this code display on screen?
Prints 0, then 2 In statement 1, x is given the initial value 0.In statement 2, x++ is a post-increment, meaning that the statement is executed as if the ++ were not there (printing 0) and then x is incremented, making it 1.In statement 3, ++x is a pre-increment, meaning that x is first incremented, making it 2 and then the statement is executed (printing 2).
What exception will be thrown if the client you write from changes between writes?
StreamHeaderException
Which of the following is the data type used for holding a single character?
Strings char int (answer is all of the above)
Suppose that a main method encounters the statementt3.join();Suppose that thread t3's run() method has completed running. What happens?
The main method proceeds to the next statement following the t3.join(); statement t3.join();means "wait here until t3 has completed running and then go on to the next statement".
Which of the following is not a loop in Java?
for-every
Which JFrame method puts all the components in their preferred space based on the selected layout manager? Assume your JFrame object is called frame
frame.pack()
Accessor and mutator methods are also called __ and __.
getters, setters
Suppose that s is a string variable pointing to the string "Indianapolis". What string does s.substring(3,8) return?
ianap
Which of the following is the correct way to test if two string variables point to strings that say exactly the same thing?
if (school.equals(university)) run the equals method for the school object comparing it to the university object
Which of the following statements correctly checks if a String object str is null?
if (str == null)
int tryAgain = JOptionPane.showConfirmDialog(null, "Do you want to try again?", "Alert", JOptionPane.YES_NO_OPTION); If you have the statement above in your program, which of the following is the best way to check if the user clicked the NO button?
if (tryAgain == JOptionPane.NO_OPTION)
What are the parameters of the method? static int[][] makeMatrix(int[] arr1, int[] arr2) throws ArrayIndexOutOfBoundsException { //some rather interesting code goes here... }
int[] arr1, int[] arr2
What is the method signature of the makeMatrix method? static int[][] makeMatrix(int[] arr1, int[] arr2) throws ArrayIndexOutOfBoundsException { //some rather interesting code goes here... }
makeMatrix(int[] , int[])
The following is legal in Java because... private void incrementByValue(int num) { //some rather interesting code here... } private void incrementByValue(double num) { //some rather interesting code here... }
method overloading
What is the accessibility modifier of the method? static int[][] makeMatrix(int[] arr1, int[] arr2) throws ArrayIndexOutOfBoundsException { //some rather interesting code goes here... }
package-private
Which of the following is the default accessibility modifier of a method?
package-private