Java MCQ Quiz - Objective Question with Answer for Java - Download Free PDF
Last updated on Apr 25, 2024
Latest Java MCQ Objective Questions
Java Question 1:
When iterating through data retrieved using a Data Reader, what type of access is possible?
Answer (Detailed Solution Below)
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?
Answer (Detailed Solution Below)
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?
Answer (Detailed Solution Below)
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?
Answer (Detailed Solution Below)
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 ?
Answer (Detailed Solution Below)
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?
Answer (Detailed Solution Below)
Java Question 6 Detailed Solution
Download Solution PDFConcept
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 |
} |
Answer (Detailed Solution Below)
Java Question 7 Detailed Solution
Download Solution PDFThe correct answer is “option 4”.
EXPLANATION:
Option 1: FALSE
“While” is not a keyword, 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");
Answer (Detailed Solution Below)
Java Question 8 Detailed Solution
Download Solution PDFThe 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);
Answer (Detailed Solution Below)
Java Question 9 Detailed Solution
Download Solution PDFConcept:
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.
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?
Answer (Detailed Solution Below)
Java Question 10 Detailed Solution
Download Solution PDFThe 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 ______
Answer (Detailed Solution Below)
Java Question 11 Detailed Solution
Download Solution PDFConcept:
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?
Answer (Detailed Solution Below)
Java Question 12 Detailed Solution
Download Solution PDFConcept
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?
Answer (Detailed Solution Below)
Java Question 13 Detailed Solution
Download Solution PDFConcept:
- 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:
Answer (Detailed Solution Below)
Java Question 14 Detailed Solution
Download Solution PDFConcept:
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 ______
Answer (Detailed Solution Below)
Java Question 15 Detailed Solution
Download Solution PDFConcept:
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
}