Java continues to be a favorite for many due to its "write once, run anywhere" capability. Whether you are preparing for an exam or an interview, or simply want to test your knowledge, multiple-choice questions (MCQs) can be an effective way to evaluate one's grasp on the topic. In this blog post, we will dive into some interesting Java MCQs that range from basic to slightly advanced, aiding both beginners and seasoned programmers alike. So, let's dive right in!
1. What does JVM stand for?
a) Java Virtual Memory
b) Java Virtual Machine
c) Java Viable Method
d) Just a Virtual Machine
Answer:
b) Java Virtual Machine
Explanation:
JVM stands for Java Virtual Machine. It's responsible for executing Java bytecode.
2. Which of the following is responsible for converting bytecode into machine code?
a) JDK
b) JRE
c) JVM
d) Java Compiler
Answer:
c) JVM
Explanation:
The Java Virtual Machine (JVM) is responsible for interpreting or just-in-time compiling Java bytecode into native machine code to be executed on the host hardware.
3. What does the JDK include?
a) Only the Java compiler
b) Java compiler and JVM
c) JRE and development tools
d) Only JVM
Answer:
c) JRE and development tools
Explanation:
The Java Development Kit (JDK) includes the Java Runtime Environment (JRE) and necessary development tools like the Java compiler (javac), debugger, and others.
4. Which component is essential for running Java applications on a user's computer?
a) JVM
b) Java Compiler
c) JDK
d) JRE
Answer:
d) JRE
Explanation:
The Java Runtime Environment (JRE) provides the necessary libraries, JVM, and other components to run Java applications. While the JVM is a part of the JRE, on its own, it doesn't have the libraries needed to execute most applications.
5. Which of these operators is used to allocate memory for an object?
a) malloc
b) alloc
c) new
d) sizeof
Answer:
c) new
Explanation:
In Java, new is used to allocate memory for an object.
6. Which of the following data types is not primitive in Java?
a) char
b) int
c) String
d) boolean
Answer:
c) String
Explanation:
The String is a class in Java, not a primitive data type.
7. Which keyword is used to create an instance of a class?
a) new
b) this
c) super
d) instance
Answer:
a) new
Explanation:
In Java, new keyword is used to create a new instance of a class.
8. What is the default value of a boolean variable in a Java class?
a) null
b) false
c) true
d) None of the above
Answer:
b) false
Explanation:
A boolean's default value is false when it's a member of a class.
9. Which of the following is used to make a decision in Java?
a) if-else
b) switch
c) both a and b
d) none of the above
Answer:
c) both a and b
Explanation:
Both if-else and switch are control statements used to make decisions based on conditions.
10. Which keyword is used to define a constant variable in Java?
a) constant
b) const
c) static
d) final
Answer:
d) final
Explanation:
In Java, the final keyword is used to declare a variable as constant, meaning its value cannot be modified after the initial assignment.
11. Which statement is used to skip the current iteration and continue with the next iteration?
a) continue
b) exit
c) break
d) skip
Answer:
a) continue
Explanation:
The continue statement skips the current iteration and proceeds to the next.
12. What is the base class for all Java classes?
a) Object
b) Base
c) Java
d) None
Answer:
a) Object
Explanation:
The Object class is the base class for all Java classes.
13. Which keyword is used in conjunction with a switch to check for a specific value?
a) if
b) check
c) decide
d) case
Answer:
d) case
Explanation:
In the switch statement, the case keyword is used to specify a block of code to be executed if the switch expression matches a value.
14. Which of the following statements is used to exit a loop prematurely?
a) continue
b) exit
c) break
d) return
Answer:
c) break
Explanation:
The break statement is used to exit a loop prematurely.
15. Which of the following data types can hold a 32-bit integer value?
a) short
b) long
c) int
d) byte
Answer:
c) int
Explanation:
The int in Java is 32 bits long and can hold a 32-bit integer value.
16. Which of these keywords is used to manually throw an exception?
a) try
b) catch
c) finally
d) throw
Answer:
d) throw
Explanation:
The throw keyword is used to manually throw an exception.
17. Which of the following is a mutable class in Java?
a) String
b) StringBuffer
c) Str
d) None of the above
Answer:
b) StringBuffer
Explanation:
StringBuffer is mutable, which means it can be changed after it's created, while String is immutable.
18. Which keyword is used to inherit a class?
a) extends
b) inherit
c) using
d) Inherits
Answer:
a) extends
Explanation:
The extends keyword is used for inheritance in Java.
19. Which of the following can store a null value?
a) int
b) String
c) char
d) byte
Answer:
b) String
Explanation:
String is an object data type and can hold a null value. The others listed are primitive types and cannot store null.
20. Which interface is the root of the Java collections hierarchy?
a) Collection
b) List
c) Set
d) Map
Answer:
a) Collection
Explanation:
The Collection interface is the root of the collection hierarchy. List and Set are sub-interfaces, while Map is not a part of the Collection hierarchy.
21. Which of the following is not a direct implementation of the Map interface?
a) HashMap
b) TreeMap
c) LinkedHashMap
d) HashSet
Answer:
d) HashSet
Explanation:
HashSet is an implementation of the Set interface, not the Map interface.
22. Which class provides a thread-safe implementation of the List interface?
a) ArrayList
b) Vector
c) LinkedList
d) HashSet
Answer:
b) Vector
Explanation:
The Vector is synchronized, which means it is thread-safe. The other options are not inherently thread-safe.
23. Which interface represents a collection that does not allow duplicate elements?
a) List
b) Set
c) Map
d) All of the above
Answer:
b) Set
Explanation:
The Set interface ensures that no duplicate elements are stored. List can have duplicates, and while Map ensures unique keys, it's not a collection of single elements like Set.
24. Which of the following collections stores elements in insertion order?
a) HashSet
b) TreeMap
c) HashMap
d) LinkedHashMap
Answer:
d) LinkedHashMap
Explanation:
LinkedHashMap stores entries in the order they were inserted. The other options do not guarantee order based on insertion.
25. Which interface should be used to represent a collection that associates keys with values?
a) Collection
b) List
c) Set
d) Map
Answer:
d) Map
Explanation:
The Map interface represents an object that maps keys to values. It cannot contain duplicate keys, and each key can map to at most one value.
26. Which method must be implemented when a class implements the Runnable interface?
a) start()
b) stop()
c) run()
d) execute()
Answer:
c) run()
Explanation:
When a class implements the Runnable interface, it must override the run() method, which contains the code to be executed in the thread.
27. What is the purpose of the "super" keyword in Java?
a) To refer to the current object
b) To invoke the superclass constructor or methods
c) To create multiple instances of a class
d) To hide data and methods within a class
Answer:
b) To invoke the superclass constructor or methods
Explanation:
The "super" keyword in Java is used to refer to the superclass (or parent class) of the current object. It is commonly used to invoke the superclass constructor or methods within the subclass. The "super" keyword allows for code reuse and accessing superclass members that may be overridden in the subclass.
28. Which of these is a wrapper class in Java?
a) Boolean
b) float
c) double
d) Char
Answer:
a) Boolean
Explanation:
In Java, wrapper classes use the initial capital letter. So, "Boolean" is correct.
29. Which method can be used to get the length of a string?
a) length
b) size
c) length()
d) getLength()
Answer:
c) length()
Explanation:
The length() method returns the length of a string in Java
30. Which of the following is not a Java loop?
a) for
b) while
c) repeat
d) do-while
Answer:
c) repeat
Explanation:
Java doesn't have a repeat loop. The other options are valid loop constructs in Java.
31. Which keyword is used to refer to the current object in a method or constructor?
a) me
b) self
c) this
d) object
Answer:
c) this
Explanation:
The this keyword refers to the current object in a method or constructor.
32. Which of these is a checked exception in Java?
a) NullPointerException
b) ArithmeticException
c) ArrayIndexOutOfBoundsException
d) IOException
Answer:
d) IOException
Explanation:
IOException is a checked exception, whereas the others listed are runtime exceptions.
33. Which operator is used in Java to check if two reference variables refer to the same object in memory?
a) ==
b) !=
c) =
d) ===
Answer:
a) ==
Explanation:
In Java, the == operator checks for reference equality, i.e., whether two reference variables refer to the same object in memory.
34. What is the default value of an instance variable of type int?
a) null
b) 0
c) -1
d) Not initialized
Answer:
b) 0
Explanation:
In Java, the default value of an instance variable of type int is 0.
35. Which of the following is not a valid way to create a thread in Java?
a) Extending Thread class
b) Implementing Runnable interface
c) Using Thread(Runnable target) constructor
d) Extending Runnable class
Answer:
d) Extending Runnable class
Explanation:
Runnable is an interface, not a class. You can't extend an interface.
36. Which keyword is used to synchronize a block of statements in Java?
a) sync
b) synchronize
c) synchronized
d) locking
Answer:
c) synchronized
Explanation:
The synchronized keyword is used to synchronize a block of statements in Java.
37. Which of the following concepts allows Java objects to be manipulated directly as if they were primitive data types?
a) Polymorphism
b) Inheritance
c) Boxing
d) Abstraction
Answer:
c) Boxing
Explanation:
Boxing allows Java objects to be viewed as if they were primitive data types.
38. Which of these is a binary operator?
a) !
b) ~
c) &&
d) ++
Answer:
c) &&
Explanation:
The && operator is a binary operator as it requires two operands. The other options are unary operators.
39. Which of the following is true about abstract classes in Java?
a) They cannot have constructors.
b) They cannot be instantiated.
c) They require all methods to be abstract.
d) They can't have instance variables.
Answer:
b) They cannot be instantiated.
Explanation:
Abstract classes cannot be instantiated directly. They can have constructors, instance variables, and both abstract and non-abstract methods.
40. Which method is called when an object is garbage collected in Java?
a) finalize()
b) delete()
c) destroy()
d) collect()
Answer:
a) finalize()
Explanation:
The finalize() method is called by the garbage collector on an object when it's being collected.
41. Which of the following data types can hold a null value?
a) int
b) String
c) char
d) boolean
Answer:
b) String
Explanation:
String is a reference type and can hold a null value. The other options are primitive types and cannot hold null values.
42. Which of the following is not an OOP principle?
a) Abstraction
b) Encapsulation
c) Polymorphism
d) Compilation
Answer:
d) Compilation
Explanation:
Compilation is not a principle of Object-Oriented Programming (OOP).
43. Which of the following is the base class for all exception classes in Java?
a) Exception
b) BaseException
c) RootException
d) Throwable
Answer:
d) Throwable
Explanation:
The Throwable class is the superclass of all errors and exceptions in Java.
44. Which package contains the Random class in Java?
a) java.util
b) java.lang
c) java.math
d) java.random
Answer:
a) java.util
Explanation:
The Random class is part of the java.util package.
45. Which of the following is not a component of Java's exception handling?
a) try
b) throw
c) catch
d) perform
Answer:
d) perform
Explanation:
perform is not related to exception handling in Java.
46. Which of the following is a valid way to add an element to the beginning of an ArrayList named list?
a) list.addFirst(element);
b) list.prepend(element);
c) list.addToStart(element);
d) list.add(0, element);
Answer:
d) list.add(0, element);
Explanation:
To add an element to the beginning of an ArrayList, you use the add(int index, E element) method with index 0.
47. Which of these methods belongs to the Object class in Java?
a) size()
b) length()
c) toString()
d) get()
Answer:
c) toString()
Explanation:
The toString() method belongs to the Object class and can be overridden by subclasses.
48. Which of these collections does not allow duplicate elements?
a) ArrayList
b) HashSet
c) LinkedList
d) Vector
Answer:
b) HashSet
Explanation:
HashSet is a set and does not allow duplicate elements.
49. What does API stand for?
a) Application Programming Interface
b) Advanced Programming Input
c) Application Protocol Input
d) Advanced Protocol Interface
Answer:
a) Application Programming Interface
Explanation:
API stands for Application Programming Interface. It provides predefined classes and methods for developers to use.
50. In Java, which keyword that does not allow you to reassign a value to a variable once it has been assigned?
a) const
b) constant
c) final
d) static
Answer:
c) final
Explanation:
Once a variable is declared as final, its value cannot be changed or reassigned.
Comments
Post a Comment