Practice Midterm Sarah Flashcards

1
Q

If you declare an array int[] list = {5, 2, 3, 4}, the highest index in the array list is

A

list[3];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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);

A

20

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Analyze the following code

boolean[] x = new boolean[3];
System.out.println("x[0] is " + x[0]);
A

c. The program runs fine and displays x[0] is false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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;
}
}

A

d. 1 11

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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] + " ");   } }
A

b. 9 10 11 12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a method header (not the bodies) for the following:
a) Return a sales commission, given the month and year

A

public static double getSalesCommission(int month, int year)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a method header (not the bodies) for the following:
b) Display the calendar for a month, given the month and year.

A

public static void displayCalendar(int month, int year)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a method header (not the bodies) for the following:c) Return the square root of a number

A

public static double squareRoot(double num)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a method header (not the bodies) for the following:
d) Test whether a number is even, and return true if it is.

A

public static boolean isEven(int num)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is method overloading?

A

Multiple methods can have the same name as long as the parameters are different.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What property do you need before you can perform a binary search on an array?

A

The array needs to be sorted in increasing order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write the line of code to invoke a method named printBoard that does not have any parameters, and does not return anything.

A

printBoard();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Declare and instantiate a 3 dimensional array named fun with dimensions 30 by 10 by 5.

A

int[][][] fun = new int[30][10][5];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Calculate the sum of all the elements in an array of doubles named temperatures

A

double sum = 0;
for(int i = 0; i < temperature.length; i++){
sum += temperature[i];
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write a method that takes in a string and returns the number of special characters that aren’t letters or digits.

A

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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write a code segment that would display the following output using nested for loops:
1
1 2
1 2 3
1 2 3 4

A

for(int i = 1; i <= 4; i++){
for (int j = 1; j <= i; j++){
System.out.print(j + “ “);
}
System.out.println();
}

17
Q

Write a method that takes an int array parameter and returns the number that occurred the most frequently in that array. The array will only contain numbers in the range of 1 to 5 inclusive.
Example 1: Input: 4 5 1 2 3 4 5 4 4
Output: 4

Example 2: Input: 3 3 3 4 4 4 3 2 1 5 3
Output: 3

A

public static int mostFrequent(int[] arr){
int maxCount = 0;
int maxNum = 0;
for (int i = 1; i <= 5; i++){
int count = 0;
for(int j = 0; j < arr.length; j++){
if(arr[j] == i){
count++;
}
}
if (count > maxCount){
maxCount = count;
maxNum = i;
}
}
return maxNum;
}

18
Q

Array input values

A

Scanner input = new Scanner(System.in);
System.out.println(“How many elements in the array: “);
int n = input.nextInt();

    double[] userArray = new double[n];

    for (int i = 0; i < userArray.length; i++){
        System.out.println("Enter a double number: ");

        userArray[i] = input.nextDouble(); }
19
Q

write a for loop to assign a value to all the elements in the array

A

//write a for loop to access and println all the elements in the array

    for (int i = 0; i < userArray.length; i++){
        System.out.println(userArray[i] + " ");
    }
20
Q

//create a new array with randon values

A

double[] randArray = new double[100];
for(int i = 0; i < randArray.length; i++){
randArray[i] = Math.random() * 100;
}
for (int i = 0; i < randArray.length; i++){
System.out.println(randArray[i] + “ “);
}

21
Q

//sum of all the elements in the array

A

double total = 0;
for (int i = 0; i < randArray.length; i++){
System.out.println(randArray[i] + “ “);
total += randArray[i];
}

22
Q

//find the largest number in the array

A

System.out.println(total);
double largest = randArray[0];
for (int i = 1; i < randArray.length; i++){
if (randArray[i] > largest){
largest = randArray[i];
}
}

23
Q

//set the element values to indeces

A

System.out.println(largest);
int[] intArray = new int[10];
for (int i = 0; i < intArray.length; i++){
intArray[i] = i;

24
Q

//shuffling elements in the Array

A

//shuffling elements in the Array
for (int i = 0; i < intArray.length; i++){
int randIndex = (int)(Math.random() * intArray.length);
//swap
int temp = intArray[1];
intArray[i] = intArray[randIndex];
intArray[randIndex] = temp;
}
//println elements for the array
for (int i = 0; i < intArray.length; i++){
System.out.println(intArray[i] + “ “);
}
System.out.println();

    //set the element values to indices
    for (int i = 0; i < intArray.length; i++){
        intArray[i] = i;
    }
25
Q

//shift all elements to the “left”

A

int temp = intArray[0];
for(int i = 1; i < intArray.length; i++){
intArray[i - 1] = intArray[i];
}
intArray[intArray.length -1] = temp;

    //println elements in array
    for (int i = 0; i < intArray.length; i++){
        System.out.println(intArray[i] + " ");
    }
    System.out.println();
    
    //println elements in array shifting one to the right 
    //make sure you are 
    temp = intArray[intArray.length -1];
    for (int i = intArray.length-1; i > 0; i--){
        intArray[i] = intArray[i-1];
    }
    intArray[0] = temp;
    //println elements in array
    for (int i = 0; i < intArray.length; i++){
        System.out.println(intArray[i] + " ");
    }
    System.out.println();
26
Q

Reverse array

A

public class ReverseArray{
public static void main(String[] args){
int[] list1 = {1,2,3,4,5};
int[] list2 = reverse(list1);
CopyArrays.printArray(list1);
CopyArrays.printArray(list2);
}
//return a new array with elements reversed
public static int[] reverse (int[] arr){
int[] reverse = new int[arr.length];
for(int i = 0, j = arr.length-1; i<arr.length; i++, j–){
reverse[j] = arr[i];
}
return reverse;
}
}

27
Q

Random Shuffle

A

public class randomShuffle{
public static void main(String[] args){

    double[] myList = {100, 90, 80, 70, 60, 50, 40, 30, 20, 10};
    for (int i = 0; i < myList.length; i++){
        //generate an index j randomly
        int j = (int)(Math.random() * myList.length);

        //swap myList[i] with myList[j]
        double temp = myList[i];
        myList[i] = myList[j];
        myList[j] = temp;
    }
    for(double i : myList){
        System.out.println(i);
    }
} }
28
Q

Random Shuffle

A

public class randomShuffle{
public static void main(String[] args){

    double[] myList = {100, 90, 80, 70, 60, 50, 40, 30, 20, 10};
    for (int i = 0; i < myList.length; i++){
        //generate an index j randomly
        int j = (int)(Math.random() * myList.length);

        //swap myList[i] with myList[j]
        double temp = myList[i];
        myList[i] = myList[j];
        myList[j] = temp;
    }
    for(double i : myList){
        System.out.println(i);
    }
} }