Computer Class 9 – Chapter 3: Programming Fundamentals
National Book Foundation (NBF)
Chapter Overview:
This chapter introduces the basics of programming: how programs are written, tested, and
executed. It covers HTML for basic webpage structure and Java for core programming concepts
like variables, data types, control structures, methods, and classes. Important example codes are
provided to help practice.
Key Concepts:
• Program: A set of instructions that a computer follows to perform a task.
• Programming languages: HTML (markup for webpages) and Java (general-purpose,
object-oriented language).
• Variables and data types: store information (int, double, boolean, String in Java).
• Control structures: sequence, selection (if-else, switch), and iteration (for, while).
• Methods/Functions: reusable blocks of code that perform a task.
• Input/Output: getting data from user and showing results.
• Debugging & testing: finding and fixing errors.
HTML Examples (structure & form):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Form</title>
</head>
<body>
<h1>Student Info</h1>
<form action="/submit" method="post">
<label> Name: <input type="text" name="name" required> </label><br>
<label> Age: <input type="number" name="age" min="1" max="100"> </label><br>
<label> Gender:
<select name="gender">
<option>Male</option>
<option>Female</option>
<option>Other</option>
</select>
</label><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
Java Examples (basic programs):
1) Hello World
// [Link]
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
2) Read input and add two numbers
// [Link] - reading input using Scanner
import [Link];
public class SumTwoNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
int sum = a + b;
[Link]("Sum = " + sum);
[Link]();
}
}
3) Factorial (method + loop)
// Factorial using loop and method
public class Factorial {
public static long factorial(int n) {
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
public static void main(String[] args) {
int n = 5;
[Link]("Factorial of " + n + " is " + factorial(n));
}
}
4) Bubble Sort (example algorithm)
// [Link] - simple sorting algorithm
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = [Link];
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] data = {5, 2, 9, 1, 5, 6};
bubbleSort(data);
for (int v : data) [Link](v + " ");
}
}
Short Questions and Answers:
Q1. What is a programming language?
A formal language with syntax and semantics used to write programs that a computer can execute.
Q2. Difference between HTML and Java?
HTML is a markup language for structuring web pages. Java is a programming language used for
writing stand-alone and server applications.
Q3. What is a variable?
A named storage location in memory used to hold values that can change during program
execution.
Q4. What is a method in Java?
A block of code that performs a specific task and can be called from other parts of the program.
Q5. What is a loop?
A control structure that repeats a block of code until a condition is met.
Long Questions and Answers:
Q1. Explain the steps to write, compile and run a Java program.
1. Write source code in a file with .java extension.
2. Compile the code using javac (javac [Link]) to get .class bytecode files.
3. Run the code using java (java ClassName) which executes bytecode on the JVM.
4. Fix any compile-time or runtime errors and test the program.
Q2. Describe common data types in Java and when to use them.
Common primitive data types:
- byte, short, int, long: integer numbers (use int for general integers).
- float, double: decimal numbers (use double for precision).
- char: single character.
- boolean: true/false values.
Use String for text (not a primitive). Choose types based on the range and precision needed.
MCQs (with answers):
1. HTML stands for:
→ Answer: a) HyperText Markup Language
2. Which of these is not a Java primitive type?
→ Answer: d) String
3. Which loop checks condition before executing the loop body?
→ Answer: b) while loop
4. Which method starts a Java program?
→ Answer: c) main()
5. Which tag is used for the largest heading in HTML?
→ Answer: a)
Fill in the Blanks (with answers):
1. ________ is used to structure web pages.
→ Answer: HTML
2. In Java, the keyword ________ is used to create a new object.
→ Answer: new
3. The entry point of a Java program is the ________ method.
→ Answer: main
4. A loop that always runs until a condition becomes false is called ________.
→ Answer: while loop
5. To print text in Java, we use ________.
→ Answer: [Link]()
Important Tips & Best Practices:
• Always comment your code for clarity.
• Use meaningful variable names.
• Test programs with different inputs (including edge cases).
• Keep methods short and focused on a single task.
• For HTML, validate your markup and close tags properly.