Definitions
Calculations
Find the Result
Write the code
Find the error
100
Integer

A whole number

100

2 + 2

4

100

What will be printed as a result of the following code segment:

public static void main(String[] args) {
System.out.println("Hello");
System.out.println("Bye");
}

Hello
Bye

100

Declare a variable called num to equal 2

int num = 2;

100

The following code segment doesn't work. Why?

public static void main(String[] args) {
int var = "Hello";
System.out.println(i);
}

The integer var is being declared as a string.

200

Boolean

A data type that holds a true or false value.

200

17 % 3

2

200

What will be printed as a result of:

public static void main(String[] args) {
    int[] nums = {1, 4, 2, 5, 3};
    for (int i = 0; i < nums.length; i++) {
        System.out.print(nums[i] + ", ");
    }
}

1, 4, 2, 5, 3,

200

Write the main method header.

public static void main(String[] args)

200

The following code segment doesn't work. Why?

int a = 4;
int b = 4;
if (a = b)
    System.out.println("Equal");

There is only one "=" in the if statement. An if statement needs two "=="

300

Instance variable

An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks.

300

What is i if:

int i = (31 % 8)/2

i = 3
300

What will be printed as a result of the code segment?

int total = 3;
int[] values = {8, 6, 4, -2};
total += values[total];
total += values[total];
System.out.println(total);

7
300

Write a for each loop for a list called list and an int called num that prints out each element of the list

for (int num : list) {
    System.out.println(num);
}

300

The following code segment doesn't work. Why?

public static void printArrayList(ArrayList<Integer> list) {
    for (int i = 0; i < list.length; i++) {
        System.out.println(list[i]);
    }
}

.length and list[i] don't work for ArrayLists. 

400

Polymorphism

Polymorphism is the ability of a reference variable to refer to objects of various types at different times.

400

What is the range of values that can be obtained by the following statement:

(int)(Math.random() * 7) + 3

(The answer should be something like: 2 to 4)

3 to 9

400

What values are stored in nums after the execution of the code segment?

int[] nums = {0, 0, 1, 1, 2, 2, 3, 3};
for (int i = 3; i < nums.length; i++) {
    nums[i + 1] = i;
}

{0, 0, 1, 2, 3, 4, 5, 6}

400

Write a bubble sort algorithm.

public static void bubbleSort(int[] list)

public static void bubbleSort(int[] list) {
    int temp = 0;
    for (int i = 0; i < list.length; i++) {
        for (int j = 1; list.length-1; j++) {
            if (list[j-1] > list[j]) {
                temp = list[j-1];
                list[j-1] = list[j];
                list[j] = temp;
            }
        }
    }

}

400

There are two errors in the following code segment. Find at least one of them.

import java.util.ArrayList;
public class Main {
   public static void makeList() {
      ArrayList<Integer> list = new ArrayList<Integer>;
      Scanner myObj = new Scanner(System.in);
      System.out.print("Enter three numbers");
      int num1 = myObj.nextLine();
      int num2 = myObj.nextLine();
      int num3 = myObj.nextLine();
      list.add(num1);
      list.add(num2);
      list.add(num3);
    }
}

java.util.scanner is not imported and there needs to be parentheses at the end of new ArrayList<Integer>

500

Data abstraction

Data abstraction is the process of hiding certain details and showing only essential information to the user.

500

What is the range of values that can be obtained by the following statement:

(int)(Math.random()*-8.5)+(4+4*3/-1);

(The answer should be something like: 2 to 4) 

-16 to -8

500

What will be printed a result of the following code segment?

int[] list = {1, 5, 3, 8, 5, 1, 2, 6};
int temp = 0;
for (int i = 0; i < list.length; i++) {
    for (int j = list.length - 3; j >= 2; j--) {
        temp = list[i];
        list[i] = list[j];
        list[j] = temp;
    }
}
for (int e : list) {
    System.out.print(e + ", ")
}

3, 8, 5, 1, 2, 6, 1, 5,

500

Write a recursive method that prints a pyramid of stars the height of the parameter like this:

*
**
***
****

public static void printStars(int h)

public static void printStars(int h) {
    if (h == 0)
        return;
    printStars(h-1);
    for (int i = 0; i < h; i++) {
        System.out.print("*");
    }
    System.out.println();
}

500

There are three errors in the following method. What are they?

import java.util.*

public class Main {
    public static void bogoSort(int[] list) {
        boolean sorted = false;
        int count = 0;
        do {
            ArrayList<Integer> temp = new ArrayList<Integer>();
            for (int i = 0; i < list.length; i++) {
                temp.add(list[i]);
            }
            for (int i = 0; i < list.length; i++) {
                int rand = (int)(Math.random*temp.size());
                list[i] = temp.get(rand);
                temp.remove(rand);
            }
            count++;
            int b = 0;
            for (int i = 1; i < list.length; i++) {
                if (list[i] >= list[i-1])
                    b++;
            }
            if (b >= list.length-1)
                sorted = true;
        } while (sorted == false);
       for (int e : list)
            System.out.printf(e);
    }

}

No semi-colon after java.util.*, System.out.printf instead of System.out.println, no parantheses after Math.random.