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

Last updated on Apr 25, 2024

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:

When iterating through data retrieved using a Data Reader, what type of access is possible?

  1. Random access to any row in the dataset
  2. Forward-only access, processing data one row at a time.
  3. Backward access, allowing you to revisit previously processed rows
  4. Both (a) and (c)

Answer (Detailed Solution Below)

Option 2 : Forward-only access, processing data one row at a time.

Java Question 1 Detailed Solution

The correct answer is Forward-only access, processing data one row at a time.

Key Points

  • Data Readers enforce forward-only access, meaning you can only process data in the order it's retrieved.

Java Question 2:

What is the main drawback of using a DataSet for large datasets?

  1. It requires an internet connection to function.
  2. It can consume significant memory due to holding the entire dataset in memory at once. 
  3. DataSets are inherently slower than all other data access methods.
  4. It cannot be used with relational databases.

Answer (Detailed Solution Below)

Option 2 : It can consume significant memory due to holding the entire dataset in memory at once. 

Java Question 2 Detailed Solution

The correct answer is It can consume significant memory due to holding the entire dataset in memory at once. 

Key Points

  • ​The primary downside of DataSets for large datasets is the increased memory footprint.

Java Question 3:

What lifecycle method of a servlet is called when the servlet is first loaded into memory?

  1. init()
  2. doGet()
  3. doPost()
  4. service()

Answer (Detailed Solution Below)

Option 1 : init()

Java Question 3 Detailed Solution

The correct answer is init()

Key Points

  • The init() method of a servlet is called only once, the first time the servlet is loaded into memory by the web container. It's used for initialization tasks like setting

Java Question 4:

What annotation is used to map a URL pattern to a servlet in Java?

  1. @WebServlet
  2. @ServletMapping
  3. @WebRoute
  4. @Servlet

Answer (Detailed Solution Below)

Option 1 : @WebServlet

Java Question 4 Detailed Solution

The correct answer is @WebServlet

Key Points

  • The @WebServlet annotation is used to declare a servlet class and map it to a specific URL pattern in a Java web application.
  • This annotation simplifies servlet configuration by eliminating the need for XML deployment descriptors.

Java Question 5:

Which keyword is used for “Debugging” in Java ?

  1. Abstract
  2. Insert
  3. Assert 
  4. Boolean
  5. Not Attempted

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.

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

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