Can You Answer These Java Programming Questions? Trivia Quiz

10 Questions | Total Attempts: 289

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:

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

  • 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

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

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

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

  • 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

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

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

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