Core Java MCQ Practice Questions

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Saranyav
S
Saranyav
Community Contributor
Quizzes Created: 1 | Total Attempts: 478
| Attempts: 478 | Questions: 25
Please wait...
Question 1 / 25
0 %
0/100
Score 0/100
1. Can there be an abstract class with no abstract methods in it? 

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.

Submit
Please wait...
About This Quiz
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,... see morethen this quiz will bring you closer to you goal. see less

2. What will be the output of the program?

public class WBFoo 

public static void main(String[] args) 
{
try 

return; 

finally 
{
System.out.println( "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".

Submit
3. Final methods cannot be overridden but overloaded ?

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.

Submit
4. 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;       }    } }

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.

Submit
5. Which interface provides the capability to store objects using a key-value pair? 

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.

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

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

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

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.

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

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

Submit
9.  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

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.

Submit
10. 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 :

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

Submit
11. 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?

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

Submit
12. 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 

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.

Submit
13. 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?

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.

Submit
14. What is the output of following block of program ? boolean var = false; if(var = true) { System.out.println("TRUE"); } else { System.out.println("FALSE"); } 

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.

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

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.

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

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.

Submit
17. 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);  }  } 

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.

Submit
18. 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);  } }

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.

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

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.

Submit
20. ________________________ makes Java platform-independent.  

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.

Submit
21. 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();  }  } 

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.

Submit
22. 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 "); } }  

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.

Submit
23. 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"); } 

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.

Submit
24. Which of the following are true about interfaces. Select the correct answer.

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.

Submit
25. 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 ;
}

Explanation

The correct answer is "for( a = 0 ; a

Submit
View My Results

Quiz Review Timeline (Updated): Sep 7, 2023 +

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
Cancel
  • All
    All (25)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Can there be an abstract class with no abstract methods in it? 
What will be the output of the program? ...
Final methods cannot be overridden but overloaded ?
What all gets printed when the following program is compiled and run....
Which interface provides the capability to store objects using a...
Public class CoreJavaOnlineTest1 { ...
What is the name of the method used to start a thread execution? 
What will be the output of the program? ...
 What all gets printed when the following gets compiled and run? ...
What will be the result of compiling and running the following code ? ...
Public class Child extends Parent { ...
What will be the output of the program? ...
Which collection class allows you to associate its elements with key...
What is the output of following block of program ? ...
Byte b = 50 ; ...
Class Weather    ...
What will be the output of the program? ...
What will be the output of the program when to try to execute ...
What will be the output of the program? ...
________________________ makes Java platform-independent.  
What will be the output of the program? ...
Consider the following code that contains the class definitions for...
What will be the output of the program? ...
Which of the following are true about interfaces. Select the correct...
What should be at line number 3 to get the total sum of array...
Alert!

Advertisement