C & Pseudocode 📝
This section covers the essential syntax and logic required for foundational programming.
1. Variables, Data Types, and Constants
A variable is a named memory location used to store data. The data type tells the compiler how
much memory to allocate and what kind of data can be stored.
Common Data Types in C:
o int: For whole numbers (e.g., 10, -150). Usually 4 bytes.
o float: For single-precision floating-point numbers (e.g., 3.14, -0.025). Usually 4 bytes.
o double: For double-precision floating-point numbers, offering more precision.
Usually 8 bytes.
o char: For a single character (e.g., 'A', '!'). Usually 1 byte.
Variable Naming Rules (Identifiers):
o Can contain letters, digits, and underscores (_).
o Must begin with a letter or an underscore.
o Cannot be a C keyword (like int, if, else). C is case-sensitive (age and Age are different
variables).
Constants: A constant is a variable whose value cannot be changed after initialization. Use
the const keyword.
const float PI = 3.14;
// PI = 3.14159; // This would cause a compiler error
2. Operators
Operators are symbols used to perform operations. Understanding their precedence (the order in
which they are evaluated) is crucial.
Arithmetic: +, -, *, /, % (modulus). Multiplication/division/modulus have higher precedence
than addition/subtraction.
Relational: ==, !=, >, <, >=, <=. Used for comparisons, resulting in true (1) or false (0).
Logical: && (AND), || (OR), ! (NOT). Used to combine or invert boolean expressions.
Assignment: =, +=, -=, *=, /=. Used to assign values. x += 5; is shorthand for x = x + 5;.
Increment/Decrement: ++, --.
o Prefix (++x): Increments the value, then uses it in the expression.
o Postfix (x++): Uses the current value in the expression, then increments it.
3. Input/Output (I/O)
Standard I/O is handled by functions in the <stdio.h> library.
printf() (Formatted Output): Prints data to the console.
o Format Specifiers: %d (int), %f (float/double), %c (char), %s (string).
o Escape Sequences: \n (new line), \t (tab).
int age = 25;
float weight = 65.5;
printf("Age: %d\nWeight: %.1f kg\n", age, weight); // %.1f prints float with 1 decimal place
scanf() (Formatted Input): Reads data from the console.
o It requires the memory address of the variable to store the data. The address-of
operator (&) is used to get this.
int number;
printf("Enter a number: ");
scanf("%d", &number); // &number provides the memory address of the variable 'number'
4. Conditional Statements
These control the flow of a program based on conditions.
if, if-else, else if Ladder: For decision-making based on one or more conditions.
// Nested if-else example
int age = 20;
int hasLicense = 1; // 1 for true, 0 for false
if (age >= 18) {
if (hasLicense == 1) {
printf("You can drive.");
} else {
printf("You are old enough, but need a license.");
} else {
printf("You are too young to drive.");
}
switch Statement: An efficient alternative to a long else if ladder for checking a variable
against a list of constant values.
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!");
break; // 'break' exits the switch statement
case 'B':
printf("Good job!");
break;
case 'C':
printf("You passed.");
break;
default: // 'default' is optional and runs if no other case matches
printf("Invalid grade.");
5. Pseudocode
Pseudocode is a plain-language description of an algorithm's steps. It has no formal syntax and is
used for planning before writing actual code.
Example: Find the larger of two numbers
START
READ number1
READ number2
IF number1 > number2 THEN
DISPLAY number1 + " is larger."
ELSE IF number2 > number1 THEN
DISPLAY number2 + " is larger."
ELSE
DISPLAY "Both numbers are equal."
END IF
END
Object-Oriented Programming (OOP) with Java ☕
OOP organizes software design around data, or objects, rather than functions and logic.
1. Programming Paradigms & OOP Principles
Procedural vs. OOP: Procedural programming involves writing a sequence of steps
(procedures). OOP involves creating objects that contain both data (attributes) and methods
(behaviors).
The Four Pillars of OOP:
1. Encapsulation: Bundling data (attributes) and methods that operate on the data into
a single unit, or "object." This is like a medical capsule that encloses medicine,
protecting it from the outside. In Java, this is achieved with classes.
2. Abstraction: Hiding complex implementation details and showing only the essential
features of the object. When you drive a car, you use the steering wheel and pedals
(the interface), without needing to know how the engine works (the
implementation).
3. Inheritance: A mechanism where a new class (subclass) inherits attributes and
methods from an existing class (superclass). This promotes code reuse. A Car class
can inherit properties from a Vehicle class.
4. Polymorphism: The ability of an object to take on many forms. Most commonly, it
means a method can do different things depending on the object it is acting upon. A
draw() method could draw a circle, a square, or a triangle.
2. Java Basics
JVM (Java Virtual Machine): Java code is compiled into an intermediate form called
bytecode. The JVM is a program that reads this bytecode and translates it into machine code
for the specific computer it's running on. This is what enables Java's "Write Once, Run
Anywhere" (WORA) philosophy.
Data Types:
o Primitive: Stored directly in memory (on the stack). They hold the actual value.
Includes int, char, boolean, double, etc.
o Reference (Non-Primitive): Stored in a different memory area (the heap). The
variable holds a reference or address pointing to the actual object. Includes String,
Array, and any custom Class objects.
Scope: Determines where a variable can be accessed.
o Class Scope: Declared within a class but outside any method. Accessible to all
methods in the class.
o Method Scope: Declared inside a method. Can only be used within that method.
o Block Scope: Declared inside a pair of curly braces {} (e.g., inside an if statement or a
loop). It's only accessible within that block.
3. User Input
Scanner Class: The standard way to get user input.
o You must import java.util.Scanner;
o Create a Scanner object: Scanner scanner = new Scanner(System.in);
o Use its methods to read data: nextInt(), nextDouble(), nextLine().
o Common Pitfall: After using nextInt(), if you need to read a full line of text with
nextLine(), you first need to consume the leftover newline character.
Java
// Complete, runnable example
import java.util.Scanner;
public class UserInputDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Hello, " + name + "! You are " + age + " years old.");
scanner.close();
Command Line Arguments: Data passed to the program at runtime.
o They are captured in the String[] args array of the main method.
o To run from the command line:
1. Compile: javac MyProgram.java
2. Run with arguments: java MyProgram John 30
Java
// MyProgram.java
public class MyProgram {
public static void main(String[] args) {
String name = args[0]; // "John"
int age = Integer.parseInt(args[1]); // "30" is converted to int 30
System.out.println(name + " is " + age + " years old.");
Operating Systems (OS)
The OS acts as the master controller for all hardware and software on a computer.
1. Core Functions of an OS
Process Management: A process is a program in execution. The OS manages its entire
lifecycle.
o Process States: A process moves through states: New (being created), Ready
(waiting for the CPU), Running (executing on the CPU), Waiting (waiting for an event
like I/O), and Terminated (finished).
Licensed by Google
* **Process Control Block (PCB):** A data structure where the OS stores all information about a
process, like its state, program counter, and memory allocation.
Memory Management: The OS efficiently allocates memory to processes and reclaims it
when they are done. Key techniques include:
o Paging: Dividing memory into fixed-size blocks called pages.
o Segmentation: Dividing memory into logical segments of varying sizes.
File and Device Management: The OS presents a simple, unified view of complex hardware.
o File System: Creates a hierarchical (tree-like) structure of directories and files.
o Drivers: Specialized software that acts as a translator between the OS and a specific
hardware device (like a printer or graphics card).
2. Kernel, Shell, and System Calls
Kernel: The nucleus of the OS. It controls everything and runs in a protected "kernel mode"
with unrestricted access to hardware.
Shell: The user interface. It runs in "user mode" with limited privileges. It takes commands
from the user and asks the kernel to execute them.
o CLI (Command Line Interface): Text-based (e.g., Bash on Linux, Command Prompt on
Windows).
o GUI (Graphical User Interface): Icon and window-based (e.g., Windows Desktop,
macOS Aqua).
System Calls: The bridge between user mode and kernel mode. When a program needs a
privileged service (like reading a file or creating a process), it makes a system call. The OS
then takes over, performs the task in kernel mode, and returns the result.
3. System Architectures
Monolithic Kernel: The entire OS (file system, memory management, device drivers) is a
single, large program running in kernel mode.
o Analogy: An all-in-one appliance.
o Pros: Very fast communication between components.
o Cons: A bug in one part (like a bad driver) can crash the whole system.
o Example: Linux.
Microkernel: The kernel is kept as small as possible, containing only essential functions like
process scheduling and memory management. Other services (like file systems) run as
separate processes in user mode.
o Analogy: A central power hub with separate appliances plugged into it.
o Pros: More secure and stable. A failing service won't crash the kernel.
o Cons: Slower due to communication overhead between user-space services and the
kernel.
Layered Architecture: The OS is organized into a hierarchy of layers. Each layer provides
services only to the layer directly above it.
o Analogy: A stack of building floors, where each floor can only access the one directly
below it.
o Pros: Simple to design and debug.
o Cons: Can be inefficient, as a request may have to pass through many layers.