Java Quiz-2

110 Questions | Attempts: 136
Share

SettingsSettingsSettings
Java Quizzes & Trivia

. Created by Rahul and Anurag


Questions and Answers
  • 1. 

    Consider the following code and choose the correct output: class Test{ public static void main(String args[]){ int a=5; if(a=3){ System.out.print("Three");}else{ System.out.print("Five");}}} Compilation error Three Five Compiles but no output

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    A. Option 1
  • 2. 

    Given: public class Batman { int squares = 81; public static void main(String[] args) { new Batman().go(); } void go() { incr(++squares); System.out.println(squares); } void incr(int squares) { squares += 10; } } What is the result? 81 82 91 92

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. Option 2
  • 3. 

    Public void foo( boolean a, boolean b) { if( a ) { System.out.println("A"); /* Line 5 */ } else if(a && b) /* Line 7 */ { System.out.println( "A && B"); } else /* Line 11 */ { if ( !b ) { System.out.println( "notB") ; } else { System.out.println( "ELSE" ) ; } } } What would be the result? If a is true and b is false then the output is "notB" If a is true and b is true then the output is "A && B" If a is false and b is false then the output is "ELSE" If a is false and b is true then the output is "ELSE"

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. Option 4
  • 4. 

    What is the value of ’n’ after executing the following code? int n = 10; int p = n + 5; int q = p - 10; int r = 2 * (p - q); switch(n) { case p: n = n + 1; case q: n = n + 2; case r: n = n + 3; default: n = n + 4; } 14 28 Compilation Error 10

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 5. 

    Public class While { public void loop() { int x= 0; while ( 1 ) /* Line 6 */ { System.out.print("x plus one is " + (x + 1)); /* Line 8 */ } } } Which statement is true? There is a syntax error on line 1 There are syntax errors on lines 1 and 6 There are syntax errors on lines 1, 6, and 8 There is a syntax error on line 6

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. Option 4
  • 6. 

    Which of the following loop bodies DOES compute the product from 1 to 10 like (1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10)? int s = 1; for (int i = 1; i <= 10; i++) { <What to put here?> } s += i * i; s++;  s = s + s * i; s *= i;

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. Option 4
  • 7. 

    Given: class Atom { Atom() { System.out.print("atom "); } } class Rock extends Atom { Rock(String type) { System.out.print(type); } } public class Mountain extends Rock { Mountain() { super("granite "); new Rock("granite "); } public static void main(String[] a) { new Mountain(); } } What is the result? Compilation fails. granite granite atom granite granite atom granite atom granite

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. Option 4
  • 8. 

    Which of the following statements are true regarding wrapper classes? (Choose TWO) String is a wrapper class Double has a compareTo() method Character has a intValue() method Byte extends Number

    • A.

      Option1

    • B.

      Option2

    • C.

      Option3

    • D.

      Option4

    Correct Answer(s)
    B. Option2
    D. Option4
  • 9. 

    What are the thing to be placed to complete the code? class Wrap { public static void main(String args[]) { _______________ iOb = ___________ Integer(100); int i = iOb.intValue(); System.out.println(i + " " + iOb); // displays 100 100 } } int, int Integer, new Integer, int int, Integer

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. Option 2
  • 10. 

    Public class SwitchTest { public static void main(String[] args) { System.out.println("value =" + switchIt(4)); } public static int switchIt(int x) { int j = 1; switch (x) { case 1: j++; case 2: j++; case 3: j++; case 4: j++; case 5: j++; default: j++; } return j + x; } } What will be the output of the program? value = 8 value = 2 value = 4 value = 6

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    A. Option 1
  • 11. 

    Given: public class Barn { public static void main(String[] args) { new Barn().go("hi", 1); new Barn().go("hi", "world", 2); } public void go(String... y, int x) { System.out.print(y[y.length - 1] + " "); } } What is the result? hi hi hi world world world Compilation fails.

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. Option 4
  • 12. 

    Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ int x=034; int y=12; int ans=x+y; System.out.println(ans); }} 40 46 compilation error Compiles but error at run time

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    A. Option 1
  • 13. 

    11. double input = 314159.26; 12. NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN); 13. String b; 14. //insert code here Which code, inserted at line 14, sets the value of b to 314.159,26? b = nf.parse( input ); b = nf.format( input ); b = nf.equals( input ); b =nf.parseObje ct( input );

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. Option 2
  • 14. 

    Consider the following code and choose the correct option: class Test{ public static void main(String ar[]){ TreeMap<Integer,String> tree = new TreeMap<Integer,String>(); tree.put(1, "one"); tree.put(2, "two"); tree.put(3, "three"); tree.put(4,"Four"); System.out.println(tree.higherKey(2)); System.out.println(tree.ceilingKey(2)); System.out.println(tree.floorKey(1)); System.out.println(tree.lowerKey(1)); }} 3 2 1 null   3 2 1 1 2 2 1 1 4 2 1 1

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    A. Option 1
  • 15. 

    Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ Long data=23; System.out.println(data); }}  23 Compilation error Compiles but error at run time None of the listed options

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. Option 2
  • 16. 

    Class AutoBox { public static void main(String args[]) { int i = 10; Integer iOb = 100; i = iOb; System.out.println(i + " " + iOb); } } whether this code work properly, if so what would be the result? No,Compilation error No, Runtime error Yes, 10, 100 Yes, 100,100

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. Option 4
  • 17. 

    Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ Long l=0l; System.out.println(l.equals(0));}} Compilation error true false 1 

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 18. 

    Int I = 0; outer: while (true) { I++; inner: for (int j = 0; j < 10; j++) { I += j; if (j == 3) continue inner; break outer; } continue outer; } System.out.println(I); What will be thr result? 3 2 4 1

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. Option 4
  • 19. 

    What will be the result of attempting to compile and run the following class? Public class IFTest{ public static void main(String[] args){ int i=10; if(i==10) if(i<10) System.out.println("a"); else System.out.println("b"); }} The code will fail to compile because the syntax of the if statement is incorrect The code will fail to compile because the compiler will not be able to determine which if statement the else clause belongs to The code will compile correctly and display the letter a,when run The code will compile correctly and display the letter b,when run

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. Option 4
  • 20. 

    What is the output of the following code : class try1{ public static void main(String[] args) { System.out.println("good"); while(false){ System.out.println("morning"); } } } good good morning morning …. compiler error runtime error

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 21. 

    Consider the following code and choose the correct output: class Test{ public static void main(String args[]){ int num=3; switch(num){ case 1: case 3: case 4: { System.out.println("bat man"); } case 2: case 5: { System.out.println("spider man"); }break; } }} bat man Compilation error bat man spider man spider man

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 22. 

    Given: int n = 10; switch(n) { case 10: n = n + 1; case 15: n = n + 2; case 20: n = n + 3; case 25: n = n + 4; case 30: n = n + 5; } System.out.println(n); What is the value of ’n’ after executing the following code? 23 32 25 Compilation Error

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 23. 

    What will be the output of following code? TreeSet map = new TreeSet(); map.add("one"); map.add("two"); map.add("three"); map.add("four"); map.add("one"); Iterator it = map.iterator(); while (it.hasNext() ) { System.out.print( it.next() + " " ); } one two three four four three two one four one  three two one two three four one

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 24. 

    Public class Test { public static void main(String [] args) { int x = 5; boolean b1 = true; boolean b2 = false; if ((x == 4) && !b2 ) System.out.print("1 "); System.out.print("2 "); if ((b2 = true) && b1 ) System.out.print("3 "); } } What is the result? 2 3 2 3 1 2 3

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 25. 

    Which of these statements are true? HashTable is a sub class of Dictionary ArrayList is a sub class of Vector LinkedList is a subclass of ArrayList Stack is a subclass of Vector

    • A.

      Option1

    • B.

      Option2

    • C.

      Option3

    • D.

      Option4

    Correct Answer(s)
    A. Option1
    D. Option4
  • 26. 

    Given: import java.util.*; public class Explorer3 { public static void main(String[] args) { TreeSet<Integer> s = new TreeSet<Integer>(); TreeSet<Integer> subs = new TreeSet<Integer>(); for(int i = 606; i < 613; i++) if(i%2 == 0) s.add(i); subs = (TreeSet)s.subSet(608, true, 611, true); subs.add(629); System.out.println(s + " " + subs); } } What is the result? Compilation fails. [608, 610, 612, 629] [608, 610] An exception is thrown at runtime. [608, 610, 612, 629] [608, 610, 629]

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 27. 

    What is the output : class try1{ public static void main(String[] args) { int x=1; if(x--) System.out.println("good"); else System.out.println("bad"); } } good bad compile error   run time error

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 28. 

    Consider the following code and choose the correct output: class Test{ public static void main(String args[]){ int num='b'; switch(num){ default :{ System.out.print("default");} case 100 : case 'b' : case 'c' : { System.out.println("brownie"); break;} case 200: case 'e': { System.out.println("pastry"); }break; } }} brownie default brownie compilation error default

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    A. Option 1
  • 29. 

    Given: int a = 5; int b = 5; int c = 5; if (a > 3) if (b > 4) if (c > 5) c += 1; else c += 2; else c += 3; c += 4; What is the value of variable c after executing the following code? 3 5 7 11

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. Option 4
  • 30. 

    Given: Float pi = new Float(3.14f); if (pi > 3) { System.out.print("pi is bigger than 3. "); } else { System.out.print("pi is not bigger than 3. "); } finally { System.out.println("Have a nice day."); } What is the result? Compilation fails. pi is bigger than 3. An exception occurs at runtime. pi is bigger than 3. Have a nice day.

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    A. Option 1
  • 31. 

    Given: public void go() { String o = ""; z: for(int x = 0; x < 3; x++) { for(int y = 0; y < 2; y++) { if(x==1) break; if(x==2 && y==1) break z; o = o + x + y; } } System.out.println(o); } What is the result when the go() method is invoked? 0 0 0 0 0 1 0 0 0 1 2 0 0 0 0 1 2 0 21

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 32. 

    Examine the following code: int count = 1; while ( ___________ ) { System.out.print( count + " " ); count = count + 1; } System.out.println( ); What condition should be used so that the code prints:1 2 3 4 5 6 7 8 count < 9 count+1 <= 8 count < 8 count != 8

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    A. Option 1
  • 33. 

    What will be the output of the program? public class Switch2 { final static short x = 2; public static int y = 0; public static void main(String [] args) { for (int z=0; z < 3; z++) { switch (z) { case y: System.out.print("0 "); /* Line 11 */ case x-1: System.out.print("1 "); /* Line 12 */ case x: System.out.print("2 "); /* Line 13 */ } } } } 0 1 2 0 1 2 1 2 2 Compilation fails at line 11 Compilation fails at line 12.

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 34. 

    Given: int x = 0; int y = 10; do { y--; ++x; } while (x < 5); System.out.print(x + "," + y); What is the result? 5,6 5,5 6,5 6,6

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. Option 2
  • 35. 

    What is the output : class Test{ public static void main(String[] args) { int a=5,b=10,c=1; if(a>c){ System.out.println("success"); } else{ break; } } } success runtime error compiler error none of the listed options

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 36. 

    Consider the following code and choose the correct output: public class Test{ public static void main(String[] args) { int x = 0; int y = 10; do { y--; ++x; } while (x < 5); System.out.print(x + "," + y); } } 5,6 5,5 6,5 6,6

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. Option 2
  • 37. 

    Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ int l=7; Long L = (Long)l; System.out.println(L); }} 7 Compilation error Compiles but error at run time None of the listed options

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. Option 2
  • 38. 

    Given: double height = 5.5; if(height-- >= 5.0) System.out.print("tall "); if(--height >= 4.0) System.out.print("average "); if(height-- >= 3.0) System.out.print("short "); else System.out.print("very short "); } What would be the Result? tall tall short short very short

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. Option 2
  • 39. 

    Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ String hexa = "0XFF"; int number = Integer.decode(hexa); System.out.println(number); }} Compilation error 1515 255 Compiles but error at run time

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 40. 

    Consider the following code and choose the correct option: int i = l, j = -1; switch (i) { case 0, 1: j = 1; case 2: j = 2; default: j = 0; }System.out.println("j = " + j); j = -1 j = 0 j = 1 Compilation fails

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. Option 4
  • 41. 

    Which of the following statements about arrays is syntactically wrong? Person[] p = new Person[5]; Person p[5]; Person[] p []; Person p[][] = new Person[2][];

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. Option 2
  • 42. 

    What will be the output of following code? import java.util.*; class I { public static void main (String[] args) { Object i = new ArrayList().iterator(); System.out.print((i instanceof List)+","); System.out.print((i instanceof Iterator)+","); System.out.print(i instanceof ListIterator); } } Prints: false, false, false Prints: false, false, true Prints: false, true, false Prints: false, true, true

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 43. 

    Given: public static void test(String str) { int check = 4; if (check = str.length()) { System.out.print(str.charAt(check -= 1) +", "); } else { System.out.print(str.charAt(0) + ", "); } } and the invocation: test("four"); test("tee"); test("to"); What is the result? r, t, t, r, e, o, Compilation fails. An exception is thrown at runtime.

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 44. 

    What will be the output of the program? int x = 3; int y = 1; if (x = y) /* Line 3 */ { System.out.println("x =" + x); } x = 1 x = 3 Compilation fails. The code runs with no output.

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 45. 

    Import java.util.SortedSet; import java.util.TreeSet; public class Main { public static void main(String[] args) { TreeSet<String> tSet = new TreeSet<String>(); tSet.add("1"); tSet.add("2"); tSet.add("3"); tSet.add("4"); tSet.add("5"); SortedSet sortedSet =_____________("3"); System.out.println("Head Set Contains : " + sortedSet); } } What is the missing method in the code to get the head set of the tree set? tSet.headSet tset.headset headSet HeadSet

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    A. Option 1
  • 46. 

    Consider the following code and choose the correct output: class Test{ public static void main(String args[]){ int num=3; switch(num){ default :{ System.out.print("default");} case 1: case 3: case 4: { System.out.println("apple"); break;} case 2: case 5: { System.out.println("black berry"); }break; } }} apple default apple compilation error default

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    A. Option 1
  • 47. 

    Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ Long L = null; long l = L; System.out.println(L); System.out.println(l); }} null 0 Compilation error Compiles but error at run time 0 null

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 48. 

    What does the following code fragment write to the monitor? int sum = 21; if ( sum != 20 ) System.out.print("You win "); else System.out.print("You lose "); System.out.println("the prize."); What does the code fragment prints? You win the prize You lose the prize. You win You lose

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    A. Option 1
  • 49. 

    Which statements are true about maps? (Choose TWO) The return type of the values() method is set Changes made in the Set view returned by keySet() will be reflected in the original map The Map interface extends the Collection interface All keys in a map are unique

    • A.

      Option1

    • B.

      Option2

    • C.

      Option3

    • D.

      Option4

    Correct Answer(s)
    B. Option2
    D. Option4
  • 50. 

    Which one do you like?Which collection implementation is suitable for maintaining an ordered sequence of objects,when objects are frequently inserted in and removed from the middle of the sequence? TreeMap HashSet Vector LinkedList

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. Option 4

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Jan 28, 2019
    Quiz Edited by
    ProProfs Editorial Team
  • Jan 08, 2019
    Quiz Created by
    Rahul Kumar
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.