0% found this document useful (0 votes)
22 views

Top 40 Java Interview Questions and Answers

Uploaded by

Rahul M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Top 40 Java Interview Questions and Answers

Uploaded by

Rahul M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Placement handbook of

JAVA PROGRAMMING
TOP 40 MCQ'S WITH ANSWERS

www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS

Ques.1) Does Java supports multithreading?

A) No
B) Yes
C) Not Sure

Ans. C) Multithreading in Java is a process of executing multiple threads


simultaneously. It is time saving too as we can perform many operations together.

Ques.2) What do you mean by javap?

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.

Ques.4) In Java, ‘char’ allocates how many bits?

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

Ques.5) Which one of these is a valid method declaration?

A. void method1
B. void method2()
C. void method3(void)
D. method4()

Ans. B) In Java, a method is declared with return type and is represented by


parentheses.

Ques.6) In Java, byte, short, int and long are

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.

Ques.7) Which of the following is false about arrays on Java?

A. A java array is always an object.


B. Arrays in Java are always allocated on heap.
C. Length of array can be changed after creation of array.
D. None of the above

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;

A. i, ii, & iii


B. i & iv
C. i, ii, iii, & iv
D. iii & ii

Ans. C) Operator++ increases value of variable by 1. x=x+1 can also be written in


shorthand for, as x+=1. Also x=+1 will set the value of x to 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 ?

A. Java source file header


B. We cannot differentiate between class and interface.
C. Extension of compilation unit.
D. The class or interface name should be postfixed with unit type.

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.

Ques.11) Which of these operators is used to allocate memory for an object?

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.

Ques.12) Which of the following has the highest memory requirement?

A. Heap
B. Stack
C. JVM
D. Class

Ans. C) JVM is the super set which contains heap, stack,


objects, pointers, etc.

Ques.13) The value of variable Sum after the following code is


executed will be

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.

Ques.14) Using which of the following, multiple inheritance in Java can be


implemented?

A. Interfaces
B. Multithreading
C. Private methods
D. Protected methods

Ans. A) Multiple inheritance in java is implemented using interfaces. Multiple interfaces


can be implemented by a class.

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

Ans. A) Because (0 & anything) is always equal to 0.

Ques.17) What is the output of the Java code snippet with a ternary operator?

String name = "java";

Page 4 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS

int marks = name == "java"?10:20;


System.out.println("Marks=" + marks);

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. D) We can write any number of ELSE-IF statements in a Java program.

Ques.19) What does AWT stand for?

A. All Window Tools


B. All Writing Tools
C. Abstract Window Toolkit
D. Abstract Writing Toolkit

Ans. C) AWT stands for Abstract Window Toolkit, it is used by applets to interact with
the user.

Ques.20) Which one creates an instance of an array?

A. int[ ] ia = new int[15];


B. char[ ] ca = "Some String";
C. float fa = new float[20];
D. int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };

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

Which two are acceptable types for x?


i. byte
ii. long
iii. char
iv. float

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

Ques.25) How can we remove an object from ArrayList?


A. remove()method
B. using iterator
C. remove ()method and using iterator
D. delete() method

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

Ans. C) ”throw” keyword is used for throwing exception manually

in java program. User defined exceptions can be thrown too.

Ques.27) Which component is responsible to optimize bytecode to machine code?


A. JVM
B. JDK
C. JIT
D. JRE

Ans. C) JIT optimizes bytecode to machine specific language code by compiling


similar bytecodes at same time. This reduces overall time taken for compilation of
bytecode to machine specific language.
Ques.28) Which of the following options is the best for generating random integer 0 or
1?
A. (int)Math.random()
B. (int)Math.random()+1
C. (int)(Math.random()+0.5)
D. (int)(Math.random()+0.2)

Ans. C)random() is a static method defined in

Math class. Syntax: static double random()

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?

public class Test

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.

Ans. B ) Because a method-local inner class can be abstract, although it means a

subclass of the inner class must be created if the abstract class is to be used (so an

abstract method-local inner class is probably not useful).

Ques.32) Which of these keywords can be used to prevent method overriding?

A. Static

B. Constant

C. Protected

Page 8 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS

D. Final

Ans. D ) to disallow a method from being overridden to specify final as a

modifier at the start of its declaration.

Ques. 33) which of these classes is the superclass of every class in java?

A. String

B. Object

C. Abstract

D. Array list

Ans. B) object class is the superclass of every class in Java.

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.

Ques. 34 ) What is the output of relational operators?

Page 9 of 15 www.mobiprep.com
TOP JAVA MCQ’s WITH ANSWERS

A. Integer

B. Boolean

C. Characters

D. Double

Ans. B)

Ques 35) Which of these have highest precedence?

A) ()

B) ++

C. *

D. >>

Ans. A) Order of precedence is (Highest to lowest) 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

Ques. 38) Which of these are types of multitasking?

A. Process based

B. Thread based

C. Process and thread based

D. None of the above

Ans. C) There are two types of multitasking: Process based multitasking

and thread based multitasking.

Ques. 39) What does not prevent JVM from terminating?

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.

Ques. 40) What is the output of this program:

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

String first =(elements.length>0)?


}
}

A. Compilation error

B. An exception is thrown at runtime

C. The variable first is set to null

D. The variable first is set to elements[0]

Ans. D) The value at the 0th position will be assigned to the variable first.

Ques. 41) All the variables of interface should be

A. Default and final

B. Default and static

C. Public, static and final

D. None of the above

Ans. C) Variables of an interface are public, static and final by default because the

interface cannot be instantiated.

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

D. None of the above

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. A) java.lang contains the Stack Overflow in Java.

Ques. 44) What is the use of an interpreter?

A. They convert bytecode to machine code.


B. They read high level code and execute them.
C. They are intermediate between JIT and JVM.
D. It is a synonym of JIT.

Ans. B) They read high level language and execute the program. Interpreters are

normally not passing through bytecode and JIT compilation.

Ques. 45) public class testmath


{
static int i = 1;
public static void main(String args[])
{
System.out.println(i+” , “);

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

Ques. 46) What exception throw by parseInt() method?

A. ArithmeticExecption
B. ClassNotFoundExecption
C. NullPointerExecption
D. NumberFormatExecption

Ans. D) parseInt()Method parses input into integer. The exception thrown by this

method is number format exception.

Ques. 47) What is the premise of equality for IdentityHashMap?


A. Reference Equality
B. Name equality
C. Hashcode equality
D. Length equality

Ans. A) IdentityHashMap is rarely used as it violates the basic contact of

implementing equals() and HashCode method().

Ques. 48) How to sort elements of Arraylist?


A. Collection.sort(listObj);
B. Collections.sort(listObj);
C. listObj.sort();

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

be defined using Comparator.

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.

Ans. C) Class getclass() is used to obtain class of an object at run time.

Page 15 of 15 www.mobiprep.com

You might also like