CIS 225 Final Study Guide

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/73

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.

74 Terms

1
New cards

An array variable can be declared and redeclared in the same block.

False

2
New cards

Which code fragment would correctly identify the number of arguments passed via the command line to a Java application, excluding the name of the class that is being invoked?

int count = args.length;

3
New cards

For the binarySearch method in Section 7.10.2, what is low and high after the first iteration of the while loop when invoking binarySearch(new int[]{1, 4, 6, 8, 10, 15, 20}, 11)?

low is 4 and high is 6

4
New cards

x.length does not exist if x is null.

True

5
New cards

public class Test {

public static void main(String[] args) {

int[] x = {0, 1, 2, 3, 4, 5};

xMethod(x, 5);

}

public static void xMethod(int[] x, int length) {

for (int i = 0; i < length; i++)

System.out.print(" " + x[i]);

}

}

The program displays 0 1 2 3 4.

6
New cards

When you return an array from a method, the method returns ________.

the reference of the array

7
New cards

The ________ method sorts the array scores of the double[] type.

java.util.Arrays.sort(scores)

8
New cards

In the following code, what is the printout for list2?

class Test {

public static void main(String[] args) {

int[] list1 = {3, 2, 1};

int[] list2 = {1, 2, 3};

list2 = list1;

list1[0] = 0; list1[1] = 1; list2[2] = 2;

for (int i = list2.length - 1; i >= 0; i--)

System.out.print(list2[i] + " ");

}

}

2 1 0

9
New cards

What is the output of the following code?

int[] myList = {1, 2, 3, 4, 5, 6};

for (int i = 1; i < myList.length; i++) {

myList[i - 1] = myList[i];

}

for (int e: myList)

System.out.print(e + " ");

2 3 4 5 6 6

10
New cards

What is the output of the following code?

public class Test {

public static void main(String[] args) {

int list[] = {1, 2, 3, 4, 5, 6};

for (int i = 1; i < list.length; i++)

list[i] = list[i - 1];

for (int i = 0; i < list.length; i++)

System.out.print(list[i] + " ");

}

}

1 1 1 1 1 1

11
New cards

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

5

12
New cards

The arraycopy method does not allocate memory space for the target array. The target array must already be created with memory space allocated.

true

13
New cards

Analyze the following code:

public class Test {

public static void main(String[] args) {

final int[] x = {1, 2, 3, 4};

int[] y = x;

x = new int[2];

for (int i = 0; i < y.length; i++)

System.out.print(y[i] + " ");

}

}

The program has a compile error on the statement x = new int[2], because x is final and cannot be changed.

14
New cards

Identify the problems in the following code.

public class Test {

public static void main(String argv[]) {

System.out.println("argv.length is " + argv.length);

}

}

If you run this program without passing any arguments, the program would display argv.length is 0.

15
New cards

Suppose a method p has the following heading:

public static int[] p()

What return statement may be used in p()?

return new int[]{1, 2, 3};

16
New cards

Binary search can be applied on an unsorted list.

false

17
New cards

What is the correct term for numbers[99]?

indexed variable

18
New cards

Given the following statement

int[] list = new int[10];

list.length has the value ________.

10

19
New cards

Which of the following statements are correct to invoke the printMax method in Listing 7.5 in the textbook?

printMax(1, 2, 2, 1, 4);

printMax(new double[]{1, 2, 3});

printMax(new int[]{1, 2, 3});

printMax(1.0, 2.0, 2.0, 1.0, 4.0);

printMax(1, 2, 2, 1, 4);

printMax(new double[]{1, 2, 3});

printMax(1.0, 2.0, 2.0, 1.0, 4.0);

20
New cards

The element in the array must be of a primitive data type.

false

21
New cards

suppose int[] numbers = new int[10], what is numbers[0]?

0

22
New cards

Use the selectionSort method presented in this section to answer this question. What is list1 after executing the following statements?

double[] list1 = {3.1, 3.1, 2.5, 6.4};

selectionSort(list1);

list1 is 2.5 3.1, 3.1, 6.4

23
New cards

What is the representation of the third element in an array called a?

a[2]

24
New cards

Show the output of the following code:

public class Test {

public static void main(String[] args) {

int[] x = {1, 2, 3, 4, 5};

increase(x);

int[] y = {1, 2, 3, 4, 5};

increase(y[0]);

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

}

public static void increase(int[] x) {

for (int i = 0; i < x.length; i++)

x[i]++;

}

public static void increase(int y) {

y++;

}

}

2 1

25
New cards

The name length is a method in an array.

false

26
New cards

Given the following program:

public class Test {

public static void main(String[] args) {

for (int i = 0; i < args.length; i++) {

System.out.print(args[i] + " ");

}

}

}

What is the output, if you run the program using the following?

java Test 1 2 3

1 2 3

27
New cards

The JVM stores the array in an area of memory, called ________, which is used for dynamic memory allocation where blocks of memory are allocated and freed in an arbitrary order.

heap

28
New cards

You can use the operator == to check whether two variables refer to the same array.

true

29
New cards

An array can be used in which of the following ways?

as a parameter of a method

as a return value of a method

as a local variable

all of the above

all of the above

30
New cards

Analyze the following code:

public class Test1 {

public static void main(String[] args) {

xMethod(new double[]{3, 3});

xMethod(new double[5]);

xMethod(new double[3]{1, 2, 3});

}

public static void xMethod(double[] a) {

System.out.println(a.length);

}

}

The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect.

31
New cards

The ________ method sorts the array scores of the double[] type.

java.util.Arrays.sort(scores)

32
New cards

________ declares an array of char.

char[] chars

33
New cards

Analyze the following code.

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.

34
New cards

The array size is specified when the array is declared.

false

35
New cards

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

4

36
New cards

Suppose int i = 5, which of the following can be used as an index for array double[] t = new double[100]?i + 6.5

Math.random() * 100

(int)(Math.random() * 100))

i

i + 10

(int)(Math.random() * 100))

i

i + 10

37
New cards

An array variable can be declared and redeclared in the same block.

false

38
New cards

The array index is not limited to int type.

true

39
New cards

What is the output of the following code?

double[] myList = {1, 5, 5, 5, 5, 1};

double max = myList[0];

int indexOfMax = 0;

for (int i = 1; i < myList.length; i++) {

if (myList[i] > max) {

max = myList[i];

indexOfMax = i;

}

}

System.out.println(indexOfMax);

1

40
New cards

Assume the signature of the method xMethod is as follows.

public static void xMethod(double[] a)

Which of the following could be used to invoke xMethod?

xMethod(new double[2]);

41
New cards

Suppose a method p has the following heading:

public static int[] p()

What return statement may be used in p()?

return new int[]{1, 2, 3};

42
New cards

What is the output of the following code?

double[] myList = {1, 5, 5, 5, 5, 1};

double max = myList[0];

int indexOfMax = 0;

for (int i = 1; i < myList.length; i++) {

if (myList[i] >= max) {

max = myList[i];

indexOfMax = i;

}

}

System.out.println(indexOfMax);

4

43
New cards

Analyze the following code:

public class Test {

public static void main(String[] args) {

int[] a = new int[4];

a[1] = 1;

a = new int[2];

System.out.println("a[1] is " + a[1]);

}

}

The program displays a[1] is 0.

44
New cards

The array size is specified when the array is declared

f

45
New cards

Analyze the following code:

public class Test {

public static void main(String[] args) {

int[] x = {1, 2, 3, 4};

int[] y = x;

x = new int[2];

for (int i = 0; i < x.length; i++)

System.out.print(x[i] + " ");

}

}

The program displays 0 0

46
New cards

Analyze the following code:

public class Test {

public static void main(String[] args) {

int[] oldList = {1, 2, 3, 4, 5};

reverse(oldList);

for (int i = 0; i < oldList.length; i++)

System.out.print(oldList[i] + " ");

}

public static void reverse(int[] list) {

int[] newList = new int[list.length];

for (int i = 0; i < list.length; i++)

newList[i] = list[list.length - 1 - i];

list = newList;

}

}

The program displays 1 2 3 4 5.

47
New cards

nalyze the following code:

public class Test {

public static void main(String[] args) {

int[] x = new int[5];

for (int i = 0; i < x.length; i++)

x[i] = i;

System.out.println(x[i]);

}

}

The program has a syntax error because i is not defined in the last statement in the main method.

48
New cards

How can you get the word "abc" in the main method from the following call?

java Test "+" 3 "abc" 2

args[2]

49
New cards

Consider the following code fragment:

int[] list = new int[10];

for (int i = 0; i <= list.length; i++) {

list[i] = (int)(Math.random() * 10);

}

Which of the following statements is true?

the code has a runtime error indicating that the array is out of bound.

50
New cards

Analyze the following code:

public class Test {

public static void main(String[] args) {

boolean[][] x = new boolean[3][];

x[0] = new boolean[1]; x[1] = new boolean[2];

x[2] = new boolean[3];

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

}

}

The program runs and displays x[2][2] is false.

51
New cards

Assume double[][] x = new double[4][5], what are x.length and x[2].length?

4 and 5

52
New cards

int[][] matrix = new int[5][5];

for (int column = 0; column < matrix[4].length; column++)

matrix[4][column] = 10;

After the loop, the last row of matrix 10, 10, 10, 10, 10.

53
New cards

Assume boolean[][] x = new boolean[5][7], what is the index variable for the element at the last row and last column in array x?

Correct!

x[4][6]

54
New cards

What is the printout of the following program?

public class Test {

public static void main(String[] args) {

int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};

for (int row = 0; row < values.length; row++) {

System.out.print(m(values[row]) + " ");

}

}

public static int m(int[] list) {

int v = list[0];

for (int i = 1; i < list.length; i++)

if (v < list[i])

v = list[i];

return v;

}

}

5 33

55
New cards

Assume double[][][] x = new double[4][5][6], what are x.length, x[2].length, and x[0][0].length?

4, 5, and 6

56
New cards

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

a[0][0]

57
New cards

Assume int[][][] x = new char[2][5][3], how many elements are in the array?

30

58
New cards

What is the output of the following code?

public class Test5 {

public static void main(String[] args) {

int[][] matrix =

{{1, 2, 3, 4},

{4, 5, 6, 7},

{8, 9, 10, 11},

{12, 13, 14, 15}};

for (int i = 0; i < 4; i++)

System.out.print(matrix[1][i] + " ");

}

}

4567

59
New cards

Analyze the following code:

int[][] matrix = new int[5][5];

for (int column = 0; column < matrix[4].length; column++);

matrix[4][column] = 10;

A syntax error, because column is not defined.

60
New cards

What is the printout of the following program?

public class Test {

public static void main(String[] args) {

int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};

int v = values[0][0];

for (int[] list : values)

for (int element : list)

if (v > element)

v = element;

System.out.print(v);

}

}

1

61
New cards

What is the output of the following code?

public class Test {

public static void main(String[] args) {

int[][] matrix =

{{1, 2, 3, 4},

{4, 5, 6, 7},

{8, 9, 10, 11},

{12, 13, 14, 15}};

for (int i = 0; i < 4; i++)

System.out.print(matrix[i][1] + " ");

}

}

2 5 9 13

62
New cards

Consider the following statements:

int[][] numbers = {{1}, {1, 2}, {1, 2, 3}};

What is numbers.length and what is numbers[1].length?

3 2

63
New cards

The keyword ________ is required to declare a class.

class

64
New cards

_______ is a construct that defines objects of the same type.

class

65
New cards

An object is an instance of a ________.

class

66
New cards

Which of the following statements are true?

The default constructor is a no-arg constructor.

A default constructor is provided automatically if no constructors are explicitly declared in the class.

At least one constructor must always be defined explicitly

Every class has a default constructor.

The default constructor is a no-arg constructor.

A default constructor is provided automatically if no constructors are explicitly declared in the class.

67
New cards

Analyze the following code.

public class Test {

int x;

public Test(String t) {

System.out.println("Test");

}

public static void main(String[] args) {

Test test = null;

System.out.println(test.x);

}

}

The program has a runtime NullPointerException because test is null while executing test.x.

68
New cards

Given the declaration Circle x = new Circle(), which of the following statement is most accurate?

You can assign an int value to x.

x contains an int value.

x contains an object of the Circle type.

x contains a reference to a Circle object.

x contains a reference to a Circle object.

69
New cards

Analyze the following code.

public class Test {

public static void main(String[] args) {

double radius;

final double PI= 3.15169;

double area = radius radius PI;

System.out.println("Area is " + area);

}

}

The program has compile errors because the variable radius is not initialized.

70
New cards

Which of the following statements are true?

Local variables do not have default values.

A variable of a primitive type holds a value of the primitive type.

A variable of a reference type holds a reference to where an object is stored in the memory.

Data fields have default values.

You may assign an int value to a reference variable.

Local variables do not have default values.

A variable of a primitive type holds a value of the primitive type.

A variable of a reference type holds a reference to where an object is stored in the memory.

Data fields have default values.

71
New cards

Which of the following statements are true?

Constructors must have the same name as the class itself.

Constructors do not have a return type, not even void.

Multiple constructors can be defined in a class.

Constructors are invoked using the new operator when an object is created.

Constructors must have the same name as the class itself.

Constructors do not have a return type, not even void.

Multiple constructors can be defined in a class.

Constructors are invoked using the new operator when an object is created.

72
New cards

Which of the following statement is most accurate?

You Answered

A reference variable is an object.

An object may contain the references of other objects.

A reference variable refers to an object.

An object may contain other objects.

An object may contain the references of other objects.

A reference variable refers to an object.

73
New cards

What are primitives?

They are the most basic data type in Java. The eight primitives are boolean , byte , char , short , int , long , float and double

74
New cards