0% found this document useful (0 votes)
10 views

MSQ

Uploaded by

Vishal kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

MSQ

Uploaded by

Vishal kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

INSIGHT OF COMPUTER

CHAPTERS AND PORTIONS


1. Revision of Class IX Syllabus
Object-Oriented Programming Concepts
 Definition: A paradigm that uses "objects" to represent data and
methods.
 Principles: Encapsulation, Inheritance, Polymorphism,
Abstraction.
Elementary Concepts of Objects and Classes
 Object: Instance of a class with a state (variables) and behavior
(methods).
 Class: Blueprint for creating objects.
Values and Data Types
 Primitive: int, char, float, boolean, etc.
 Non-Primitive: Arrays, Strings, Classes.
Operators in Java
 Arithmetic: +, -, *, /, %.
 Relational: >, <, >=, <=, ==, !=.
 Logical: &&, ||, !.
Input in Java
 Use Scanner class:
 Scanner sc = new Scanner(System.in);
 int a = sc.nextInt();
Conditional Constructs
 if, if-else, switch.
Iterative Constructs
 Loops: for, while, do-while.
Nested Loops
 Loop within another loop.

2. Class as the Basis of Computation


 Objects: Represent real-world entities, encapsulating state and
behavior.
 Classes: Act as templates for objects.
 Primitive Data Types: int, char, float, etc.
 Composite Data Types: Classes, Arrays.

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:

1. Object-Oriented Programming Concepts


Example: Object and Class
class Car {
String brand; // Attribute
int speed;

void display() { // Method


System.out.println("Brand: " + brand);
System.out.println("Speed: " + speed);
}
}

public class Main {


public static void main(String[] args) {
Car myCar = new Car(); // Object creation
myCar.brand = "Toyota";
myCar.speed = 120;
myCar.display();
}
}
Practice Questions:
1. Create a class Book with attributes: title, author, price.
Add a method displayDetails() and create an object to test
it.
2. Define a class Student with attributes name and marks.
Write a method to determine if the student has passed
(marks ≥ 40).

2. User-Defined Methods
Example: Static vs Non-Static Methods
class MathOperations {
static int add(int a, int b) { // Static method
return a + b;
}

int subtract(int a, int b) { // Non-static method


return a - b;
}
}

public class Main {


public static void main(String[] args) {
System.out.println("Sum: " + MathOperations.add(10, 20));
// Static method call
MathOperations obj = new MathOperations();
System.out.println("Difference: " + obj.subtract(20, 10)); //
Non-static method call
}
}
Practice Questions:
1. Write a program with a method isPrime(int num) to check
if a number is prime.
2. Create a method calculateFactorial(int num) using
recursion.

3. Constructors
Example: Parameterized Constructor
class Rectangle {
int length, width;

Rectangle(int l, int w) { // Parameterized constructor


length = l;
width = w;
}

int calculateArea() {
return length * width;
}
}

public class Main {


public static void main(String[] args) {
Rectangle rect = new Rectangle(5, 10);
System.out.println("Area: " + rect.calculateArea());
}
}
Practice Questions:
1. Create a class Circle with radius as an attribute. Use a
constructor to initialize it and calculate the area.
2. Write a program demonstrating constructor overloading.

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;

for (int i = 0; i < n - 1; i++) {


for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) { // Swap if out of order
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

System.out.println("Sorted Array: ");


for (int num : arr) {
System.out.print(num + " ");
}
}
}
Practice Questions:
1. Implement selection sort for an array of integers.
2. Write a program to perform binary search on a sorted
array.

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

public Account(double initialBalance) {


balance = initialBalance;
}

public void deposit(double amount) { // Public method


balance += amount;
}

public double getBalance() { // Public getter


return balance;
}
}

public class Main {


public static void main(String[] args) {
Account acc = new Account(500);
acc.deposit(200);
System.out.println("Balance: " + acc.getBalance());
}
}
Practice Questions:
1. Create a class Person with private attributes name and
age. Use getter and setter methods to access them.
2. Implement a program to demonstrate the use of
protected specifier.

You might also like