Arrays In Java! Programming Trivia Questions Quiz

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 Catherine Halcomb
C
Catherine Halcomb
Community Contributor
Quizzes Created: 1428 | Total Attempts: 5,929,496
Questions: 5 | Attempts: 204

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

    Correct Answer
    B. Java MyFile 1 3 2 2
    Explanation
    The correct answer is "java MyFile 1 3 2 2". This is because in the code snippet, the program is accessing the third command-line argument (args[3]) and assigning it to the variable arg3. So, to obtain the desired result "Arg is 2", we need to pass the value 2 as the third command-line argument.

    Rate this question:

  • 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

    Correct Answer
    B. 4 5
    Explanation
    The code initializes an array called "planets" with four elements: "Mercury", "Venus", "Earth", and "Mars". The first print statement outputs the length of the array, which is 4. The second print statement accesses the element at index 1 of the array (which is "Venus") and outputs its length, which is 5. Therefore, the output is "4 5".

    Rate this question:

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

    Correct Answer
    A. Int [] array = new int[2];
    Explanation
    The correct answer is "int [] array = new int[2];". This code fragment declares an array named "array" of type int with a length of 2. This allows the subsequent lines of code to assign values to the elements of the array and print them out as 10:20.

    Rate this question:

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

    Correct Answer
    E. Grid[2][0] = 'x';
    Explanation
    The line of code "grid[2][0] = 'x';" adds an 'x' into the grid so that the grid contains three consecutive x's. This is because it assigns the value 'x' to the element at index (2,0) in the 2D array, which is the bottom left corner of the grid.

    Rate this question:

  • 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

    Correct Answer
    E. X: 4
    Explanation
    The code creates two arrays, nums1 and nums2. The size of nums1 is set to 3, but it is then reassigned to nums2. Therefore, nums1 now refers to the same array as nums2, which has a length of 5. When trying to access the element at index 3 of nums1, it will be accessing the element at index 3 of nums2, which is 4. So, the result will be "x: 4".

    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 22, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Sep 22, 2018
    Quiz Created by
    Catherine Halcomb
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.