Core Java MCQ Practice Questions

25 Questions | Total Attempts: 303

SettingsSettingsSettings
Please wait...
Core Java Quizzes & Trivia

Java MCQ is one of the commonly used computer-programming tools used by programmers today. If you are a programmer seeking to become better in your line of duty, then this quiz will bring you closer to you goal.


Questions and Answers
  • 1. 
     What all gets printed when the following gets compiled and run? public class test {     public static void main(String args[]) {          int i=1, j=1;         try {             i++;              j--;             if(i/j > 1)                 i++;        }         catch(ArithmeticException e) {             System.out.println(0);         }         catch(ArrayIndexOutOfBoundsException e) {            System.out.println(1);         }         catch(Exception e) {             System.out.println(2);         }         finally {             System.out.println(3);         }         System.out.println(4);      } }   here
    • A. 

      0 2 3 4

    • B. 

      3 4

    • C. 

      0 3 4

    • D. 

      0 3

  • 2. 
    Consider the following code that contains the class definitions for class ABC, XYZ, and Alphabet. What will be the output of this code?  class ABC extends XYZ { ABC () { super (); System.out.print (" ABC "); } public static void main (String args[]) { XYZ x1 = new ABC (); } }   class XYZ extends Alphabet { XYZ () { System.out.print (" XYZ "); } }   class Alphabet { void Alphabet () { System.out.print (" Alphabet "); } }  
    • A. 

      ABC XYZ Alphabet

    • B. 

      XYZ ABC Alphabet

    • C. 

      ABC XYZ

    • D. 

      XYZ ABC

  • 3. 
    What will be the output of the program when to try to execute   public class Arizona {  int id;  String name;    public Arizona() {  this(”aryabhatta”);  System.out.print(”first “);  }    public Arizona(String name) {  this(420, “aryabhatta”);  System.out.print(”second “);  }    public Arizona(int id, String name) {  this.id = id;  this.name = name;  System.out.print(”third “);  }   public static void main(String[] args) { Arizona b = new Arizona();  System.out.print(b.name +“ “ + b.id);  } }
    • A. 

      First second third aryabhatta 420

    • B. 

      Third second first aryabhatta 0

    • C. 

      Third second first aryabhatta 420

    • D. 

      Compiler error

  • 4. 
    What will be the result of compiling and running the following code ? public class LoopCheck {     public static void main(String args[]) {         int i = 0;         int x = 10;                while ( x > 6 ) {             System.out.print(++i + " ");             x--;         }     } } Select the correct answer :
    • A. 

      1 2 3 4 5

    • B. 

      0 1 2 3 4

    • C. 

      1 2 3 4

    • D. 

      0 1 2 3

  • 5. 
    What all gets printed when the following program is compiled and run. Select the two correct answers.   public class test {    public static void main(String args[]) {       int i, j=1;       i = (j>1)?2:1;       switch(i) {         case 0: System.out.println(0); break;         case 1: System.out.println(1);         case 2: System.out.println(2); break;         case 3: System.out.println(3); break;       }    } }
    • A. 

      0

    • B. 

      1 2

    • C. 

      1

    • D. 

      3

  • 6. 
    ________________________ makes Java platform-independent.  
    • A. 

      JVM

    • B. 

      Java syntax

    • C. 

      Java API

    • D. 

      Bytecodes

  • 7. 
    Byte b = 50 ; b = b * 2 ; System.out.println( " b = " + b ) ;   The above fraction of code prints b = 100.
    • A. 

      True

    • B. 

      False

  • 8. 
    Final methods cannot be overridden but overloaded ?
    • A. 

      True

    • B. 

      False

  • 9. 
    Class Weather         {        static boolean isRaining;        public static void main(String args[])          {          System.out.print(isRaining);        }           } 
    • A. 

      Prints true

    • B. 

      Prints false

    • C. 

      Does not compile as boolean is not initialized

    • D. 

      None

  • 10. 
    What is the name of the method used to start a thread execution? 
    • A. 

      Init();

    • B. 

      Run();

    • C. 

      Start();

    • D. 

      Yield();

  • 11. 
    What is the output of following block of program ? boolean var = false; if(var = true) { System.out.println(“TRUE”); } else { System.out.println(“FALSE”); } 
    • A. 

      Prints TRUE

    • B. 

      Prints FALSE

    • C. 

      Compilation Error

  • 12. 
    Which interface provides the capability to store objects using a key-value pair? 
    • A. 

      Java.util.Set

    • B. 

      Java.util.Map

    • C. 

      Java.util.List

    • D. 

      Java.util.Collection

  • 13. 
    What will be the output of the program?   String s = "hello";  Object o = s;  if( o.equals(s) ) { System.out.println("A");  }  else { System.out.println("B");  }  if( s.equals(o) ) { System.out.println("C");  }  else {  System.out.println("D");  }   1. A 2. B 3. C 4. D 
    • A. 

      2 and 4

    • B. 

      1 and 2

    • C. 

      3 and 4

    • D. 

      1 and 3

  • 14. 
    What will be the output of the program?   public class X  {  public static void main(String [] args)  { try  { badMethod();  System.out.print("A");  }  catch (Exception ex)  { System.out.print("B");  }  finally  { System.out.print("C");  }  System.out.print("D");  }  public static void badMethod()  { throw new Error();  }  } 
    • A. 

      ABCD

    • B. 

      C is printed before exiting with an error message.

    • C. 

      BC is printed before exiting with an error message.

    • D. 

      Compilation Fails

  • 15. 
    Can there be an abstract class with no abstract methods in it? 
    • A. 

      Yes

    • B. 

      No

  • 16. 
    What will be the output of the program?   public class WithoutBook { static int x;  boolean catch1() { x++;  return true;  }  public static void main(String[] args) { if ((catch1() | catch1()) || catch1())  x++;  System.out.println(x);  }  } 
    • A. 

      1

    • B. 

      2

    • C. 

      3

    • D. 

      Compilation Error

  • 17. 
    What will be the output of the program?   String s = "ABC";  s.toLowerCase();  s += "def";  System.out.println(s); 
    • A. 

      Abc

    • B. 

      ABC

    • C. 

      ABCdef

    • D. 

      Compiler Error

  • 18. 
    What will be the output of the program?   try  { Float f1 = new Float("3.0"); int x = f1.intValue(); byte b = f1.byteValue(); double d = f1.doubleValue(); System.out.println(x + b + d); } catch (NumberFormatException e)  { System.out.println("bad number"); } 
    • A. 

      Compilation fails on line 3.

    • B. 

      Compilation fails on line 5.

    • C. 

      Prints bad number

    • D. 

      9.0

  • 19. 
    What will be the output of the program? String a = "newspaper"; a = a.substring(5,7); char b = a.charAt(1); a = a + b; System.out.println(a);
    • A. 

      Apa

    • B. 

      Apea

    • C. 

      App

    • D. 

      Apep

  • 20. 
    What should be at line number 3 to get the total sum of array "sum" ? public int totalSum( int[] sum ){ int a, b= 0 ; //which 'for' loop should be here(line :3)  {  b += sum[ a++ ] ; } return b ; }
    • A. 

      For( a = 1 ; i

    • B. 

      For( a= 0 ; a< sum.length ; )

    • C. 

      For( a = 0 ; a< sum.length() ; a++ )

    • D. 

      For( a = 0 ; a< sum.length ; a++ )

  • 21. 
    Public class Child extends Parent { public static void main(String[] args) { Parent p = new Child(); p.method(); } void method(){ System.out.println("Child method"); } } class Parent { void method(){ System.out.println("Parent method"); } } What is the output?
    • A. 

      Compile time exception

    • B. 

      Runtime exception

    • C. 

      Child method

    • D. 

      Parent method

  • 22. 
    What will be the output of the program? public class WBFoo  {  public static void main(String[] args)  { try  {  return;  }  finally  { System.out.println( "Finally" );  }  }  }
    • A. 

      Finally

    • B. 

      Compilation fails

    • C. 

      An exception is thrown at runtime

    • D. 

      The code runs with no output

  • 23. 
    Which of the following are true about interfaces. Select the correct answer.
    • A. 

      Methods declared in interfaces are implicitly private.

    • B. 

      Variables declared in interfaces are implicitly public, static, and final.

    • C. 

      The keyword implements indicate that an interface inherits from another.

    • D. 

      An interface can not extend any number of interfaces.

  • 24. 
    Which collection class allows you to associate its elements with key values, and allows you to retrieve objects in FIFO (first-in, first-out) sequence?
    • A. 

      Java.util.ArrayList

    • B. 

      Java.util.LinkedHashMap

    • C. 

      Java.util.HashMap

    • D. 

      Java.util.TreeMap

  • 25. 
    Public class CoreJavaOnlineTest1 { public static void main(String[] args) { String s1 = "withoutbook"; String s2 = s1; s1 = null; System.out.println("s1:"+s1+" s2:"+s2); } }
    • A. 

      S1:null s2:withoutbook

    • B. 

      S1:null s2:null

    • C. 

      Exception

    • D. 

      None of the above

Back to Top Back to top