Practice Midterm Sarah Flashcards
If you declare an array int[] list = {5, 2, 3, 4}, the highest index in the array list is
list[3];
How many times is the println statement executed?
for (int i = 0; i < 10; i++)
for (int j = 0; j < 2; j++)
System.out.println(i * j);
20
Analyze the following code
boolean[] x = new boolean[3]; System.out.println("x[0] is " + x[0]);
c. The program runs fine and displays x[0] is false.
What is the output of the following code:
public class Test {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4, 5};
increase(x);
int[] y = {1, 2, 3, 4, 5}; increase(y[0]); System.out.println(y[0] + " " + x[0]); }
public static void increase(int[] x) {
for (int i = 0; i < x.length; i++)
x[i] += 10;
}
public static void increase(int y) {
y += 10;
}
}
d. 1 11
What is the output of the following code?
public class Test {
public static void main(String[] args) {
int[][] matrix =
{{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
for (int i = 0; i < matrix.length; i++) System.out.print(matrix[2][i] + " "); } }
b. 9 10 11 12
Write a method header (not the bodies) for the following:
a) Return a sales commission, given the month and year
public static double getSalesCommission(int month, int year)
Write a method header (not the bodies) for the following:
b) Display the calendar for a month, given the month and year.
public static void displayCalendar(int month, int year)
Write a method header (not the bodies) for the following:c) Return the square root of a number
public static double squareRoot(double num)
Write a method header (not the bodies) for the following:
d) Test whether a number is even, and return true if it is.
public static boolean isEven(int num)
What is method overloading?
Multiple methods can have the same name as long as the parameters are different.
What property do you need before you can perform a binary search on an array?
The array needs to be sorted in increasing order.
Write the line of code to invoke a method named printBoard that does not have any parameters, and does not return anything.
printBoard();
Declare and instantiate a 3 dimensional array named fun with dimensions 30 by 10 by 5.
int[][][] fun = new int[30][10][5];
Calculate the sum of all the elements in an array of doubles named temperatures
double sum = 0;
for(int i = 0; i < temperature.length; i++){
sum += temperature[i];
}
Write a method that takes in a string and returns the number of special characters that aren’t letters or digits.
public static int numSpecCharacters(String ch){
int count = 0;
for(int i = 0; i < ch.length(); i++){
char x = ch.charAt(i);
if(!Character.isLetterOrDigit(x)){
count++;
}
}
return count;
}