1.
Programs on Basic Concepts (Variables, Data Types, Operators, Control Statements)**
Q1:What are primitive data types in Java?
A:Primitive data types include byte, short, int, long, float, double, char, and boolean. They store
simple values directly in memory.
Q2: How is a ‘for’ loop different from a ‘while’ loop?
A: ‘for’ loops are used when the number of iterations is known. Initialization, condition, and
increment are part of the loop header. ‘while’ loops are used when iterations are based on a
condition checked before each iteration.
Q3: Describe the use of break and continue statements in loops.
A: `break` exits the loop completely. `continue` skips to the next iteration without executing the
remaining statements in the current loop.
Q4: What is the purpose of relational operators?
A:Relational operators (==, !=, >, <, >=, <=) compare two values and return a boolean result.
Q5: How do you control multiple conditions in Java?
A:Use logical operators (`&&`, `||`) and nested or cascading if-else statements to manage
multiple conditions.
2. Programs on Operations with Strings**
Q1: How do you compare two strings in Java?
A: Use the `equals()` method for value comparison. `==` compares reference (memory location).
Q2: What is the difference between String and StringBuilder?
A: A String is immutable, whereas StringBuilder is mutable and allows modification without
creating new objects.
Q3: How do you extract a substring from a string?
A: Use the `substring(int beginIndex, int endIndex)` method of the String class.
Q4: Which method converts a string to all uppercase letters?
A: The `toUpperCase()` method.
Q5: How do you check for a specific character or sequence in a string?
A: Use the `contains()` or `indexOf()` methods.
3. Programs using Classes and Objects (Constructors, Overloading, this keyword)**
Q1: What is a constructor?
A: A constructor is a special method that initializes objects when they are created.
Q2: How do you overload constructors?
A:By defining multiple constructors with different parameter lists in a class.
Q3: What is the purpose of the ‘this’ keyword?
A: ‘this’ refers to the current object and is used to access instance variables and methods.
Q4: Difference between static and instance variables?
A: Static variables are shared among all instances. Instance variables belong to each object.
Q5: Why encapsulate class members?
A: To restrict direct access and protect data, ensuring integrity.
4. Programs on Inheritance
Q1: What is inheritance?
A: Inheritance enables one class to acquire properties and methods of another (extends
keyword).
Q2: How is method overriding achieved?
A: By defining a method with the same signature in the subclass as in the parent class.
Q3: What does the ‘super’ keyword do?
A: ‘super’ refers to the parent class and is used to access or invoke parent class methods or
constructors.
Q4: Can constructors be inherited?
A: No, but a subclass can call a parent’s constructor using `super()`.
Q5: Difference between ‘extends’ and ‘implements’?
A: `extends` is used for classes to inherit classes; `implements` is used by classes to implement
interfaces.
5. Programs on Polymorphism**
Q1:What is polymorphism?
A: The ability of a method or object to take multiple forms. Achieved via method overloading
and overriding.
Q2. Explain method overloading.
A: Defining multiple methods with the same name but different parameter lists within a class.
Q3: What is method overriding?
A: Redefining a parent's method in the subclass with the same signature.
Q4: How does dynamic binding relate to polymorphism?
A: Dynamic (late) binding allows the JVM to decide the method to invoke at runtime, supporting
runtime polymorphism.
Q5: How are interfaces related to polymorphism?
A: Interfaces allow a class to implement multiple behaviors, supporting polymorphic code.
6. Programs on Abstract Classes and Interfaces**
Q1: What is an abstract class?
A: A class with one or more abstract methods, which must be implemented in child classes.
Q2: Why use interfaces?
A: For achieving full abstraction and multiple inheritance in Java.
Q3: Can abstract classes have constructors?
A: Yes, they can have constructors and non-abstract methods.
Q4: Can interfaces have concrete methods?
A:Prior to Java 8, no. From Java 8 onward, interfaces can have default and static methods.
Q5: What is the difference between abstract methods and interface methods?
A:Abstract class methods can have some implementation; interface methods are implicitly
abstract (no body in implementation until Java 8).
7. Programs on Packages and Access Modifiers**
Q1: What are packages?
A:Packages are namespaces for organizing classes and interfaces.
Q2: What is the use of access modifiers?
A: Control visibility of classes, fields, and methods (`private`, `protected`, `public`, default).
Q3:Difference between import statement and package declaration?
A: `package` defines the package for a class; `import` brings other packages/classes into scope.
Q4: Can you access private members outside the class?
A:No, private members are only accessible within the same class.
Q5: How do you create a user-defined package in Java?
A:Use `package packageName;` at the top of your Java file.
8. Programs on Exception Handling**
Q1: Why is exception handling needed?
A: To manage runtime errors and prevent application crashes.
Q2: How do you handle exceptions in Java?
A: Using try-catch blocks, and finally for cleanup.
Q3: What is a checked exception?
A:Exceptions that must be either caught or declared in the method signature.
Q4: How do you create a custom exception?
A: By extending the Exception or RuntimeException class.
Q5:What is the role of the finally block?
A:Executes code after try-catch, regardless of whether an exception was thrown.
9. Programs on File I/O**
Q1:How do you read a file in Java?
A. Use FileReader, BufferedReader, Scanner, or InputStream classes.
Q2:How do you write to a file?
A:Use FileWriter, BufferedWriter, or OutputStream classes.
Q3:What is serialization in File I/O?
A:Converting objects into a byte stream for storage or transmission.
Q4: What exceptions can occur during File I/O?
A:FileNotFoundException, IOException.
Q5: How do you close a file resource?
A: Use the `close()` method; with try-with-resources from Java 7, resources auto-close.
10. Programs on GUI (AWT/Applets/Swing)**
Q1:What is AWT in Java?
A:Abstract Window Toolkit; used for building graphical user interfaces.
Q2: What are Java applets?
A: Small Java applications that run in a web browser.
Q3: What is Swing?
A:A set of APIs for building advanced GUI components, offering more features than AWT.
Q4:How do you handle button clicks in GUI programs?
A: By implementing ActionListener interface and overriding `actionPerformed()` method.
Q5: What is the layout manager in Java GUI programming?
A: Classes (like BorderLayout, GridLayout, FlowLayout) that manage component positioning in
containers.