- T/F
-- Static typing (not to be confused with the keyword "static") can reduce programming errors and improve the run-time.
-- The following code will print out 1:
String[] strings = new String[10];
strings[0] = "banana";
System.out.println(strings.length);
-- Given a class B that extends class A, the following code is legal (will compile and run) assuming B has a zero parameter constructor:
ArrayList a = new ArrayList();
B b = new B();
a.add(b);
- Given the following class:
public class NumberHolder{
private int num;
public NumberHolder(int someNum){
num = someNum;
}
public int getNum(){
return num;
}
}
write a compareTo method that takes another NumberHolder as a parameter and compares the two numerically.
- What is the benefit of using the "final" keyword?
- What does this method do and what is the big-O runtime?
public String mystery(ArrayList input ){
String mysteryVar = "";
int c = 0;
for( int i = 0; i < input.size(); i++ ){
int num = 1;
for( int j = i+1; j < input.size(); j++ ){
if( input.get(i).equals(input.get(j))){
num++;
}
}
if( num > c ){
c = num;
mysteryVar = input.get(i);
}
}
return mysteryVar;
}
- What does the main method in the Mystery class below print out?
public class Mystery {
private int mystery;
private int original;
public Mystery(int m){
mystery = m;
original = m;
}
public void mystery1(int num){
mystery += num;
}
public void mystery2(){
mystery = original;
}
public boolean mystery3(){
return mystery == original;
}
public String toString(){
return "The mystery number is: " + mystery;
}
public static void main(String[] args){
Mystery m = new Mystery(0);
System.out.println(m);
System.out.println(m.mystery3());
System.out.println("--");
m.mystery1(10);
System.out.println(m);
System.out.println(m.mystery3());
System.out.println("--");
m.mystery2();
System.out.println(m);
System.out.println(m.mystery3());
System.out.println("--");
}
}
- Write a method called reverseFile that reverses the ordering of the lines in a file. (Hint: use an ArrayList).