What is an array in Java?
An array is a data structure that stores multiple values of the same type in a single variable.
What is a loop in Java?
A loop is a chunk of code that repeats multiple times.
What is a class in Java?
A class is a blueprint for creating objects, defining their properties and methods.a
What is a method in Java?
A method is a block of code thas performs a particular task, which can get executed when called.
What is a conditional statement in Java?
A conditional statement allows the program to execute certain code based on if a condition is true or false.
How do you declare and initialize an array of integers with 5 elements in Java?
int[] myArray = new int[5];
or
int[] myArray = {1, 2, 3, 4, 5};
What is the difference between a for loop and a while loop?
A for loop is used when you know the number of iterations before entering the loop. A while loop is used when the number of iterations is not known and depends on a condition being true.
What is the difference between "inheritance" and "polymorphism" in Java?
Inheritance allows one class to acquire properties and methods from another class. Polymorphism allows the same method or object to behave differently based on the context
How do you call a function in Java?
myFunction();
What statement allows a program to execute a block of code only if a specified condition is true.
If statements.
How do you remove items from array lists? Remove eggs from this array list.
// dp = dairy products
ArrayList<String> dp = new ArrayList<String>();
dp.add("Yogurt");
cars.add("Cheese");
cars.add("Eggs");
Use remove function.
cars.remove(2);
How do you use a for loop to print the numbers from 1 to 10?
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
What is encapsulation in Java, and how is it implemented?
Encapsulation is the bundling of data and methods in a single class while restricting access to them. It is implemented using access modifiers like `private`, `protected`, and `public` and providing getter and setter methods to control access to private fields.
What type of method does not return any value and is used to execute a block of code without producing a result.
Void method
Who is the goat.
Lebron James
What is a for-each loop.
A for-each loop is a loop that can only be used on a collection of items. It will loop through the collection and each time through the loop it will use the next item from the collection. It starts with the first item in the array and continues through in order to the last item in the array.
What is an interface in Java, and how is it different from a class?
An interface is a reference type in Java that can contain abstract methods and constants. Unlike a class, it cannot have instance variables, constructors, or method implementations (until Java 8+ added default methods). A class implements an interface to provide concrete implementations for its methods.
How do you make a method that takes to two integers a returns their sum.
public int sum(int a, int b) {
return a + b;
}
Who created Java?
a. Guido van Rossum
b. Lebron James
c. James Gosling
d. Brendan Eich
Make a short program that iterates over an array using a for-each loop.
int[] myArray = {1, 2, 3, 4, 5};
for (int i : myArray) {
System.out.println(i);
}
What are nested loops used for? Make an example of one. The code can do nothing just show that you mostly understand what a nested loop is.
Nested loops are used to repeat a loop within another loop.
// Simple multiplication table that go's to 5.
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print(i * j + " ");
}
System.out.println();
}
What are the key differences between abstract classes and interfaces in Java?
1. Abstract Classes: Can have both abstract and concrete methods. A class can only extend one abstract class.
2. Interfaces: Can only contain abstract methods (before Java 8) and constants. A class can implement multiple interfaces.
3. Abstract classes can have instance variables, but interfaces cannot (only constants are allowed).
If I called this function with sumOf(5), what would it return? Explain why.
public int sumOf(int k) {
int sum = 0;
for (int i = 1; i <= k; i++) {
sum += i * i;
}
return sum;
}
It would return 55 because the sum of 12 + 22 + 32 + 42 + 52 = 55.