PSC Sessional Question Bank Solution
PSC Sessional Question Bank Solution
1. ARITHMETIC OPERATORS
THESE OPERATORS ARE USED TO PERFORM BASIC ARITHMETIC OPERATIONS .
OPERATO EXAMPL
DESCRIPTION RESULT
R E
GIVES THE SUM OF A AND
+ ADDITION A + B
B
DIFFERENCE BETWEEN A
- SUBTRACTIONA - B
AND B
MULTIPLICATI
* A * B PRODUCT OF A AND B
ON
QUOTIENT OF A DIVIDED
/ DIVISION A / B
BY B
REMAINDER OF A DIVIDED
% MODULUS A % B
BY B
2. RELATIONAL OPERATORS
THESE OPERATORS COMPARE TWO VALUES .
OPERATO EXAMPL
DESCRIPTION RESULT
R E
== EQUAL TO A == B TRUE IF A EQUALS B
!= NOT EQUAL TO A != B TRUE IF A IS NOT EQUAL TO B
> GREATER THAN A >B TRUE IF A IS GREATER THAN B
< LESS THAN A <B TRUE IF A IS LESS THAN B
GREATER THAN OR TRUE IF A IS GREATER THAN OR
>= A >= B
EQUAL TO EQUAL TO B
LESS THAN OR EQUAL TRUE IF A IS LESS THAN OR EQUAL
<= A <= B
TO TO B
3. LOGICAL OPERATORS
THESE OPERATORS ARE USED TO PERFORM LOGICAL OPERATIONS .
OPERATODESCRIPTIOEXAMPL
RESULT
R N E
LOGICAL TRUE IF BOTH A AND B
&& A && B
AND ARE TRUE
TRUE IF EITHER A OR
` ` LOGICAL OR `A B`
B IS TRUE
LOGICAL
! !A TRUE IF A IS FALSE
NOT
4. ASSIGNMENT OPERATORS
THESE OPERATORS ARE USED TO ASSIGN VALUES TO VARIABLES .
OPERATO EXAMPL
DESCRIPTION EXPLANATION
R E
= ASSIGNMENT A = 10 ASSIGNS VALUE 10 TO A
ADDITION ADDS 10 TO A AND ASSIGNS THE RESULT
+= A += 10
ASSIGNMENT TO A
SUBTRACTION SUBTRACTS 10 FROM A AND ASSIGNS
-= A -= 10
ASSIGNMENT THE RESULT TO A
MULTIPLICATION MULTIPLIES A BY 10 AND ASSIGNS THE
*= A *= 10
ASSIGNMENT RESULT TO A
DIVISION DIVIDES A BY 10 AND ASSIGNS THE
/= A /= 10
ASSIGNMENT RESULT TO A
MODULUS A %=FINDS THE REMAINDER OF A DIVIDED BY
%=
ASSIGNMENT 10 10 AND ASSIGNS IT TO A
5. BITWISE OPERATORS
THESE OPERATORS ARE USED TO PERFORM BIT -LEVEL OPERATIONS .
OPERATODESCRIPTIO
EXAMPLERESULT
R N
BITWISE BITS THAT ARE 1 IN BOTH
& A & B
AND A AND B BECOME 1
BITS THAT ARE 1 IN
BITWISE
` ` `A B`EITHER A OR B
OR
BECOME 1
BITS THAT ARE 1 IN A OR
BITWISE
^ A ^ B B, BUT NOT BOTH , BECOME
XOR
1
BITWISE ALL BITS IN A ARE
~ ~A
NOT INVERTED
OPERATODESCRIPTIO
EXAMPLERESULT
R N
SHIFTS A'S BITS TO THE
<< LEFT SHIFTA << 2
LEFT BY 2 PLACES
RIGHT SHIFTS A'S BITS TO THE
>> A >> 2
SHIFT RIGHT BY 2 PLACES
6. MISCELLANEOUS OPERATORS
OPERATO
DESCRIPTION EXAMPLE
R
RETURNS THE SIZE OF
SIZEOF SIZEOF (A)
A VARIABLE
& ADDRESS OF &A (RETURNS THE ADDRESS OF VARIABLE A)
* POINTER *PTR (POINTER TO A VARIABLE)
(A > B) ? A : B (RETURNS A IF THE CONDITION
?: TERNARY OPERATOR
IS TRUE, OTHERWISE RETURNS B)
int main() {
int num1, num2;
return 0;
}
Q2a Explain Operator Precedence and Associativity.
ANS Operator Precedence and Associativity
Operator precedence and associativity are rules that determine the order
in which different operators in an expression are evaluated.
Operator Precedence:
Operator precedence defines the order in which operators are evaluated
in expressions. Operators with higher precedence are evaluated before
operators with lower precedence.
Q2b Develop a C Program to print the area and circumference of a Circle.
ANS #include <stdio.h>
#define PI 3.14159
int main() {
float radius, area, circumference;
return 0;
}
Q5a What are the different types of loops used in C? Explain pre tested
and post-tested loops with suitable examples
ANS Types of Loops in C
1. for Loop: Repeats a block of code a specific number of times.
c
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("i = %d\n", i);
}
return 0;
}
2. while Loop: Repeats a block of code while a condition is true (pre-tested
loop).
c
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("i = %d\n", i);
i++;
}
return 0;
}
3. do-while Loop
The do-while loop is a post-tested loop, meaning the loop body is executed
at least once before the condition is tested. This is useful when you want
the loop to execute at least once regardless of the condition.
Syntax:
c
do {
// Code to be executed
} while (condition);
Example:
c
#include <stdio.h>
int main() {
int i = 0;
do {
printf("i = %d\n", i);
i++;
} while (i < 5);
return 0
Q5b Develop a C Program to implement a simple calculator to perform
addition, subtraction, multiplication and division operations using switch
construct. Display appropriate messages for invalid operator and divide by
zero error
ANS #include <stdio.h>
int main() {
char operator;
double num1, num2, result;
return 0;
}
Q6a Explain switch statement with example
ANS Certainly! The switch statement in C is used to execute one block of
code among many options based on the value of an expression or variable.
It's like an advanced form of the if-else ladder, which makes the code more
readable and easier to manage when dealing with multiple conditions.
Example
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
return 0;
}
Q6b Develop a C Program to find the roots of quadratic equation for non-
zero co- efficient using if-else ladder construct.
Ans #include <stdio.h>
#include <math.h>
int main() {
int a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
// Calculating discriminant
discriminant = b * b - 4 * a * c;
return 0;
}
Q9a What is 2D array? How two dimension arrays is declared and
initialized?
ANS A two-dimensional array, often known as a 2D array, is an array of
arrays. It can be visualized as a matrix or a table where the elements are
arranged in rows and columns.
Declaration of 2D Array
To declare a 2D array, you specify the data type, the name of the array,
and the number of rows and columns.
Syntax:
type arrayName[rows][columns];
Initialization of 2D Array
You can initialize a 2D array at the time of declaration by providing the
values in a nested curly braces format.
Example
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
}; // Initializes a 3x4 matrix with specific values
Q9b
Algorithm for Selection Sort
1. Start with the first element as the minimum.
2. Compare the current minimum with the next element.
3. If the next element is smaller, update the minimum.
4. Swap the first element with the minimum element.
5. Repeat the process for the subsequent elements.
Selection Sort Program in C
#include <stdio.h>
int main() {
int n, i, j, minIndex, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
Q10a Explain searching and sorting with example
ANS Searching
Searching is finding a specific element in a collection of elements.
1. Linear Search
Linear search checks each element one by one until the desired element is
found or the end of the array is reached.
Example: #include <stdio.h>
int main() {
int arr[] = {4, 2, 7, 1, 9};
printf("Element found at index %d\n", linearSearch(arr, 5, 7));
return 0;
}
2. Binary Search
Binary search is efficient but requires a sorted array. It divides the search
interval in half repeatedly.
Example: #include <stdio.h>
int binarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) return mid; // Found
if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1; // Not found
}
int main() {
int arr[] = {1, 2, 4, 7, 9};
printf("Element found at index %d\n", binarySearch(arr, 5, 7));
return 0;
}
Sorting
Sorting arranges the elements in a specific order, usually ascending or
descending.
1. Bubble Sort
Bubble sort repeatedly compares and swaps adjacent elements to sort the
array.
Example: #include <stdio.h>
int main() {
int arr[] = {4, 2, 7, 1, 9};
bubbleSort(arr, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
2. Insertion Sort
Insertion sort builds the sorted array one item at a time by repeatedly
inserting the current item into its correct position.
Example: #include <stdio.h>
int main() {
int arr[] = {4, 2, 7, 1, 9};
insertionSort(arr, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Q10b
Ans #include <stdio.h>
int main() {
int matrix[3][3], transpose[3][3];
2. STRCAT()
CONCATENATES (APPENDS) ONE STRING TO THE END OF ANOTHER.
SYNTAX:
CHAR STR1[100] = "H ELLO, ", STR2[] = "W ORLD!";
STRCAT (STR1, STR2); // STR1 NOW CONTAINS "H ELLO, W ORLD!"
3. STRLEN()
RETURNS THE LENGTH OF A STRING.
SYNTAX:
CHAR STR[] = "H ELLO";
SIZE _T LEN = STRLEN (STR); // LEN IS 5
4. STRCMP()
COMPARES TWO STRINGS LEXICOGRAPHICALLY .
SYNTAX:
CHAR STR1[] = "H ELLO", STR2[] = "W ORLD";
INT RESULT = STRCMP (STR1, STR2); // RESULT IS NEGATIVE SINCE "HELLO" <
"WORLD"
5. STRNCPY()
COPIES A SPECIFIED NUMBER OF CHARACTERS FROM ONE STRING TO ANOTHER .
SYNTAX:
CHAR STR1[100], STR2[100] = "H ELLO";
STRNCPY (STR1, STR2, 3); // STR1 NOW CONTAINS "H EL"
6. STRNCAT()
APPENDS A SPECIFIED NUMBER OF CHARACTERS FROM ONE STRING TO
ANOTHER .
SYNTAX:
CHAR STR1[100] = "H ELLO", STR2[] = "W ORLD";
STRNCAT (STR1, STR2, 3); // STR1 NOW CONTAINS "H ELWOR"
7. STRNCMP()
COMPARES A SPECIFIED NUMBER OF CHARACTERS FROM TWO STRINGS.
SYNTAX:
CHAR STR1[] = "H ELLO", STR2[] = "H ELLY ";
INT RESULT = STRNCMP (STR1, STR2, 4); // RESULT IS ZERO SINCE FIRST 4
CHARACTERS ARE THE SAME
8. STRTOK()
SPLITS A STRING INTO TOKENS BASED ON A DELIMITER.
SYNTAX:
CHAR STR[] = "H ELLO,WORLD,HERE";
CHAR* TOKEN = STRTOK(STR, ",");// TOKEN NOW CONTAINS "HELLO".
SUBSEQUENT CALLS SPLIT THE REST.
9. STRCHR()
SEARCHES FOR THE FIRST OCCURRENCE OF A CHARACTER IN A STRING .
SYNTAX:
CHAR STR[] = "H ELLO";
CHAR* PTR = STRCHR(STR, 'E'); // PTR POINTS TO "ELLO"
10. STRRCHR()
SEARCHES FOR THE LAST OCCURRENCE OF A CHARACTER IN A STRING .
SYNTAX:
CHAR STR[] = "H ELLO WORLD";
CHAR* PTR = STRRCHR (STR, 'O'); // PTR POINTS TO "ORLD"
Q13b What is a pointer? Explain how the pointer variable declared and
initialized with example.
Declaring and Initializing a Pointer
Step 1: Declaration
You declare a pointer using an asterisk (*) before the pointer variable's
name.
int *ptr; // Declares a pointer to an integer
Step 2: Initialization
You initialize the pointer by assigning it the address of a variable. The
address-of operator & is used to get the address of a variable.
int value = 42; // A normal integer variable
int *ptr = &value; // Pointer variable ptr stores the address of value
int main() {
// Create the first node
struct Node *head = createNode(1);
// Create the second node and link it to the first node
head->next = createNode(2);
// Create the third node and link it to the second node
head->next->next = createNode(3);
return 0;
}
Q14b
Ans Using Inbuilt Function: strcmp
Here's a short program that uses the inbuilt strcmp function to compare
two strings:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "hello";
char str2[] = "world";
if (strcmp(str1, str2) == 0) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}
return 0;
}
Without Using String Function
Now, let's compare two strings without using any string functions.
#include <stdio.h>
int main() {
char str1[] = "hello";
char str2[] = "world";
if (compareStrings(str1, str2) == 0) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}
return 0;
}
Q17a Explain in brief 1) Call by value 2) Call by reference
Ans CALL BY VALUE
DEFINITION: IN CALL BY VALUE , A COPY OF THE ACTUAL PARAMETER 'S VALUE IS
PASSED TO THE FUNCTION . THE FUNCTION OPERATES ON THIS COPY, SO ANY
CHANGES MADE TO THE PARAMETER INSIDE THE FUNCTION DO NOT AFFECT THE
ORIGINAL VALUE .
EXAMPLE:
C
#INCLUDE <STDIO.H>
INT MAIN() {
INT X = 10;
MODIFY VALUE (X);
PRINTF ("X = %D\N", X); // O UTPUT: X = 10 (REMAINS UNCHANGED )
RETURN 0;
}
CALL BY REFERENCE
DEFINITION: IN CALL BY REFERENCE , A REFERENCE (OR POINTER ) TO THE
ACTUAL PARAMETER IS PASSED TO THE FUNCTION . THE FUNCTION OPERATES
ON THE ADDRESS OF THE PARAMETER , SO ANY CHANGES MADE TO THE
PARAMETER INSIDE THE FUNCTION DIRECTLY AFFECT THE ORIGINAL VALUE .
EXAMPLE:
C
#INCLUDE <STDIO.H>
Q17b
Ans Recursion is a programming technique where a function calls itself to
solve smaller instances of the same problem until it reaches a base case.
It's like solving a big problem by breaking it down into simpler, smaller
problems.
Example: Finding Factorial Using Recursion
The factorial of a number nn is the product of all positive integers less
than or equal to nn. It's denoted as n!n!.
Factorial Calculation:
Base Case: 0!=10! = 1 and 1!=11! = 1
Recursive Case: n!=n×(n−1)!
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num); // Read the input number
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d is %d\n", num, factorial(num));
}
return 0;
}
Q18a Explain in brief Parameter passing to function.
Ans Pass by Value
When you pass parameters by value, you are sending a copy of the actual
parameter's value. Changes made to the parameter inside the function do
not affect the original value.
void passByValue(int num) {
num = 10; // Changes won't affect the original parameter
}
int main() {
int value = 5;
passByValue(value);
// value is still 5 after the function call
return 0;
}
Pass by Reference
When you pass parameters by reference, you are sending the address of
the parameter. This allows the function to modify the original variable's
value.
void passByReference(int *num) {
*num = 10; // Changes will affect the original parameter
}
int main() {
int value = 5;
passByReference(&value);
// value is now 10 after the function call
return 0;
}
Q18b Develop a c-program to print Fibonacci series first 10 number using
recursion.
Ans #include <stdio.h>
int main() {
int n = 10; // Number of Fibonacci numbers to print
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n"); // Print a new line at the end
return 0;
}