50 Java MCQ With Answers
50 Java MCQ With Answers
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 Architecture
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
✓ 8
Show Explanation
✓ 32 and 64
32 and 32
64 and 64
64 and 32
Show Explanation
Correct Answer
The size of float and double in java is 32 and 64.
Byte to int
✓ Int to long
Long to int
Short to int
Show Explanation
Correct Answer
Automatic type conversion is possible in Int to long.
Compile error
Throws exception
✓ 24 I
Show Explanation
Correct Answer
Get Placed at Top Product Companies with Scaler
24 I will be printed.
5. 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
Show Explanation
Correct Answer
This will give compile error - “Lossy conversion from int to short”
✓ -127
127
129
Show Explanation
Correct Answer
Range of byte data in java is -128 to 127. But the byte data type in java is cyclic in nature.
Show Explanation
Correct Answer
char[] ch = new char[5] is the correct syntax for declaring a character array.
✓ 120 200 14
120 200 16
None
Show Explanation
Correct Answer
016 is an octal number, its equivalent decimal number is 14. Hence answer is B.
Show Explanation
Correct Answer
When an array is passed to a method, a reference of the array is received by the method.
int[] A = (1, 2, 3)
int[][] A = {1,2,3}
Show Explanation
Correct Answer
int[] A = {1, 2, 3} is the valid way of declaring arrays.
11. 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];
}
✓ 1
Show Explanation
Correct Answer
a.length = 5
A[0] = a[(0 + 3) % 5] = a[3] = 1
So, a[0] = a[3] = 1
A[1] = a[(2 + 3) % 5] = a[0] = 1
Therefore, a[1] = 1;
Object references
✓ objects
None
Show Explanation
Correct Answer
Arrays are objects in java. It is a container that holds a fixed number of items of a single type.
At compile time
None
Show Explanation
Correct Answer
The object created with new keyword during run-time.
Show Explanation
Correct Answer
A package is a collection of classes and interfaces.
I and II
II and III
Only III
✓ I, II and III
Show Explanation
Correct Answer
Static methods must only access static data and can call other static methods. Moreover they cannot refer this or super.
16. Identify the keyword among the following that makes a variable belong to a class,rather than
Get Placed at Topbeing defined
Product for each
Companies instance
with Scaler of the
class.
final
✓ static
volatile
abstract
Show Explanation
Correct Answer
Static keyword makes a variable belong to a class,rather than being defined for each instance of the class.
17. 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
None
Show Explanation
Correct Answer
Only solution class can directly access and change the value of the variable res.
✓ java.lang.Object
java.lang.String
java.lang.util
None
Show Explanation
Correct Answer
toString() is defined in java.lang.Object.
✓ An int value
None
Show Explanation
Correct Answer
compareTo() returns an int value
abc
✓ bc
bcd
cd
Show Explanation
Correct Answer
str.substring(start, end) returns the string from s[start] till s[end - 1]
true
✓ -1
Show Explanation
Correct Answer
Since, t isn’t present in the string str, it returns -1.
one
two
✓ onetwo
twoone
Show Explanation
Correct Answer
concat attached both the string. Hence answer is C.
None.
Show Explanation
Correct Answer
replace() replaces all the occurrences of the oldcharacter by the newcharacter.
24. To which of the following does the class string belong to.
✓ java.lang
java.awt
java.applet
java.string
Show Explanation
Correct Answer
string class belongs to java.lang.
✓ 3
None
Show Explanation
Correct Answer
Using the new keyword creates an object everytime. Hence, 2 objects are created for first two statement. Next, a string is declared
which creates another object. For the fourth statement, since, a string ”Interviewbit” already exists, it doesn’t create an additional
object again. Hence, answer is 3.
✓ 13
20
Show Explanation
Correct Answer
String class has 13 constructors.
101
100
None
Show Explanation
✓ TRUE
FALSE
Compile error
None
Show Explanation
Correct Answer
Since, LHS matches RHS, hence the output is TRUE.
✓ 50
22
10
None
Show Explanation
Correct Answer
Explanation - x* = 3 + 7 is equivalent to x * (3 + 7) = x * 10. Therefore, x = 50.
30. Identify the return type of a method that does not return any value.
int
✓ void
double
Get Placed at Top Product Companies with Scaler
None
Show Explanation
Correct Answer
void does not return any value.
✓ 3.0
4.0
Show Explanation
Correct Answer
floor returns largest integer that is less than or equal to the given number.
32. Where does the system stores parameters and local variables whenever a method is invoked?
Heap
✓ Stack
Array
Tree
Show Explanation
Correct Answer
The system stores parameters and local variables in a stack.
public
protected
private
✓ static
Show Explanation
34. What is the variables declared in a class for the use of all methods of the class called?
Object
✓ Instance variables
Reference variable
None
Show Explanation
Correct Answer
It is know as instance variable.
No return type
void
None
Show Explanation
Correct Answer
Implicit return type of constructor is the class object in which it is defined.
None
Show Explanation
Correct Answer
finalize() method is called before garbage collection.
Solution()
public Solution(void)
✓ public Solution()
Show Explanation
Correct Answer
public Solution() is the prototype of the default constructor.
Solution(){}
public Solution(){}
Solution(void){}
Show Explanation
Correct Answer
Both A and B are correct way of declaring constructor.
✓ 6
Show Explanation
Get Placed at Top Product Companies with Scaler
Correct Answer
Since, the loop runs till 6, the value of i is 6.
✓ 10
11
Show Explanation
Correct Answer
Interviewbit will be printed 10 times, starting from count = 0.
for(; ;)
for(int i = 0; ;i++)
Show Explanation
Correct Answer
All of the above are infinite loop.
Abstract class
✓ Interface
Class
Method
Show Explanation
✓ catch
throw
final
none
Show Explanation
Correct Answer
Answer- A) Exception created by try block is caught in catch block.
44. Which of the following exception is thrown when divided by zero statement is executed?
NullPointerException
NumberFormatException
✓ ArithmeticException
None
Show Explanation
Correct Answer
ArithmeticException is thrown when divided by zero statement is executed.
✓ java.lang.package
java.util.package
java.io.package
None
Show Explanation
Correct Answer
System class is defined in java.lang.package.
46. Identify the interface which is used to declare core methods in java?
Get Placed at Top Product Companies with Scaler
Comparator
EventListener
Set
✓ Collection
Show Explanation
Correct Answer
Collection is used to declare core methods in java.
47. Which of the following statements are true about finalize() method?
Show Explanation
Correct Answer
The finalize() method can be called Zero or one times.
Show Explanation
Correct Answer
>>>> is Zero fill right shift.
Object oriented
✓ Use of pointers
Show Explanation
Correct Answer
Java does have the concept of pointers.
50. Which of the following is used to find and fix bugs in the program?
JDK
JRE
JVM
✓ JDB
Show Explanation
Correct Answer
JDB is used to find and fix bugs in the program.
Blog About Us
FAQ Contact Us
Interview Preparation