Is Exam Chp 6 & 7

59 Questions | Total Attempts: 117

SettingsSettingsSettings
Is Exam Chp 6 & 7 - Quiz

.


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

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

      6

    • B. 

      5

    • C. 

      7

    • D. 

      9

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

  • 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

  • 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

  • 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];

  • 7. 
        How many elements are in array double[] list = new double[5]?  
    • A. 

      4

    • B. 

      5

    • C. 

      0

    • D. 

      6

  • 8. 
        What is the correct term for numbers[99]?  
    • A. 

      Index variable

    • B. 

      Indexed variable

    • C. 

      Array

    • D. 

      Array variable

  • 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.

  • 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.

  • 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'};

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

  • 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

  • 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};

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

      4

    • B. 

      2

    • C. 

      1

    • D. 

      3

  • 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

  • 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.

  • 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.

  • 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

  • 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

  • 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

  • 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

  • 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.

  • 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.

  • 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.

Back to Top Back to top