Arrays In Java! Programming Trivia Questions Quiz

5 Questions | Total Attempts: 125

SettingsSettingsSettings
Arrays In Java! Programming Trivia Questions Quiz - Quiz

Are you looking to test your knowledge on arrays in java? This programming trivia questions quiz is exactly what you need as it will help you make the best use of java programming and the different ways that you can get the most from it. Do give it a shot and all the best as you dive deeper into all things java.


Questions and Answers
  • 1. 
    Given the code snippet from a compiled Java source file: public class MyFile{ public static void main(String[] args) { String arg1 = args[1]; String arg2 = args[2]; String arg3 = args[3]; System.out.println("Arg is "+ arg3); } } Which command-line arguments should you pass to the program to obtain the following result? Arg is 2
    • A. 

      Java MyFile 0 1 2 3

    • B. 

      Java MyFile 1 3 2 2

    • C. 

      Java MyFile 1 2 2 3 4

    • D. 

      Java MyFile 2 2 2

  • 2. 
    Given the following code: public static void main(String[] args) { String[] planets = {"Mercury", "Venus", "Earth", "Mars"}; System.out.println(planets.length); System.out.println(planets[1].length()); } What is the output?
    • A. 

      4 21

    • B. 

      4 5

    • C. 

      5 4

    • D. 

      3 5

    • E. 

      4 4

    • F. 

      4 7

  • 3. 
    Given the code fragment: public class Test{ public static void main(String[] args) { /* line 3*/ array[0] = 10; array[1] = 20; System.out.print(array[0]+ ": "+array[1]); } } Which code fragment, when inserted at line 3, enables the code to print 10:20?
    • A. 

      Int [] array = new int[2];

    • B. 

      Int [] array; array = int[2];

    • C. 

      Int array = new int[2];

    • D. 

      Int array[2];

  • 4. 
    The following grid shows the state of a 2D array: This grid is created with the following code:  char[][] grid = new char[3][3]; grid[1][1] = 'x'; grid[0][0] = 'o'; grid[2][1] = 'x'; grid[0][1] = 'o'; grid[2][2] = 'x'; grid[1][2] = 'o'; //line n1 Which line of code, when inserted in place of //line n1, adds an x into the grid so that the grid contains three consecutive x's?
    • A. 

      Grid[1][2] = 'x';

    • B. 

      Grid[1][3] = 'x';

    • C. 

      Grid[0][2] = 'x';

    • D. 

      Grid[3][1] = 'x';

    • E. 

      Grid[2][0] = 'x';

  • 5. 
    Given the code fragment: int nums1[] = new int[3]; int nums2[] = {1,2,3,4,5}; nums1 = nums2; System.out.print("x: " + nums1[3]); What is the result?
    • A. 

      ArrayIndexOutOfBoundsException is thrown in runtime.

    • B. 

      Compilation fails

    • C. 

      X: 0

    • D. 

      X: null

    • E. 

      X: 4

    • F. 

      X: 3

Back to Top Back to top