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

Computer QA Chapter 4

Grade 10 federal board Chapter 4 question answer

Uploaded by

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

Computer QA Chapter 4

Grade 10 federal board Chapter 4 question answer

Uploaded by

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

Computer theory Q & A Chapter:4

Q.1.What do you know about control structure.

Ans. Control structures manage the flow of execution in programming:


1. Sequential: Executes code line by line.
2. Selection: Makes decisions (e.g., if, else, switch).
3. Repetition: Repeats code (e.g., for, while, do-while).
4. Jump: Alters flow (e.g., break, continue, return).

They enable dynamic and efficient programs.

Q.2.What is the difference b/w control and conditional statements.

Ans. Control Statements: Broad category that manages execution flow, including loops and
jumps (e.g., for, while, break).

Conditional Statements: Subset of control statements focused on decision-making (e.g., if,


else, switch).

Q.3.Write the structure of if statement. Give on example.

Ans. Structure of an if Statement


Plaintext:
if (condition) {
// code to execute if condition is true
}

Example
Python:
age = 18

if age >= 18:


print("You are an adult.")

In this example, if the age is 18 or older, the message "You are an adult." will be printed.
Q.4.Write the structure of if else statements. Give one examples

Ans. Structure of an if-else Statement


Plaintext:
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}

Example
Python:
age = 16

if age >= 18:


print("You are an adult.")
else:
print("You are a minor.")

In this example, if the age is 18 or older, it prints "You are an adult." Otherwise, it prints "You
are a minor."

Q.5.Write the structure of if else if. Give one example

Ans. The structure of an if...else if...else statement typically looks like this:

Plaintext:

if (condition1) {
// code block to execute if condition1 is true
} else if (condition2) {
// code block to execute if condition1 is false and condition2 is true
} else {
// code block to execute if both condition1 and condition2 are false
}

Example:

Here's a simple example in JavaScript that checks a person's age and determines if they are a
child, a teenager, or an adult:

JavaScript:

let age = 16;


if (age < 13) {
console.log("You are a child.");
} else if (age >= 13 && age < 20) {
console.log("You are a teenager.");
} else {
console.log("You are an adult.");
}

In this example:

 If age is less than 13, it prints "You are a child."


 If age is between 13 and 19, it prints "You are a teenager."
 If age is 20 or older, it prints "You are an adult."

Q.6.Write a note on switch statement.

Ans. A switch statement is a control structure that allows execution of different code blocks
based on the value of a variable. It consists of a switch keyword, cases for each possible value,
and an optional default case. Each case typically ends with a break statement to prevent fall-
through. Switch statements are useful for simplifying multiple conditions compared to using
several if-else statements, making the code cleaner and more readable.

Q.7.Write a advantage and disadvantage of switch statements.

Ans. Advantage:
Clarity and Readability: Switch statements simplify the handling of multiple conditions,
making the code easier to read and understand compared to a series of if-else statements.

Disadvantage:

Limited Flexibility: Switch statements can only evaluate a single expression and typically work
with discrete values, making them less versatile than if-else statements for complex conditions.

Q.8.What is the nested selection structure. Give one example.


Ans. A nested selection structure is a programming construct where one selection statement
(like an if or switch) is placed inside another. This allows for more complex decision-making by
evaluating multiple conditions at different levels.

Example:

Here's an example using nested if statements in Python:

Python:
age = 20
citizenship = "USA"

if age >= 18:


if citizenship == "USA":
print("You are eligible to vote.")
else:
print("You must be a U.S. citizen to vote.")
else:
print("You must be at least 18 years old to vote.")

Explanation:

In this example:

 The outer if checks if the person is 18 or older.


 If true, the inner if checks if the person is a U.S. citizen.
 Different messages are printed based on the evaluations, demonstrating how nested
selection structures can handle multiple criteria.

Q.9.What is the difference b/w break and continuous.

Ans. Break:
 Exits the nearest loop entirely.
 Use Case: Stop looping based on a condition.

Continue:

 Skips the current iteration and continues to the next.


 Use Case: Bypass certain conditions but keep looping.
Chapter:5

Q.1.Write a note on loop structure and describe it’s type.

Ans. A loop structure allows repeated execution of code based on a condition.


Types:

1. For Loop: Iterates a specified number of times, often using a counter.


o Example:

python
Copy code
for i in range(5):
print(i)

2. While Loop: Repeats as long as a condition is true, checking before each iteration.
o Example:

Python:

i = 0
while i < 5:
print(i)
i += 1

3. Do-While Loop: Similar to while, but checks the condition after executing the loop
body, ensuring at least one iteration.
o Example:

Javascript:

let i = 0;
do {
console.log(i);
i++;
} while (i < 5);

In summary, loops are essential for repetitive tasks, with different types offering flexibility based
on the situation.
Q.2.Write a structure of for statements. Give one example.

Ans. Structure of a For Statement


A typical for statement consists of three main components:

1. Initialization: Sets a starting point (often a counter).


2. Condition: Evaluated before each iteration; the loop continues as long as this condition is
true.
3. Increment/Decrement: Updates the counter after each iteration.

General Syntax:
Plaintext:

for (initialization; condition; increment/decrement) {


// Code to be executed in each iteration
}

Example in Python:
Python:

for i in range(5): # Initialization and condition


print(i) # Code block executed in each iteration

Output:

0
1
2
3
4

In this example, the loop initializes i to 0, continues as long as i is less than 5, and increments i
by 1 in each iteration, printing the current value of i.

Q.3.Write a structure for while statements. Give one example.

Ans. Structure of a While Statement


A while statement consists of two main components:
1. Condition: Evaluated before each iteration; the loop continues as long as this condition is
true.
2. Code Block: The set of instructions to be executed as long as the condition holds true.

General Syntax:
Plaintext:

while (condition) {
// Code to be executed in each iteration
}

Example in Python:
Python:

i = 0 # Initialization
while i < 5: # Condition
print(i) # Code block executed in each iteration
i += 1 # Increment

Output:
0
1
2
3
4

In this example, the loop starts with i initialized to 0 and continues as long as i is less than 5. In
each iteration, it prints the value of i and increments it by 1.

Q.4.Write a structure of do while statements. Give one example

Ans. Structure of a Do-While Statement


A do-while statement consists of two main components:

1. Code Block: The set of instructions that are executed at least once.
2. Condition: Evaluated after each iteration; the loop continues as long as this condition is
true.
General Syntax:
Plaintext:

do {
// Code to be executed
} while (condition);

Example in JavaScript:
Javascript:

let i = 0; // Initialization
do {
console.log(i); // Code block executed
i++; // Increment
} while (i < 5); // Condition

Output:
0
1
2
3
4

In this example, the loop starts with i initialized to 0. The code block is executed first, printing
the value of i, and then i is incremented. The condition is checked afterward, ensuring the loop
continues until i is no longer less than 5.

Q.5.What is the difference b/w while and do-while loops.

Ans. Differences between While and Do-While Loops:


1. Condition Evaluation:
o While Loop: Condition is checked before executing the loop body. It may not
execute at all if the condition is false.
o Do-While Loop: Condition is checked after executing the loop body. It always
executes at least once.
2. Execution Guarantee:
o While Loop: May not run if the condition is false initially.
o Do-While Loop: Runs at least once, even if the condition is false.
Example:

 While Loop:

C:

int i = 5;
while (i < 5) {
// No output
}

 Do-While Loop:

C:

int j = 5;
do {
// Output: 5
} while (j < 5);

Use a while loop for pre-condition checks and a do-while loop for guaranteed execution.

Q.6.Write the structure of nested loops. Give one example.

Ans. Structure of Nested Loops


Nested loops consist of one loop inside another loop. The inner loop executes completely for
each iteration of the outer loop.

General Syntax:
plaintext
Copy code
for (initialization; condition; increment) {
// Outer loop code
for (initialization; condition; increment) {
// Inner loop code
}
}
Example in C:

Here's an example that prints a multiplication table:

C:

#include <stdio.h>

int main() {
int i, j;

// Outer loop for the first number (1 to 5)


for (i = 1; i <= 5; i++) {
// Inner loop for the second number (1 to 5)
for (j = 1; j <= 5; j++) {
printf("%d x %d = %d\n", i, j, i * j);
}
printf("\n"); // Add a blank line after each table
}

return 0;
}

Output:
Python:

1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10

...

5 x 5 = 25

In this example, the outer loop iterates through numbers 1 to 5, and for each iteration of the outer
loop, the inner loop also iterates through 1 to 5, creating a multiplication table.

You might also like