1/73
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
An array variable can be declared and redeclared in the same block.
False
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;
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
x.length does not exist if x is null.
True
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.
When you return an array from a method, the method returns ________.
the reference of the array
The ________ method sorts the array scores of the double[] type.
java.util.Arrays.sort(scores)
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
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
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
How many elements are in array double[] list = new double[5]?
5
The arraycopy method does not allocate memory space for the target array. The target array must already be created with memory space allocated.
true
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.
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.
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};
Binary search can be applied on an unsorted list.
false
What is the correct term for numbers[99]?
indexed variable
Given the following statement
int[] list = new int[10];
list.length has the value ________.
10
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);
The element in the array must be of a primitive data type.
false
suppose int[] numbers = new int[10], what is numbers[0]?
0
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
What is the representation of the third element in an array called a?
a[2]
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
The name length is a method in an array.
false
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
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
You can use the operator == to check whether two variables refer to the same array.
true
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
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.
The ________ method sorts the array scores of the double[] type.
java.util.Arrays.sort(scores)
________ declares an array of char.
char[] chars
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.
The array size is specified when the array is declared.
false
Assume int[] t = {1, 2, 3, 4}. What is t.length?
4
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
An array variable can be declared and redeclared in the same block.
false
The array index is not limited to int type.
true
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
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]);
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};
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
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.
The array size is specified when the array is declared
f
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
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.
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.
How can you get the word "abc" in the main method from the following call?
java Test "+" 3 "abc" 2
args[2]
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.
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.
Assume double[][] x = new double[4][5], what are x.length and x[2].length?
4 and 5
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.
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]
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
Assume double[][][] x = new double[4][5][6], what are x.length, x[2].length, and x[0][0].length?
4, 5, and 6
What is the index variable for the element at the first row and first column in array a?
a[0][0]
Assume int[][][] x = new char[2][5][3], how many elements are in the array?
30
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
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.
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
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
Consider the following statements:
int[][] numbers = {{1}, {1, 2}, {1, 2, 3}};
What is numbers.length and what is numbers[1].length?
3 2
The keyword ________ is required to declare a class.
class
_______ is a construct that defines objects of the same type.
class
An object is an instance of a ________.
class
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.
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.
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.
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.
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.
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.
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.
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