MCQ's Based on Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Java MCQ Questions

1. Which of these components are used in a Java program for compilation, debugging,
and execution?

a. JDK

b. JVM

i
c. JRE

in
d. JIT

Answer: (a) JDK Sa


2. Which of these literals can be contained in a float data type variable?

a. -3.4e+050

b. +1.7e+308
l
hi

c. -3.4e+038

d. -1.7e+308
ik

Answer: (b) -3.4e+038

3. What is BigDecimal.ONE?
N

a. it is a custom-defined statement

b. it is a wrong statement

c. it is a static variable that has a value of 1 on a scale of 0

d. it is a static variable that has a value of 1 on a scale of 10


Answer: (c) it is a static variable that has a value of 1 on a scale of 0

4. When an expression consists of int, double, long, float, then the entire expression will
get promoted into a data type that is:

a. float

b. double

c. int

i
d. long

in
Answer: (b) double

5. Which of the following operators can operate on a boolean variable?

A. &&
Sa
B. ==

C. ?:
l
hi

D. +=

a) C & B
ik

b) A & D

c) A, B & D
N

d) A, B & C

Answer: (d) A, B & C

6. Out of these statements, which ones are incorrect?

a. The Brackets () have the highest precedence


b. The equal to = operator has the lowest precedence

c. The addition operator + and the subtraction operator – have an equal precedence

d. The division operator / has comparatively higher precedence as compared to a


multiplication operator

Answer: (d) The division operator / has comparatively higher precedence as compared
to a multiplication operator

7. What will be the output of the following Java program?

i
in
class Output

{
Sa
public static void main(String args[])

int p = 1;
l
int q = 2;
hi

int r = 3;

p |= 4;
ik

q >>= 1;
N

r <<= 1;

p ^= r;

System.out.println(q + ” ” + q + ” ” + r);

}
a. 3 3 6

b. 2 3 4

c. 2 2 3

d. 3 1 6

Answer: (d) 3 1 6

8. What will be the output of the following Java code snippet?

i
in
class P

enum Enums extends P


Sa
{

PQR, QRS, RST, STU;


l
hi

a) Compilation Error
ik

b) Runtime Error
N

c) EnumNotDefined Exception

d) It runs successfully

Answer: (a) Compilation Error

9. When does Overloading not occur?


a. When more than a single method have the same name, yet different types or number
of parameters and different method signature

b. When more than a single method have the same name, the same signature, but have
different numbers of signature

c. When more than a single method have the same signature, same name, and the
same number of parameters have different types

d. When more than a single method have the same name, the same number and types

i
of parameters, and yet different signatures

in
Answer: (d) When more than a single method have the same name, the same number
and types of parameters, and yet different signatures
Sa
10. What is it called when the child object also gets killed when the parent object is
killed in the program?

a. Encapsulation

b. Association
l
hi

c. Aggregation

d. Composition
ik

Answer: (d) Composition

11. How does one identify if a compilation unit is an interface or class from a .class file?
N

a. Extension of the compilation unit

b. Java source file header

c. The class and interface cannot be differentiated

d. The unit type must be used to postfix interface or class name


Answer: (b) Java source file header

12. Out of these methods of the String class, which one can be used for testing the
strings for equality?

a. isequals()

b. isequal()

c. equals()

i
d. equal()

in
Answer: (c) equals()

13. What would happen to the thread whenever the garbage collection kicks off?
Sa
a. The garbage collection won’t happen until the running of the thread

b. The thread would continue its operation

c. The garbage collection and the thread don’t interfere with each other
l
hi

d. The thread would be paused while the running of the garbage collection

Answer: (d) The thread would be paused while the running of the garbage collection
ik

14. Out of these, which one is the correct way of calling a constructor that has no
parameters of the superclass A by the subclass B?
N

a. superclass.();

b. super(void);

c. super();

d. super.A();

Answer: (c) super();


15. Out of these methods of the Object class, which one can clone an object?

a. Object clone()

b. clone()

c. Object copy()

d. copy()

Answer: (a) Object clone()

i
in
16. Out of these packages, which one contains an abstract keyword?

a. java.util

b. java.lang

c. java.system
Sa
d. java.io

Answer: (b) java.lang


l
hi

17. Out of these methods, which one can be used for converting all the characters
present in a String into an Array of characters?
ik

a. both getChars() & toCharArray()

b. both charAt() & getChars()


N

d. charAt()

d. all of the mentioned

Answer: (a) both getChars() & toCharArray()

18. What value is returned by the compareTo() function in case the invoking string
happens to be greater than the compared string?
a. a value that is greater than zero

b. a value that is less than zero

c. zero

d. none of the above

Answer: (a) a value that is greater than zero

19. Out of these exceptions, which one is thrown by the compareTo() method that is

i
defined in a double wrapper?

in
a. SystemException

b. ClassCastException

c. IOException
Sa
d. CastException

Answer: (b) ClassCastException


l
hi

20. Where does the String Pool get stored?

a. Metaspace
ik

b. Java Stack

c. Java Heap
N

d. Permanent Generation

Answer: (c) Java Heap

21. Out of these data members of the HttpResponse class, which one is used for the
storage of the response that is from an http server?

a. address
b. status

c. statusCode

d. statusResponse

Answer: (c) statusCode

22. Out of these methods, which one makes the raw MIME formatted string?

a. toString()

i
in
b. getString()

c. parse()

d. parseString()

Answer: (c) parse()


Sa
23. The remover() method throws which of these exceptions:

a. ObjectNotFoundException
l
hi

b. IllegalStateException

c. IOException
ik

d. SystemException
N

Answer: (b) IllegalStateException

24. Out of the following, which one is a superclass of all the exception type classes?

a. String

b. RuntimeExceptions

c. Catchable
d. Throwable

Answer: (d) Throwable

25. What happens when we call two threads that have the same priority to process
simultaneously?

a. Both of the threads will be simultaneouslyexecuted

b. Any one of the threads can be executed first lexicographically

i
c. Itdepends on the OS

in
d. There will be no execution of threads

Answer: (c) Itdepends on the OS


Sa
26. Out of these classes, which one is used for reading strings and characters in Java
from the console?

a. StringReader
l
b. BufferedReader
hi

c. InputStreamReader

d. BufferedStreamReader
ik

Answer: (b) BufferedReader


N

27. Out of these operators, which one can be used to get the run time info about an
object?

a. Info

b. getInfo

c. getinfoof
d. instanceof

Answer: (d) instanceof

28. Out of these classes, which one allows a user to define their own formatting pattern
for time and dates?

a. UsersDateFormat

b. ComplexDateFormat

i
c. SimpleDateFormat

in
d. DefinedDateFormat

Answer: (c) SimpleDateFormat


Sa
29. Which method can we use in an applet to output a string?

a. transient()

b. drawString()
l
hi

c. print()

d. display()
ik

Answer: (b) drawString()

30. The public int start() returns what?


N

a. the start index of the previous match

b. the start index of the current match

c. the start index of the input string

d. None of the above


Answer: (a) the start index of the previous match

31. Which one is a superclass of the ContainerEvent class out of the following?

a. ComponentEvent

b. InputEvent

c. ItemEvent

d. WindowEvent

i
in
Answer: (a) ComponentEvent

32. _______ is a superclass of all the Adapter classes.

a. ComponentEvent

b. Applet
Sa
c. InputEvent

d. Event
l
hi

Answer: (b) Applet

33. Which method in Java generates boolean random values?


ik

a. randomBoolean()
N

b. nextBoolean()

c. generateBoolean()

d. previousBoolean()

Answer: (b) nextBoolean()

34. Which class produces objects with respect to their geographical locations?
a. Simple TimeZone

b. Date

c. Locale

d. TimeZone

Answer: (c) Locale

35. Which method is used for notifying the observer about the change in the observed

i
object?

in
a. notify()

b. update()

c. observed()
Sa
d. check()

Answer: (b) update()


l
hi

36. Which package is used to remotely invoke a method?

a. java.awt
ik

b. java.rmi

c. java.applet
N

d. java.util

Answer: (b) java.rmi

37. What are the uses of generics?

a. The generics make a code more readable and optimised


b. The generics make a code faster

c. The generics add stability to a code. They do so by making more bugs detectable at
the runtime

d. The generics add stability to a code. They do so by making more bugs detectable at
the compile time

Answer: (d) The generics add stability to a code. They do so by making more bugs
detectable at the compile time

i
38. Which mechanism helps in the process of naming as well as visibility control of the

in
classes and their content?

a. Packages

b. Interfaces
Sa
c. Object

d. None of the above


l
Answer: (a) Packages
hi

39. The configuration is stored in which of the file database tables:


ik

a. .sql

b. .ora
N

c. .hbm

d. .dbm

Answer: (c) .hbm

40. The primary use of Files.lines(Path path) is that it:


a. reads the lines that are from a file as the Stream

b. reads the files that are at the path specified as the String

c. counts the total number of lines for the files at the specified path

d. reads the filenames at the specified path

Answer: (a) reads the lines that are from a file as the Stream

41. Which keywords for the purpose of upper bound a wildcard?

i
in
a. bound

b. stop

c. implements

d. extends
Sa
Answer: (d) extends

42. Out of the following statements, which one is not true about the Java beans?
l
hi

a. It extends the java.io.Serializable class

b. It implements the java.io.Serializable interface


ik

c. It provides getter and setter methods for its properties


N

d. It provides us with no argument constructor

Answer: (a) It extends the java.io.Serializable class

43. What does the abbreviation MVC pattern stand for?

a. Model View Class

b. Mock View Class


c. Mock View Controller

d. Model View Control

Answer: (d) Model View Control

44. The advantage of using the PreparedStatement in Java is:

a. More memory usage

b. Prevents SQL injection

i
in
c. Encourages SQL injection

d. Slow performance

Sa
Answer: (b) Prevents SQL injection

45. How does one move from some desired step to another one?

a. logger.error

b. logger.log
l
hi

c. System.out.println

d. breakpoints
ik

Answer: (d) breakpoints


N

46. What would happen if the IP Address of the host can’t be determined?

a) IOException is thrown

b) The system will exit with no message

c) Temporary IP Address will be assigned

d) UnknownHostException is thrown
Answer: (d) UnknownHostException is thrown

47. The storage capacity of a single cookie is:

a) 4095 MegaBytes

b) 4095 bytes

c) 2048 bytes

d) 2048 MegaBytes

i
in
Answer: (b) 4095 bytes

48. Which action variable helps in including a file in the JSP?

a) jsp:plugin

b) jsp:include
Sa
c) jsp:getProperty

d) jsp:setProperty
l
hi

Answer: (b) jsp:include

49. ______ file defines dependency in maven.


ik

a) dependency.xml
N

b) build.xml

c) version.xml

d) pom.xml

Answer: (d) pom.xml

50. The main difference between AutoCloseable and Closeable is that:


a) AutoCloseable throws IOException; Closeable throws Exception

b) AutoCloseable is an implementation; Closeable is a concept

c) AutoCloseable throws Exception; Closeable throws IOException

d) AutoCloseable is a concrete class and Closeable is an interface

Answer: (c) AutoCloseable throws Exception; Closeable throws IOException

i
in
Thanks & Regards
Sa
Nikhil Saini
Technical Trainer IMSEC
l
hi
ik
N

You might also like