Recursion
File I/O
For loops
1D arrays
2D arrays
100

What are the two things that a recursive method needs?

A recursive method must have a base case, and a recursive call. The recursive call should be called on a simpler instance of the same problem.

100

When you are trying to read from a file and add the line "Scanner scnr = new Scanner(file);" what must you also do?

Surround the line with a try-catch block, or add a "throws FileNotFoundException" to your method signature.

100

How many times will this loop run?

for (int i = 0; i < 9; i += 2) {
      /*statements*/              
}                                         

This loop will run when i is equal to 0, 2, 4, 6, and 8, so 5 times.

100

What is the output of the following code?

boolean[] wins = {true, false, false};

System.out.println(wins);                

This will print the address of the array "wins" in memory. In order to print its contents, we need to iterate with a loop.

100

Given an array
double[][] hello = new double[100][50];
How do you access the last element?

hello[99][49]

200

What happens if the base case is never met?

stack overflow

200

What is the error in the following code?
File fileObj;                          
Scanner inFS;                      
int[] num;                          
int numElem = 0;                 
inFS = new Scanner(fileObj);
numElem = inFS.nextInt();   
num = new int[numElem];    

The underlying file has not been properly initialized. It needs to be opened with fileObj= new File("myfile.txt"), for example.

200

What will be the output of the following java-like pseudocode?

for (int i = 19; i > 0; i /= 2) {
     S.O.Pln("Hi " + i);                    
}                                           

Hi 19
Hi 9
Hi 4
Hi 2
Hi 1

200

Given:                              

int[] yearsArr = new int[4];
yearsArr[0] = 1999;          
yearsArr[1] = 2012;          
yearsArr[2] = 2025;          

What value would it get assigned to the following variable: int currYear = yearsArr[3]?

0
When yearArr is created, all array elements are initialized with the default int value, or 0. So, currYear is assigned with 0.

200

Write a method that prints the contents of a 2D int array an element per line

public static void print(int [][] a) {        
    for(int i=0; i < a.length; i++) {        
        for(int j=0; j < a[i].length; j++) {
           S.O.Pln(a[i][j]);                      
        }                                                
    }                                                    
}                                                         

300

Write a method that recursively determines the nth fibonacci number.
Remember, f0 = 0, f 1 = 1, and fn = fn-1 + fn-2 for every n greater than 1.

300

Given the following code, what is missing to correctly append "Midas" to an existing file outfile.txt?
         
PrintWriter outFS =    new PrintWriter("outfile.txt");

FileWriter fw = new FileWriter("outfile.txt", true);
PrintWriter outPW =    new PrintWriter(fw);        
outPW.println("Midas");                                     
outPW.close();                                                  

300

Give an example of a for loop that would print "hi" infinitely

for(int i=1; i!=4; i+=2){
    S.O.P.("hi");              
}                                   

300

Design a method m3 that takes a non empty 1D array A of characters and a character c, and returns true if c is the last element of A and false if c is not the last element of A. 

E.g., m3({‘a’,’b’,’c’,’d’}, ‘a’) should return false.

E.g., m3({‘a’,’b’,’c’,’d’}, ‘d’) should return true.

public static boolean m3(char[] A, char c){
    return A[A.length -1] == c;                  
}                                                             

300

Given a 2D array of ints A return its contents into a 1D array B where the first row of the 2D array are first in the 1D array followed by the second row, and so on and so forth. For example:
A = {{1,2,3}, {4,5,6}, {7,8,9}}
B = {1,2,3,4,5,6,7,8,9}  

public static int[] convert(int [][] a) {        
    int [] b = new int[a.length*a[0].length];
    for(int i=0; i < a.length; i++) {          
        for(int j=0; j < a[i].length; j++) {  
            b[(i*a.length)+j] = a[i][j];          
        }                                                  
    }                                                      
    return b;                                            
}                                                            

400

Trace the code:                                         
trace("Hello World!", "He110 w0rld!!!")
Show your work by using a stack to keep track of your variables.

400

Write a method named askFileName that repeatedly prompts the user for a file name until the user types the name of a file that exists on the system. Once you get a good file name, return that file name as a String. Here is a sample dialogue from one call to your method, assuming that the file good.txt does exist and the others do not:

Type a file name: bad.txt        
Type a file name: not_here.txt
Type a file name: good.txt      

400

Convert the following code that uses a while loop into a for loop:

String str = "I'm excited";  
int s= str.length();            
while(str.length() < s+ 4){
     str += "!";                  
}   

str = "I'm excited";                                                
for(int s=str.length(); str.length() < s+4; str+= "!"){}

400

Write a Java program to sum the values of an array, if the number is negative change it to positive.

int sum = 0;                              
for(int i = 0; i < arr.length; i++){
     if(arr[i] < 0){                        
        arr[i] = arr[i] * -1;            
     }                                          
     sum = sum + arr[i];              
}                                               

400

Write a method called isJagged that given a 2D array of chars return true if the number of columns differs across rows or false otherwise.

public static boolean isJagged(char [][] a) {
    int column1 = a[0].length;                    
    for(int i=0; i < a.length; i++) {            
        if (a[i].length != column1) {              
           return true;                                  
        }                                                    
    }                                                        
    return false;                                          
}                                                              

500

Write a recursive method named moveToEnd that accepts a string s and a character c as parameters, and returns a new string similar to s but with all instances of c moved to the end of the string. The relative order of the other characters should be unchanged from their order in the original string s. If the character is a letter of the alphabet, all occurrences of that letter in either upper or lowercase should be moved to the end and converted to uppercase. If s does not contain c, it should be returned unmodified.

500

Write a method that reads an input file of temperatures, with real numbers representing daily high temperatures such as:
16.2   23.2                  
   19.2 7.7  22.9            

Your program should prompt for the file name to read, then read its data and print the change in temperature between each pair of neighboring days.

Input file? weather.txt        
16.2 to 23.2, change = 7.0  
23.2 to 19.2, change = -4.0
19.2 to 7.7, change = -11.5
7.7 to 22.9, change = 15.2   

If there are any non-numeric tokens of input in the file, your program should skip over them and ignore them. You may assume that the user types the name of a file that exists and is readable.

500

Write a method called secondLargest that given an array of doubles, find the 2nd largest number.
For example:                                      
{3.4, 2.1, 5.2, 8.4, 7.8 , 9.5}
2nd largest = 8.4                  

public static double secondLargest(double[] arr) {
      double largest = Double.NEGATIVE_INFINITY;
      double second = largest;                              
      for(int i = 0; i< arr.length; i++) {                
             if(arr[i] > largest) {                              
               second = largest;                              
               largest = arr[i];                                  
             }else if(arr[i] > second) {                    
                   second = arr[i];                              
             }                                                        
      }                                                                
      return second;                                             
}                                                                       

500

Write a method called swapColumns that given a 2D array of chars, and two ints x & y swaps the elements of the columns x & y and returns the modified 2D array.
Assume the 2D array isn't jagged and columns x & y exist in the 2D array

public static char[][] swapColumns(char[][] a, int x, int y) {
    for(int row=0; row < a.length; row++) {        
        char temp = a[row][x];                              
        a[row][x] = a[row][y];                              
        a[row][y] = temp;                                    
    }                                                                  
    return a;                                                       
}                                                                           

M
e
n
u