All Flashcards
What are the differences between arrays and ArrayLists?
Arrays: Fixed size, primitive and object types, length property. ArrayLists: Dynamic size, object types only, methods like add(), remove(), size().
What are the differences between == and .equals() for Strings?
==: Compares object references. .equals(): Compares the content of the strings.
What are the differences between for and while loops?
for: Used when the number of iterations is known. while: Used when the number of iterations is unknown, and depends on a condition.
What are the differences between method overloading and method overriding?
Overloading: Multiple methods with the same name but different parameters in the same class. Overriding: A subclass providing a different implementation for a method already defined in its superclass.
What are the differences between x++ and ++x?
x++: Post-increment, returns the original value then increments. ++x: Pre-increment, increments then returns the new value.
What are the steps to create an object?
- Declare a variable of the class type. 2. Use the
newkeyword followed by the class constructor. 3. Assign the result to the variable.
What are the steps to traverse an array?
- Use a
forloop or enhancedforloop. 2. Initialize a counter (if using aforloop). 3. Access each element using its index.
What are the steps to traverse a 2D array?
- Use nested
forloops. 2. The outer loop iterates through rows. 3. The inner loop iterates through columns.
What are the steps in calling a superclass constructor?
- Use the
super()keyword in the subclass constructor. 2. Provide the necessary parameters that match the superclass constructor's signature. 3. This call must be the first statement in the subclass constructor.
What are the steps to implement recursion?
- Identify the base case(s). 2. Define the recursive step, calling the method itself with a modified input. 3. Ensure the recursive step moves towards the base case.
What is the value of x after: int x = 5; x += 3;?
8
What is the value of y after: int y = 10; y /= 2;?
5
What is the output of: String s = "Hello"; System.out.println(s.substring(1, 4));?
ell
What is the output of: int[] arr = {1, 2, 3}; System.out.println(arr[1]);?
2
What is the output of: ArrayList<String> list = new ArrayList<>(); list.add("A"); System.out.println(list.get(0));?
A
What is the output of: int[][] matrix = {{1, 2}, {3, 4}}; System.out.println(matrix[1][0]);?
3
What is the output: int x = 5; if (x > 3) System.out.print("A"); else System.out.print("B");?
A
What is the output: for(int i = 0; i < 3; i++) System.out.print(i);?
012
What is wrong with: String s = "Hello"; s[0] = 'J';?
Strings are immutable; you cannot change a character at a specific index directly.
What is the output: int x = 5; System.out.println(x++); System.out.println(x);?
5 6
What is the output: int x = 5; System.out.println(++x); System.out.println(x);?
6 6
What is the output: String str1 = "hello"; String str2 = new String("hello"); System.out.println(str1 == str2);?
false
What is the output: String str1 = "hello"; String str2 = new String("hello"); System.out.println(str1.equals(str2));?
true