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

Answers or Algorithm Year2023

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

Answers or Algorithm Year2023

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

Question 1: What is Python?

Python is a high-level, interpreted programming language known for its


readability and simplicity. It supports multiple programming paradigms,
including procedural, object-oriented, and functional programming.

Question 2: Define the following terms applied in algorithm:

a) Debugging: The process of finding and fixing errors or bugs in a program. b)


Program linking: The process of combining various pieces of code and data
together to form a single executable program.

Question 3: State any five (5) rules of naming a variable.

1. A variable name must start with a letter or an underscore.


2. A variable name cannot start with a number.
3. A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _).
4. Variable names are case-sensitive (age, Age, and AGE are three different
variables).
5. Reserved words (such as int, float, etc.) cannot be used as variable
names.

Question 4: Define framework in Python and give two (2) examples.

A framework in Python is a collection of modules and packages that help


developers to build applications by providing a structure and common
functionalities. Examples include Django and Flask.

Question 5: List all steps followed to push and pop in a stack.

1. Push:
o Check if the stack is full.
o If not full, increment the top pointer.
o Add the element at the position pointed to by the top pointer.
2. Pop:
o Check if the stack is empty.
o If not empty, return the element at the position pointed to by the
top pointer.
o Decrement the top pointer.

Question 6: Find the output of the following program:

If the value of a=20, b=3, what will be the value of the sum?

c
Copy code
#include <stdio.h>

int main() {
int a = 20;
int b = 3;
int sum = a + b;
printf("%d", sum);
return 0;
}

Output: The value of sum is 23.

Question 7: Explain IDE in Python and give two (2) examples.

An Integrated Development Environment (IDE) is a software application that


provides comprehensive facilities to programmers for software development.
Examples include PyCharm and Jupyter Notebook.

Question 8: Explain four (4) rules for switch statement in C language.

1. The switch statement evaluates an expression.


2. The value of the expression is compared with the values of each case.
3. If there is a match, the associated block of code is executed.
4. The break statement is used to terminate a case and prevent the
execution of subsequent cases.

Question 9: Describe any two (2) main types of errors available in a


programming language.

1. Syntax errors: Errors that occur when the code violates the syntax rules
of the programming language.
2. Runtime errors: Errors that occur during the execution of a program,
often due to illegal operations such as dividing by zero.

Question 10: Write an algorithm to read n number of values in an array


and display them in reverse order.

1. Start
2. Declare an array arr and integer n
3. Read the value of n
4. For i from 0 to n-1:
o Read arr[i]
5. For i from n-1 to 0:
o Print arr[i]
6. Stop
Question 11: Write a C program that will display odd numbers from 50 to
10.

c
Copy code
#include <stdio.h>

int main() {
int i;
for(i = 50; i >= 10; i--) {
if(i % 2 != 0) {
printf("%d ", i);
}
}
return 0;
}

Question 12: Develop a C program to display the days of the week by


using switch case statement.

c
Copy code
#include <stdio.h>

int main() {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch(day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
case 3: printf("Wednesday"); break;
case 4: printf("Thursday"); break;
case 5: printf("Friday"); break;
case 6: printf("Saturday"); break;
case 7: printf("Sunday"); break;
default: printf("Invalid day number");
}
return 0;
}

Question 13: Use sum() function to develop a simple C program that will
add two numbers.

c
Copy code
#include <stdio.h>

int sum(int a, int b) {


return a + b;
}

int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum: %d", sum(a, b));
return 0;
}

Question 14: Consider the following code snippet. What are the values of i
and n after the code is executed?

c
Copy code
int i = 10;
int n = i++ % 5;

After execution:

 i is 11
 n is 0

If using prefix increment operator:

c
Copy code
int i = 10;
int n = ++i % 5;

After execution:

 i is 11
 n is 1

Question 15: Compare for loop with while loop.

 For loop: Used when the number of iterations is known beforehand. It


has an initialization, condition, and increment/decrement in one line.
 While loop: Used when the number of iterations is not known
beforehand. It only has a condition and the initialization and
increment/decrement must be handled inside the loop body.
Question 16: Refer to the table below and write an algorithm to print the
grade obtained by a student.

Marks Grades
Above 75 O
60-75 A
50-60 B
40-50 C
Less than 40 D

Algorithm:

1. Start
2. Declare integer marks
3. Read the value of marks
4. If marks > 75, print "Grade O"
5. Else if marks >= 60, print "Grade A"
6. Else if marks >= 50, print "Grade B"
7. Else if marks >= 40, print "Grade C"
8. Else, print "Grade D"
9. Stop

Question 17: Analyze the expression provided in the first column and
complete the second column of the table below.

Assume that a=5, b=10, c=15, and d=0.

Expression True or False


(a != 7) True
((c >= 15)
(d/c < (a*b)) True
(!((a+d) >= (c-b+1))) False
((c > b) && (b < a)) False

Question 18: Analyze the following program and provide its output.

c
Copy code
#include <stdio.h>

int main() {
int i;
int arr[5] = {1, 5, 9};
for (i = 0; i < 5; i++){
printf("%d ", arr[i]);
}
return 0;
}

Output: 1 5 9 0 0

Question 18b: Explain realloc and give its syntax.

realloc is used to resize a previously allocated memory block. Syntax:

c
Copy code
void *realloc(void *ptr, size_t size);

 ptr is the pointer to the memory block to be resized.


 size is the new size of the memory block.

Question 19: With the help of examples, explain function overloading in


C++.

Function overloading allows multiple functions with the same name but
different parameters to be defined. Example:

cpp
Copy code
#include <iostream>
using namespace std;

void print(int i) {
cout << "Printing int: " << i << endl;
}

void print(double f) {
cout << "Printing float: " << f << endl;
}

void print(string s) {
cout << "Printing string: " << s << endl;
}

int main() {
print(10);
print(5.5);
print("Hello");
return 0;
}

Question 20: Write a Pseudocode program of binary search algorithms.

1. Start
2. Declare array arr and integers low, high, mid, target
3. Initialize low = 0, high = n-1
4. While low <= high:
o Set mid = (low + high) / 2
o If arr[mid] == target, return mid
o Else if arr[mid] < target, set low = mid + 1
o Else, set high = mid - 1
5. If target not found, return -1
6. Stop

Question 21a

Develop a JavaScript program that will calculate the volume of a box of


length = 30, width = 50, and height = 5.

javascript
Copy code
let length = 30;
let width = 50;
let height = 5;
let volume = length * width * height;
console.log("Volume of the box is: " + volume);

Output:

yaml
Copy code
Volume of the box is: 7500

Question 21b

Describe the following programming paradigms:

i) Object-Oriented Programming (OOP):

 Definition: OOP is a programming paradigm based on the concept of


"objects," which can contain data in the form of fields (often known as
attributes or properties), and code in the form of procedures (often
known as methods).
 Key Concepts:
oEncapsulation: Bundling the data (attributes) and methods
(functions) that operate on the data into a single unit called an
object.
o Inheritance: A mechanism by which one class can inherit
properties and methods from another class.
o Polymorphism: The ability to present the same interface for
different underlying data types.
o Abstraction: Hiding the complex implementation details and
showing only the essential features of the object.
 Examples: C++, Java, Python.

ii) Functional Programming:

 Definition: Functional programming is a paradigm that treats


computation as the evaluation of mathematical functions and avoids
changing-state and mutable data.
 Key Concepts:
o First-Class Functions: Functions are treated as first-class
citizens, meaning they can be passed as arguments, returned from
other functions, and assigned to variables.
o Pure Functions: Functions that have no side effects; the output
value is only determined by its input values.
o Immutability: Data is immutable, meaning it cannot be changed
after it is created.
o Higher-Order Functions: Functions that can take other functions
as arguments or return them as results.
 Examples: Haskell, Lisp, and Python (when using functional
programming features).

You might also like