Computer Science Grade 10 Send-Ups Exam 2024 FIC Kohat
Answer Key for SECTION-B (Marks 22)
Q.2 Short Questions……….
i. Define Algorithm. Also describe its role.
Definition of Algorithm
An algorithm is a step-by-step procedure or set of rules to solve a specific problem or perform a
task.
Role of an Algorithm
• Serves as a blueprint for solving problems systematically.
• Ensures tasks are completed efficiently, consistently, and logically.
Alternative Answer
Candid Solutions: A programmer identifies solutions by analyzing the problem requirements,
testing different logic paths, and considering edge cases to ensure robustness.
ii. Why is it important to terminate a statement with a semicolon in C language?
Importance
• In C, the semicolon marks the end of a statement, allowing the compiler to identify
statement boundaries.
What Happens if Omitted?
• Compilation errors occur because the compiler cannot parse the code correctly.
Alternative Answer
Two Commonly Used Format Specifiers in C:
1. %d - Used for integers (e.g., printf("%d", num);).
2. %f - Used for floating-point numbers (e.g., printf("%f", value);).
iii. Write two characteristics of high-level languages.
By: Waseem Khan FIC Kohat
Characteristics
1. User-friendly: High-level languages use syntax closer to human language, making them
easier to understand and write.
2. Portability: High-level programs can run on multiple platforms with minimal or no
modifications.
Alternative Answer
Why Java is Ideal for Network Computing:
• Platform Independence: Java programs run on any device with a JVM.
• Built-in Libraries: Java provides networking APIs like java.net for seamless
communication.
iv. Explain the use of conditional operators with an example.
Use of Conditional Operators
• Conditional operators (? :) simplify decision-making by evaluating a condition and
choosing between two expressions.
Example
int a = 10, b = 20, max;
max = (a > b) ? a : b; // Assigns max to the larger value.
printf("Maximum: %d", max);
Alternative Answer
C Program with if Statement:
#include <stdio.h>
int main()
{
int num = 10;
if (num > 5)
{
printf("Number is greater than 5");
}
return 0;
By: Waseem Khan FIC Kohat
}
v. Write a C program that prints numbers from 1 to 10 using a For loop.
Program:
#include <stdio.h>
int main()
{
for (int i = 1; i <= 10; i++)
{
printf("%d ", i);
}
return 0;
}
Alternative Answer
Print Uppercase Letters Using While Loop:
#include <stdio.h>
int main()
{
int ascii = 65; // ASCII value of 'A'
while (ascii <= 90)
{
printf("%c", ascii);
ascii++;
}
return 0;
}
vi. Differentiate between compiler and interpreter.
Compiler Interpreter
Translates entire code into machine
Translates and executes code line-by-line.
code.
Slower as translation and execution happen
Faster execution after compilation.
simultaneously.
By: Waseem Khan FIC Kohat
Alternative Answer
Evaluate Expressions:
1. 9+6×(5+3)=579 + 6 \times (5 + 3) = 57
2. 120/15/5=1.6120 / 15 / 5 = 1.6
vii. Algorithm to Find Interest on a Given Amount
Algorithm:
1. Start.
2. Input principal, rate, time.
3. Calculate interest: Interest=Principal×Rate×Time/100\text{Interest} = \text{Principal}
\times \text{Rate} \times \text{Time} / 100.
4. Output the interest.
5. End.
Alternative Answer
Flowchart for Even Numbers (50 to 100):
• Start → Set num = 50 → Check num <= 100 → Print num if even → Increment
num → Repeat → End.
viii. Output of the Given Program
Program Analysis:
int x, y, z1, z2, z3, z4;
x = 18; y = 2;
z1 = x / y; // z1 = 9
z2 = x % y; // z2 = 0
z3 = ++x; // z3 = 19
z4 = y++; // z4 = 2 (post-increment)
Output:
By: Waseem Khan FIC Kohat
z1=9
z2=0
z3=19
z4=2
Alternative Answer
Nested Loop for Multiplication Table:
#include <stdio.h>
int main()
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 4; j++)
{
printf("%d x %d = %d\t", i, j, i * j);
}
printf("\n");
}
return 0;
}
ix. Break Statement in Switch Case
Example of Break Statement:
switch (choice) {
case 1: printf("Option 1"); break;
case 2: printf("Option 2"); break;
default: printf("Invalid option");
}
• Break stops further case execution once the correct case executes.
Alternative Answer
Continue Statement:
for (int i = 1; i <= 5; i++)
{
if (i == 3) continue; // Skips iteration when i == 3
By: Waseem Khan FIC Kohat
printf("%d ", i);
}
x. Define Reserved Words
Definition
Reserved words are keywords predefined in a programming language that have special
meanings and cannot be used for other purposes, like variable names.
Examples: if, while, return.
Alternative Answer
Mathematical Functions:
1. sqrt(x) - Square root.
2. pow(x, y) - Power.
Header File: <math.h>.
xi. Write the Given Statements Using Alternate Syntax
1. Using if-else for k = (a+b>20)? a*3*b : a-b;:
2. if (a + b > 20)
3. k = a * 3 * b;
4. else
5. k = a - b;
6. Using Conditional Operator for if (x>y) {...:
7. z = (x > y) ? (x + y) / 2 : (x - 4 * y);
SECTION C
Answer Key for LONG QUESTIONS Q.3, Q.4, Q.5, and Q.6
Q.3
By: Waseem Khan FIC Kohat
1. Explain the Conditional (Ternary) Operator in C
Explanation
The conditional operator (? :) is a shorthand for the if-else statement. It evaluates a
condition and returns one of two expressions based on whether the condition is true or false.
Syntax
condition ? expression_if_true : expression_if_false;
Example
#include <stdio.h>
int main()
{
int a = 10, b = 20, max;
max = (a > b) ? a : b;
printf("Maximum is: %d", max);
return 0;
}
Comparison with if-else Statement
• Conditional Operator: Compact and suitable for simple conditions.
• If-Else Statement: Easier to read for complex conditions but requires more lines of code.
Equivalent if-else:
if (a > b)
max = a;
else
max = b;
2. Program to Calculate the Area of a Triangle
#include <stdio.h>
int main()
{
float base, height, area;
By: Waseem Khan FIC Kohat
printf("Enter base and height of the triangle: ");
scanf("%f %f", &base, &height);
area = 0.5 * base * height;
printf("Area of the triangle: %.2f", area);
return 0;
}
Q.4
1. Algorithm to Find the Product of Given Numbers
1. Start.
2. Input the numbers num1 and num2.
3. Calculate the product: product = num1 * num2.
4. Output the product.
5. End.
2. Algorithm to Find the Sum of a Sequence
1. Start.
2. Input the starting and ending numbers of the sequence (start, end).
3. Initialize sum = 0.
4. Loop from start to end:
a. Add the current number to sum.
5. Output the sum.
6. End.
Q.5
1. Purpose of "The Include Preprocessor Directive" in C
The #include directive is used to include external files, such as header files, into a program.
It allows the use of pre-defined functions and macros.
Example:
#include <stdio.h>
By: Waseem Khan FIC Kohat
int main()
{
printf("Hello, World!");
return 0;
}
Why Include stdio.h?
The stdio.h header file provides standard input/output functions like printf and scanf.
2. Program to Demonstrate Explicit Typecasting in C
#include <stdio.h>
int main()
{
int a = 5, b = 2;
float result;
result = (float)a / b; // Explicit typecasting
printf("Result: %.2f", result);
return 0;
}
Explanation
• The (float)a convert’s a to a floating-point number before division.
• Without typecasting, integer division would occur, resulting in a truncated value.
Q.6
1. Program to Print the Sum of Squares of Numbers from 1 to 10
#include <stdio.h>
int main()
{
int sum = 0;
By: Waseem Khan FIC Kohat
for (int i = 1; i <= 10; i++)
{
sum += i * i; // Add square of i to sum
}
printf("Sum of squares: %d", sum);
return 0;
}
2. Program to Calculate Net Pay Based on Basic Pay
#include <stdio.h>
int main()
{
float basicPay, houseRent, netPay;
printf("Enter Basic Pay: ");
scanf("%f", &basicPay);
if (basicPay < 25000)
houseRent = 0.3 * basicPay;
else if (basicPay <= 40000)
houseRent = 0.4 * basicPay;
else
houseRent = 0.5 * basicPay;
netPay = basicPay + houseRent;
printf("Net Pay: %.2f", netPay);
return 0;
}
By: Waseem Khan FIC Kohat