Top 40 Java Interview Questions and Answers
Top 40 Java Interview Questions and Answers
JAVA PROGRAMMING
TOP 40 MCQ'S WITH ANSWERS
www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
A) No
B) Yes
C) Not Sure
A. Java Compiler
B. Java Disassembler
C. Java Debugger
D. Java Interpreter
Ans. B) javap command or Java Disassembler disassembles one or more class files.
It is used to get the information of any class or interface.
Ques.3) A process in OOP concept that involves recognizing and focusing on the
important characteristics of a situation or object is known as:
A. Abstraction
B. Polymorphism
C. Encapsulation
D. Inheritance
Ans. A) Abstraction is a process of hiding the implementation details from the user.
In other words, the user will have the information on what the object does instead
of how it does it.
A. 16
B. 8
C. 32
D. 64
Ans. A) Java uses Unicode system not ASCII code system and to represent a Unicode
system 8 bit is not enough to represent all characters, so Java uses 2 byte for
characters.
Page 1 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
A. void method1
B. void method2()
C. void method3(void)
D. method4()
A. Unsigned
B. Signed
C. None of these
D. Both A) and B)
Ans. A) Signed data means the data has to be declared before we use it and thus
these datatypes are signed .Only String is unsigned.
Ans. C) In Java, arrays are objects, they have members like length. The length
member is final and cannot be changed. All objects are allocated on heap in Java,
so arrays are also allocated on heap.
Ques.8) With x =0, which of the following are legal lines of Java code for changing
the value of x to 1?
i. x++;
ii. x= x+1;
iii. x+=1;
iv. x=+1;
Page 2 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
Ques.9) Can 8 byte long data type be automatically type cast to 4 byte float data type?
A. True
B. False
Ans. A) Both data types have different memory representation that’s why 8 byte
integral data type can be stored to 4-byte float data type.
Ques.10) How can we identify whether a compilation unit is class or interface from a
.class file ?
Ans. A) The Java source file contains a header that declares the type of class or
interface ,its visibility with respect to other classes, its name and any superclass it may
extend.
A. malloc
B. alloc
C. give
D. new
Ans. D) Operator new dynamically allocates memory for an object and returns a
reference to it. This reference is addressed in memory of the object allocated by
new.
A. Heap
B. Stack
C. JVM
D. Class
Page 3 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
Sum=20
opt=5
Sum= Sum + (++opt);
A. 20
B. 26
C. 25
D. 5
Ans. B) The value of Sum is: 26, because opt is pre-incremented. So, the original
value of Sum(20) will be added to the incremented value of opt(i.e., 5+1=6). Therefore,
20+6=26.
A. Interfaces
B. Multithreading
C. Private methods
D. Protected methods
Ques.15) Which of these methods is used to explicitly set the priority of a thread?
A. set()
B. make()
C. setPriority()
D. makePriority()
Ans. C) The default value of priority given to a thread is 5 but we can explicitly change
that value between the permitted values 1 & 10, using the method setPriority().
Ques.16) What is the output of a Bitwise AND (&) operation if one of the
inputs/operands is 0?
A. 0
B. 1
C. 0 or 1
D. None of the above
Ques.17) What is the output of the Java code snippet with a ternary operator?
Page 4 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
A. Marks=0
B. Marks=10
C. Marks=20
D. Compile error
Ans. B) Because name ==”java” is the correct statement that’s why the
expression just after the ternary operator is printed.
Ques.18) What is the maximum number of ELSE-IF statements that can present in
between starting IF and ending ELSE statements?
A. 32
B. 64
C. 128
D. None of the above
Ans. C) AWT stands for Abstract Window Toolkit, it is used by applets to interact with
the user.
Ans. A) It uses correct array declaration and correct array construction, while others
generate a compile error.
Ques.21) switch(x)
{
default:
System.out.println("Hello");
}
Page 5 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
A. i & iii
B. ii & iv
C. i & iv
D. ii & iii
Ans. A) Switch statements are based on integer expressions and since both
bytes and chars can implicitly be widened to an integer, these can also be used.
Short is wrapper class and reference types can not be used as variables.
Ques.22) Which of the following would compile without error?
A. int a = Math.abs(-5);
B. int b = Math.abs(5.0);
C. int c = Math.abs(5.5F);
D. int d = Math.abs(5L);
Ans. A) The return value of the Math.abs() method is always the same as the type of
the parameter passed into that method.
Ques.23) What is the numerical range of char?
A. 0 to 32767
B. 0 to 65535
C. -256 to 255
D. -32768 to 32767
Ans. B) The char type is integral but unsigned. The range of a variable of type
char is from 0 to 65535. Java characters are Unicode, which is a 16-bit
encoding capable of representing a wide range of international characters.
Ques.24) Which of these methods of ArrayList class is used to obtain the present size
of an object?
A. size()
B. length()
C. index()
D. capacity()
Ans. A)
Page 6 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
Ans. C) There are two ways to remove an object from ArrayList. We can use an
overloaded method remove(int index) or remove(Object obj). We can also use it as an
iterator to remove the object.
Ques.26) Which of the following keywords is used for throwing exception manually?
A. finally
B. try
C. throw
D. catch
random() always returns a value in the range 0.0 1 to 1.99 that will be '1' when
typecasted to int. not selected.
Ques.29) What will be the output of the program?
Page 7 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
{
public static void main(String [] args)
{
signed int x = 10;
for (int y=0; y<5; y++, x--)
System.out.print(x + ", ");
}
}
A. 10,9,8,7,6
B. 9,8,7,6,5
C. Compilation fails
D. An exception is thrown at runtime
Ans. C) The word "signed" is not a valid modifier keyword in the Java language. All
number primitives in Java are signed. Hence the Compilation will fail.
Ques.31) Which is true about a method-local inner class?
A. It must be marked final.
B. It can be marked abstract.
C. It can be marked public.
D. It can be marked static.
subclass of the inner class must be created if the abstract class is to be used (so an
A. Static
B. Constant
C. Protected
Page 8 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
D. Final
Ques. 33) which of these classes is the superclass of every class in java?
A. String
B. Object
C. Abstract
D. Array list
Ques. 33) Which operator is used to invert all the digits in binary representation of a
number ?
A) ~
B) <<<
C) >>>
D) ^
Ans. A) Unary not operator ~ inverts all of the bits of its operant in binary
representation.
Page 9 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
A. Integer
B. Boolean
C. Characters
D. Double
Ans. B)
A) ()
B) ++
C. *
D. >>
Ques. 36) Which of the following is a superclass of all exception type classes?
A. Catchable
B. RuntimeExceptions
C. String
D. Throwable
Ans. D) Throwable is built in class and all exception types are subclass of this class.
Page 10 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
A. Process based
B. Thread based
A. Process
B. Daemon thread
C. User thread
D. JVM thread
Ans. B) Daemon thread runs in the background and does not prevent JVM from
terminating.
class Abc
{
public static void main(String[]args)
{
String[] elements = {“for”,”tea”,”too”};
Page 11 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
A. Compilation error
Ans. D) The value at the 0th position will be assigned to the variable first.
Ans. C) Variables of an interface are public, static and final by default because the
Ques. 42) Which of these methods is given parameters via command line arguments?
A. main()
B. Recursive()method
Page 12 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
C. Any method
Ans. A) Only the main() method can be given parameters via using command line
arguments.
Ques 43) Which of these packages contains the exception Stack Overflow in Java?
A. java.lang
B. java.util
C. java.io
D. java.system
Ans. B) They read high level language and execute the program. Interpreters are
Page 13 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
m(i);
System.out.println(i);
}
public void m(int i)
{
i += 2;
}
}
The output of the program will be
A) 1,3
B) 3,1
C) 1,1
D) 1,0
Ans. C) 1,1
A. ArithmeticExecption
B. ClassNotFoundExecption
C. NullPointerExecption
D. NumberFormatExecption
Ans. D) parseInt()Method parses input into integer. The exception thrown by this
Page 14 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS
D. Sorter.sortAsc(listObj);
Ans. B) Collections provide a method to sort the list. The order of sorting can
Ques. 49) Which of these methods of Object class is used to obtain class of an object
at run time?
A. get()
B. void getclass()
C. Class getclass()
D. None of the mentioned.
Page 15 of 15 www.mobiprep.com