MSQ
MSQ
3. User-Defined Methods
Definition: Blocks of code that perform a specific task.
Types:
o Static Methods: Called using the class name.
o Non-Static Methods: Called using an object.
Overloading: Methods with the same name but different
parameters.
4. Constructors
Definition: Special methods for initializing objects.
Types:
o Default Constructor: No parameters.
o Parameterized Constructor: With parameters.
Constructor Overloading: Multiple constructors in a class.
5. Library Classes
Wrapper Classes
Definition: Provide methods for primitive types (Integer,
Character, etc.).
Key Methods:
o int parseInt(String s): Converts string to integer.
o boolean isDigit(char ch): Checks if a character is a digit.
o char toUpperCase(char ch): Converts to uppercase.
Autoboxing and Unboxing
Autoboxing: Primitive to Wrapper conversion.
Unboxing: Wrapper to Primitive conversion.
6. Encapsulation
Access Specifiers:
o Private: Accessible only within the class.
o Public: Accessible from anywhere.
o Protected: Accessible within the package and subclasses.
Scope of Variables:
o Local: Inside a method.
o Instance: Non-static and unique to each object.
o Class: Static and shared among all objects.
7. Arrays
Single Dimensional:
int[] arr = {1, 2, 3};
Two Dimensional:
int[][] matrix = new int[2][2];
Sorting:
o Selection Sort: Find the smallest element and place it in
order.
o Bubble Sort: Repeatedly swap adjacent elements if they are
in the wrong order.
Searching:
o Linear Search: Sequentially check each element.
o Binary Search: Divide and conquer (requires sorted array).
8. String Handling
String Class
Immutable and represents sequences of characters.
Key Methods:
o String toLowerCase(): Converts to lowercase.
o int length(): Returns string length.
o char charAt(int index): Returns character at index.
Examples:
Palindrome Check:
String reversed = new StringBuilder(input).reverse().toString();
if (input.equals(reversed)) System.out.println("Palindrome");
PRACTICE QUESTIONS
Here are detailed examples, programs, and practice questions
for the requested topics:
2. User-Defined Methods
Example: Static vs Non-Static Methods
class MathOperations {
static int add(int a, int b) { // Static method
return a + b;
}
3. Constructors
Example: Parameterized Constructor
class Rectangle {
int length, width;
int calculateArea() {
return length * width;
}
}
4. Arrays
Example: Bubble Sort
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 3, 8, 6, 2};
int n = arr.length;
5. String Handling
Example: Palindrome Check
public class Palindrome {
public static void main(String[] args) {
String str = "radar";
String reversed = new
StringBuilder(str).reverse().toString();
if (str.equals(reversed)) {
System.out.println("Palindrome");
} else {
System.out.println("Not a Palindrome");
}
}
}
Practice Questions:
1. Write a program to count the number of vowels in a
string.
2. Create a program to compare two strings
lexicographically.
6. Encapsulation
Example: Access Specifiers
class Account {
private double balance; // Private variable