Core Java MCQ Basics 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 Chanks
C
Chanks
Community Contributor
Quizzes Created: 1 | Total Attempts: 596
Questions: 25 | Attempts: 596

SettingsSettingsSettings
Core Java Quizzes & Trivia

Are you looking for ways to advance your Java MCQ knowledge? The quiz below is set and designed not only to test your Java MCQ knowledge but to also advance it. All the best and enjoy learning.


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
    The code will throw an ArithmeticException because of the line "if(i/j > 1)" where j is equal to 0. Since the catch block for ArithmeticException is present, it will execute and print 0. After the catch block, the code will execute the finally block and print 3. Finally, it will print 4. Therefore, the output will be 0 3 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 program creates an object of the class Arizona and calls the default constructor. The default constructor then calls the constructor with a String parameter, passing "aryabhatta". The constructor with a String parameter then calls the constructor with an int and a String parameter, passing 420 and "aryabhatta". Finally, the constructor with an int and a String parameter initializes the id and name variables and prints "third".

    In the main method, the name and id variables of the Arizona object are printed, which are "aryabhatta" and 420 respectively. Therefore, the output of the program will be "third second first aryabhatta 420".

    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". The code creates an object of class ABC, which is a subclass of XYZ. When the object is created, the constructor of class ABC is called, which in turn calls the constructor of class XYZ using the "super" keyword. The constructor of class XYZ then prints "XYZ". Finally, the main method prints "ABC". There is no output for "Alphabet" because the method in class Alphabet is mistakenly named "Alphabet" instead of being a constructor.

    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 with a value of 0 and x with a value of 10. The while loop condition checks if x is greater than 6. Since x is initially 10, the condition is true and the loop is executed. Inside the loop, the value of i is incremented by 1 and printed, followed by a space. Then, the value of x is decremented by 1. This process continues until x becomes 6. Therefore, the loop will run 4 times, printing the values of i (1, 2, 3, 4) followed by a space each time. Thus, the correct answer is 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
    The correct answers are 1 and 2. When the program is run, the value of j is 1, so the value of i is set to 1. The switch statement then checks the value of i. Since i is 1, the case 1 statement is executed and "1" is printed. However, there is no break statement after the case 1 statement, so the program continues to the next case. Since i is 1, the case 2 statement is also executed and "2" is printed. Therefore, the output of the program is "1 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 are the compiled code instructions that can be executed by the Java Virtual Machine (JVM). The JVM is responsible for interpreting and executing these bytecodes, allowing Java programs to run on any platform that has a JVM installed. This makes Java platform-independent because the same bytecode can be executed on different operating systems without the need for recompilation. Therefore, bytecodes are what make Java programs portable and able to run on any system that supports the JVM.

    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 will not print "b = 100" because the expression "b = b * 2" will result in a compilation error. This is because the result of the multiplication operation is an int, and it cannot be automatically converted to a byte without an explicit cast. To fix this, the code should be written as "b = (byte) (b * 2)" to explicitly cast the result to a byte.

    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. When a method is declared as final, it means that it cannot be modified or overridden by any subclass. However, final methods can still be overloaded, which means that multiple methods with the same name but different parameters can exist in the same class. Overloading allows for different versions of the method to be called depending on the arguments passed to it. Therefore, the statement that final methods cannot be overridden but overloaded is true.

    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 code snippet declares a static boolean variable "isRaining" but does not initialize it. In Java, boolean variables are automatically initialized to false if no value is assigned to them. Therefore, when the main method is called and the code prints the value of "isRaining", it will output false.

    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();". In Java, the start() method is used to start the execution of a thread. When start() is called, it creates a new thread and invokes the run() method of that thread. The run() method contains the code that will be executed by the thread. Calling the start() method allows the thread to run concurrently with other threads in the program.

    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 correct answer is "Prints TRUE". In the if statement, the condition is var = true, which is an assignment statement. The value of var is set to true, and since the assignment statement returns the assigned value, the condition is true. Therefore, the code inside the if block is executed, and "TRUE" is printed.

    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 interface that provides the capability to store objects using a key-value pair is java.util.Map. This interface allows us to associate a unique key with a value, and then retrieve the value using the key. It does not allow duplicate keys, ensuring that each key is unique within the map. Additionally, the Map interface provides various methods to manipulate and access the key-value pairs, such as adding, removing, and updating entries.

    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". This is because the "equals" method in Java compares the content of the objects, and since the content of both "o" and "s" is "hello", both comparisons will return true. Therefore, the program will print "A" and "C".

    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.

      BC is printed before exiting with an error message.

    • B.

      Compilation Fails

    • C.

      ABCD

    • D.

      C is printed before exiting with an error message.

    Correct Answer
    D. C is printed before exiting with an error message.
    Explanation
    The program throws an Error in the badMethod() method. Since the Error is not caught by a catch block for Error, it is propagated to the calling method. The catch block for Exception does not catch Errors, so it is skipped. However, the finally block is always executed, so "C" is printed before the program exits 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 contain both abstract and non-abstract methods. While abstract methods are meant to be overridden by subclasses, non-abstract methods can have their own implementation. So, it is possible for an abstract class to have only non-abstract methods and no abstract methods.

    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. This is because the variable 'x' is not initialized before it is used in the catch1() method and the main method. Therefore, the compiler will throw an error indicating that the variable 'x' may not have been initialized.

    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 all the characters in "s" to lowercase. However, the return value of "toLowerCase()" is not assigned to any variable, so the original value of "s" remains unchanged.
    Then, the string "def" is concatenated to "s" using the "+=" operator, resulting in "ABCdef".
    Finally, the value of "s" is printed using the "println()" method, which outputs "ABCdef" to the console.

    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". The intValue() method is then called on f1, which returns the integer value 3. The byteValue() method is called next, which returns the byte value 3. The doubleValue() method is called last, which returns the double value 3.0. The sum of x, b, and d is then printed, which is 9.0. Since there is no NumberFormatException thrown, the catch block is not executed. Therefore, the output of the program will be 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 first assigns the substring of "newspaper" starting from index 5 and ending at index 7 to the variable 'a'. This results in 'a' being assigned the value "ap". Then, the character at index 1 of 'a' is assigned to the variable 'b', which is 'p'. Next, 'b' is concatenated to 'a', resulting in 'a' being assigned the value "app". Finally, the program prints the value of 'a', which is "app".

    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 iterates over each element in the array "sum" and adds it to the variable "b" using the "+=" operator. The variable "a" is initialized to 0 and incremented by 1 in each iteration until it reaches the length of the array "sum". This loop ensures that all elements in the array are added to the variable "b", resulting in the total sum of the array.

    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 this code will be "Child method". This is because the main method creates an instance of the Child class and assigns it to a variable of type Parent. When the method() is called on this variable, it calls the method defined in the Child class, overriding the method defined in the Parent class. Therefore, the "Child method" is printed to the console.

    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". In the main method, a try block is present with a return statement. However, before the return statement is executed, the finally block is always executed. Therefore, "Finally" will be printed to the console.

    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 means that an interface cannot inherit from multiple interfaces. In Java, a class can implement multiple interfaces, but an interface itself can only extend one interface. This is because interfaces are used to define a contract for classes to implement, and allowing an interface to extend multiple interfaces would create ambiguity and make it difficult to enforce the contract. Therefore, an interface can only extend one interface, if any.

    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 is a collection class that allows you to associate its elements with key values. It also maintains the order of insertion, allowing you to retrieve objects in FIFO (first-in, first-out) sequence. This means that elements will be returned in the same order they were added to the LinkedHashMap. Therefore, java.util.LinkedHashMap is the correct answer for this question.

    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
    The code initializes a variable s1 with the value "withoutbook" and then assigns the same value to variable s2. After that, s1 is set to null. When the code prints the values of s1 and s2, it will show s1 as null and s2 as "withoutbook". This is because s2 is a separate variable that holds the value "withoutbook" and is not affected by the null assignment to s1.

    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
  • Jun 10, 2014
    Quiz Created by
    Chanks
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.