Can You Answer These Java Programming Questions? Trivia 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,932,599
Questions: 10 | Attempts: 524

SettingsSettingsSettings
Can You Answer These Java Programming Questions? Trivia Quiz - Quiz

Can you answer these java programming questions? There are a lot of people who think that they can never fail any question they are asked when it comes to java programming, and to help you see if you are right in assuming you are one of these people, we have prepared the quiz below. Do give it a try and get to see if you are all that!


Questions and Answers
  • 1. 

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

    • A.

      An ArrayIndexOutOfBoundsException is thrown in runtime.

    • B.

      Compilation fails.

    • C.

      1: 2: 3: 4: 5:

    • D.

      1: 2: 3:

    Correct Answer
    C. 1: 2: 3: 4: 5:
    Explanation
    The code creates two arrays, nums1 and nums2. The size of nums1 is set to 3 and nums2 is initialized with values {1,2,3,4,5}. Then, the reference of nums2 is assigned to nums1, making nums1 refer to the same array as nums2. The for-each loop is used to iterate over the elements of nums1 and print them. Since nums1 now refers to an array with 5 elements, the loop will print all the elements of nums2, resulting in the output "1: 2: 3: 4: 5:". Therefore, the correct answer is "1: 2: 3: 4: 5:".

    Rate this question:

  • 2. 

    Given the code fragment: public static void main(String[] args) { int [] arr = {1,2,3,4}; int i = 0; do { System.out.print(arr[i] + " "); i++; } while (i < arr.length -1); } What is the result?

    • A.

      1 2 3 4 Followed by an ArrayIndexOutBoundsException

    • B.

      1 2 3

    • C.

      1 2 3 4

    • D.

      Compilations fails.

    Correct Answer
    B. 1 2 3
    Explanation
    The code fragment initializes an array called "arr" with values 1, 2, 3, and 4. It then uses a do-while loop to iterate through the array and print each element followed by a space. The loop continues as long as the variable "i" is less than the length of the array minus 1.

    In this case, the length of the array is 4, so the loop will iterate 3 times (0, 1, 2). The elements at indexes 0, 1, and 2 of the array are 1, 2, and 3 respectively. Therefore, the result will be "1 2 3".

    Rate this question:

  • 3. 

    Given the code fragment: public static void main(String[] args) { int [][]arr = new int[2][4]; arr [0] = new int[]{1,3,5,7}; arr [1] = new int[]{1,3}; for (int[] a : arr) { for (int i : a) { System.out.print(i + " "); } System.out.println(); } } What is the result?

    • A.

      1 3 5 7 1 3

    • B.

      1 3 1 3

    • C.

      1 3 1 3 0 0

    • D.

      Compilation fails.

    • E.

      1 3 Followed by an ArrayIndexOutOfBoundsException

    Correct Answer
    A. 1 3 5 7 1 3
    Explanation
    The code creates a 2-dimensional array `arr` with 2 rows and 4 columns. The first row is initialized with the values 1, 3, 5, and 7, while the second row is initialized with the values 1 and 3.

    The nested for loop iterates over each row of the array and then over each element in the row. It prints each element followed by a space. After printing all the elements in a row, it prints a new line.

    Therefore, the output will be:
    1 3 5 7
    1 3

    Rate this question:

  • 4. 

    Given the code fragment: public static void main(String[] args) { int [] stack = {10,20,30}; int size = 3; int idx = 0; /*line n1*/ System.out.println("The Top element: "+ stack[idx]); } Which code fragment, inserted at line n1, prints The Top element: 30?

    • A.

      while (idx <= size - 1){ idx++; }

    • B.

      do { idx++; } while (idx <= size);

    • C.

      while (idx < size) { idx++; }

    • D.

      do { idx++; } while (idx >= size);

    • E.

      do { idx++; } while (idx < size -1);

    Correct Answer
    E. do { idx++; } while (idx < size -1);
    Explanation
    The code fragment that is inserted at line n1 is a do-while loop. This loop will increment the value of idx by 1 each time it runs, until idx is less than size - 1. Since the initial value of idx is 0 and size is 3, the loop will run twice and the value of idx will become 2. After the loop, the statement System.out.println("The Top element: "+ stack[idx]); will print "The Top element: 30" because idx is now pointing to the last element of the stack array, which is 30.

    Rate this question:

  • 5. 

    Given the code fragment: class Z{ public static void main(String[] args) { int ii = 0; int jj = 7; for ( ii = 0; ii < jj - 1; ii = ii +2) { System.out.println(ii + " "); } } } What is the result?

    • A.

      2 4

    • B.

      0 2 4 6

    • C.

      0 2 4

    • D.

      Compilations fails.

    Correct Answer
    C. 0 2 4
    Explanation
    The code fragment initializes two variables, ii and jj, with values 0 and 7 respectively. Then, it enters a for loop that continues as long as ii is less than jj - 1. Inside the loop, ii is incremented by 2 in each iteration and its value is printed. Therefore, the loop will execute 3 times, resulting in the output "0 2 4".

    Rate this question:

  • 6. 

    Given the code fragment: public static void main(String[] args) { int array[] = {0,20,30,40,50}; int x= array.length; /*line n1*/ } Which two code fragments can be independently inserted at line n1 enable the code to print the elements of the array in reverse order? (Choose two)

    • A.

      while (x >= 0) { System.out.print(array[x]); x--; }

    • B.

      do { x--; System.out.print(array[x]); } while ( x >= 0);

    • C.

      do { System.out.print(array[x]); --x; } while (x >= 0);

    • D.

      while(x > 0) { x--; System.out.print(array[x]); }

    • E.

      while (x > 0) { System.out.print(array[--x]); }

    Correct Answer(s)
    D. while(x > 0) { x--; System.out.print(array[x]); }
    E. while (x > 0) { System.out.print(array[--x]); }
    Explanation
    The two code fragments can be independently inserted at line n1 to enable the code to print the elements of the array in reverse order. The first code fragment uses a while loop to iterate through the array starting from the last element and printing each element. The second code fragment also uses a while loop, but it decrements the value of x before printing the element, effectively printing the elements in reverse order.

    Rate this question:

  • 7. 

    Given the code fragment:       int wd = 0; String days[] = {"sun", "mon", "wed", "sat"}; for(String s: days){ switch (s) { case "sun": wd -= 1; break; case "mon": wd++; case "wed": wd +=2; } } System.out.println(wd); What is the result?

    • A.

      Compilation fails.

    • B.

      -1

    • C.

      4

    • D.

      3

    Correct Answer
    C. 4
    Explanation
    The code fragment initializes a variable "wd" to 0 and creates an array "days" with four elements. The code then enters a for-each loop where it iterates over each element in the "days" array. Inside the loop, there is a switch statement that checks the value of each element. In the case of "sun", it subtracts 1 from "wd". In the case of "mon", it adds 1 to "wd" and falls through to the next case without a break statement. In the case of "wed", it adds 2 to "wd". Since there is no break statement after the "wed" case, the code continues to execute the next line outside the switch statement, which is the end of the for-each loop. Therefore, the final value of "wd" is 4, which is the correct answer.

    Rate this question:

  • 8. 

    Given: class Test{ public static void main(String[] args) { int numbers[]; numbers = new int[2]; numbers[0] = 10; numbers[1] = 20; numbers = new int[4]; numbers[2] = 30; numbers[3] = 40; for (int x : numbers) { System.out.print(" " + x); } } } What is the result?

    • A.

      Compilation fails.

    • B.

      10 20 30 40

    • C.

      0 0 30 40

    • D.

      An exception is thrown at runtime.

    Correct Answer
    C. 0 0 30 40
    Explanation
    The code initializes an array called "numbers" with a size of 2 and assigns values to the first two elements. Then, it reassigns a new array of size 4 to "numbers" and assigns values to the last two elements. Finally, it prints out the elements of "numbers" using a for-each loop. Since the first two elements were not assigned any values in the second assignment, they default to 0. Therefore, the result is "0 0 30 40".

    Rate this question:

  • 9. 

    Given the code fragment: class Tee{ public static void main(String[] args) { String cs[] = {"US", "UK"}; int wc = 0; while (wc < cs.length) { int count = 0; do {                ++count; } while (count < cs[wc].length()); System.out.println(cs[wc] + ": " + count); wc++; } } } What is the result?

    • A.

      US: 2 UK: 2

    • B.

      US: 3 UK: 3

    • C.

      US: 2 UK: 4

    • D.

      An ArrayIndexOutOfBoundsException is thrown at runtime.

    Correct Answer
    A. US: 2 UK: 2
    Explanation
    The given code fragment initializes an array of strings with two elements: "US" and "UK". It then uses a while loop to iterate through each element of the array. Inside the loop, a do-while loop is used to count the number of characters in each string. The count variable is incremented until it reaches the length of the current string. After each iteration of the do-while loop, the string and the count are printed. Since both "US" and "UK" have a length of 2, the result will be "US: 2" and "UK: 2".

    Rate this question:

  • 10. 

    Given the code fragment: class TestA{ int count = 0; int i = 0; public void changeCount(){ while(i < 5){ i++; count++; } } public static void main(String[] args) { TestA check1 = new TestA(); TestA check2 = new TestA(); check1.changeCount(); check2.changeCount(); System.out.println(check1.count + " : "+ check2.count); } } What is the result?

    • A.

      5 : 5

    • B.

      10 : 10

    • C.

      5: 10

    • D.

      Compilation fails.

    Correct Answer
    A. 5 : 5
    Explanation
    The code creates two instances of the TestA class, check1 and check2. Both instances call the changeCount() method, which increments the i variable and the count variable inside a while loop. Since the while loop condition is i < 5, the loop will run 5 times for each instance. Therefore, both check1 and check2 will have their count variable incremented 5 times. As a result, the output will be "5 : 5".

    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.