21. What is the correct way to declare a package in Java?
- a) package my.package;
- b) Package my.package;
- c) #package my.package
- d) import my.package;
Answer: A - The package declaration uses lowercase 'package' keyword followed by the package name.
22. Which method is called when an object is garbage collected?
- a) finalize()
- b) destroy()
- c) cleanup()
- d) delete()
Answer: A - The finalize() method is called by the garbage collector before an object is reclaimed.
23. What is autoboxing in Java?
- a) Automatic conversion between primitive types and their wrapper classes
- b) Automatic packaging of classes into JAR files
- c) Automatic memory allocation for objects
- d) Automatic exception handling
Answer: A - Autoboxing is the automatic conversion between primitive types and their corresponding wrapper classes.
24. Which of these is not a valid Java variable name?
- a) _variable1
- b) $variable
- c) 1variable
- d) variableOne
Answer: C - Variable names cannot start with a digit in Java.
25. What is the purpose of the 'break' statement in a switch block?
- a) To exit the program
- b) To skip the current iteration
- c) To terminate the switch statement
- d) To continue to the next case
Answer: C - The break statement terminates the switch block, preventing fall-through to subsequent cases.
26. Which collection maintains insertion order and allows duplicates?
- a) HashSet
- b) TreeSet
- c) ArrayList
- d) HashMap
Answer: C - ArrayList maintains insertion order and allows duplicate elements.
27. What is the default capacity of an ArrayList in Java?
Answer: C - The default initial capacity of an ArrayList is 10.
28. Which keyword is used to prevent method overriding?
- a) static
- b) final
- c) private
- d) sealed
Answer: B - The final keyword prevents a method from being overridden in subclasses.
29. What is the output of: System.out.println(10 + 20 + "30");
- a) 3030
- b) 60
- c) 102030
- d) 3030
Answer: A - First 10+20 is calculated (30), then concatenated with "30" to produce "3030".
30. Which of these is not a primitive data type in Java?
- a) int
- b) float
- c) String
- d) boolean
Answer: C - String is a class, not a primitive data type.
31. What is the purpose of the 'super' keyword?
- a) To call superclass methods or constructors
- b) To create a superclass object
- c) To refer to the current class
- d) To make a class inheritable
Answer: A - The super keyword is used to access superclass methods, constructors, and variables.
32. Which exception is thrown when dividing an integer by zero?
- a) NullPointerException
- b) ArithmeticException
- c) DivideByZeroException
- d) MathException
Answer: B - Dividing an integer by zero throws an ArithmeticException.
33. What is the default value of a char variable?
- a) ''
- b) '\u0000'
- c) null
- d) ' '
Answer: B - The default value of char is '\u0000' (null character).
34. Which of these is not a valid Java loop construct?
- a) for
- b) while
- c) do-while
- d) until
Answer: D - Java doesn't have an 'until' loop construct.
35. What is the purpose of the 'volatile' keyword?
- a) To make a variable constant
- b) To indicate a variable may be modified by multiple threads
- c) To prevent garbage collection
- d) To optimize variable access
Answer: B - volatile ensures visibility of changes to variables across threads.
36. Which method is used to compare two strings for equality?
- a) equals()
- b) compare()
- c) == operator
- d) equalTo()
Answer: A - The equals() method compares the content of strings for equality.
37. What is the parent interface of all collection interfaces?
- a) Collection
- b) Iterable
- c) List
- d) Object
Answer: B - Iterable is the root interface for all collection classes.
38. Which of these is not a valid method of Object class?
- a) toString()
- b) equals()
- c) hashCode()
- d) length()
Answer: D - length() is not a method of Object class (it's from String and arrays).
39. What is the purpose of the 'strictfp' keyword?
- a) To enforce strict floating-point calculations
- b) To make methods execute faster
- c) To prevent method overriding
- d) To optimize memory usage
Answer: A - strictfp ensures consistent floating-point calculations across platforms.
40. Which annotation indicates a method overrides a superclass method?
- a) @Override
- b) @Overrides
- c) @Super
- d) @Inherit
Answer: A - The @Override annotation indicates a method is intended to override a superclass method.