A whole number
2 + 2
4
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
Declare a variable called num to equal 2
int num = 2;
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.
Boolean
A data type that holds a true or false value.
17 % 3
2
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,
Write the main method header.
public static void main(String[] args)
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 "=="
Instance variable
An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks.
What is i if:
int i = (31 % 8)/2
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);
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);
}
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.
Polymorphism
Polymorphism is the ability of a reference variable to refer to objects of various types at different times.
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
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}
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;
}
}
}
}
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>
Data abstraction
Data abstraction is the process of hiding certain details and showing only essential information to the user.
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
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,
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();
}
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.