⭐ IMPORTANT QUESTIONS – JAVA (2 MARKS) ⭐
Unit – 1
1. Define: “Static Data”.
Ans: Static data is shared among all objects of a class. It belongs to the class, not objects.
Memory is allocated only once for static variables.
Useful for common properties like counters.
Eg : class Test {
static int x = 10;
}
2. What is general form of import statement?
Ans: import package_name.class_name;
import package_name.*;
Used to access classes from other packages.
Avoids writing fully qualified names.
3. Write a note on Java buzzwords.
Ans: Key features: Simple, Object-Oriented, Platform Independent, Secure, Robust, Multithreaded.
Also includes Distributed and High Performance.
These features make Java widely used.
4. Write the general form of a method declaration in java.
Ans: returnType methodName(parameters) {
// body
Defines behavior of an object.
Can return value or be void.
5. Write a note on static method.
Ans: A method that belongs to the class and can be called without object.
Can access only static data directly.
Used for utility or common operations.
class Test {
static void show() {
[Link]("Hello");
6. List out the different types of operators in Java.
Ans: Arithmetic, Relational, Logical, Assignment, Bitwise, Unary, Ternary.
Used to perform operations on variables and values.
Each type has specific purpose.
7. What is Object-Oriented Programming?
Ans: A programming concept based on objects and classes.
Supports reuse using inheritance.
Improves modularity and maintainability.
8. What is the purpose of the main method in a Java program?
Ans: Entry point of Java program. Execution starts here.
JVM calls this method automatically.
Must be public, static, and void.
public static void main(String[] args) { }
9. Identify the primary difference between String and String Buffer classes in Java.
Ans: String: Immutable
StringBuffer: Mutable
String is slower for modification.
StringBuffer is faster for frequent changes.
String s = "Hi";
StringBuffer sb = new StringBuffer("Hi");
10. Why java is known as platform independent?
Ans: Because it uses bytecode that runs on JVM on any system.
“Write once, run anywhere” concept.
No need to recompile for different OS.
11. Write down the rules for naming classes
Ans: Start with uppercase letter.
Should not use keywords or spaces.
Use meaningful names.
Example: MyClass
Unit – 2
12. Define: “Array”.
Ans: Collection of same type elements stored in contiguous memory.
Size is fixed once declared.
Accessed using index values.
int a[] = {1,2,3};
13. Mention the advantages of constructor.
Ans: Initializes objects automatically.
Reduces need for separate initialization method.
Improves code readability.
14. Show the declaration of Abstract classes.
Ans: abstract class Test {
abstract void show();
Cannot be instantiated.
Used to achieve abstraction.
15. State the use of dynamic method dispatch.
Ans: Method call resolved at runtime.
Supports runtime polymorphism.
Based on object type, not reference type.
A obj = new B();
[Link]();
16. What do you mean by inheritance?
Ans: Acquiring properties of one class into another.
Promotes code reuse.
Uses keyword extends.
class B extends A { }
17. State the rules for naming classes in Java.
Ans: Same as Unit-1 rules.
Use CamelCase naming.
Example: StudentData
18. What is method overriding?
Ans: Redefining parent method in child class.
Achieves runtime polymorphism.
Method signature must be same.
class A { void show(){} }
class B extends A { void show(){} }
19. Mention the general form of package statement.
Ans: package package_name;
Used to group related classes.
Helps avoid name conflicts.
20. Define a user-defined class in Java.
Ans: Class created by programmer.
Contains variables and methods.
Used to define objects.
class Student { }
21. Identify the different types of inheritance in Java.
Ans: Single, Multilevel, Hierarchical.
Multiple inheritance via interfaces only.
Improves code structure.
Unit – 3
22. Give the example for Runnable interface.
Ans: class MyThread implements Runnable {
public void run() {
[Link]("Thread running");
Used to create threads.
Preferred over extending Thread class.
23. What do you mean by synchronization?
Ans: Controls access of multiple threads to shared resources.
Prevents data inconsistency.
Uses synchronized keyword.
24. What are the usages of final keyword?
Ans: Variable: constant
Method: cannot override
Class: cannot extend
Improves security and stability.
25. Show the purpose of Abstract Window Tool Kit.
Ans: Used to create GUI components like buttons, windows.
Platform dependent.
Part of Java GUI library.
26. Comment on garbage collection.
Ans: Automatic memory management to delete unused objects.
Handled by JVM.
Reduces memory leaks.
27. What is the purpose of the finally block in exception handling?
Ans: Executes always after try-catch.
Used for cleanup operations.
Runs even if exception occurs.
28. Identify the difference between throw and throws in Java.
Ans: throw: used to throw exception
throws: declares exception
throw used inside method.
throws used in method signature.
29. How to stop a thread?
Ans: Using flag or interrupt method.
Avoid using stop() (deprecated).
Ensures safe thread termination.
[Link]();
30. What is the use of catch block
Ans: Handles exceptions.
Prevents program crash.
Can have multiple catch blocks.
Unit – 4
31. Define: “Swing”.
Ans: Java GUI toolkit for building applications.
Platform independent.
Provides rich components.
32. What is the use of J Label control?
Ans: Displays text or images.
Used in GUI forms.
Does not allow user input.
33. What is JscrollPane control?
Ans: Adds scrollbars to components.
Used for large content display.
Improves usability.
34. What is the purpose of the AWT class hierarchy in Java?
Ans: Defines structure of GUI classes.
Shows parent-child relationships.
Helps in understanding components.
35. Define a top-level container in Swing.
Ans: Main window container like JFrame.
Holds other components.
Required for GUI apps.
36. What is the function of the JLabel component in Swing?
Ans: Displays text or icon.
Non-editable component.
Used for labels in forms.
37. What is AWT?
Ans: Abstract Window Toolkit for GUI.
Platform dependent.
Contains basic components.
Unit – 5
38. Comment on Enumeration.
Ans: Used to iterate collection elements.
Works with legacy classes like Vector.
Has methods like hasMoreElements().
39. Define the term “Adapter Classes”.
Ans: Provide default implementation of interfaces.
Used in event handling.
Reduces coding effort.
40. What is an adapter class in Java?
Ans: Predefined classes like WindowAdapter.
Override only required methods.
Used with listeners.
41. What is the role of the Comparator interface?
Ans: Used to sort objects.
Defines custom sorting logic.
Implements compare() method.
42. Name container classes
Ans: Store multiple objects.
Examples: ArrayList, Vector, HashSet.
Provide dynamic storage