Java MCQ Quiz - Objective Question with Answer for Java - Download Free PDF

Last updated on Apr 13, 2023

Latest Java MCQ Objective Questions

Java Question 1:

Among the following is not a popular IDE for Java development? 

  1.  NetBeans
  2. IntelliJ IDEA 
  3. Visual Studio Code 
  4. Eclipse

Answer (Detailed Solution Below)

Option 3 : Visual Studio Code 

Java Question 1 Detailed Solution

NetBeans, IntelliJ IDEA, and Eclipse are three of the most popular IDEs for Java development.

  • NetBeans is an open-source IDE that supports multiple languages, including Java, and offers features such as code highlighting, debugging, and profiling.
  • IntelliJ IDEA is a commercial IDE that offers advanced features such as intelligent code completion, refactoring, and unit testing.
  • Eclipse is an open-source IDE that supports multiple programming languages, including Java, and offers features such as code editing, debugging, and testing.

Visual Studio Code is not a popular IDE for Java development, although it can be used with extensions for Java development.

Hence, the correct answer is Option 3.

Java Question 2:

What is the function of javap command?

  1. This command is used to run the Java compiler.
  2. A command to run a non-optimized version of the Java compiler.
  3. A command to create an HTML document in API style.
  4. A command to separate Java class files.

Answer (Detailed Solution Below)

Option 4 : A command to separate Java class files.

Java Question 2 Detailed Solution

Answer - A command to separate Java class files.

Exaplanation -

The javap command is used to decompile a compiled Java class file and display its contents in a human-readable format. The output of the javap command includes information such as the name and type of the class, the list of methods and fields defined in the class, and the bytecode instructions for each method.

The javap command is often used by developers to understand how Java code is compiled and executed, as well as to troubleshoot issues with their Java applications.

Java Question 3:

What is the output of the following JAVA code?

String s = “exam:” + 9 + 9 + 9;

System.out.println(s); 

  1. exam ∶ 27
  2. exam ∶ 
  3. exam 27
  4. exam ∶ 999 

Answer (Detailed Solution Below)

Option 4 : exam ∶ 999 

Java Question 3 Detailed Solution

The output of the following Java code will be: exam:999

Explanation - 

  • In the code, a string variable 's' is declared and initialised with the value "exam:" + 9 + 9 + 9.
  • This expression is evaluated from left to right, and since the + operator is used with a string and an int value, Java converts the int values to String values and performs string concatenation.
  • So, the first + operator concatenates the string "exam:" with the int value 9, resulting in the String "exam:9".
  • The second + operator concatenates the previous String result "exam:9" with the int value 9, resulting in the string "exam:99".
  • Finally, the third + operator concatenates the previous String result "exam:99" with the int value 9, resulting in the final string value of "exam:999".
  • The System.out.println() statement prints the value of the string variable s to the console, which is "exam:999".

Java Question 4:

Which of the following statements is/are correct regarding the programming of JAVA?

I. A class that is marked as final cannot be overwritten.

II. A method that is marked as final cannot be overridden.

  1. Neither I nor II
  2. Both I and II
  3. Only I
  4. Only II

Answer (Detailed Solution Below)

Option 2 : Both I and II

Java Question 4 Detailed Solution

Answer - Both statements are correct.

Explanation -
Statement 1 - "A class that is marked as final cannot be overwritten"

When a class is declared as final, it means that it cannot be subclassed or extended by other classes. Therefore, no other class can inherit the final class, and the final class cannot be overwritten.

For example, the String class in Java is declared as final, which means it cannot be subclassed or extended by other classes. If you try to create a subclass of the String class, it will result in a compile-time error.

Statement 2 - "A method that is marked as final cannot be overridden"
When a method is declared as final, it means that it cannot be overridden by any subclass of the class in which the method is declared. Therefore, the final method's implementation is fixed and cannot be changed by any subclass.

For example, suppose a superclass has a final method named doSomething(), and a subclass wants to override this method. In that case, it will not be possible, and if the subclass tries to override the final method, it will result in a compile-time error.

In summary, the final keyword in Java can be used to prevent classes from being subclassed or extended and to prevent methods from being overridden by subclasses. This helps to ensure the correctness and stability of the code and prevents unexpected behavior caused by modifications to the superclass or its methods.

Java Question 5:

A java program that is embedded in HTML document and runs in the context of java capable browser is known as ______.

  1. canvas
  2. frame window
  3. doctags
  4. applet

Answer (Detailed Solution Below)

Option 4 : applet

Java Question 5 Detailed Solution

Answer - applet

Explanation -

Canvas - Canvas is a HTML5 element that allows dynamic, scriptable rendering of 2D shapes and bitmap images. It provides an immediate mode graphics API for drawing shapes, text, images, and other graphical objects on a web page. Canvas is typically used to create interactive visualizations, games, and other web applications that require dynamic graphics.

Frame Window - A frame window is a rectangular area on a computer screen that displays a separate document or web page. It is typically used to display multiple documents or web pages within a single application window. Frame windows are implemented using HTML frameset or iframe elements and can be manipulated using JavaScript or other scripting languages.

Doctags - Doctags is not a recognised term in the context of Java or web development. It is possible that this term is being used in a specific context that is not clear from the question.

Applet - An applet is a Java program that is embedded within an HTML document and runs in the context of a Java-capable web browser. Applets are typically used to create interactive web applications that can respond to user input and communicate with a server. Applets are written in the Java programming language and are compiled into bytecode that can be executed by the Java Virtual Machine (JVM). When an applet is embedded within an HTML document, the web browser downloads the applet bytecode and runs it within the browser's JVM.

Top Java MCQ Objective Questions

What is the use of 'javac' command?

  1. Execute a java program
  2. Debug a java program
  3. Interpret a java program
  4. Compile a java program

Answer (Detailed Solution Below)

Option 4 : Compile a java program

Java Question 6 Detailed Solution

Download Solution PDF

Concept

The javac command in Java compiles a program from a command prompt.

It reads a Java source program from a text file and creates a compiled Java class file.

Syntax

javac filename [options]

For example, to compile a program named Abc.java, use this command:

javac Abc.java

Consider the following Java code fragment. Which of the following statement is true?

Line No

Code Statement

1

public class While

2

{

3

      public void loop()

4

      {

5

         int x = 0;

6

         while (1)

7

          {

8

System.out.println(“x plus one is” + (x + 1));

9

           }

10

      }

11

}

  1. There is syntax error in line no. 1
  2. There are syntax errors in line nos. 1 & 6
  3. There is syntax error in line no. 8
  4. There is syntax error in line no. 6

Answer (Detailed Solution Below)

Option 4 : There is syntax error in line no. 6

Java Question 7 Detailed Solution

Download Solution PDF

The correct answer is “option 4”.

EXPLANATION:

Option 1: FALSE

While” is notkeyword, hence it is valid to use it as a class name.

Option 2: FALSE

Since Option 1 is false, so this option is by default false.

Option 3: FALSE

Any string operation can contain an equation type of expression.

So, this is not any kind of syntax error.

Option 4: TRUE

Java uses condition as Boolean expressions like:   

while(true) or while(false)

Hence, while(1) is the wrong syntax for java.

while(1) will give compiler time error as it treats as type mismatch to convert from integer to boolean value.

Hence, the correct answer is “option 4”.

Mistake Points 

Here Class name is While used which is not a keyword. while is a keyword in java but not While. So, we can use it as class name. 

What is the output of the following java code?

int m = 1000;

int k = 3000;

while (+ + m < – – k);

System.out.println(m);

  1. 2000
  2. Error
  3. 1000
  4. 4000

Answer (Detailed Solution Below)

Option 1 : 2000

Java Question 8 Detailed Solution

Download Solution PDF

Concept:

Java is an object-oriented programming language that produces software for multiple platforms. In the bottom-up approach first designing, beginning from the base level to the abstract level is done. e.g.-In c++/java starts designing from class from the basic level of the programming features and then goes to the main part of the program. 

Analysis: 
Here m stores 1000 and k stores 3000.

While loop condition is true until (++m < – –k) and it is not executing any statements because of the semi-colon(;). 

Every time comparing the condition (++m < – –k) m and k values will pre-increment and pre-decrement respectively. 

Ex:

1001 < 2999 true do nothing.

1002 < 2998 true do nothing.

1003 < 2997  true do nothing

......

1998<2002 true do nothing.

1999<2001 true do nothing.

2000<2000 false come out from while loop and prints the value of m.

And prints the m=2000,

Hence the correct answer is 2000.

Note:

while (+ + m < – –k);

In this statement m and k values will be, the amount which increased is equal to the amount decreased. So the condition is false when it is mean between the two numbers.

if m=500 and k=3000

the loop condition false when = \(m+{{(k-m) }\over 2}\)

m=500+1250.

Which of the following is/are the rules to declare variables in Java?

  1. Java keywords cannot be used as variable names
  2. All of the options
  3. Variable names are case-sensitive
  4. The first character must be a letter

Answer (Detailed Solution Below)

Option 2 : All of the options

Java Question 9 Detailed Solution

Download Solution PDF

The variable is the basic unit of storage in a Java program. A variable is defined by the combination of an identifier, a type, and an optional initializer.

Variable names cannot be keyword, it is case- sensitive and the first character must be a letter.

In Java, all variables must be declared before they can be used. The basic form of a variable declaration is:

type identifier [ = value ][, identifier [= value ] …];

Examples:

int a, b;

int a = 10, b = 20, c = 30;

int a, b = 20, c;

Confusion point

In Java, variable name can also start with underscore character “_”, or a dollar sign “$”.

Best Possible answer is chosen.

Which of the following methods will create a string in Java?

I: String S = "Hello Java”;

II: String S2 = new String (“Hello Java");

  1. Only I
  2. Only II
  3. Both I and II
  4. Neither I nor II

Answer (Detailed Solution Below)

Option 3 : Both I and II

Java Question 10 Detailed Solution

Download Solution PDF

The correct answer is option 3.

Concept:

A string is an object in Java that indicates a collection of characters or char values. A Java string object is created using the java.lang.String class.

A String object can be created in one of two ways:

By string literal:

Java String literal is created by using double-quotes.

Example:

 String s=“Welcome”;  

By new keyword:

Java String is created by using the keyword “new”.

Example:

String s=new String(“Welcome”);  

Hence the correct answer is Both I and II.

What is garbage collection in the context of Java?

  1. The operating system periodically deletes all of the Java files available on the system.  
  2. When all references to an object are gone, then the memory used by the object is automatically reclaimed. 
  3.  Any java package imported in a program and not being used, is automatically deleted. 
  4. The java virtual machine (JVM) checks the output of any java program and deleted anything that does not make sense at all. 

Answer (Detailed Solution Below)

Option 2 : When all references to an object are gone, then the memory used by the object is automatically reclaimed. 

Java Question 11 Detailed Solution

Download Solution PDF

Concept:

  • Java garbage collection is the process of releasing unused memory.
  • Sometimes some objects are no longer required by the program and when there is no reference to an object, then that object should be released.
  • This process is known as garbage collection.

Explanation:

  • Garbage collection process is done by JVM (java virtual machine). Unreachable and unusable objects are available for garbage collection.
  • With garbage collection, the programmer has no need to worry about dereferencing the object. It increases memory efficiency and less memory leakage. 
  • For this, make the object reference null so that it is deleted by garbage collection.

 

Example: obj obj1 = new obj();

obj1 = null;              // for garbage collection

A condition that is caused by run-time error in a computer program is known as: 

  1. Syntax error
  2. Fault
  3. Semantic error
  4. Exception

Answer (Detailed Solution Below)

Option 4 : Exception

Java Question 12 Detailed Solution

Download Solution PDF

Concept:

In computer programming, an exception is a special condition encountered during program execution or run time. Example: if the program tries to open a file that does not exist, divide by zero, etc.

Explanation:

When an error occurs within a method, the method creates an object known as an exception object which contains information about the error. This process is known as throwing an exception.

After this, the run time system finds something to handle it. Run time system searches the list of methods that can handle the exception. Search begins with the method in which the error occurred and proceeds through the list of methods in reverse order. Exception handler chosen is said to catch the exception.

There are two blocks to handle an exception: try and catch block.

Try block is used to enclose the code that throws an exception. It is used within the method. Catch block is used to handle the exception by declaring the type of exception with the parameter. Catch block must be used after the try block.

Syntax :

try

{

//code that throws exception

}

catch(exception_class_name){}

Additional information sent when an exception is thrown may be placed in ______

  1. The throw keyword
  2. The function that caused the error
  3. The catch block
  4. An object of the exception class

Answer (Detailed Solution Below)

Option 3 : The catch block

Java Question 13 Detailed Solution

Download Solution PDF

Concept:

An exception is an unexpected event which occurs during run time which disrupts the normal flow of execution. Example: division by zero.

There are two blocks in this: try and catch block.

Explanation:

try block:

It contains set of statements where an exception can occur. It is always followed by a catch block.

catch block:

In this block, exceptions are handled. Additional information sent when an exception is thrown is placed in this block. A single try block can have multiple catch blocks. Throw keyword is used to transfer control from try block to catch block.

syntax:

try

{

// statement that causes exception

}

catch

{

//statements that handle exception

}

The static keyword word is used in public static void main() declaration in Java__________.

  1. To enable the JVM to make call to the main(), as class has not been instantiated.
  2. To enable the JVM to make call to the main(), as class has not been inherited.
  3. To enable the JVM to make call to the main(), as class has not been loaded.
  4. To enable the JVM to make call to the main(), as class has not been finalized.

Answer (Detailed Solution Below)

Option 1 : To enable the JVM to make call to the main(), as class has not been instantiated.

Java Question 14 Detailed Solution

Download Solution PDF

Concept:

The static keyword word is used in the public static void main() declaration in Java to enable the JVM to make a call to the main(), as class has not been instantiated.

Reason:

main() method:

The main() method, in Java, is the entry point for the JVM(Java Virtual Machine) into the java program. JVM launches the java program by invoking the main() method o enable the JVM to make call to the main(), as class has not been instantiated.

Static is a keyword.

  • The role of adding static before any entity is to make that entity a class entity.
  • It means that adding static before methods and variables makes them class methods and class variables respectively, instead of instance methods and instance variables.
  • Hence, static methods and variables can be directly accessed with the help of Class, which means that there is no need to create objects in order to access static methods or variables.

Parent class of all java classes is ______

  1. Java.lang.system
  2. Java.lang.object
  3. Java.lang.class
  4. Java.lang,reflect.object

Answer (Detailed Solution Below)

Option 2 : Java.lang.object

Java Question 15 Detailed Solution

Download Solution PDF

Concept:

Classes in java are the collection of objects which has some similar properties. A class in java contains fields, methods, constructors, blocks, interfaces.

Explanation:

Everything is associated with classes and objects. An object is an instance of the class which has some state and behavior.

An object class(java.lang.object) is the parent class of all the java classes.  It can be used when we do not know about the type of the object. By having the object as the super class , without knowing the type we can pass around objects using the object declaration.

Get Free Access Now