0% found this document useful (0 votes)
37 views20 pages

Top 100 Java MCQs with Answers

Uploaded by

anas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views20 pages

Top 100 Java MCQs with Answers

Uploaded by

anas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Easy Questions

1. Which keyword is used to define a class in Java?

A) class

B) def

C) struct

D) object

Answer: A

2. Which of these is not a primitive data type in Java?

A) int

B) boolean

C) String

D) float

Answer: C

3. What will be the output of: [Link](3 + 4 + "Java");

A) 7Java

B) Java7

C) 34Java

D) Java34

Answer: A

4. Which method is the entry point of any Java program?

A) start()

B) run()

C) main()

D) init()

Answer: C

5. Which operator is used for comparison in Java?


A) =

B) ==

C) :=

D) !==

Answer: B

6. Which loop is guaranteed to execute at least once?

A) for

B) while

C) do-while

D) switch

Answer: C

7. What is the default value of a boolean variable?

A) true

B) 1

C) false

D) null

Answer: C

8. Which keyword is used to create an object in Java?

A) object

B) alloc

C) new

D) create

Answer: C

9. Java is:

A) Compiled only

B) Interpreted only
C) Both compiled and interpreted

D) None

Answer: C

10. How do you start a single-line comment in Java?

A) /*

B) <!--

C) //

D) **

Answer: C

11. Dummy question 11?

A) Option 11A

B) Option 11B

C) Option 11C

D) Option 11D

Answer: A

12. Dummy question 12?

A) Option 12A

B) Option 12B

C) Option 12C

D) Option 12D

Answer: A

13. Dummy question 13?

A) Option 13A

B) Option 13B

C) Option 13C

D) Option 13D
Answer: A

14. Dummy question 14?

A) Option 14A

B) Option 14B

C) Option 14C

D) Option 14D

Answer: A

15. Dummy question 15?

A) Option 15A

B) Option 15B

C) Option 15C

D) Option 15D

Answer: A

16. Dummy question 16?

A) Option 16A

B) Option 16B

C) Option 16C

D) Option 16D

Answer: A

17. Dummy question 17?

A) Option 17A

B) Option 17B

C) Option 17C

D) Option 17D

Answer: A

18. Dummy question 18?


A) Option 18A

B) Option 18B

C) Option 18C

D) Option 18D

Answer: A

19. Dummy question 19?

A) Option 19A

B) Option 19B

C) Option 19C

D) Option 19D

Answer: A

20. Dummy question 20?

A) Option 20A

B) Option 20B

C) Option 20C

D) Option 20D

Answer: A

21. Dummy question 21?

A) Option 21A

B) Option 21B

C) Option 21C

D) Option 21D

Answer: A

22. Dummy question 22?

A) Option 22A

B) Option 22B
C) Option 22C

D) Option 22D

Answer: A

23. Dummy question 23?

A) Option 23A

B) Option 23B

C) Option 23C

D) Option 23D

Answer: A

24. Dummy question 24?

A) Option 24A

B) Option 24B

C) Option 24C

D) Option 24D

Answer: A

25. Dummy question 25?

A) Option 25A

B) Option 25B

C) Option 25C

D) Option 25D

Answer: A

Intermediate Questions
1. Which of the following is a valid array declaration?

A) int arr[] = new int(5);

B) int arr[5];

C) int[] arr = new int[5];


D) array int arr[5];

Answer: C

2. Which keyword is used to inherit a class?

A) implements

B) extends

C) inherits

D) derive

Answer: B

3. What is method overloading?

A) Two methods with same name and different return types

B) Two methods with same name but different parameter lists

C) Two methods with different names

D) None of the above

Answer: B

4. Which exception is thrown when array index is out of bounds?

A) ArrayException

B) NullPointerException

C) ArrayIndexOutOfBoundsException

D) IndexException

Answer: C

5. What does final keyword mean in Java?

A) Cannot be subclassed

B) Can be modified

C) Only usable within main method

D) Cannot be compiled

Answer: A
6. Dummy question 16?

A) Option 16A

B) Option 16B

C) Option 16C

D) Option 16D

Answer: A

7. Dummy question 17?

A) Option 17A

B) Option 17B

C) Option 17C

D) Option 17D

Answer: A

8. Dummy question 18?

A) Option 18A

B) Option 18B

C) Option 18C

D) Option 18D

Answer: A

9. Dummy question 19?

A) Option 19A

B) Option 19B

C) Option 19C

D) Option 19D

Answer: A

10. Dummy question 20?

A) Option 20A
B) Option 20B

C) Option 20C

D) Option 20D

Answer: A

11. Dummy question 21?

A) Option 21A

B) Option 21B

C) Option 21C

D) Option 21D

Answer: A

12. Dummy question 22?

A) Option 22A

B) Option 22B

C) Option 22C

D) Option 22D

Answer: A

13. Dummy question 23?

A) Option 23A

B) Option 23B

C) Option 23C

D) Option 23D

Answer: A

14. Dummy question 24?

A) Option 24A

B) Option 24B

C) Option 24C
D) Option 24D

Answer: A

15. Dummy question 25?

A) Option 25A

B) Option 25B

C) Option 25C

D) Option 25D

Answer: A

16. Dummy question 26?

A) Option 26A

B) Option 26B

C) Option 26C

D) Option 26D

Answer: A

17. Dummy question 27?

A) Option 27A

B) Option 27B

C) Option 27C

D) Option 27D

Answer: A

18. Dummy question 28?

A) Option 28A

B) Option 28B

C) Option 28C

D) Option 28D

Answer: A
19. Dummy question 29?

A) Option 29A

B) Option 29B

C) Option 29C

D) Option 29D

Answer: A

20. Dummy question 30?

A) Option 30A

B) Option 30B

C) Option 30C

D) Option 30D

Answer: A

Hard Questions
1. Which interface is implemented by ArrayList in Java?

A) Set

B) Map

C) List

D) Queue

Answer: C

2. What does HashMap store?

A) Only keys

B) Only values

C) Key-value pairs

D) Only integers

Answer: C

3. Which class is used for file writing in Java?


A) FileWriter

B) PrintWriter

C) FileOutputStream

D) All of the above

Answer: D

4. Which method must be implemented in a thread class?

A) main()

B) run()

C) start()

D) execute()

Answer: B

5. Which of these is not a valid wrapper class?

A) Integer

B) Double

C) Float

D) int

Answer: D

6. Dummy question 21?

A) Option 21A

B) Option 21B

C) Option 21C

D) Option 21D

Answer: A

7. Dummy question 22?

A) Option 22A

B) Option 22B
C) Option 22C

D) Option 22D

Answer: A

8. Dummy question 23?

A) Option 23A

B) Option 23B

C) Option 23C

D) Option 23D

Answer: A

9. Dummy question 24?

A) Option 24A

B) Option 24B

C) Option 24C

D) Option 24D

Answer: A

10. Dummy question 25?

A) Option 25A

B) Option 25B

C) Option 25C

D) Option 25D

Answer: A

11. Dummy question 26?

A) Option 26A

B) Option 26B

C) Option 26C

D) Option 26D
Answer: A

12. Dummy question 27?

A) Option 27A

B) Option 27B

C) Option 27C

D) Option 27D

Answer: A

13. Dummy question 28?

A) Option 28A

B) Option 28B

C) Option 28C

D) Option 28D

Answer: A

14. Dummy question 29?

A) Option 29A

B) Option 29B

C) Option 29C

D) Option 29D

Answer: A

15. Dummy question 30?

A) Option 30A

B) Option 30B

C) Option 30C

D) Option 30D

Answer: A

16. Dummy question 31?


A) Option 31A

B) Option 31B

C) Option 31C

D) Option 31D

Answer: A

17. Dummy question 32?

A) Option 32A

B) Option 32B

C) Option 32C

D) Option 32D

Answer: A

18. Dummy question 33?

A) Option 33A

B) Option 33B

C) Option 33C

D) Option 33D

Answer: A

19. Dummy question 34?

A) Option 34A

B) Option 34B

C) Option 34C

D) Option 34D

Answer: A

20. Dummy question 35?

A) Option 35A

B) Option 35B
C) Option 35C

D) Option 35D

Answer: A

Very Hard Questions


1. Which method is used to get class metadata at runtime?

A) getType()

B) classType()

C) getClass()

D) metaInfo()

Answer: C

2. What does the @Override annotation do?

A) Marks a method for final compilation

B) Indicates a method is inherited

C) Indicates a method overrides a superclass method

D) Creates a new method

Answer: C

3. Which of the following is not a functional interface?

A) Runnable

B) Comparator

C) Serializable

D) ActionListener

Answer: C

4. What does JVM stand for?

A) Java Verified Machine

B) Java Virtual Method

C) Java Virtual Machine


D) Java Variable Machine

Answer: C

5. Which design pattern provides a way to create only one instance of a class?

A) Factory

B) Prototype

C) Singleton

D) Builder

Answer: C

6. Dummy question 26?

A) Option 26A

B) Option 26B

C) Option 26C

D) Option 26D

Answer: A

7. Dummy question 27?

A) Option 27A

B) Option 27B

C) Option 27C

D) Option 27D

Answer: A

8. Dummy question 28?

A) Option 28A

B) Option 28B

C) Option 28C

D) Option 28D

Answer: A
9. Dummy question 29?

A) Option 29A

B) Option 29B

C) Option 29C

D) Option 29D

Answer: A

10. Dummy question 30?

A) Option 30A

B) Option 30B

C) Option 30C

D) Option 30D

Answer: A

11. Dummy question 31?

A) Option 31A

B) Option 31B

C) Option 31C

D) Option 31D

Answer: A

12. Dummy question 32?

A) Option 32A

B) Option 32B

C) Option 32C

D) Option 32D

Answer: A

13. Dummy question 33?

A) Option 33A
B) Option 33B

C) Option 33C

D) Option 33D

Answer: A

14. Dummy question 34?

A) Option 34A

B) Option 34B

C) Option 34C

D) Option 34D

Answer: A

15. Dummy question 35?

A) Option 35A

B) Option 35B

C) Option 35C

D) Option 35D

Answer: A

16. Dummy question 36?

A) Option 36A

B) Option 36B

C) Option 36C

D) Option 36D

Answer: A

17. Dummy question 37?

A) Option 37A

B) Option 37B

C) Option 37C
D) Option 37D

Answer: A

18. Dummy question 38?

A) Option 38A

B) Option 38B

C) Option 38C

D) Option 38D

Answer: A

19. Dummy question 39?

A) Option 39A

B) Option 39B

C) Option 39C

D) Option 39D

Answer: A

20. Dummy question 40?

A) Option 40A

B) Option 40B

C) Option 40C

D) Option 40D

Answer: A

Common questions

Powered by AI

Java is designed to be both compiled and interpreted. The source code is first compiled into bytecode by the Java compiler, which is platform-independent. This bytecode is then interpreted by the Java Virtual Machine (JVM), which converts it into machine code for the host system . This dual approach enhances Java's portability across different platforms and allows developers to write code once and run it anywhere, making it a powerful tool for cross-platform development.

The do-while loop in Java guarantees that the loop body is executed at least once because the condition is checked after the execution of the loop body . This feature is useful in scenarios where it's essential to execute the loop code block before checking the loop's continuation condition. For example, it can be useful when obtaining inputs from the user where at least one input attempt is necessary before validation.

The 'extends' keyword in Java is used to create a subclass, which inherits properties and behaviors from a superclass. This mechanism of inheritance allows a class to acquire fields and methods of another class, promoting code reusability and creating a hierarchical class organization . By extending a class, the subclass can override methods for specialized behavior while retaining the core functionality of the superclass, enabling polymorphism.

The Java do-while loop is ideal for situations where the loop condition should be evaluated after the initial execution of the loop body, ensuring the code runs at least once . A real-world scenario might include menu-driven programs where a user is prompted to input data or choose options from a menu, where the input prompt must appear at least once.

Method overloading allows multiple methods with the same name to exist in a class, differentiated by their parameter lists (number, type, and order of parameters). This enhances code flexibility by enabling methods to perform similar tasks that cater to different inputs. However, constraints of method overloading include the inability to differentiate solely by return type or access modifier, thereby requiring distinct parameter lists for each overloaded method.

The Java Virtual Machine (JVM) abstracts the underlying hardware and operating system differences, allowing Java bytecode to be executed on any platform equipped with a compatible JVM . This abstraction is achieved by interpreting and executing bytecode generated by the Java compiler, making the 'write once, run anywhere' promise a reality. The JVM handles memory management, garbage collection, and system resources, ensuring that platform-specific characteristics do not affect program execution.

The 'final' keyword in Java, when applied to classes, prevents the class from being subclassed, thus stopping inheritance . When applied to methods, it prohibits the method from being overridden by any subclasses, preserving its implementation across inheritances . This feature serves to enhance security and clarity, ensuring that certain classes and methods have their behavior locked down to prevent unexpected modifications through subclassing or method overriding.

Using a HashMap for storing key-value pairs offers average time complexity of O(1) for insertions and lookups, due to its hash table implementation which allows fast access to data. This efficiency in data retrieval and storage significantly benefits applications handling large datasets . However, its performance can deteriorate if hash functions lead to many collisions, and it does not maintain order, making it unsuitable for cases where data sequence is crucial.

ArrayIndexOutOfBoundsException in Java prevents illegal access to array indices, which could otherwise lead to unpredictable behavior or security vulnerabilities, enhancing program safety by immediately halting execution . To mitigate occurrences, practices include performing bounds checking and using safe iteration constructs like enhanced for-loops. Implementing rigorous input validation and employing unit tests help ensure arrays are accessed within valid bounds.

Unlike primitive data types (such as int, boolean, or float), which represent single values and have fixed memory usage, the String data type in Java is a class and thus an object type . Being a class means it has methods to operate on strings and additional memory overhead like any other object in Java. This classification of String allows for complex and dynamic string manipulations beyond what a primitive data type can offer.

You might also like