Test Study Weeks 4-7 Flashcards

1
Q

What are the two types of methods in Java?

A

Void methods perform a task and terminate, while value-returning methods return a value to the caller.

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

What are the key components of a method declaration?

A

Modifiers, return type, method name, and parameter list.

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

What is a method signature?

A

A method’s name and parameter list (number, type, and order of parameters).

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

How do you call a method in Java?

A

By using its name followed by parentheses: methodName();

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

What is the difference between arguments and parameters?

A

Arguments are values passed to a method; parameters are the variables that receive those values.

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

How does Java pass primitive data types to a method?

A

By value, meaning a copy of the value is passed, and modifications inside the method do not affect the original variable.

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

What happens when an object reference is passed to a method?

A

The memory address of the object is passed, so changes made inside the method affect the original object.

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

Why are Strings immutable in Java?

A

Once created, String objects cannot be modified. Changing a String creates a new object.

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

What is a local variable?

A

A variable declared inside a method that is accessible only within that method.

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

How do you return a value from a method?

A

Using the return statement followed by a value. Example: return number * 2;

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

What is an array in Java?

A

A collection of elements of the same data type, stored in contiguous memory locations.

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

How do you declare and initialize an array?

A

int[] numbers = new int[5]; or int[] numbers = {1, 2, 3, 4, 5};

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

What is zero-based indexing?

A

Arrays start indexing at 0, so the first element is at index 0.

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

Can you change the size of an array after creation?

A

No, arrays have a fixed size once initialized.

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

How do you access an array element?

A

Using its index: numbers[0] = 10; assigns 10 to the first element.

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

What happens if you access an array out of bounds?

A

Java throws an ArrayIndexOutOfBoundsException.

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

How do you find the length of an array?

A

Using the length property: array.length;

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

What is an off-by-one error?

A

A common mistake where a loop iterates one index too far, causing an exception.

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

How do you iterate over an array?

A

Using a for loop: for (int i = 0; i < array.length; i++) or an enhanced for loop: for (int num : array).

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

How do you copy an array correctly?

A

Using a loop: for (int i = 0; i < arr1.length; i++) arr2[i] = arr1[i];

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

What happens when you assign one array reference to another?

A

Both references point to the same array, not a copy.

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

How do you compare two arrays for equality?

A

Using Arrays.equals(arr1, arr2) for content comparison (not ==, which checks references).

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

What is a partially filled array?

A

An array where some elements are unused, tracked with a separate counter variable.

24
Q

What is a two-dimensional array?

A

An array of arrays, arranged in rows and columns. Example: int[][] matrix = new int[3][4];

25
Q

What is a ragged array?

A

A 2D array where rows have different lengths.

26
Q

How do you sum all elements in a 2D array?

A

Using nested loops: for (int row = 0; row < matrix.length; row++) for (int col = 0; col < matrix[row].length; col++) total += matrix[row][col];

27
Q

What is a class in Java?

A

A blueprint for creating objects, containing fields (data) and methods (behavior).

28
Q

What is an object?

A

An instance of a class with unique values for its fields.

29
Q

What are instance variables?

A

Variables that belong to each object of a class and store its state.

30
Q

What are instance methods?

A

Methods that operate on an object’s instance variables.

31
Q

What is data hiding?

A

A principle where instance variables are kept private and accessed through methods.

32
Q

What are accessor and mutator methods?

A

Accessors (getters) return field values, and mutators (setters) modify them.

33
Q

What is a constructor?

A

A special method that initializes an object when it is created.

34
Q

What are the characteristics of a constructor?

A

1) Same name as the class. 2) No return type. 3) Called automatically when an object is created.

35
Q

What is a no-arg constructor?

A

A constructor with no parameters that sets default values.

36
Q

What is constructor overloading?

A

Defining multiple constructors with different parameter lists.

37
Q

What is method overloading?

A

Defining multiple methods with the same name but different parameters.

38
Q

What is the this keyword used for?

A

Refers to the current object, useful for distinguishing between instance variables and parameters.

39
Q

What is the toString method?

A

A method that returns a string representation of an object.

40
Q

What happens when toString is not overridden?

A

It prints the memory address of the object.

41
Q

What is the equals method used for?

A

Compares object contents instead of memory addresses.

42
Q

What is a copy constructor?

A

A constructor that creates a new object as a copy of an existing one.

43
Q

What is object aggregation?

A

When an object contains a reference to another object (“has-a” relationship).

44
Q

What is method chaining?

A

Calling multiple methods on the same object in one statement: obj.method1().method2();

45
Q

What is the static keyword used for?

A

Declares class-level variables and methods shared among all instances.

46
Q

How do you call a static method?

A

Using the class name: Math.sqrt(25);

47
Q

What is garbage collection?

A

Automatic memory management that removes unreferenced objects.

48
Q

What is a null reference?

A

A reference that does not point to any object.

49
Q

How do you check if a reference is null?

A

if (obj != null) { … }

50
Q

What is shadowing in Java?

A

When a local variable has the same name as an instance variable, hiding it.

51
Q

What is a package in Java?

A

A way to organize related classes in separate namespaces.

52
Q

How do you import a package?

A

import java.util.Scanner; or import java.util.*;

53
Q

What is the difference between == and .equals() in Java?

A

== compares memory addresses, while .equals() compares actual content.

54
Q

What is a deep copy vs. a shallow copy?

A

A deep copy creates a new object with copied values; a shallow copy copies the reference.

55
Q

What is the purpose of the super keyword?

A

Calls a parent class’s constructor or method.

56
Q

What is polymorphism?

A

The ability of different classes to use the same method names, often through method overriding.

57
Q

What is an interface in Java?

A

A collection of abstract methods that a class can implement.