Java MCQ Quiz - Objective Question with Answer for Java - Download Free PDF
Last updated on Sep 14, 2022
Latest Java MCQ Objective Questions
Java MCQ Question 1:
Which of the following statements is/are true regarding JAVA?
A. Constants that cannot be changed are declared using the 'Static' keyword.
B. A class can only inherit one class that can implement multiple interfaces.
Answer (Detailed Solution Below)
Java MCQ Question 1 Detailed Solution
Correct Answer: Option 2
Explanation:
- Constants that cannot be changed are declared using the final keyword. The static keyword is used for functions and variables to specify that they will be instantiated exactly once for any number of object instances and the same value will be used across all objects.
- Java does not support multiple inheritances directly. When a class inherits two different classes, it is known as multiple inheritances. Java does not allow for a class to inherit more than one class. However, two classes can be inherited if one of them is an interface. An interface is a schematic representation of a class containing variables and abstract methods. So, a class can inherit one class, that can implement multiple interfaces.
Therefore, statement A is incorrect and statement B is correct.
Java MCQ Question 2:
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 MCQ Question 2 Detailed Solution
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.
Java MCQ Question 3:
____________________ is an API to develop Graphical User Interface (GUI) or Windows-based applications in Java.
Answer (Detailed Solution Below)
Java MCQ Question 3 Detailed Solution
The correct answer is option 1.
Concept:
Java Abstract Window Toolkit:
The Java AWT (Abstract Window Toolkit) API is used to create a graphical user interface (GUI) or windows-based Java applications. It is a set of application program interfaces (APIs) that Java programmers use to construct GUI components like buttons, scroll bars, and windows. Sun Microsystems, the company that invented Java, created AWT as part of the Java Foundation Classes (JFC).
Hence the correct answer is Java Abstract Window Toolkit.
Additional Information
- A Java Applet is a sort of application that generates dynamic information by being embedded in a webpage. It is a client-side application that runs within the browser.
- The Java Regex or Regular Expression is an API to define a pattern for searching or manipulating strings.
- Java I/O (Input and Output) is used to process the input and produce the output.
Java MCQ Question 4:
What will the result of the following code:
Answer (Detailed Solution Below)
Java MCQ Question 4 Detailed Solution
The correct answer is option 3.
Key Points
- The Main is declared where the main function is declared inside it.
- The integer variable a is initialized with value 21, and this statement is terminated with a semicolon.
- The variable b is initialized but not declared in the statement.
- One more variable num is declared and initialized by an expression (a/b).
- System.out.println is sued to print the variable num.
- Every variable need to be declared or defined with a particular datatype before its initialization.
- Hence the given code throws a compilation error.
Java MCQ Question 5:
What is the output of this program?
Answer (Detailed Solution Below)
Java MCQ Question 5 Detailed Solution
Here, ++ have higher priority than *.
++g is called as the pre-increment operator in java which will just increase the g value by 1.
By solving the equation will be reduced to print(4*g)
as, g value is 8 (given in the question),
So, answer turns out to be 32 here.
Top Java MCQ Objective Questions
What is the use of 'javac' command?
Answer (Detailed Solution Below)
Java MCQ 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 MCQ 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 is/are the rules to declare variables in Java?
Answer (Detailed Solution Below)
Java MCQ Question 8 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.
What is garbage collection in the context of Java?
Answer (Detailed Solution Below)
Java MCQ Question 9 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 MCQ Question 10 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){}
The static keyword word is used in public static void main() declaration in Java__________.
Answer (Detailed Solution Below)
Java MCQ Question 11 Detailed Solution
Download Solution PDFConcept:
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.
In Java, the Dynamic Array are known as:
Answer (Detailed Solution Below)
Java MCQ Question 12 Detailed Solution
Download Solution PDFConcept:
Dynamic arrays are those arrays that are allocated memory at the run time with the help of a heap. Thus Dynamic array can change its size during run time
A dynamic array expands as you add more elements. So you don't need to determine the size ahead of time.
Important Points
Vector is like a dynamic array that can grow or shrink its size. Unlike an array, we can store n-number of elements in it as there is no size limit. It is a part of the Java Collection framework since Java 1.2. It is found in java.util package and implements the List interface, so we can use all the methods of the List interface here. It is similar to the ArrayList, but with two differences-
- Vector is synchronized.
- Java Vector contains many legacy methods that are not part of a collections framework.
Java Vector class Declaration
public class Vector
extends Object
implements List
Hence the correct answer is option 1
Parent class of all java classes is ______
Answer (Detailed Solution Below)
Java MCQ Question 13 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.
Java is a _________ language. This means that you must be explicit about what type of data you are working with.
Answer (Detailed Solution Below)
Java MCQ Question 14 Detailed Solution
Download Solution PDFConcept:
Java is a high-level, secure, and strongly typed language. It was developed by sun microsystems. we can create web applications, mobile and enterprise applications using java.
Explanation:
A programming language that requires a variable to be defined and variable it is. It means when declaring a variable, its type must also be specified.
A strongly typed language means that you must be explicit about what type of data you are working with. In java, typing errors are detected during compilation. Examples of strongly typed language are Java, Ruby, Python etc.
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 MCQ Question 15 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.
Which of the following is NOT a java primitive type?
Answer (Detailed Solution Below)
Java MCQ Question 16 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 will be the output of the following Java program?
class test
{public static void main (String args [ ])
{
System.out.println(20 + 30 + "Java");
System.out.println("Java" + 20 + 30);
}
}
Answer (Detailed Solution Below)
50Java
Java2030
Java MCQ Question 17 Detailed Solution
Download Solution PDFAccording to the Associativity Rule.
class test
{public static void main (String args [ ])
{
System.out.println(20 + 30 + "Java"); ; // evaluated as (10 + 20) + "Java"
System.out.println("Java" + 20 + 30); // evaluated as ("Java" + 10) + 20 .
}
}
So Output will be
50Java
Java2030
Therefore option 4 is correct.
Consider the following Java code segment:
public class while /*line1*/
{
public void loop()
{
int x=0;
while(true) /*line 6*/
{
System.out.println(“x plus one is” + (x + 1));
}
}
}
Which of the following is true?
Answer (Detailed Solution Below)
Java MCQ Question 18 Detailed Solution
Download Solution PDFConcept:
There are some rules for class names in java i.e. class name should be noun with the first letter of each internal word in capital and we cannot use any keyword as the class name in java.
Code:
public class while /*line1*/
// above, a keyword is used as class name i.e. while which is error
{
public void loop()
{
int x=0;
while(true) /*line 6*/
{
System.out.println(“x plus one is” + (x + 1));
}
}
}
So, there is syntax error in line 1.
Additional information sent when an exception is thrown may be placed in ______
Answer (Detailed Solution Below)
Java MCQ Question 19 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
}
Which of the following statements is/are TRUE regarding JAVA ?
(a) Constants that cannot be changed are declared using the ‘static’ keyword.
(b) A class can only inherit one class but can implement multiple interfaces.
Answer (Detailed Solution Below)
Java MCQ Question 20 Detailed Solution
Download Solution PDFThe correct answer is option 2.
Statement a: FALSE
In Java, to declare any variable as constant, static and final modifiers are used
Statement b: TRUE
In JAVA, A class can implement multiple interfaces but the class can inherit only one class
To inherit class, extends is used and to inherit an interface, implements keyword is used
Hence the correct answer is Only (b) is TRUE.
What correction is required for following Java code snippet to compile?
int [ ] X = new int [10];
for (int P = 0; P <= X.length (); P++)
X [P] = 5;
Answer (Detailed Solution Below)
Java MCQ Question 21 Detailed Solution
Download Solution PDFIn java, for an integer variable X to find the length of the array we should use X.length instead of x.length().
Corect code:
int [] X = new int [10]; // x is an integer array
for (int P = 0; P < X.length; P++) // so, X.length should be used and P < X.length
X [P] = 5;
Important Point:
String X;
X.length() is used for String data type
Which of the following statements is/are true regarding JAVA?
A. Constants that cannot be changed are declared using the 'Static' keyword.
B. A class can only inherit one class that can implement multiple interfaces.
Answer (Detailed Solution Below)
Java MCQ Question 22 Detailed Solution
Download Solution PDFCorrect Answer: Option 2
Explanation:
- Constants that cannot be changed are declared using the final keyword. The static keyword is used for functions and variables to specify that they will be instantiated exactly once for any number of object instances and the same value will be used across all objects.
- Java does not support multiple inheritances directly. When a class inherits two different classes, it is known as multiple inheritances. Java does not allow for a class to inherit more than one class. However, two classes can be inherited if one of them is an interface. An interface is a schematic representation of a class containing variables and abstract methods. So, a class can inherit one class, that can implement multiple interfaces.
Therefore, statement A is incorrect and statement B is correct.
Answer (Detailed Solution Below)
Java MCQ Question 23 Detailed Solution
Download Solution PDFHere, ++ have higher priority than *.
++g is called as the pre-increment operator in java which will just increase the g value by 1.
By solving the equation will be reduced to print(4*g)
as, g value is 8 (given in the question),
So, answer turns out to be 32 here.
What is the output of this program?
class char_increment {
public static void main (String args[ ])
{
char c1 = 'D';
char c2 = 84;
c2++;
c1++;
System.out.println(c1 + " " + c2);
}
}
Answer (Detailed Solution Below)
Java MCQ Question 24 Detailed Solution
Download Solution PDFCorrect answer: option 1
Explanation:
- When c2++ is executed, the value of c2 becomes 85. This is the ASCII value for the character 'U'
- When c1++ is executed, the value of c1 becomes the character 'E'
- When c1 and c2 are printed with a space is between them, the output is E U
Important Note:
- When a char variable is assigned a number, the number is treated as an ASCII code and the corresponding character is stored in the variable.
- When a char variable is incremented, the corresponding ASCII value of the character is incremented. This results in the character held by the variable being modified.
What will be the output of the following Java program?
public class SumOfArray
{
public static void main(String[ ] args) {
int [ ] arr = new int [ ] {1, 2, 3, 4};
int sum = 0;
for (int i = 0; i < arr.length; i++)
{
sum = sum + arr[i];
}
System.out.println("Sum of all the elements of an arry: " + sum);
}
}
Answer (Detailed Solution Below)
Java MCQ Question 25 Detailed Solution
Download Solution PDFThe correct answer is option 2.
Concept:
An array in Java is an object that contains elements of the same data type. Furthermore, the items of an array are kept in a single memory address. It's a data structure where we save items that are comparable. In a Java array, we can only hold a fixed number of items.
The given Java code is,
public class SumOfArray
{
public static void main(String[ ] args)
int [ ] arr = new int [ ] {1, 2, 3, 4};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i]; }
System.out.printIn("Sum of all the elements of an arry: " + sum); }
}
Explanation:
"arr" is an integer array it has four elements like,
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
And initially, the sum has zero, and each iteration of the for-loop adds to the sum. At end of the sum, the variable has added all elements in an integer arr of four elements.
sum=1+2+3+4;
And sum=10
Prints like Sum of all the elements of an array: 10 as output.
Hence the correct answer is the Sum of all the elements of an array: 10.