Core Java MCQ Practice Questions

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 Saranyav
S
Saranyav
Community Contributor
Quizzes Created: 1 | Total Attempts: 474
Questions: 25 | Attempts: 474

SettingsSettingsSettings
Core Java MCQ Practice Questions - Quiz

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

    Correct Answer
    C. 0 3 4
    Explanation
    When the code is compiled and run, the following will be printed: 0, 3, and 4.

    This is because the code enters the try block and encounters the line "if(i/j > 1)". Since j is equal to 0, an ArithmeticException is thrown. The catch block for ArithmeticException is executed, printing 0.

    After the catch block, the finally block is executed, printing 3.

    Finally, the line "System.out.println(4)" is executed, printing 4.

    Rate this question:

  • 2. 

    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

    Correct Answer
    C. Third second first aryabhatta 420
    Explanation
    The output of the program will be "third second first aryabhatta 420". This is because the program creates an instance of the Arizona class in the main method. When this instance is created, the constructor with no arguments is called. This constructor then calls the constructor with a String argument, which in turn calls the constructor with an int and a String argument. Each constructor prints "third", "second", and "first" respectively. Finally, the main method prints the name and id of the instance, which are "aryabhatta" and 420 respectively.

    Rate this question:

  • 3. 

    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

    Correct Answer
    D. XYZ ABC
    Explanation
    The output of this code will be "XYZ ABC".

    In the main method, an object of type ABC is created and assigned to a variable of type XYZ. When this object is created, the constructor of ABC is called. The constructor of ABC calls the constructor of XYZ using the "super" keyword. The constructor of XYZ then prints "XYZ".

    After the constructor of XYZ is called, the constructor of ABC continues executing and prints "ABC".

    There is no output of "Alphabet" because the method with the same name is actually a constructor, not a regular method, and constructors do not have a return type.

    Rate this question:

  • 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

    Correct Answer
    C. 1 2 3 4
    Explanation
    The code initializes two variables, i and x, with the values 0 and 10 respectively. The while loop will continue as long as x is greater than 6. Inside the loop, the code prints the value of i incremented by 1, followed by a space. Then, it decrements the value of x by 1. This process repeats until x becomes less than or equal to 6. Therefore, the output of the code will be "1 2 3 4".

    Rate this question:

  • 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

    Correct Answer
    B. 1 2
    Explanation
    When the program is run, the value of j is 1. The expression (j>1) evaluates to false, so the value of i is assigned as 1. Then, the switch statement checks the value of i. Since i is 1, the case 1 is matched and the statement System.out.println(1) is executed. However, there is no break statement after this case, so the program continues to execute the next case. In this case, the statement System.out.println(2) is executed. Therefore, the output of the program will be 1 and 2.

    Rate this question:

  • 6. 

    ________________________ makes Java platform-independent.  

    • A.

      JVM

    • B.

      Java syntax

    • C.

      Java API

    • D.

      Bytecodes

    Correct Answer
    D. Bytecodes
    Explanation
    Bytecodes make Java platform-independent because they are the intermediate representation of Java source code that can be executed on any platform that has a Java Virtual Machine (JVM). The JVM interprets the bytecodes and translates them into machine code that is specific to the underlying operating system and hardware. This allows Java programs to run on any platform that has a JVM installed, without the need for recompilation or modification.

    Rate this question:

  • 7. 

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

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The code snippet provided multiplies the value of the variable b by 2. However, since b is declared as a byte, which is a primitive data type in Java that can only store values from -128 to 127, the result of the multiplication exceeds the range of a byte. This causes an overflow and the value of b becomes -56 instead of 100. Therefore, the correct answer is False.

    Rate this question:

  • 8. 

    Final methods cannot be overridden but overloaded ?

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Final methods in Java cannot be overridden by subclasses. Once a method is declared as final in a superclass, it cannot be modified or overridden in any of its subclasses. However, final methods can still be overloaded. Overloading refers to the concept of having multiple methods with the same name but different parameters. So, even though a final method cannot be overridden, it can still be overloaded in order to provide different implementations based on the parameters.

    Rate this question:

  • 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

    Correct Answer
    B. Prints false
    Explanation
    The correct answer is "Prints false" because the default value for a boolean variable is false if it is not explicitly initialized. In this case, the boolean variable "isRaining" is not initialized, so its default value of false is printed.

    Rate this question:

  • 10. 

    What is the name of the method used to start a thread execution? 

    • A.

      Init();

    • B.

      Run();

    • C.

      Start();

    • D.

      Yield();

    Correct Answer
    C. Start();
    Explanation
    The correct answer is "start();". This method is used to start the execution of a thread. When this method is called, the thread's run() method is invoked, and the thread begins executing concurrently with the main thread. The init() method is used for initializing an applet, run() method is the entry point for a thread, and yield() is used to pause the execution of the current thread and give a chance to other threads to execute.

    Rate this question:

  • 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

    Correct Answer
    A. Prints TRUE
    Explanation
    The output of the program is "TRUE" because the condition inside the if statement is assigning the value of true to the variable var. Since the assignment is successful, the condition evaluates to true and the code inside the if block is executed, which prints "TRUE" to the console.

    Rate this question:

  • 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

    Correct Answer
    B. Java.util.Map
    Explanation
    The correct answer is java.util.Map. The Map interface in Java provides the capability to store objects using a key-value pair. It allows you to associate a unique key with a value, and you can retrieve the value by using the key. This interface is commonly used when you need to store and retrieve data based on a specific identifier or key.

    Rate this question:

  • 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

    Correct Answer
    D. 1 and 3
    Explanation
    The output of the program will be "A" and "C". In the first if statement, the equals() method is used to compare the object o with the string s. Since they have the same value, the condition is true and "A" is printed. In the second if statement, the equals() method is used to compare the string s with the object o. Again, they have the same value, so the condition is true and "C" is printed.

    Rate this question:

  • 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

    Correct Answer
    B. C is printed before exiting with an error message.
    Explanation
    The program will throw an Error in the badMethod() method. Since the Error is not caught in the catch block, it will be caught in the finally block. Therefore, the output will be "C" before exiting with an error message.

    Rate this question:

  • 15. 

    Can there be an abstract class with no abstract methods in it? 

    • A.

      Yes

    • B.

      No

    Correct Answer
    A. Yes
    Explanation
    Yes, there can be an abstract class with no abstract methods in it. An abstract class is a class that cannot be instantiated and is meant to be subclassed. It can have both abstract and non-abstract methods. Even if there are no abstract methods in the abstract class, it can still serve as a base class for other classes and provide common functionality or attributes.

    Rate this question:

  • 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

    Correct Answer
    D. Compilation Error
    Explanation
    The program will result in a compilation error. The reason for this is that the variable "x" is not initialized before it is used in the catch1() method and in the main method. In Java, local variables must be initialized before they can be used. Therefore, the code will not compile successfully.

    Rate this question:

  • 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

    Correct Answer
    C. ABCdef
    Explanation
    The program will output "ABCdef".
    The variable 's' is initially assigned the value "ABC". The method 'toLowerCase()' is called on 's', which converts the string to lowercase. However, since strings are immutable in Java, the original value of 's' is not modified. The concatenation operator '+=' is then used to append the string "def" to 's'. Finally, 's' is printed, resulting in the output "ABCdef".

    Rate this question:

  • 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

    Correct Answer
    D. 9.0
    Explanation
    The program creates a Float object f1 with the value "3.0". It then uses the intValue(), byteValue(), and doubleValue() methods to convert the Float object to an int, byte, and double respectively. Finally, it prints the sum of x (int), b (byte), and d (double), which is 9.0. There is no NumberFormatException thrown, so the catch block is not executed. Therefore, the correct answer is 9.0.

    Rate this question:

  • 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

    Correct Answer
    C. App
    Explanation
    The program starts with the string "newspaper". The substring method is used to extract a portion of the string, starting from index 5 and ending at index 7. This results in the string "ape". The charAt method is then used to retrieve the character at index 1 of the substring, which is 'p'. The string "a" is then concatenated with the character 'p', resulting in the string "app". Finally, the string "app" is printed as the output.

    Rate this question:

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

    Correct Answer
    D. For( a = 0 ; a< sum.length ; a++ )
    Explanation
    The correct answer is "for( a = 0 ; a< sum.length ; a++ )". This for loop will iterate over each element in the array "sum" and add it to the variable "b". The variable "a" is used as the index to access each element in the array. The loop will continue until "a" is less than the length of the array "sum".

    Rate this question:

  • 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

    Correct Answer
    C. Child method
    Explanation
    The output of the code will be "Child method". This is because the main method in the Child class is creating an instance of the Child class and assigning it to a reference variable of the Parent class. When the method() is called on this reference variable, it will invoke the method() defined in the Child class, which prints "Child method".

    Rate this question:

  • 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

    Correct Answer
    A. Finally
    Explanation
    The program will output "Finally". The code inside the try block contains a return statement, which will cause the program to exit the main method. However, before the program exits, the finally block is executed, which prints "Finally" to the console. Therefore, the output of the program is "Finally".

    Rate this question:

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

    Correct Answer
    D. An interface can not extend any number of interfaces.
    Explanation
    An interface can not extend any number of interfaces. This means that an interface cannot inherit from multiple interfaces. In Java, a class can implement multiple interfaces, but an interface itself can only extend a single interface.

    Rate this question:

  • 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

    Correct Answer
    B. Java.util.LinkedHashMap
    Explanation
    A LinkedHashMap in Java allows you to associate elements with key values, just like a HashMap. However, it also maintains the order of insertion, which means that you can retrieve the objects in the same order they were added, making it suitable for implementing a FIFO sequence. This is different from ArrayList, which is not designed for key-value associations, and HashMap and TreeMap, which do not guarantee the insertion order.

    Rate this question:

  • 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

    Correct Answer
    A. S1:null s2:withoutbook
    Explanation
    In this code, the variable s1 is initially assigned the value "withoutbook". Then, the variable s2 is assigned the value of s1, which is "withoutbook". After that, s1 is set to null. Finally, the values of s1 and s2 are printed, which are "null" and "withoutbook" respectively. Therefore, the correct answer is "s1:null s2:withoutbook".

    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
  • Sep 07, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Aug 26, 2013
    Quiz Created by
    Saranyav
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.