Java Question Bank - Answers
### Chapter 1: Basic Syntactical Constructs in Java
1. **Syntax of Array Declaration & Initialization:**
```java
int[] arr = new int[5]; // Declaration
int[] arr = {1, 2, 3, 4, 5}; // Initialization
```
2. **Primitive Data Types in Java:**
- byte (1 byte)
- short (2 bytes)
- int (4 bytes)
- long (8 bytes)
- float (4 bytes)
- double (8 bytes)
- char (2 bytes)
- boolean (1 bit)
3. **Tokens in Java:**
- Keywords, Identifiers, Literals, Operators, Separators
4. **For-each Syntax and Example:**
```java
for (int num : array) {
System.out.println(num);
}
```
5. **String vs StringBuffer:**
- String is immutable, StringBuffer is mutable.
- StringBuffer is faster for modifications.
6. **instanceOf and Dot Operator:**
```java
if (obj instanceof ClassName) { ... }
obj.methodName();
```
7. **Two Bitwise & Logical Operators:**
- Bitwise: `&`, `|`
- Logical: `&&`, `||`
8. **Class and Object Example:**
```java
class Student {
int id;
String name;
}
```
9. **Features of Java:**
- Platform-independent, Object-Oriented, Secure, Robust
10. **Type Casting:**
- Implicit and Explicit conversion.
```java
double d = 10; // Implicit
int x = (int) 10.5; // Explicit
```
### Chapter 2: Inheritance, Interface, and Package
1. **Built-in Packages & Classes:**
- java.util (ArrayList, Scanner)
- java.io (File, BufferedReader)
2. **Creating and Accessing Packages:**
```java
package myPackage;
import myPackage.*;
```
3. **Creating and Importing Packages:**
```java
package myPack;
class Example { ... }
```
4. **Inheritance Types:**
- Single, Multilevel, Hierarchical, Multiple (via interfaces)
5. **Keywords: final, super, abstract**
- `final` prevents modification.
- `super` calls the parent class.
- `abstract` defines abstract methods.
6. **Interface in Java:**
```java
interface MyInterface { void display(); }
```
7. **Method Overloading vs Overriding:**
- Overloading: Same method name, different parameters.
- Overriding: Redefining parent class method.
8. **Class vs Interface:**
- A class can have implementations; an interface has only method signatures.
9. **Program for Multiple Inheritance:**
```java
interface A { void show(); }
interface B { void display(); }
class C implements A, B { ... }
```
10. **Interest Interface Example:**
```java
interface Interest {
double RATE = 0.25;
double simpleInterest(double p, double t);
}
```
---
More solutions and Java programs will be added in the complete PDF.