CSC 222- Final Exam
Given the that s is defined with the line: String s= "snow day"; What would be returned by s.charAt(3)?
'w'
What do you use to test if two Strings have the same value?
.equals
What is the value that will be store in variable x when the following code is executed? double x = 4/5;
0.0
In the Die class, the method getNumSides returns the field numSides. This is an example of a:
Accessor Method
In your function, you declare variable x to be type int. You can later in the function assign the value "Hello" to x.
false; cannot assign a string to int variable
In a Model-View-Controller design pattern, the model classes are responsible for handling the input and output from the view
false; controller
If a method does not return a value, the return type should be none
false; does not need to contain a return statement
If you want to run you code multiple times, unlike with a compiler, an interpreter is only need once.
false; interpreters translate one instruction at a time, and then execute that instruction immediately (so it runs multiple times)
Good software is tightly coupled
false; loosely coupled
You can use binary search on the following list without needed to re-order it first: 1, 7, 3, 10, 6, 5
false; must be ordered
In Java, to get the length of the string, s, you would use: len(s)
false; s.length()
The add method of ArrayList adds an item to the beginning of the list.
false; to the end of a list
Which keyword do you use to designate that a variable cannot be changed
final
Describe the binary search algorithm
finds the position of a target value within a sorted array
Which ArrayList method would you use if you wanted to determine where in the list a specific value was stored?
indexOf(Type obj)
What is a closely related mechanism for polymorphic behavior, allows for large-scale code reuse
inheritance (ex. SavingsAccount class inheritcs common features of BankAccount class- public class SavingsAccount extends BankAccount {)
Write the method that returns the sum of the items in an ArrayList<Integers>. /** * Returns the sum of the items of the ArrayList * @param nums the ArrayList to sum * @return the sum of the values in the ArrayList public int sumItems(ArrayList<Integer> nums){ //Your code here }
int Sum = 0 for( i= 0; i>nums.size(ArrayList); i++){ Sum += i } return Sum; }
If you want to write a for loop where i goes from 1 to 10, what should go inside the parenthesis below? for (INSERT CODE HERE){ ... }
int i = 1; i<11; i++
What is this an example of: public class myList<E> implements List<E> {. . .})
interface
what is this and example of: List<String> words1 = new ArrayList<String>();
interface using a variable of the interface type to refer to any object that implements it
this is an example of: public void DoSomething(List<String> words) { . . .}
interface using interface type for a parameter and pass any object that implements it
What is being described? an object of the derived class is still an object of the parent class
is-A relationship
In the Model-View-Controller Design pattern, what is the Cave class from Hunt the Wumpus an example of?
model
What enable the creation of generic libraries (ex. Collections)?
polymorphism
What is this an example of: BankAccount acc1 = new SavingsAccount(4.0); BankAccount acc2 = new CheckingAccount(); acc1.deposit(100.0); acc2.deposit(100.0);
polymorphism (the same method call's ability to refer to different methods when called on different objects)
What keyword is used to limit access to a method to within the class and prevent users from calling it?
private
Generics provide a way for you to write code where the type of the object is specified when it instantiated
true
If you assign an object to an interface type (e.g. UnoCard c = new ActionCard("skip")), then you can only call methods defined by the interface
true
In order to sort an ArrayList, the the items stored in the ArrayList need to be comparable
true
Insertion sort is O(N^2) in the worst case
true
It is a bad idea to use "magic numbers" because it is not clear what they represent, which decreases the readability of the code.
true
Java can automatically type-cast an int into a double, but not a double into an int.
true
The Scanner class can be used to read in a file as input
true
The nextInt(parameter) method will generate a random integer between 0 (inclusive) and parameter (exclusive)
true
True/false: in polymorphism the same method call (ex. get or size) can refer to different pieces of code when called on different objects
true
Unlike in BlueJ where to perform actions you manipulate objects directly, in most cases you will create a "driver" class with a main method that is called automatically.
true
When two Integers are stored in an ArrayList, you cannot use == to determine if their values are the same
true
While merge sort and quick sort are both O(N log N) algorithms, quick sort tends to be faster than merge sort
true
You can use the += shorthand for both Strings and numbers
true
true/false: Only public and protected methods can be called by the super keyword.
true
true/false:inheritance can derive a new class from an existing one by automatically inheriting all of the fields and methods of the existing class
true
True/false: you can extend a class without any knowledge of its internals
true; only need to know which methods to override
What type of Java statement should you use if you want your code to handle an exception, such as the java.io.FileNotFoundException
try-catch
In the Model-View-Controller Design pattern, what is the Terminal Window from Hunt the Wumpus an example of?
view
What is printed by the following code? ArrayList<String> words = new ArrayList<String>(); words.add("private"); words.add("public"); words.add("void"); words.add("int"); words.add("double"); words.add("String"); words.add("Die"); System.out.println(words.get(2)); int num = words.size(); System.out.println(num); if(num > 6){ System.out.println(words.get(6)); } System.out.println(num);
void 7 Die 7
Which of the following is not true of parallel lists? A. You can make an ordering change in one list without needing to make an ordering change in another list B. They do not promote a highly cohesive system C. Elements at the same index in different lists are related D. When re-ordering parallel lists, you need to make sure any change you apply to one list applies to all
A
What would be printed if the following method were called. Make sure you use the correct formatting given the code (e.g. whether the values are on the same line or different lines or whether they are ints or doubles). public void quiz2() { int n = 9; while (n > 0) { if (n % 2 == 0) { System.out.println( n ); } else { System.out.println ( n + 1 ); } n = n - 3; } }
10 8 6
Given the following method, what is printed when I call the method with 7 as the parameter? public void quiz1(int num){ num = num/2; System.out.println(num); num = (int) ( Math.round( (double) num/2 ) ); System.out,println(num); num = num*num + 1; System.out.println(num); }
3 2 5
Which of the following conditions evaluates to true? A. ! (4 > 6) B. (4 == 5) || (5 > 5) C. (4 != 5) && ( 6 >= 10) D. (4 > 5) && (6 <= 6)
A
Which of the following statements will not have an error if this is the first instance of the variable being declared? A.int x = 5; B. int y = 8 C. int z = 4.5; D. char let = "a";
A
Which of the following types of variables is the best match for the following description: they are initialized when the method is called, only accessible within the method and only accessible while the method executes A. Global Variables B. Local Variables C. Parameters D. Fields
C
Which of the following is not true about enumerated types? A. You specify the value by ENUMTYPE.VALUE B. There is no ordering with enumerated types and therefore, compareTo cannot be called on them C. By convention, the values of enumerated types are in all caps D. Invalid input (e.g WOMPUS instead of WUMPUS) are caught by the compiler
B
Which of the following is not true about recursion A. There is some overhead involved in recursion due to the additional method calls, but it is usually relatively small B. It uses loops to solve the problem C. The efficiency of a recursive algorithm may be different than the efficiency of the iterative counterpart D. Any problem that can be solved recursively can also theoretically be solved iteratively
B
Which of the following is the correct syntax for a for-each loop to go through an ArrayList of Strings called states? A. for-each(String state : states) B. for( String state : states) C. for-each(state in states) D. for(state in states)
B
Which of the following is not true about interfaces? A. The interface is used to express the commonality between classes B. The interface allows us to create methods that will work on any object that implements that interface C. The interface implements all the methods included in the interface D. The interfaces provides a list of methods that must be implemented by any class that implements that interface
C
Which method for reading input is best if you want to read in data that is numbers with decimals? A. next() B. nextLine() C. nextInt() D. nextDouble()
D
Which of the following is not a reason why using System.out.format may be better than rounding the value before returning it in a function A. You can control not only the number of decimals of a value, but also the spacing B. System.out.format also allows you to format Strings C. Some functions may need the exact value D. System.out.format permanently changes the variables value
D
Which of the following is not important when determining if a recursive algorithm successfully solves the problem? A. The base case is handled correctly B. The results from the recursive calls are combined properly C. Each recursive call gets closer to the base case D. The amount of work done in the recursive call is small
D
Which of the following is not true about the isLetter method in the Character class? A. It returns true if ch is a letter B. It is a static method C. It should be called on the class Character, not a specific instance D. It should be called on a Character object created from the char value
D
Which of the following is not true of arrays A. The size of the array is fixed at creation B. There is no need for autoboxing or unboxing as you can store primitive types, like ints, directly C. You can access items using the [] notation D. Arrays are more abstract than ArrayLists
D
You can only add an item to the end of the ArrayList
false
You must manually convert from an int to an Integer when storing the int in an ArrayList<Integer>
false;
The Compareable interface requires the class to implement the equals method.
false; Comparable method
public enum Day { MON, TUES, WED, THURS, FRI, SAT, SUN } What is the output of the following code, given the above enum definition? Day d1 = Day.MON; Day d2 = Day.SAT; if (d1.equals(d2)) { System.out.println("Hello"); } else { System.out.println("Goodbye"); } if (d1.compareTo(d2) < 0) { System.out.println("Snow"); } else { System.out.println("Rain"); } System.out.println(d1);
Goodbye Snow MON
How are inheritance and interfaces similar?
Inheritance holds the same capabilities as interfaces: (- can assign a SavingsAccount object to variable of type BankAccount - can pass a CheckingAccount object to a method with a BankAccount parameter)
The big-oh of merge sort is N2
false; O(n log n)
The String methods modify and replace the String that they are called on.
false; String methods create and manipulate the String they are called on
Sequential search is faster than binary search
false; binary search is faster
The state of an object is:
The collection of methods that define the behavior of an object
The comment at the top of a class should include:
The description of the class and the author and version
You have a file full of data that you want to store in an ArrayList. Each line of the file has two pieces of related data that need to be kept together. What is the best option for storage?
Use parallel lists to store the data
Given the following code to build an ArrayList: ArrayList<String> states = new ArrayList<String>(); states.add("Iowa"); states.add("Indiana"); states.add("Washington"); states.add("Nebraska"); Which state is removed by the call states.remove(2)
Washington
How can inheritance add new functionality?
add fields or methods
How well code maps to a single entity or behavior is a measure of its
cohesion
What is inheritance used to express?
commonality between classes
In an is-A relationship, anywhere an object of the parent class is expected, a ___________ can be provided.
derived object
If you wanted to assign 4.5 to a variable, what is the type you want give the variable?
double
A for loop cannot be nested inside an if statement
false
Classes are typically private
false
Different instances of the same class must have the same values for the properties of the class
false
If a class implements an interface, you are not allowed to add any methods to the class that are not declared in the interface
false
In order to use the Java Standard Library, you must see and understand exactly how the classes are implemented
false
The else is required when you have an if statement
false
Given the String methods: String toUpperCase() returns copy of String with all letters uppercase String toLowerCase() returns copy of String with all letters lowercase In the space below, complete the method based on the documentation /** * Will change the str to uppercase if it (the string) is an even length or lower case if it is an odd lenght * @param str the String to transform * @return str in uppercase if length is even or lowercase if length is odd */ public String changeCase(String str) {
public String changeCase(String str) { if (str.length()%2 = 0){ return str.toUpperCase(); }else{ return str.toLowerCase(); } }
What is the correct first line of the method declaration (the line that includes the name) for the constructor of a Die object with 1 parameter (the number of sides)
public void Die( int numSides )
Which sorting algorithm is described below? It picks a pivot element from the list Partitions the list so that all elements <= the item are to the left of the pivot and all items > than the pivot are to the right Recursively sorts the partitions
quick sort
Which sorting algorithm is described below: Find the smallest item and put it in the 1st spot Find the 2nd smallest item and put it in the 2nd spot Find the 3rd smallest item and put it in the 3rd spot ...
selection sort
Which method is used to determine how many items are in the ArrayList
size()
What is used inside a sub-class method definition to call a method defined in the super class.
super
Which location in a list is the first spot the sequential search algorithm will check for the desired item?
the first item in the list
What happens when an interface defines a set of methods that a class must implement and any class implements those methods
the interface is "certified"
What can interfaces use in place of a class name when declaring variables of passing values to methods?
the interface name (ex. List<String> words = new MyList<String>(); public int sum(List<Integer> data) { int sum = 0; for (int i = 0; i < data.size(); i++) { sum += data.get(i); } return sum; } )
Which location in a list is the first spot the binary search algorithm will check for the desired item?
the middle item in the list
Inheritance is utilized to...
to build off the existing class (add, set, indexOf)
A mutator method is a method that changes the objects state.
true
A recursive algorithm is one that refers to itself when solving a problem
true
A while loop is the best choice when you want the body to execute as long as n is positive?
true
An enumerated type is used to list all the possible values
true
ArrayLists must specify the type of object stored and can only store that object type
true
Big-Oh notation describes the rate-of-growth of the algorithm in the long run (e.g. when the size of the problem is large)
true