Java Methods, Arrays, and Object-Oriented Programming: Practice Questions for Chapters 6-9

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/56

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

57 Terms

1
New cards

What are the benefits of using a method?

Reuse code, reduce complexity, easy to maintain.

2
New cards

What keyword can be used as a return type for a method that does not return any value?

void

3
New cards

What does the signature of a method consist of?

Method name and parameter list.

4
New cards
<p>What must all Java applications have?</p>

What must all Java applications have?

public static void main(String[] args)

5
New cards

Where do arguments to methods always appear?

Within parentheses.

6
New cards

What area of memory stores parameters and local variables each time a method is invoked?

A stack.

7
New cards

What is the return type of a main method?

void

8
New cards

What is the method header for returning a sales commission given the sales amount and commission rate?

public static double getCommission(double salesAmount, double commissionRate)

9
New cards

What causes a compile error in the provided program?

public class Test {

public static void main(String[] args) {

System.out.println(m(2));

}

public static int m(int num) {

return num;

}

public static void m(int num) {

System.out.println(num);

}

}

The program has a compile error because the two methods m have the same signature.

10
New cards

What is a variable defined inside a method called?

A local variable.

11
New cards

What happens to variable k after the block executes?

{

int k = 2; nPrint("A message", k);

}

System.out.println(k);

k is not defined outside the block. So, the program has a compile error.

12
New cards

What is method overloading?

Two methods with the same name, defined in the same class, is called method overloading.

13
New cards

What is the size of an array once created?

The size is fixed.

14
New cards

Which declarations are correct for an array of int values?

int[] a; and int a[];

15
New cards

True or False: Every element in an array has the same type.

True

16
New cards

True or False: The array size is fixed after an array reference variable is declared.

False.

17
New cards

What is the expression to create an array of the int type with size 40?

new int[40]

18
New cards

Which array declarations are incorrect?

int[] a = new int(2);, int a = new int[2];, int a() = new int[2];

19
New cards

What is the highest index in an array double[] list = new double[5]?

4.

20
New cards

What is list.length for int[] list = new int[6]?

6.

21
New cards

How many elements are in array double[] list = new double[5]?

5.

22
New cards

What does the program output when x[0] is printed?

public class Test {

public static void main(String[] args) {

int[] x = new int[3];

System.out.println("x[0] is " + x[0]);

} }

The program runs fine and displays x[0] is 0.

23
New cards

What is the array index type and its lowest index?

The array index type is int and its lowest index is 0.

24
New cards

Which statements are valid for array declarations?

double d[] = new double[30]; and int[] i = {3, 4, 3, 2};

25
New cards

What is list[1] for double[] list = {3.4, 2.0, 3.5, 5.5}?

2.0.

26
New cards

What is t.length for int[] t = {1, 2, 3, 4}?

4.

27
New cards
<p>What is the output of this code?</p>

What is the output of this code?

1 1 1 1 1 1.

28
New cards
<p>What is the output of the code that finds the index of the maximum value in myList?</p>

What is the output of the code that finds the index of the maximum value in myList?

1.

29
New cards

What happens when list2 is assigned to list1?

list2 points to the same array as list1.

30
New cards

What method copies the sourceArray to the targetArray?

System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

31
New cards

True or False: When an array is passed to a method, a new array is created.

False.

32
New cards

What is the output of the program that prints the second element of the array x initialized with {1, 2, 3}?

Value is 2.0

33
New cards

When you pass an array to a method, what does the method receive?

The reference of the array

34
New cards

Where does the JVM store arrays for dynamic memory allocation?

Heap

35
New cards

Which of the following array initializations is correct?

String[] list = new String[]{"red", "yellow", "green"}; and String[] list = {"red", "yellow", "green"};

36
New cards

What is the content of the list after the first iteration of selectionSort on {3.1, 3.1, 2.5, 6.4, 2.1}?

2.1, 3.1, 2.5, 6.4, 3.1

37
New cards

Which of the following is a correct header for the main method?

public static void main(String[] args) and public static void main(String args[]) and public static void main(String[] x) and public static void main(String x[])

38
New cards
<p>Which statement about multidimensional arrays is correct?</p>

Which statement about multidimensional arrays is correct?

char[][] charArray = {{'a', 'b'}, {'c', 'd'}};

39
New cards

What is the indexed variable for the element at the first row and first column in array a?

a[0][0]

40
New cards

True or False: When creating an int[][] matrix with new int[5][5], the element values are automatically initialized to 0.

True

41
New cards

What is the output of the code that prints the second column of a 2D matrix?

2 5 9 13

42
New cards

What is the output of the code that prints data[1][0][0] from a 3D array?

public class Test {

public static void main(String[] args) {

int[][][] data = {{{1, 2}, {3, 4}},

{{5, 6}, {7, 8}}};

System.out.print(data[1][0][0]);

}

}

5

43
New cards
<p>What’s the output of the following code? </p>

What’s the output of the following code?

4

<p>4</p>
44
New cards

How do you declare a three-dimensional array in Java?

int[][][] m = new int[4][6][5];

45
New cards

What does System.out.println(array[0][0][0]) print for the given 3D array?

1

46
New cards

Can the rows in a two-dimensional array have different lengths?

Yes, they are ragged arrays.

47
New cards

What are the lengths of the rows in the array x = {{1, 2}, {3, 4, 5}, {5, 6, 5, 9}}?

2, 3, and 4

48
New cards

What is an Object in Object-Oriented Programming?

An instance of a class.

49
New cards

What is a Constructor in Object-Oriented Programming?

A special kind of method that is invoked to create an object.

50
New cards

What is the State of an object in Object-Oriented Programming?

The properties or attributes represented by data fields.

51
New cards

What is the Behavior of an object in Object-Oriented Programming?

Defined by methods.

52
New cards

What is a Class in Object-Oriented Programming?

A template or blueprint that defines an object's data fields and methods.

53
New cards

What is the default value for a boolean, numeric, and object type in Java?

false, 0, null

54
New cards

What is an instance method?

A method that is associated with an individual object.

55
New cards

What is a class variable?

Variables that are shared by every instance of a class.

56
New cards

What does the declaration Circle[] x = new Circle[10] imply?

x contains a reference to an array and each element can hold a reference to a Circle object.

57
New cards

What keyword is used to refer to the current instance of a class?

this.