













































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














































































Question 1. What is the main philosophy behind Java’s “Write Once, Run Anywhere” motto? A) Java programs can only run on Windows B) Java programs must be compiled on each platform C) Java bytecode runs on any platform with a JVM D) Java source code is platform dependent Answer: C Explanation: Java code is compiled into platform-independent bytecode, which is executed by the JVM available on any platform, enabling “Write Once, Run Anywhere”. Question 2. Which component is responsible for converting Java bytecode into machine code at runtime? A) JDK B) JRE C) JVM D) JIT Compiler Answer: C Explanation: The Java Virtual Machine (JVM) interprets and executes Java bytecode, converting it to machine code at runtime. Question 3. What is the primary function of the Java Development Kit (JDK)? A) Only runs Java programs B) Only compiles Java programs C) Provides tools to develop, compile, and run Java applications D) Only interprets bytecode Answer: C
Explanation: The JDK includes the compiler, JVM, and other tools required to develop, compile, and run Java programs. Question 4. Which of the following is NOT a primitive data type in Java? A) int B) boolean C) String D) char Answer: C Explanation: String is not a primitive type; it is a class in Java. Question 5. What is the value range of the Java int data type? A) - 127 to 128 B) - 32,768 to 32, C) - 2,147,483,648 to 2,147,483, D) 0 to 4,294,967, Answer: C Explanation: int is a 32-bit signed integer with this range. Question 6. Which operator is used for performing bitwise AND in Java? A) && B) & C) || D) |
Answer: A Explanation: The loop prints 0, then 1, then 2. Question 10. Which statement is used to immediately exit the current loop in Java? A) exit B) continue C) stop D) break Answer: D Explanation: break terminates the current loop. Question 11. What is the default value of a boolean variable in a Java class? A) true B) false C) 0 D) null Answer: B Explanation: The default value for boolean fields is false. Question 12. Which keyword is used to define a constant variable in Java?
A) static B) final C) const D) constant Answer: B Explanation: final makes a variable constant. Question 13. Which access modifier allows visibility only within the same package? A) public B) protected C) private D) default (no modifier) Answer: D Explanation: No modifier (default) restricts access to the same package. Question 14. What does the 'this' keyword refer to in Java? A) The superclass B) The current object C) The package D) The main method Answer: B Explanation: 'this' refers to the current class instance. Question 15. Which method acts as the entry point for a standalone Java application?
A) Having multiple classes with the same name B) Having multiple constructors with different parameters C) Having multiple methods with the same name D) Using the super() method Answer: B Explanation: Constructor overloading means multiple constructors with different parameter lists. Question 19. Which of the following best describes encapsulation? A) Hiding implementation details B) Using only static methods C) Inheriting from another class D) Overloading methods Answer: A Explanation: Encapsulation is about hiding internal implementation and exposing only necessary details. Question 20. Which access modifier provides the most restrictive access? A) public B) protected C) private D) package-private Answer: C Explanation: private restricts access to within the same class only. Question 21. How do you call a superclass constructor from a subclass in Java?
A) super() B) this() C) extends() D) base() Answer: A Explanation: super() invokes the parent class constructor. Question 22. What does method overriding allow in Java? A) Changing return type only B) Defining the same method in a subclass with different parameters C) Providing a new implementation for a method in a subclass D) Using the constructor of the superclass Answer: C Explanation: Overriding allows a subclass to provide its own implementation of a superclass method. Question 23. What will happen if you try to instantiate an abstract class? A) It compiles and runs B) Compilation error C) Runtime error D) The class acts as an interface Answer: B Explanation: Abstract classes cannot be instantiated directly. Question 24. Which keyword is used to define an interface in Java?
Question 27. Which of the following is NOT a valid way to declare an array in Java? A) int arr[]; B) int[] arr; C) int arr = new int[5]; D) int[] arr = {1,2,3}; Answer: C Explanation: You cannot assign an array with 'new' directly to a non-array variable. Question 28. What is the output of: int[] arr = {1, 2, 3}; System.out.println(arr[1]); A) 1 B) 2 C) 3 D) 0 Answer: B Explanation: Arrays are zero-indexed; arr[1] is 2. Question 29. What does Arrays.sort() do? A) Sorts an array in descending order B) Sorts an array in ascending order C) Reverses an array D) Finds the median Answer: B
Explanation: Arrays.sort() sorts arrays in ascending order by default. Question 30. What is the main difference between String and StringBuilder in Java? A) String is mutable, StringBuilder is immutable B) Both are mutable C) String is immutable, StringBuilder is mutable D) Both are immutable Answer: C Explanation: String is immutable; StringBuilder is mutable and more suitable for string modifications. Question 31. Which method checks if two strings have the same sequence of characters? A) equals() B) == C) compareTo() D) toString() Answer: A Explanation: equals() checks for content equality. Question 32. What does the compareTo() method return when two strings are equal? A) true B) false C) 0 D) - 1 Answer: C
Explanation: Box
Explanation: ArrayList allows fast random access due to its backing array. Question 39. Which Set implementation maintains insertion order? A) HashSet B) TreeSet C) LinkedHashSet D) EnumSet Answer: C Explanation: LinkedHashSet preserves the order in which elements are inserted. Question 40. Which Map implementation sorts keys in natural order? A) HashMap B) TreeMap C) LinkedHashMap D) WeakHashMap Answer: B Explanation: TreeMap sorts keys according to their natural ordering or a provided Comparator. Question 41. Which interface provides methods to iterate and modify a collection? A) Iterator B) ListIterator C) Enumeration D) Comparator Answer: B
Explanation: try-with-resources ensures that resources are closed automatically. Question 45. Which method is used to throw an exception in Java? A) throw() B) throws C) throw D) try Answer: C Explanation: throw is used to explicitly throw an exception. Question 46. Which hierarchy does java.io.IOException belong to? A) Error B) RuntimeException C) Checked Exception D) AssertionError Answer: C Explanation: IOException is a checked exception. Question 47. What is the role of the finally block? A) Executes only if no exception occurs B) Executes only if an exception occurs C) Executes regardless of exception occurrence D) Skips resource release Answer: C
Explanation: finally always executes after try/catch, used for resource cleanup. Question 48. Which stream is suitable for reading character data from a file? A) FileInputStream B) FileReader C) ObjectOutputStream D) DataInputStream Answer: B Explanation: FileReader reads character streams from files. Question 49. Which interface must a class implement to be serializable? A) Runnable B) Serializable C) Externalizable D) Cloneable Answer: B Explanation: Serializable is a marker interface for object serialization. Question 50. Which class is used to write formatted text to a file? A) FileInputStream B) PrintWriter C) FileReader D) BufferedInputStream Answer: B
Explanation: Runnable provides the run() method for thread implementation. Question 54. Which method is used to pause a thread? A) pause() B) stop() C) sleep() D) wait() Answer: C Explanation: sleep() pauses the current thread for a specified duration. Question 55. What does the synchronized keyword do? A) Makes a class abstract B) Disables serialization C) Ensures thread safety by locking D) Speeds up execution Answer: C Explanation: synchronized ensures only one thread accesses a block or method at a time. Question 56. Which method wakes up a single thread waiting on an object’s monitor? A) notify() B) notifyAll() C) signal() D) wakeup() Answer: A
Explanation: notify() wakes up one waiting thread. Question 57. Which package provides advanced concurrency utilities? A) java.io B) java.util C) java.util.concurrent D) java.lang Answer: C Explanation: java.util.concurrent contains advanced concurrency classes. Question 58. What is a lambda expression in Java? A) A way to define a class B) A method reference C) A concise way to represent an anonymous function D) A type of interface Answer: C Explanation: Lambda expressions provide a shorthand for writing anonymous functions. Question 59. Which interface represents a boolean-valued function of one argument? A) Function B) Predicate C) Consumer D) Supplier Answer: B