Instructions:
1. This question paper
comprises of two sections. Write answer of both the sections in separate answer
books.
2. From Section I, Q.1 is
compulsory, attempt any THREE from Q. 2 to Q. 5
3. From Section II, Q.6
is compulsory, attempt any THREE from Q. 7 to Q. 10
4. Make suitable
assumptions wherever necessary.
5. Start new question on
new page.
Section-A (30 Marks)
Q.1 Objective Type Questions - (State, Define, List, etc) (6 Marks)
- Define a flowchart.
- List different types of loops in C.
- State the difference between ‘if-else’ and ‘switch case’.
Q.2 Answer the following questions.
A) State the properties of an algorithm. (2 Marks)
B) Explain the structure of a C program. (6 Marks)
Q.3 Answer the following questions.
C) What is the role of a data type in C? (2 Marks)
D) Write a program to swap two numbers using a third variable. (6 Marks)
Q.4 Answer the following questions.
A) Define an array. (2 Marks)
B) Write a program to find the maximum of three numbers using nested if. (6 Marks)
Q.5 Answer the following questions.
A) Explain recursion. (2 Marks)
B) Write a program to generate the Fibonacci series using recursion. (6 Marks)
Section-B (30 Marks)
Q.6 Objective Type Questions - (State, Define, List, etc) (6 Marks)
- Define an algorithm.
- What is the use of the ‘for’ loop?
- State two properties of a recursive function.
Q.7 Answer the following questions.
A) List the different data types in C. (2 Marks)
B) Explain type conversion in C with examples. (6 Marks)
Q.8 Answer the following questions.
A) What is a string? (2 Marks)
B) Write a program to check whether a string is a palindrome. (6 Marks)
Q.9 Answer the following questions.
A) Define a function. (2 Marks)
B) Write a program to calculate the factorial of a number using recursion. (6 Marks)
Q.10 Answer the following questions.
A) Define matrix multiplication. (2 Marks)
B) Write a program to multiply two matrices. (6 Marks)
SOLUTIONS
Section-A (30 Marks)
Q.1 Objective Type Questions (6 Marks)
a) Define a flowchart.
A flowchart is a graphical representation of an algorithm or a process, where different operations are represented by different symbols connected by arrows, showing the flow of the process step-by-step.
b) List different types of loops in C.
for
loopwhile
loopdo-while
loop
c) State the difference between ‘if-else’ and ‘switch case’.
if-else
: It checks for conditions and executes a block of code if the condition is true. It can handle both simple and complex conditions.switch case
: It is used to select one among many code blocks based on the value of a variable. It works well for equality comparisons with discrete values.
Q.2 Answer the following questions.
A) State the properties of an algorithm. (2 Marks)
- Finiteness: The algorithm must terminate after a finite number of steps.
- Definiteness: Each step must be clear and unambiguous.
- Input: The algorithm must have well-defined inputs.
- Output: It must produce well-defined outputs.
- Effectiveness: The steps should be basic enough to be executed manually if necessary.
B) Explain the structure of a C program. (6 Marks)
- Preprocessor Directives: Contains
#include
to include libraries and #define
for macros. - Main Function: Every C program has a
main()
function where execution starts. - Variable Declarations: Declares variables before using them.
- Body: Contains the logic of the program enclosed in curly braces
{}
. - Return Statement: Returns a value to the operating system, typically
return 0;
.
Q.3 Answer the following questions.
C) What is the role of a data type in C? (2 Marks)
Data types in C define the type and size of data that can be stored in a variable, such as int
, float
, char
, etc. They help the compiler allocate memory and decide the operations that can be performed on the data.
D) Write a program to swap two numbers using a third variable. (6 Marks)
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Q.4 Answer the following questions.
A) Define an array. (2 Marks)
An array is a collection of elements of the same data type stored in contiguous memory locations, accessed using an index.
B) Write a program to find the maximum of three numbers using nested if
. (6 Marks)
#include <stdio.h>
int main() {
int a, b, c, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
if (a > c)
max = a;
else
max = c;
} else {
if (b > c)
max = b;
else
max = c;
}
printf("Maximum number is: %d\n", max);
return 0;
}
Q.5 Answer the following questions.
A) Explain recursion. (2 Marks)
Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem, with a base condition to terminate the recursive calls.
B) Write a program to generate the Fibonacci series using recursion. (6 Marks)
#include <stdio.h>
int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return (fibonacci(n - 1) + fibonacci(n - 2));
}
int main() {
int n, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
Section-B (30 Marks)
Q.6 Objective Type Questions (6 Marks)
a) Define an algorithm.
An algorithm is a step-by-step procedure or set of rules designed to perform a specific task or solve a particular problem.
b) What is the use of the for
loop?
The for
loop is used for iterating a block of code a specified number of times. It is typically used when the number of iterations is known beforehand.
c) State two properties of a recursive function.
- Base case: It must have a base condition to terminate the recursive calls.
- Recursive case: The function should call itself with a smaller problem size to move towards the base case.
Q.7 Answer the following questions.
A) List the different data types in C. (2 Marks)
int
(integer)float
(floating-point)double
(double-precision floating-point)char
(character)void
B) Explain type conversion in C with examples. (6 Marks)
Type conversion in C is converting one data type to another.
- Implicit conversion: Automatically performed by the compiler, e.g., converting
int
to float
. - Explicit conversion (casting): Done manually by the programmer, e.g.,
(float)a / b
.
Example:
#include <stdio.h>
int main() {
int a = 5;
int b = 2;
float result;
result = (float)a / b;
printf("Result = %.2f\n", result);
return 0;
}
Q.8 Answer the following questions.
A) What is a string? (2 Marks)
A string is a sequence of characters terminated by a null character (\0
).
B) Write a program to check whether a string is a palindrome. (6 Marks)
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
printf("Enter a string: ");
gets(str);
strcpy(rev, str);
strrev(rev);
if (strcmp(str, rev) == 0)
printf("The string is a palindrome.\n");
else
printf("The string is not a palindrome.\n");
return 0;
}
Q.9 Answer the following questions.
A) Define a function. (2 Marks)
A function is a block of code designed to perform a specific task, which can be called by its name when needed.
B) Write a program to calculate the factorial of a number using recursion. (6 Marks)
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
Q.10 Answer the following questions.
A) Define matrix multiplication. (2 Marks)
Matrix multiplication is an operation that takes two matrices and produces a third matrix, where each element of the resulting matrix is calculated as the dot product of the corresponding row and column elements.
B) Write a program to multiply two matrices. (6 Marks)
#include <stdio.h>
int main() {
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
if (c1 != r2) {
printf("Matrix multiplication not possible.\n");
return 0;
}
printf("Enter elements of first matrix:\n");
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
scanf("%d", &a[i][j]);
printf("Enter elements of second matrix:\n");
for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
scanf("%d", &b[i][j]);
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
result[i][j] = 0;
for (k = 0; k < c1; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Resultant matrix:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
No comments:
Post a Comment