Is Exam Chp 6 & 7

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By G
G
G
Community Contributor
Quizzes Created: 1 | Total Attempts: 185
Questions: 59 | Attempts: 185

SettingsSettingsSettings
Math Quizzes & Trivia

.


Questions and Answers
  • 1. 

    Suppose int[] list = {4, 5, 6, 2, 1, 0}, what is list[0]?

    • A.

      4

    • B.

      3

    • C.

      8

    • D.

      6

    Correct Answer
    A. 4
    Explanation
    The given code declares an integer array named "list" and initializes it with the values 4, 5, 6, 2, 1, and 0. The question asks for the value at index 0 of the array, which is the first element. In this case, the first element is 4.

    Rate this question:

  • 2. 

    Suppose int[] list = {4, 5, 6, 2, 1, 0}, what is list.length?   

    • A.

      6

    • B.

      5

    • C.

      7

    • D.

      9

    Correct Answer
    A. 6
    Explanation
    The correct answer is 6. The length property of an array in Java returns the number of elements in the array. In this case, the array "list" has 6 elements, so the length is 6.

    Rate this question:

  • 3. 

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

    • A.

      A[2]

    • B.

      A{2}

    • C.

      A[3]

    • D.

      A(3)

    Correct Answer
    A. A[2]
    Explanation
    The correct answer is a[2]. In programming, arrays are zero-indexed, which means that the first element is at index 0, the second element is at index 1, and so on. Therefore, the third element in the array called 'a' would be at index 2.

    Rate this question:

  • 4. 

       If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.  

    • A.

      2.0

    • B.

      3.4

    • C.

      3.5

    • D.

      5.5

    Correct Answer
    A. 2.0
    Explanation
    In the given array declaration, double[] list = {3.4, 2.0, 3.5, 5.5}, the index starts from 0. Therefore, list[1] refers to the element at the second position in the array, which is 2.0.

    Rate this question:

  • 5. 

        If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________.  

    • A.

      1

    • B.

      3

    • C.

      2

    • D.

      4

    Correct Answer
    B. 3
    Explanation
    The highest index in the array list is 3. This is because in Java, arrays are zero-indexed, meaning that the first element in the array has an index of 0. Since the array has four elements, the highest index would be 3.

    Rate this question:

  • 6. 

        Which of the following is incorrect?  

    • A.

      Int[] a = new int(2);

    • B.

      Int a() = new int[2];

    • C.

      Int a = new int[2];

    • D.

      int a[] = new int[2];

    • E.

      Int[] a = new int[2];

    Correct Answer(s)
    A. Int[] a = new int(2);
    B. Int a() = new int[2];
    C. Int a = new int[2];
  • 7. 

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

    • A.

      4

    • B.

      5

    • C.

      0

    • D.

      6

    Correct Answer
    B. 5
    Explanation
    The correct answer is 5 because the given statement "double[] list = new double[5]" creates an array called "list" with a size of 5. This means that the array can hold 5 elements.

    Rate this question:

  • 8. 

        What is the correct term for numbers[99]?  

    • A.

      Index variable

    • B.

      Indexed variable

    • C.

      Array

    • D.

      Array variable

    Correct Answer
    B. Indexed variable
    Explanation
    An indexed variable refers to a variable that is accessed or referenced using an index or position within an array or collection. In this case, the term "numbers[99]" indicates that the variable "numbers" is being accessed or referenced at the index or position 99. Therefore, the correct term for "numbers[99]" is an indexed variable.

    Rate this question:

  • 9. 

    Public class Test {   public static void main(String[] args) {     int[] x = new int[3];     System.out.println("x[0] is " + x[0]);   } }  

    • A.

      The program has a compile error because the size of the array wasn't specified when declaring the array.

    • B.

      He program has a runtime error because the array element x[0] is not defined.

    • C.

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

    Correct Answer
    C. The program runs fine and displays x[0] is 0.
    Explanation
    The program runs fine and displays x[0] is 0 because when an array of integers is created without specifying the values, the default value for each element is 0. In this case, the array "x" is initialized with a size of 3, and since no values are assigned to the elements, they all default to 0. Therefore, when printing the value of x[0], it will display 0.

    Rate this question:

  • 10. 

     What would be the result of attempting to compile and run the following code? public class Test {   public static void main(String[] args) {     double[] x = new double[]{1, 2, 3};     System.out.println("Value is " + x[1]);   } } 

    • A.

      The program compiles and runs fine and the output "Value is 2.0" is printed.

    • B.

      The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}.

    • C.

      The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3};

    • D.

      The program compiles and runs fine and the output "Value is 1.0" is printed.

    Correct Answer
    A. The program compiles and runs fine and the output "Value is 2.0" is printed.
    Explanation
    The program compiles and runs fine because the syntax new double[]{1, 2, 3} is correct for creating a new array of doubles with the specified values. The output "Value is 2.0" is printed because the statement x[1] retrieves the value at index 1 of the array, which is 2.0.

    Rate this question:

  • 11. 

    Which one do you like?

    • A.

      Char[2] charArray = {'a', 'b'};

    • B.

      Char[] charArray = {'a', 'b'};

    • C.

      Char[] charArray = new char[]{'a', 'b'};

    • D.

      Char[] charArray = new char[2]; charArray = {'a', 'b'};

    Correct Answer(s)
    B. Char[] charArray = {'a', 'b'};
    C. Char[] charArray = new char[]{'a', 'b'};
    Explanation
    The correct answers are "char[] charArray = {'a', 'b'};" and "char[] charArray = new char[]{'a', 'b'};". Both of these options initialize a character array with the values 'a' and 'b'. The first option uses the shorthand syntax to directly assign the values to the array. The second option explicitly creates a new character array and assigns the values to it. The other options either have syntax errors or do not correctly initialize the array with the given values.

    Rate this question:

  • 12. 

    Select the ones you like

    • A.

      Int[] i = {3, 4, 3, 2};

    • B.

      Int i = new int(30);

    • C.

      Double d[] = new double[30];

    • D.

      Char[] c = new char();

    Correct Answer(s)
    A. Int[] i = {3, 4, 3, 2};
    C. Double d[] = new double[30];
    Explanation
    The correct answer is int[] i = {3, 4, 3, 2}; because it initializes an integer array "i" with the values 3, 4, 3, and 2.

    The correct answer is double d[] = new double[30]; because it declares and initializes a double array "d" with a size of 30.

    Rate this question:

  • 13. 

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

    • A.

      I

    • B.

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

    • C.

      I + 10

    • D.

      Math.random() * 100

    Correct Answer(s)
    A. I
    B. (int)(Math.random() * 100))
    C. I + 10
    Explanation
    The index for an array should be an integer value. In this case, option I, (int)(Math.random() * 100), generates a random integer between 0 and 99, which can be used as an index for the array double[] t. Similarly, option i + 10 adds 10 to the value of i (which is 5), resulting in 15, which is also a valid index for the array. However, option Math.random() * 100 generates a random decimal number between 0 and 100, which is not a valid index for an array.

    Rate this question:

  • 14. 

    What would be the result of attempting to compile and run the following code? public class Test {   public static void main(String[] args) {     double[] x = new double[]{1, 2, 3};     System.out.println("Value is " + x[1]);   } } 

    • A.

      He program compiles and runs fine and the output "Value is 2.0" is printed.

    • B.

      The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}.

    • C.

      The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3};

    • D.

      The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0};

    Correct Answer
    A. He program compiles and runs fine and the output "Value is 2.0" is printed.
    Explanation
    The given code declares and initializes an array of doubles, x, with the values 1, 2, and 3. It then prints out the value at index 1 of the array, which is 2.0. Since there are no syntax errors or other issues with the code, it will compile and run successfully, resulting in the output "Value is 2.0".

    Rate this question:

  • 15. 

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

    • A.

      4

    • B.

      2

    • C.

      1

    • D.

      3

    Correct Answer
    A. 4
    Explanation
    The correct answer is 4 because the length property of an array in Java returns the number of elements in the array, and in this case, the array "t" has 4 elements (1, 2, 3, and 4).

    Rate this question:

  • 16. 

    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);  

    • A.

      0

    • B.

      1

    • C.

      4

    • D.

      3

    Correct Answer
    B. 1
    Explanation
    The code initializes an array called "myList" with the values 1, 5, 5, 5, 5, and 1. It then sets the variable "max" to the first element of the array (1) and "indexOfMax" to 0.

    The for loop iterates through the elements of the array starting from the second element. If an element is greater than the current maximum value (max), it updates the max value and the index of the max value (indexOfMax).

    In this case, the max value is 5, which is at index 1 in the array. Therefore, the output of the code is 1.

    Rate this question:

  • 17. 

     Analyze the following code: public class Test {    public static void main(String[] args) {     int[] x = new int[5];     int i;     for (i = 0; i < x.length; i++)       x[i] = i;     System.out.println(x[i]);   } }

    • A.

      The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.

    • B.

      He program displays 0 1 2

    • C.

      The program displays 4.

    • D.

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

    Correct Answer
    A. The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.
    Explanation
    The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException. This is because the variable "i" is incremented in the for loop until it reaches the length of the array "x". However, after the loop ends, the value of "i" is equal to "x.length", which is out of the bounds of the array. Therefore, when the program tries to access the element at index "i" in the array "x", it throws an ArrayIndexOutOfBoundsException.

    Rate this question:

  • 18. 

    Analyze the following code: public class Test {   public static void main(String[] args) {     double[] x = {2.5, 3, 4};     for (double value: x)       System.out.print(value + " ");   } }

    • A.

      The program displays 2.5, 3.0 4.0

    • B.

      The program displays 2.5 3.0 4.0

    • C.

      The program displays 2.5, 3, 4

    • D.

      The program has a syntax error because value is undefined.

    Correct Answer
    A. The program displays 2.5, 3.0 4.0
    Explanation
    The given code declares an array of doubles named x and initializes it with the values 2.5, 3, and 4. It then uses a for-each loop to iterate over each element in the array and print it out. The System.out.print statement is used to print each value, followed by a space. Therefore, the program will display the values 2.5, 3.0, and 4.0, with each value separated by a space.

    Rate this question:

  • 19. 

    What is the output of the following code?      int[] myList = {1, 2, 3, 4, 5, 6};      for (int i = myList.length - 2; i >= 0; i--) {        myList[i + 1] = myList[i];      }      for (int e: myList)        System.out.print(e + " ");

    • A.

      1 1 2 3 4 5

    • B.

      1 2 3 4 5 6

    • C.

      6 1 2 3 4 5

    • D.

      6 2 3 4 5 1

    Correct Answer
    A. 1 1 2 3 4 5
    Explanation
    The code starts by initializing an array called myList with values 1, 2, 3, 4, 5, and 6.

    Then, it enters a for loop that iterates from the second-to-last element of the array (index length - 2) to the first element (index 0).

    Inside the loop, it shifts each element one position to the right by assigning the value of the current element to the next element.

    After the loop, another for loop is used to print each element of the modified myList array.

    Therefore, the output is 1 1 2 3 4 5, which represents the modified array after shifting the elements.

    Rate this question:

  • 20. 

    What is output of the following code: public class Test {    public static void main(String[] args) {     int[] x = {120, 200, 016};     for (int i = 0; i < x.length; i++)       System.out.print(x[i] + " ");   } }

    • A.

      120 200 14

    • B.

      120 200 16

    • C.

      016 is a compile error. It should be written as 16.

    • D.

      120 200 20

    Correct Answer
    A. 120 200 14
    Explanation
    The code initializes an integer array named x with three values: 120, 200, and 016. The for loop iterates over the array and prints each value followed by a space. The output of the code is "120 200 14" because the value 016 is interpreted as an octal number and its decimal equivalent is 14.

    Rate this question:

  • 21. 

    What is 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] + " ");   } }

    • A.

      2 3 4 5 6 1

    • B.

      1 2 3 4 5 6

    • C.

      2 3 4 5 6 1

    • D.

      1 1 1 1 1 1

    Correct Answer
    D. 1 1 1 1 1 1
    Explanation
    The output of the code is "1 1 1 1 1 1". This is because the code is using a for loop to iterate through the elements of the "list" array, starting from index 1. In each iteration, it assigns the value of the previous element (i - 1) to the current element (i). Since the loop starts from index 1, the first element (index 0) remains unchanged. Therefore, all the elements in the array end up having the value of the first element, which is 1.

    Rate this question:

  • 22. 

     In the following code, what is the output for list2? public class Test {   public static void main(String[] args) {     int[] list1 = {1, 2, 3};     int[] list2 = {1, 2, 3};     list2 = list1;     list1[0] = 0; list1[1] = 1; list2[2] = 2;     for (int i = 0; i < list2.length; i++)       System.out.print(list2[i] + " ");   } }

    • A.

      123

    • B.

      012

    • C.

      234

    • D.

      013

    Correct Answer
    B. 012
    Explanation
    The output for list2 is "012" because in the code, list2 is assigned to list1 using the statement "list2 = list1". This means that list2 and list1 now refer to the same array object. Then, the values of list1[0], list1[1], and list2[2] are modified to 0, 1, and 2 respectively. Since list2 and list1 are referencing the same array, these modifications also affect list2. Therefore, when the for loop is executed, it prints the elements of list2 which are 0, 1, and 2.

    Rate this question:

  • 23. 

    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] + " ");   } }  

    • A.

      The program displays 1 2 3 4

    • B.

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

    • C.

      The program displays 0 0

    • D.

      The elements in the array x cannot be changed, because x is final.

    Correct Answer
    B. The program has a compile error on the statement x = new int[2], because x is final and cannot be changed.
    Explanation
    The given code will result in a compile error on the statement x = new int[2]. This is because the variable x is declared as final, which means it cannot be reassigned to a new array. Therefore, trying to assign a new int array to x will cause a compilation error.

    Rate this question:

  • 24. 

    Analyze the following code. int[] list = new int[5]; list = new int[6];  

    • A.

      The code has compile errors because the variable list cannot be changed once it is assigned.

    • B.

      The code can compile and run fine. The second line assigns a new array to list.

    • C.

      The code has compile errors because you cannot assign a different size array to list.

    • D.

      The code has runtime errors because the variable list cannot be changed once it is assigned.

    Correct Answer
    B. The code can compile and run fine. The second line assigns a new array to list.
    Explanation
    The correct answer is that the code can compile and run fine. The second line assigns a new array to list. This is because the variable "list" is declared as an array of integers and initially assigned a new array of size 5. However, in the second line, a new array of size 6 is assigned to the variable "list". This is allowed in Java, and the code will compile and run without any errors.

    Rate this question:

  • 25. 

    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]);   } }  

    • A.

      The program has a compile error because new int[2] is assigned to a.

    • B.

      He program has a runtime error because a[1] is not initialized.

    • C.

      The program displays a[1] is 0.

    • D.

      The program displays a[1] is 1.

    Correct Answer
    C. The program displays a[1] is 0.
    Explanation
    The program displays a[1] is 0 because when a new int array of size 2 is assigned to 'a', the previous array of size 4 is no longer accessible. Therefore, when the statement 'System.out.println("a[1] is " + a[1])' is executed, it accesses the new array which has not been initialized, resulting in a default value of 0 being displayed.

    Rate this question:

  • 26. 

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

    • A.

      A copy of the array

    • B.

      The reference of the array

    • C.

      A copy of the first element

    • D.

      He length of the array

    Correct Answer
    B. The reference of the array
    Explanation
    When you return an array from a method, the method returns the reference of the array. This means that the method is returning the memory address where the array is stored, rather than creating a new copy of the array. This allows other parts of the program to access and manipulate the same array.

    Rate this question:

  • 27. 

     Suppose a method p has the following heading: public static int[] p() What return statement may be used in p()?

    • A.

      Return int[]{1, 2, 3};

    • B.

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

    • C.

      Return {1, 2, 3};

    • D.

      Return 1;

    Correct Answer
    B. Return new int[]{1, 2, 3};
    Explanation
    The correct answer is "return new int[]{1, 2, 3};" because the method p() is declared to return an int array. The syntax "new int[]{1, 2, 3}" creates a new int array and initializes it with the values 1, 2, and 3. Therefore, this return statement is valid and will return an int array containing the values 1, 2, and 3.

    Rate this question:

  • 28. 

    The reverse method is defined in the textbook. What is list1 after executing the following statements? int[] list1 = {1, 2, 3, 4, 5, 6}; list1 = reverse(list1);

    • A.

      654321

    • B.

      123456

    • C.

      234567

    • D.

      765432

    Correct Answer
    A. 654321
    Explanation
    The given code snippet initializes an array called "list1" with the values 1, 2, 3, 4, 5, and 6. It then calls the "reverse" method on "list1" and assigns the returned value back to "list1". The "reverse" method likely reverses the order of the elements in the array. Therefore, after executing these statements, "list1" will contain the values in reverse order, which are 6, 5, 4, 3, 2, and 1.

    Rate this question:

  • 29. 

    Which of the following declarations are correct?

    • A.

      Public static void print(double... numbers, String name)

    • B.

      Public static void print(double... numbers)

    • C.

      Public static void print(int n, double... numbers)

    • D.

      Public static void print(String... strings, double... numbers)

    Correct Answer(s)
    B. Public static void print(double... numbers)
    C. Public static void print(int n, double... numbers)
    Explanation
    The correct declarations are "public static void print(double... numbers)" and "public static void print(int n, double... numbers)". These declarations are correct because they use the varargs syntax, which allows the methods to accept a variable number of arguments of the specified type. The first declaration accepts any number of double values, while the second declaration accepts an int value followed by any number of double values.

    Rate this question:

  • 30. 

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

    • A.

      PrintMax(1, 2, 2, 1, 4);

    • B.

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

    • C.

      PrintMax(1.0, 2.0, 2.0, 1.0, 4.0);

    • D.

      Option 4

    Correct Answer(s)
    A. PrintMax(1, 2, 2, 1, 4);
    B. PrintMax(new double[]{1, 2, 3});
    C. PrintMax(1.0, 2.0, 2.0, 1.0, 4.0);
    Explanation
    The correct answer is option 4, which includes all three statements: printMax(1, 2, 2, 1, 4), printMax(new double[]{1, 2, 3}), and printMax(1.0, 2.0, 2.0, 1.0, 4.0). These statements correctly invoke the printMax method in Listing 7.5 from the textbook.

    Rate this question:

  • 31. 

    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)?

    • A.

      Low is 5 and high is 4

    • B.

      Low is 3 and high is 6

    • C.

      Low is 0 and high is 6

    • D.

      Low is 6 and high is 5

    Correct Answer
    A. Low is 5 and high is 4
  • 32. 

    Use the selectionSort method presented in this section to answer this question. Assume list is {3.1, 3.1, 2.5, 6.4, 2.1}, what is the content of list after the first iteration of the outer loop in the method?

    • A.

      2.1, 3.1, 2.5, 6.4, 3.1

    • B.

      3.1, 3.1, 2.5, 2.1, 6.4

    • C.

      2.1, 2.5, 3.1, 3.1, 6.4

    • D.

      2.5, 3.1, 3.1, 6.4, 2.1

    Correct Answer
    A. 2.1, 3.1, 2.5, 6.4, 3.1
    Explanation
    After the first iteration of the outer loop in the selectionSort method, the smallest element in the list is moved to the beginning. In this case, the smallest element is 2.1. Therefore, the content of the list after the first iteration would be 2.1, 3.1, 2.5, 6.4, 3.1.

    Rate this question:

  • 33. 

    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);

    • A.

      Ist1 is 3.1, 3.1, 2.5, 6.4

    • B.

      List1 is 2.5 3.1, 3.1, 6.4

    • C.

      List1 is 6.4, 3.1, 3.1, 2.5

    • D.

      List1 is 3.1, 2.5, 3.1, 6.4

    Correct Answer
    B. List1 is 2.5 3.1, 3.1, 6.4
    Explanation
    The selectionSort method arranges the elements of the array in ascending order. In this case, the array list1 contains the elements 3.1, 3.1, 2.5, and 6.4. After executing the selectionSort method, the smallest element is moved to the first position, resulting in the array becoming 2.5, 3.1, 3.1, and 6.4. Therefore, the correct answer is "list1 is 2.5 3.1, 3.1, 6.4".

    Rate this question:

  • 34. 

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

    • A.

      Java.util.Arrays(scores)

    • B.

      Java.util.Arrays.sorts(scores)

    • C.

      Java.util.Arrays.sort(scores)

    • D.

      Njava.util.Arrays.sortArray(scores)

    Correct Answer
    C. Java.util.Arrays.sort(scores)
    Explanation
    The correct answer is java.util.Arrays.sort(scores). This method is used to sort the array "scores" of the double[] type. The sort() method is a part of the Arrays class in the java.util package and it arranges the elements of the array in ascending order.

    Rate this question:

  • 35. 

    Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return?  

    • A.

      2

    • B.

      -1

    • C.

      2

    • D.

      4

    Correct Answer
    C. 2
    Explanation
    The correct answer is 2 because the binarySearch() method in the Arrays class searches for the specified element (30) in the given array (scores). It returns the index of the element if it is found in the array, otherwise it returns a negative value. In this case, since 30 is present in the array at index 2, the method returns 2.

    Rate this question:

  • 36. 

    Ssume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 3) return?  

    • A.

      -2

    • B.

      -1

    • C.

      1

    • D.

      2

    Correct Answer
    A. -2
    Explanation
    The value returned by java.util.Arrays.binarySearch(scores, 3) is -2. This method searches for the specified element (in this case, 3) in the array (scores) using the binary search algorithm. Since 3 is not present in the array, the method returns the index of the next smaller element (-2) as a negative value.

    Rate this question:

  • 37. 

     Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of System.out.println(java.util.Arrays.toString(scores))?  

    • A.

      [1, 20, 30, 40, 50]

    • B.

      {1, 20, 30, 40, 50}

    • C.

      {1 20 30 40 50}

    • D.

      1 20 30 40 50]

    Correct Answer
    A. [1, 20, 30, 40, 50]
    Explanation
    The output of System.out.println(java.util.Arrays.toString(scores)) will be [1, 20, 30, 40, 50]. This is because the Arrays.toString() method converts the array elements into a string representation, and the output is enclosed in square brackets with each element separated by a comma and a space.

    Rate this question:

  • 38. 

    How can you get the word "abc" in the main method from the following call? java Test "+" 3 "abc" 2

    • A.

      Args[0]

    • B.

      Args[2]

    • C.

      Args[3]

    • D.

      Args[1]

    Correct Answer
    B. Args[2]
    Explanation
    In the given code, the main method is called with three arguments: "3", "abc", and "2". The arguments are stored in the args array. The answer "args[2]" indicates that the word "abc" can be obtained from the main method by accessing the element at index 2 of the args array.

    Rate this question:

  • 39. 

        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)?

    • A.

      Ow is 5 and high is 4

    • B.

      Ow is 3 and high is 6

    • C.

      Low is 0 and high is 6

    • D.

      Low is 6 and high is 5

    Correct Answer
    A. Ow is 5 and high is 4
  • 40. 

    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?  

    • A.

      Nt count = args.length - 1;

    • B.

      Int count = args.length;

    • C.

      Int count = 0; while (args[count] != null) count ++;

    • D.

      Int count=0; while (!(args[count].equals(""))) count ++;

    Correct Answer
    B. Int count = args.length;
    Explanation
    The correct answer is "int count = args.length;" because the "args.length" returns the number of elements in the "args" array, which represents the number of arguments passed via the command line. By subtracting 1 from the length, it excludes the name of the class that is being invoked.

    Rate this question:

  • 41. 

    Which correctly creates an array of five empty Strings?  

    • A.

      String[] a = {"", "", "", "", ""};

    • B.

      String[] a = new String [5];

    • C.

      String[5] a;

    • D.

      String[ ] a = new String [5]; for (int i = 0; i < 5; a[i++] = null);

    Correct Answer
    A. String[] a = {"", "", "", "", ""};
    Explanation
    The correct answer is `String[] a = {"", "", "", "", ""};` because it creates an array of five empty strings by initializing each element with an empty string.

    Rate this question:

  • 42. 

     Identify the problems in the following code. public class Test {   public static void main(String argv[]) {     System.out.println("argv.length is " + argv.length);   } } 

    • A.

      If you run this program without passing any arguments, the program would have a runtime error because argv is null.

    • B.

      The program has a compile error because String args[] is wrong and it should be replaced by String args[].

    • C.

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

    • D.

      If you run this program without passing any arguments, the program would have a runtime error because argv is null.

    Correct Answer
    C. If you run this program without passing any arguments, the program would display argv.length is 0.
    Explanation
    The correct answer is that if you run this program without passing any arguments, the program would display argv.length is 0. This is because when no arguments are passed, the length of the argv array would be 0, indicating that no arguments were provided.

    Rate this question:

  • 43. 

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

    • A.

      A[0][0]

    • B.

      A[1][1]

    • C.

      A[0][1]

    • D.

      A[1][0]

    Correct Answer
    A. A[0][0]
    Explanation
    The index variable for the element at the first row and first column in array a is a[0][0]. This is because in a two-dimensional array, the first index represents the row number and the second index represents the column number. In this case, a[0][0] represents the element in the first row (row 0) and first column (column 0) of array a.

    Rate this question:

  • 44. 

    When you create an array using the following statement, the element values are automatically initialized to 0. int[][] matrix = new int[5][5];

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    When you create a two-dimensional array using the statement "int[][] matrix = new int[5][5];", the element values are automatically initialized to 0. This means that all the elements in the array will have a value of 0 by default.

    Rate this question:

  • 45. 

        How many elements are array matrix (int[][] matrix = new int[5][5])?  

    • A.

      25

    • B.

      30

    • C.

      36

    • D.

      25

    Correct Answer
    A. 25
    Explanation
    The given array matrix is declared as int[5][5], which means it is a 2-dimensional array with 5 rows and 5 columns. To find the total number of elements in the array, we multiply the number of rows (5) by the number of columns (5), which gives us 25. Therefore, the correct answer is 25.

    Rate this question:

  • 46. 

    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]);   } }

    • A.

      The program has a compile error because new boolean[3][] is wrong.

    • B.

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

    • C.

      The program has a runtime error because x[2][2] is null.

    • D.

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

    Correct Answer
    B. The program runs and displays x[2][2] is false.
    Explanation
    The program runs and displays x[2][2] is false because the code initializes a 2D boolean array, x, with different sizes for each row. The first row has a size of 1, the second row has a size of 2, and the third row has a size of 3. When accessing x[2][2], it is accessing the third row (index 2) and the third element (index 2) of that row. Since the array is initialized with boolean values, and boolean default values are false, x[2][2] will be false.

    Rate this question:

  • 47. 

        Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, what are x.length are x[0].length?  

    • A.

      3 & 2

    • B.

      2 and 1

    • C.

      2 and 2

    • D.

      3 and 3

    Correct Answer
    A. 3 & 2
    Explanation
    The given code initializes a 2D array x with 3 rows and 2 columns. The length of an array is the number of elements it contains. Therefore, x.length will give the number of rows in the array, which is 3. Similarly, x[0].length will give the number of columns in the array, which is 2.

    Rate this question:

  • 48. 

    Assume int[][] x = {{1, 2}, {3, 4, 5}, {5, 6, 5, 9}}, what are x[0].length, x[1].length, and x[2].length?

    • A.

      2,3,4

    • B.

      4,3,2

    • C.

      1,2,3

    • D.

      5,4,3

    Correct Answer
    A. 2,3,4
    Explanation
    The variable x is a 2-dimensional array with 3 rows. The length of x[0] is 2 because it represents the length of the first row which contains 2 elements. The length of x[1] is 3 because it represents the length of the second row which contains 3 elements. The length of x[2] is 4 because it represents the length of the third row which contains 4 elements.

    Rate this question:

  • 49. 

    When you create an array using the following statement, the element values are automatically initialized to 0. int[][] matrix = new int[5][5];

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    When an array is created using the statement "int[][] matrix = new int[5][5];", the element values are automatically initialized to 0. This means that all the elements in the array will have the value 0 by default.

    Rate this question:

  • 50. 

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

    • A.

      A[1][1]

    • B.

      A[0][0]

    • C.

      A[1][2]

    • D.

      A[3][4]

    Correct Answer
    B. A[0][0]
    Explanation
    The index variable for the element at the first row and first column in array a is a[0][0]. In programming, arrays are usually zero-indexed, which means that the first element is accessed using the index 0. Therefore, the element at the first row and first column in array a can be accessed using the index a[0][0].

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 20, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • May 07, 2018
    Quiz Created by
    G
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.