Java MCQ
Java is a high-level, general-purpose, object-oriented programming language. It is easy and used to develop any kind of program. Apart from this, it can also be used in Android development.
Editions of Java
- Java mainly has three editions -
- Java Standard Edition(SE): develop applications that run on the desktop.
- Java Enterprise Edition(EE): develop server-side applications.
- Java Micro Edition(ME): develop applications for mobile devices.
Java Development Kit
- Set of programs that enable us to develop our programs.
- Contains JRE(Java Runtime Environment) that is used to run our programs.
Java Architecture
- Java Architecture consists of three main components:
- JVM: One of the main features of Java is Write Once Run Anywhere, i.e. it is platform-independent. It can run on any OS irrespective of the environment because of Java Virtual Machine.
- JRE: Java Runtime Environment provides an environment for the Java programs to be executed.
- JDK: It is the software development environment that is mainly used in the development of Java applications and applets.
Java MCQ Questions
Number of primitive data types in Java are?
6
7
8
9
What is the size of float and double in java?
32 and 64
32 and 32
64 and 64
64 and 32
Automatic type conversion is possible in which of the possible cases?
Byte to int
Int to long
Long to int
Short to int
Find the output of the following code.
int Integer = 24;
char String = ‘I’;
System.out.print(Integer);
System.out.print(String);
Compile error
Throws exception
I
24 I
Find the output of the following program.
public class Solution{
public static void main(String[] args){
short x = 10;
x = x * 5;
System.out.print(x);
}
}
50
10
Compile error
Exception
Find the output of the following program.
public class Solution{
public static void main(String[] args){
byte x = 127;
x++;
x++;
System.out.print(x);
}
}
-127
127
129
2
Select the valid statement.
char[] ch = new char(5)
char[] ch = new char[5]
char[] ch = new char()
char[] ch = new char[]
Find the output of the following program.
public class Solution{
public static void main(String[] args){
int[] x = {120, 200, 016};
for(int i = 0; i < x.length; i++){
System.out.print(x[i] + “ “);
}
}
}
120 200 016
120 200 14
120 200 16
None
When an array is passed to a method, what does the method receive?
The reference of the array
A copy of the array
Length of the array
Copy of first element
Select the valid statement to declare and initialize an array.
int[] A = {}
int[] A = {1, 2, 3}
int[] A = (1, 2, 3)
int[][] A = {1,2,3}
Find the value of A[1] after execution of the following program.
int[] A = {0,2,4,1,3};
for(int i = 0; i < A.length; i++){
A[i] = A[(A[i] + 3) % A.length];
}
0
1
2
3
Arrays in java are-
Object references
objects
Primitive data type
None
When is the object created with new keyword?
At run time
At compile time
Depends on the code
None
Identify the corrected definition of a package.
A package is a collection of editing tools
A package is a collection of classes
A package is a collection of classes and interfaces
A package is a collection of interfaces
Identify the correct restriction on static methods.
- They must access only static data
- They can only call other static methods.
- They cannot refer to this or super.
I and II
II and III
Only III
I, II and III
Identify the keyword among the following that makes a variable belong to a class,rather than being defined for each instance of the class.
final
static
volatile
abstract
Identify what can directly access and change the value of the variable res.
package com.mypackage;
public class Solution{
private int res = 100;
}
Any class
Only Solution class
Any class that extends Solution
None
In which of the following is toString() method defined?
java.lang.Object
java.lang.String
java.lang.util
None
compareTo() returns
True
False
An int value
None
Identify the output of the following program.
String str = “abcde”;
System.out.println(str.substring(1, 3));
abc
bc
bcd
cd
Identify the output of the following program.
String str = “Hellow”;
System.out.println(str.indexOf(‘t));
0
1
true
-1
Identify the output of the following program.
Public class Test{
Public static void main(String argos[]){
String str1 = “one”;
String str2 = “two”;
System.out.println(str1.concat(str2));
}
}
one
two
onetwo
twoone
What does the following string do to given string str1.
String str1 = “Interviewbit”.replace(‘e’,’s’);
Replaces single occurrence of ‘e’ to ‘s’.
Replaces all occurrences of ‘e’ to ‘s’.
Replaces single occurrence of ‘s’ to ‘e’.
None.
To which of the following does the class string belong to.
java.lang
java.awt
java.applet
java.string
How many objects will be created in the following?
String a = new String(“Interviewbit”);
String b = new String(“Interviewbit”);
Strinc c = “Interviewbit”;
String d = “Interviewbit”;
2
3
4
None
Total constructor string class have?
3
7
13
20
Find the output of the following code.
int ++a = 100;
System.out.println(++a);
101
Compile error as ++a is not valid identifier
100
None
Find the output of the following code.
if(1 + 1 + 1 + 1 + 1 == 5){
System.out.print(“TRUE”);
}
else{
System.out.print(“FALSE”);
}
TRUE
FALSE
Compile error
None
Find the output of the following code.
Public class Solution{
Public static void main(String… argos){
Int x = 5;
x * = (3 + 7);
System.out.println(x);
50
22
10
None
Identify the return type of a method that does not return any value.
int
void
double
None
Output of Math.floor(3.6)?
3
3.0
4
4.0
Where does the system stores parameters and local variables whenever a method is invoked?
Heap
Stack
Array
Tree
Identify the modifier which cannot be used for constructor.
public
protected
private
static
What is the variables declared in a class for the use of all methods of the class called?
Object
Instance variables
Reference variable
None
What is the implicit return type of constructor?
No return type
A class object in which it is defined
void
None
When is the finalize() method called?
Before garbage collection
Before an object goes out of scope
Before a variable goes out of scope
None
Identify the prototype of the default constructor.
Public class Solution {}
Solution(void)
Solution()
public Solution(void)
public Solution()
Identify the correct way of declaring constructor.
Public class Solution {}
Solution(){}
public Solution(){}
Solution(void){}
Both (A) and (B)
Find the output of the following code.
Public class Solution{
Public static void main(String args[]){
Int i;
for(i = 1; i < 6; i++){
if(i > 3) continue;
}
System.out.println(i);
}
}
3
4
5
6
How many times will “Interviewbit” be printed.
Int count = 0;
do{
System.out.println(“Interviewbit”);
count++;
} while(count < 10);
8
9
10
11
Identify the infinite loop.
for(; ;)
for(int i = 0; i < 1; i--)
for(int i = 0; ;i++)
All of the above
What is Runnable?
Abstract class
Interface
Class
Method
Exception created by try block is caught in which block
catch
throw
final
none
Which of the following exception is thrown when divided by zero statement is executed?
NullPointerException
NumberFormatException
ArithmeticException
None
Where is System class defined?
java.lang.package
java.util.package
java.io.package
None
Identify the interface which is used to declare core methods in java?
Comparator
EventListener
Set
Collection
Which of the following statements are true about finalize() method?
It can be called Zero or one times
It can be called Zero or more times
It can be called Exactly once
It can be calledOne or more times
What does the operator >>>> do?
Right shift operator
Left shift operator
Zero fill left shift
Zero fill right shift
Identify the incorrect Java feature.
Object oriented
Use of pointers
Dynamic
Architectural neural
Which of the following is used to find and fix bugs in the program?
JDK
JRE
JVM
JDB