Friday, September 16, 2022

Java MCQ Questions

James Gosling developed JAVA at Sun Microsystems in May 1995. The main purpose of the JAVA programming language is to provide programmers with one-time write and run, anywhere functionality. The compiled Java code can be run on any platform that supports Java without recompiling.

The importance of JAVA are:

  • Develop applications and platforms for a variety of devices such as computers, laptops, game consoles, Blu-ray players, car navigation systems, medical monitors, parking meters, lottery terminals, smartphones, etc.
  • It is also an important language for networks, especially data centers that store and send web-based data.
  • Java is also used to create dynamic miniature programs that run with or embed web pages.

MCQs on Java Programming

Practise Java MCQ questions to prepare better for your GATE exam. We also advise all the aspirants to practise question papers from previous years to understand the Java MCQ questions and their patterns. Meanwhile, check out a list of questions that will help you practise it:

Java MCQ Questions

1) What is Literal in Java?
A) Literal is the value that is given or assigned to a variable.
B) Literal is a data type
C) Literal is similar to String
D) None of the above
Answer [=]
A
Explanation:

Examples: 123, 45.67f, 'C', "abc", false

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

a. JDK

b. JVM

c. JRE

d. JIT

Answer: (a) JDK

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

a. -3.4e+050

b. +1.7e+308

c. -3.4e+038

d. -1.7e+308

Answer: (b) -3.4e+038

3. What is BigDecimal.ONE?

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

d. long

Answer: (b) double

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

A. &&

B. ==

C. ?:

D. +=

a) C & B

b) A & D

c) A, B & D

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?

class Output

{

public static void main(String args[])

{

int p = 1;

int q = 2;

int r = 3;

p |= 4;

q >>= 1;

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?

class P

{

}

enum Enums extends P

{

PQR, QRS, RST, STU;

}

a) Compilation Error

b) Runtime Error

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 of parameters, and yet different signatures

Answer: (d) When more than a single method have the same name, the same number and types of parameters, and yet different signatures

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

c. Aggregation

d. Composition

Answer: (d) Composition

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

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()

d. equal()

Answer: (c) equals()

13. What would happen to the thread whenever the garbage collection kicks off?

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

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

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?

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()

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

a. java.util

b. java.lang

c. java.system

d. java.io

Answer: (b) java.lang

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

a. both getChars() & toCharArray()

b. both charAt() & getChars()

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 defined in a double wrapper?

a. SystemException

b. ClassCastException

c. IOException

d. CastException

Answer: (b) ClassCastException

20. Where does the String Pool get stored?

a. Metaspace

b. Java Stack

c. Java Heap

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()

b. getString()

c. parse()

d. parseString()

Answer: (c) parse()

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

a. ObjectNotFoundException

b. IllegalStateException

c. IOException

d. SystemException

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

c. Itdepends on the OS

d. There will be no execution of threads

Answer: (c) Itdepends on the OS

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

a. StringReader

b. BufferedReader

c. InputStreamReader

d. BufferedStreamReader

Answer: (b) BufferedReader

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

c. SimpleDateFormat

d. DefinedDateFormat

Answer: (c) SimpleDateFormat

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

a. transient()

b. drawString()

c. print()

d. display()

Answer: (b) drawString()

30. The public int start() returns what?

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

Answer: (a) ComponentEvent

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

a. ComponentEvent

b. Applet

c. InputEvent

d. Event

Answer: (b) Applet

33. Which method in Java generates boolean random values?

a. randomBoolean()

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 object?

a. notify()

b. update()

c. observed()

d. check()

Answer: (b) update()

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

a. java.awt

b. java.rmi

c. java.applet

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

38. Which mechanism helps in the process of naming as well as visibility control of the classes and their content?

a. Packages

b. Interfaces

c. Object

d. None of the above

Answer: (a) Packages

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

a. .sql

b. .ora

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?

a. bound

b. stop

c. implements

d. extends

Answer: (d) extends

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

a. It extends the java.io.Serializable class

b. It implements the java.io.Serializable interface

c. It provides getter and setter methods for its properties

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

c. Encourages SQL injection

d. Slow performance

Answer: (b) Prevents SQL injection

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

a. logger.error

b. logger.log

c. System.out.println

d. breakpoints

Answer: (d) breakpoints

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

Answer: (b) 4095 bytes

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

a) jsp:plugin

b) jsp:include

c) jsp:getProperty

d) jsp:setProperty

Answer: (b) jsp:include

49. ______ file defines dependency in maven.

a) dependency.xml

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

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

a. JDK

b. JVM

c. JRE

d. JIT

Answer: (a) JDK


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

class Output

{

public static void main(String args[])

{

int p = 1;

int q = 2;

int r = 3;

p |= 4;

q >>= 1;

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


53. 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 of parameters, and yet different signatures

Answer: (d) When more than a single method have the same name, the same number and types of parameters, and yet different signatures


54. 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?

a. superclass.();

b. super(void);

c. super();

d. super.A();

Answer: (c) super();


55. Out of these packages, which one contains an abstract keyword?

a. java.util

b. java.lang

c. java.system

d. java.io

Answer: (b) java.lang

18) Which of the following is true about the anonymous inner class?

  1. It has only methods
  2. Objects can't be created
  3. It has a fixed class name
  4. It has no class name

 

Answer: (d) It has no class name

Explanation: Anonymous inner classes are the same as the local classes except that they don't have any name. The main use of it is to override methods of classes or interfaces. And the rest three options are false about the anonymous inner classes as it can have both methods and objects. It does not have any fixed came name.

Hence, the correct answer is option(d).


21) An interface with no fields or methods is known as a ______.

  1. Runnable Interface
  2. Marker Interface
  3. Abstract Interface
  4. CharSequence Interface

 

Answer: (b) Marker Interface

Explanation: An interface with no methods and fields is known as the marker interface. In other words, an empty interface (containing no fields and methods) is called a marker interface. In Java, the most commonly used marker interfaces are Serializable, Cloneable, Remote, and ThreadSafe interfaces. Marker interfaces are also known as the Tag interface. It is used to tell the JVM or compiler that the particular class has special behavior.

Following is the code snippet of a maker interface:

public interface Cloneable 
{
    // empty 
}

Hence, the correct answer is option (b).


30) Which keyword is used for accessing the features of a package?

  1. package
  2. import
  3. extends
  4. export

 

Answer: (b) import

Explanation: The import keyword is used to access the classes and interfaces of a particular package to the current file. In other words, it is used to import the user-defined and built-in classes and interfaces into the source file of java so that the current file can easily access the other packages by directly using its name. For example,

   import java.awt.*; 
   import java.lang.Object;

The first import statement imports all the classes and interfaces of java.awt package. Whereas, the second import statement only imports the Object class of the java.lang package.

The package keyword is used to create a new package. The extends keyword indicates that the new class is derived from the base or parent class using inheritance, and export is not a keyword in Java.

Hence, the correct answer is option (b).


41) What is meant by the classes and objects that dependents on each other?

  1. Tight Coupling
  2. Cohesion
  3. Loose Coupling
  4. None of the above

 

Answer: (a) Tight Coupling

Explanation: In tight coupling, a group of classes and objects are highly dependent on each other. Tight coupling is also used in some cases, like when an object creates some other objects that are going to be used by them.

Tight coupling is the correct answer as it is used when the logic of one class is called by the logic of another class.

Hence, the correct option is (a).


1) Which of the following option leads to the portability and security of Java?

  1. Bytecode is executed by JVM
  2. The applet makes the Java code secure and portable
  3. Use of exception handling
  4. Dynamic binding between objects

 

Answer: (a) Bytecode is executed by the JVM.

Explanation: The output of the Java compiler is bytecode, which leads to the security and portability of the Java code. It is a highly developed set of instructions that are designed to be executed by the Java runtime system known as Java Virtual Machine (JVM). The Java programs executed by the JVM that makes the code portable and secure. Because JVM prevents the code from generating its side effects. The Java code is portable, as the same byte code can run on any platform.

Hence, the correct answer is option (a).


Command to execute a compiled java program is :

A javac

B java

C run

D execute

..

No comments:

Post a Comment

techguruspeaks

  Core Java programming Introduction to Java Java Features Writing a Simple Java Program Java Keywords and Comments Variables, Identifiers &...