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

Last updated on Oct 30, 2023

JAVA is a versatile and widely-used programming language known for its platform independence. It provides a robust and object-oriented approach to software development, making it suitable for a range of applications. JAVA programs are compiled into bytecode that can run on any Java Virtual Machine (JVM), allowing cross-platform compatibility. It offers features like automatic memory management, exception handling, and extensive libraries, making it efficient and convenient for developers. MCQs on JAVA are crucial for competitive exams which have Computer Knowledge and Awareness as one of the components. Find out how much you know about this programming language by solving the given JAVA MCQs.

Latest Java MCQ Objective Questions

Java Question 1:

Java Virtual Machine (JVM) is used to execute architectural neutral byte code. Which of the following is needed by the JVM for execution of Java code?

  1. Class loader only
  2. Class loader and Java Interpreter
  3. Class loader, Java Interpreter and API
  4. Java Interpreter only
  5. None of the above

Answer (Detailed Solution Below)

Option 2 : Class loader and Java Interpreter

Java Question 1 Detailed Solution

Concept:

Java virtual machine is component of technology for its hardware and operating system independence, the small size of its compiled code and its ability to protect users from malicious programs.

Explanation:

Java virtual machine is an abstract computing machine. Like a real computing machine, it has an instructions set and manipulated various memory areas at run time.

  • Java virtual machine knows nothing of java programming language, only of  a particular binary format, the class file format.
  • A class file contains JVM instructions or bytecodes and a symbol table, as well as other information.
  • JVM imposes strong syntactical and structural constraints on code in a class file.
  • Class loader and interpreter is needed by JVM to execute Java code.
  • Basic structure in JVM are: heap store, stack, method area, runtime constant pools, PC.

Java Question 2:

Consider the below Java code:

class Test {
    int i = 1;
    Test increment() {
        i++;
        return this;
    }
    void print() {
        System.out.println("i = " + i);
    }
}

public class Main {
    public static void main(String args[]) {
        Test obj = new Test();
        obj.increment().increment().increment().print();
    }
}

What is the output of the program?

  1. The program will throw a runtime error
  2. The program will throw a compile-time error
  3. i = 1
  4. i = 4

Answer (Detailed Solution Below)

Option 4 : i = 4

Java Question 2 Detailed Solution

The correct answer is i=4

Key PointsIn the code, 'increment()' method is called three times on the 'obj' object. Every call increments the value of 'i' by 1. Initially, 'i' is 1. Therefore, after three calls, 'i' becomes 4.

Java Question 3:

Which of the following statements are TRUE regarding Java Exceptions?

  1. An exception is an irrecoverable condition and cannot be handled using try-catch blocks
  2. The exception class is a subcalss of the Throwable class
  3. Exceptions are divided into checked and unchecked exceptions
  4. Exceptions are mainly caused by the application.

  1. 1,2,3
  2. 1,2
  3. 2,3,4
  4. 1,2,3,4

Answer (Detailed Solution Below)

Option 3 : 2,3,4

Java Question 3 Detailed Solution

The correct answer is 2,3,4

Exception:

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

Option 1: An exception is an irrecoverable condition and cannot be handled using try-catch blocks.

                False, An exception is an unrecoverable circumstance that can be handled by using try-catch blocks. Any code statements that may raise or throw an                  exception should be placed in a try block, and any statements needed to manage the exception or exceptions should be placed in one or more catch                    blocks below the try block. Each catch block contains the exception type as well as any further lines required to handle that exception type.

Option 2: The exception class is a subclass of the Throwable class.

                 True, The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class are thrown                    by the Java Virtual Machine or can be thrown by the Java throw statement.

Option 3: Exceptions are divided into checked and unchecked exceptions.

                  True, Exceptions are further subdivided into checked (compile-time) and unchecked (run-time) exceptions. All subclasses of RuntimeException are                         unchecked exceptions, whereas all subclasses of Exception besides RuntimeException are checked exceptions.

Option 4: Exceptions are mainly caused by the application.

                   True, Exceptions are mainly caused by the application itself.

Example:

NullPointerException occurs when an application tries to access a null object or ClassCastException occurs when an application tries to cast incompatible class types.

Hence the correct answer is options 2,3,4

Java Question 4:

Which keyword is used for “Debugging” in Java ?

  1. Abstract
  2. Insert
  3. Assert 
  4. Boolean
  5. None of the above

Answer (Detailed Solution Below)

Option 3 : Assert 

Java Question 4 Detailed Solution

The correct answer is option 3.

Key Points

  • The keyword used for "Debugging" in Java is "Assert".
  • It checks whether a particular condition is true or false and if it is false, it throws an error. 
  • Assert is used to check the correctness of a program during development and testing.
  • It helps in identifying the errors in the code and fixing them before the code is released.
  • Assert statements are usually removed from the code during production as they can impact performance and are not required for normal functioning of the code.

 Additional Information

  • Abstract: It is a keyword used to declare a class or method as abstract, meaning it cannot be instantiated and must be extended or implemented by a subclass.
  • Insert: It is not a keyword in Java.
  • Boolean: It is a keyword used to declare a Boolean variable or method. It has two possible values - true and false.

Java Question 5:

Which keyword is used for “Debugging” in Java ?

  1. Abstract
  2. Insert
  3. Assert 
  4. Boolean

Answer (Detailed Solution Below)

Option 3 : Assert 

Java Question 5 Detailed Solution

The correct answer is option 3.

Key Points

  • The keyword used for "Debugging" in Java is "Assert".
  • It checks whether a particular condition is true or false and if it is false, it throws an error. 
  • Assert is used to check the correctness of a program during development and testing.
  • It helps in identifying the errors in the code and fixing them before the code is released.
  • Assert statements are usually removed from the code during production as they can impact performance and are not required for normal functioning of the code.

 Additional Information

  • Abstract: It is a keyword used to declare a class or method as abstract, meaning it cannot be instantiated and must be extended or implemented by a subclass.
  • Insert: It is not a keyword in Java.
  • Boolean: It is a keyword used to declare a Boolean variable or method. It has two possible values - true and false.

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. 

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 8 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 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 9 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 10 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 is NOT a java primitive type? 

  1. short
  2. long
  3. long double
  4. boolean

Answer (Detailed Solution Below)

Option 3 : long double

Java Question 11 Detailed Solution

Download Solution PDF

Concept

The eight primitive data types supported by the Java programming language are:

  • byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). 

  • short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).

  • int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. .

  • long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, .

  • float: The float data type is a single-precision 32-bit IEEE 754 floating point.

  • double: The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice. 

  • boolean: The boolean data type has only two possible values: true and false. 

  • char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

Hence there are 8 primitive data types, that is, byte, short, int, long, float, double, boolean and char

Additional Information

Non-primitive data types - such as String, Arrays, ArrayList, Integer, Long,  etc.

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

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

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 14 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 15 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

}

Get Free Access Now