0% found this document useful (0 votes)
13 views13 pages

Pps 2022

The document provides answers to various programming and computer science questions, including the use of loops, string functions, sorting algorithms, and data structures like arrays and structures. It also includes programming examples for finding roots of quadratic equations, calculating factorials, and checking if numbers are even or odd. Additionally, it discusses the differences between structures and unions in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views13 pages

Pps 2022

The document provides answers to various programming and computer science questions, including the use of loops, string functions, sorting algorithms, and data structures like arrays and structures. It also includes programming examples for finding roots of quadratic equations, calculating factorials, and checking if numbers are even or odd. Additionally, it discusses the differences between structures and unions in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Here are the answers to the questions from the provided document:

Group-A (Very Short Answer Type Question)


1.​ Answer any ten of the following:
○​ (I) What is the use of continue in loop? The continue statement in a loop is used
to skip the rest of the current iteration and proceed to the next iteration of the loop.
○​ (II) What is the string function used to reverse a string? The strrev() function is
used to reverse a string.
○​ (III) What is the time complexity of Bubble sort? The time complexity of Bubble
sort is O(n^2).
○​ (IV) What are preprocessor? Processors are programs that process source code
before compilation.
○​ (V) Recursion uses which data structure? Recursion uses the stack data
structure.
○​ (VI) What header file is used for string operation? The <string.h> header file is
used for string operations.
○​ (VII) What is nested if-else? Nested if-else refers to an if-else statement inside
another if-else statement.
○​ (VIII) What is the use of goto statement? The goto statement is used to transfer
control of the program to a labeled statement.
○​ (IX) What is linear searching in an array? Linear searching in an array is a
method for finding an element within an array by sequentially checking each
element until a match is found or the whole array has been searched.
○​ (X) What is Macro? A macro is a piece of code in a program that is replaced by the
preprocessor with the content of the macro.
○​ (XI) What is the value of the following if a=2 ? printf("%d",--a); The value will be
1. (The --a is a pre-decrement operator, so a becomes 1, and then its value is
printed).
○​ (XII) What is goto statement in C? The goto statement in C is a jump statement
that is used to transfer control from one part of the program to another.
Group-B (Short Answer Type Question)
Answer any three of the following:
1.​ What is structure? What Is array of structures? Show with an example.
○​ A structure in C is a user-defined data type that allows you to combine different
data types into a single unit. It's a way to create a record.
○​ An array of structures is a collection of structures of the same type. Each element
in the array is a structure, allowing you to store multiple records efficiently.
○​ Example:​
#include <stdio.h>​

// Define a structure for a student​
struct Student {​
char name[50];​
int roll;​
float marks;​
};​

int main() {​
// Declare an array of structures for 3 students​
struct Student students[3];​

// Assign values to the first student​
strcpy(students[0].name, "Alice");​
students[0].roll = 101;​
students[0].marks = 85.5;​

// Assign values to the second student​
strcpy(students[1].name, "Bob");​
students[1].roll = 102;​
students[1].marks = 92.0;​

// Assign values to the third student​
strcpy(students[2].name, "Charlie");​
students[2].roll = 103;​
students[2].marks = 78.9;​

// Print the details of all students​
for (int i = 0; i < 3; i++) {​
printf("Student Name: %s, Roll: %d, Marks: %.2f\n",​
students[i].name, students[i].roll,
students[i].marks);​
}​

return 0;​
}​

2.​ Write a program to find roots of a quadratic equation.​


#include <stdio.h>​
#include <math.h>​

int main() {​
double a, b, c, discriminant, root1, root2, realPart,
imagPart;​

printf("Enter coefficients a, b and c: ");​
scanf("%lf %lf %lf", &a, &b, &c);​

discriminant = b * b - 4 * a * c;​

// condition for real and different roots​
if (discriminant > 0) {​
root1 = (-b + sqrt(discriminant)) / (2 * a);​
root2 = (-b - sqrt(discriminant)) / (2 * a);​
printf("root1 = %.2lf and root2 = %.2lf\n", root1, root2);​
}​
// condition for real and equal roots​
else if (discriminant == 0) {​
root1 = root2 = -b / (2 * a);​
printf("root1 = root2 = %.2lf\n", root1);​
}​
// if roots are not real​
else {​
realPart = -b / (2 * a);​
imagPart = sqrt(-discriminant) / (2 * a);​
printf("root1 = %.2lf+%.2lfi and root2 = %.2lf-%.2lfi\n",
realPart, imagPart, realPart, imagPart);​
}​

return 0;​
}​

3.​ Draw flowchart to evaluate factorial of a numbers.​


graph TD​
A[Start] --> B[Read Number N];​
B --> C{N < 0?};​
C -- Yes --> D[Print "Factorial not defined for negative
numbers"];​
C -- No --> E[Initialize Factorial = 1, i = 1];​
E --> F{i <= N?};​
F -- Yes --> G[Factorial = Factorial * i];​
G --> H[i = i + 1];​
H --> F;​
F -- No --> I[Print Factorial];​
I --> J[End];​
D --> J;​

4.​ What is conditional operator? The conditional operator (also known as the ternary
operator) is a shorthand way to write an if-else statement. It takes three operands and
evaluates to one of two expressions based on a condition. The syntax is: condition ?
expression_if_true : expression_if_false;
5.​ What will be output of the code below?​
#include <stdio.h>​
int main()​
{​
int a[5]={1,2,3,4,5};​
int i;​
for ( (i=0;i<5;i++))​
if ( ((char)a[i]=='5'))​
printf("96d\n",a[i]);​
else​
printf("FAIL\n");​
}​
Output:​
FAIL​
FAIL​
FAIL​
FAIL​
FAIL​
Explanation: The condition ((char)a[i]=='5') is the key.
○​ a[i] is an integer.
○​ (char)a[i] casts the integer value to its ASCII character equivalent.
○​ '5' is the ASCII character '5'.
○​ The integer values 1, 2, 3, 4, 5 do not correspond to the ASCII value of the
character '5'. The ASCII value of '5' is 53. Since none of the elements in the array a
have an integer value of 53, the condition ((char)a[i]=='5') will always be false,
leading to "FAIL" being printed for every element.
Group-C (Long Answer Type Question)
Answer any three of the following:
1.​ (a) What is the difference between Array and Structure? Show with example.
○​ Array:
■​ An array is a collection of elements of the same data type.
■​ Elements are stored in contiguous memory locations.
■​ Accessed using an index (e.g., arr[0]).
■​ Used for storing lists of similar items.
○​ Structure:
■​ A structure is a collection of elements (members) that can be of different data
types.
■​ Members are stored in contiguous memory locations, but padding might
occur.
■​ Accessed using a member access operator (.) (e.g., student.name).
■​ Used for creating a single record that groups related data of various types.
Example:#include <stdio.h>​
#include <string.h>​

// Example of an Array​
void arrayExample() {​
int numbers[3] = {10, 20, 30}; // Array of integers​
printf("Array Example:\n");​
for (int i = 0; i < 3; i++) {​
printf("numbers[%d] = %d\n", i, numbers[i]);​
}​
}​

// Example of a Structure​
struct Person {​
char name[50];​
int age;​
float height;​
};​

void structureExample() {​
struct Person p1; // Variable of type struct Person​
strcpy(p1.name, "John Doe");​
p1.age = 30;​
p1.height = 1.75;​

printf("\nStructure Example:\n");​
printf("Person Name: %s\n", p1.name);​
printf("Person Age: %d\n", p1.age);​
printf("Person Height: %.2f\n", p1.height);​
}​

int main() {​
arrayExample();​
structureExample();​
return 0;​
}​
(b) What are the differences between Structure and Union? Show with
example.
○​ Structure:
■​ Members of a structure are allocated separate memory locations.
■​ All members can be accessed simultaneously.
■​ The total memory occupied by a structure is the sum of the memory occupied
by all its members (plus any padding).
○​ Union:
■​ Members of a union share the same memory location. Only one member can
hold a value at any given time.
■​ Accessing one member when another has been recently assigned a value
might lead to unexpected results (data corruption for the previous member).
■​ The total memory occupied by a union is equal to the size of its largest
member.
Example:#include <stdio.h>​
#include <string.h>​

// Example of a Structure​
struct StudentStruct {​
int id;​
char grade;​
float percentage;​
};​

// Example of a Union​
union StudentUnion {​
int id;​
char grade;​
float percentage;​
};​

int main() {​
printf("Structure Example:\n");​
struct StudentStruct s_struct;​
s_struct.id = 1;​
s_struct.grade = 'A';​
s_struct.percentage = 90.5;​

printf("Struct ID: %d, Grade: %c, Percentage: %.2f\n",
s_struct.id, s_struct.grade, s_struct.percentage);​
printf("Size of StudentStruct: %zu bytes\n", sizeof(struct
StudentStruct)); // Will be sum of sizes (e.g., 4+1+4 = 9, with
padding might be 12)​

printf("\nUnion Example:\n");​
union StudentUnion s_union;​
s_union.id = 2;​
printf("Union ID: %d\n", s_union.id);​

s_union.grade = 'B'; // id will be overwritten​
printf("Union Grade: %c\n", s_union.grade);​
// printf("Union ID after grade assignment: %d\n",
s_union.id); // This might show garbage or the ASCII value of 'B'​

s_union.percentage = 85.0; // grade will be overwritten​
printf("Union Percentage: %.2f\n", s_union.percentage);​
// printf("Union Grade after percentage assignment: %c\n",
s_union.grade); // This will show garbage​

printf("Size of StudentUnion: %zu bytes\n", sizeof(union
StudentUnion)); // Will be size of largest member (float, usually
4 bytes)​

return 0;​
}​
(c) Write a program using structure to take 10 students name, roll, marks and print
the records with average marks.#include <stdio.h>​
#include <string.h>​

// Define the structure for a student​
struct Student {​
char name[50];​
int roll;​
float marks;​
};​

int main() {​
struct Student students[10]; // Array of 10 student structures​
float totalMarks = 0.0;​
int i;​

// Input data for 10 students​
printf("Enter details for 10 students:\n");​
for (i = 0; i < 10; i++) {​
printf("\nStudent %d:\n", i + 1);​
printf("Enter Name: ");​
scanf("%s", students[i].name);​
printf("Enter Roll Number: ");​
scanf("%d", &students[i].roll);​
printf("Enter Marks: ");​
scanf("%f", &students[i].marks);​
totalMarks += students[i].marks;​
}​

// Print the records​
printf("\n--- Student Records ---\n");​
for (i = 0; i < 10; i++) {​
printf("Name: %s, Roll: %d, Marks: %.2f\n",
students[i].name, students[i].roll, students[i].marks);​
}​

// Calculate and print average marks​
float averageMarks = totalMarks / 10.0;​
printf("\nAverage Marks of all students: %.2f\n",
averageMarks);​

return 0;​
}​

2.​ (a) Write a program to check a number is even or odd.​


#include <stdio.h>​

int main() {​
int num;​
printf("Enter an integer: ");​
scanf("%d", &num);​

if (num % 2 == 0) {​
printf("%d is an even number.\n", num);​
} else {​
printf("%d is an odd number.\n", num);​
}​
return 0;​
}​
(b) Write a program to check a number is prime or not.​
#include <stdio.h>​

int main() {​
int n, i, flag = 0;​
printf("Enter a positive integer: ");​
scanf("%d", &n);​

// 0 and 1 are not prime numbers​
if (n == 0 || n == 1)​
flag = 1;​

for (i = 2; i <= n / 2; ++i) {​
if (n % i == 0) {​
flag = 1;​
break;​
}​
}​

if (flag == 0)​
printf("%d is a prime number.\n", n);​
else​
printf("%d is not a prime number.\n", n);​

return 0;​
}​
(c) Add all even numbers from an array.​
#include <stdio.h>​

int main() {​
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};​
int size = sizeof(arr) / sizeof(arr[0]);​
int sumOfEvens = 0;​

printf("Array elements: ");​
for (int i = 0; i < size; i++) {​
printf("%d ", arr[i]);​
}​
printf("\n");​

for (int i = 0; i < size; i++) {​
if (arr[i] % 2 == 0) {​
sumOfEvens += arr[i];​
}​
}​

printf("Sum of even numbers in the array: %d\n", sumOfEvens);​
return 0;​
}​

3.​ (a) What is an Operating system? An Operating System (OS) is system software that
manages computer hardware and software resources and provides common services for
computer programs. It acts as an intermediary between the user and the computer
hardware.(b) What are the functions of operation system? The main functions of an
operating system include:
○​ Memory Management: Manages the primary and secondary memory, allocating
and deallocating memory space to programs.
○​ Process Management: Handles the creation, scheduling, and termination of
processes.
○​ Device Management: Manages all I/O devices, allocating them to processes as
needed.
○​ File Management: Organizes and manages files and directories on storage
devices.
○​ Security: Protects the system from unauthorized access.
○​ Error Detection: Monitors for and handles system errors.
○​ Job Accounting: Keeps track of time and resources used by various users and
jobs.
(c) What is booting? Booting is the process of starting a computer. It involves loading the
operating system from secondary storage (like a hard drive) into the computer's main
memory (RAM) so that the computer can function.(d) What are the differences between
compiler and Interpreter?
Feature Compiler Interpreter
Process Translates the entire source Translates and executes the
code into machine code at source code line by line.
once.
Execution Generates an executable file Executes directly without
(e.g., .exe). Execution generating an intermediate
happens after compilation. executable file.
Error Detection Shows all errors after Stops execution at the first
compilation, before error encountered.
execution.
Speed Generally faster execution Generally slower execution
once compiled. due to line-by-line translation.
Memory Requires more memory Requires less memory as it
during compilation. processes line by line.
Example C, C++, Java (uses a Python, JavaScript, PHP
compiler to bytecode)
4.​ (a) Write a program to print diagonal matrix from 5\times5 matrix. This problem
usually refers to printing the main diagonal elements. If it means printing the matrix with
only diagonal elements and others as zero, that's a different interpretation. Assuming
printing main diagonal elements.​
#include <stdio.h>​

int main() {​
int matrix[5][5] = {​
{1, 2, 3, 4, 5},​
{6, 7, 8, 9, 10},​
{11, 12, 13, 14, 15},​
{16, 17, 18, 19, 20},​
{21, 22, 23, 24, 25}​
};​

printf("Main Diagonal Elements of the 5x5 Matrix:\n");​
for (int i = 0; i < 5; i++) {​
printf("%d ", matrix[i][i]);​
}​
printf("\n");​

return 0;​
}​
If the question implies printing the matrix such that only diagonal elements are shown and
others are zero:​
#include <stdio.h>​

int main() {​
int matrix[5][5] = {​
{1, 2, 3, 4, 5},​
{6, 7, 8, 9, 10},​
{11, 12, 13, 14, 15},​
{16, 17, 18, 19, 20},​
{21, 22, 23, 24, 25}​
};​

printf("Diagonal Matrix (other elements zero):\n");​
for (int i = 0; i < 5; i++) {​
for (int j = 0; j < 5; j++) {​
if (i == j) {​
printf("%d ", matrix[i][j]);​
} else {​
printf("0 ");​
}​
}​
printf("\n");​
}​
return 0;​
}​
(b) Write a program to concatenate two strings without using strcat() function in C.​
#include <stdio.h>​
#include <string.h> // For strlen​

int main() {​
char str1[100]; // Make sure str1 has enough space for
concatenation​
char str2[50];​

printf("Enter first string: ");​
fgets(str1, sizeof(str1), stdin);​
str1[strcspn(str1, "\n")] = 0; // Remove trailing newline​

printf("Enter second string: ");​
fgets(str2, sizeof(str2), stdin);​
str2[strcspn(str2, "\n")] = 0; // Remove trailing newline​

int i = 0, j = 0;​

// Find the end of the first string​
while (str1[i] != '\0') {​
i++;​
}​

// Append the second string to the first​
while (str2[j] != '\0') {​
str1[i] = str2[j];​
i++;​
j++;​
}​
str1[i] = '\0'; // Null-terminate the concatenated string​

printf("Concatenated string: %s\n", str1);​

return 0;​
}​
(c) Write a program to sort n elements from an array. This example uses Bubble Sort,
a common and easy-to-understand sorting algorithm.​
#include <stdio.h>​

int main() {​
int n;​
printf("Enter the number of elements: ");​
scanf("%d", &n);​

int arr[n]; // Declare array of size n​

printf("Enter %d integers:\n", n);​
for (int i = 0; i < n; i++) {​
scanf("%d", &arr[i]);​
}​

// Bubble Sort algorithm​
for (int i = 0; i < n - 1; i++) {​
for (int j = 0; j < n - i - 1; j++) {​
if (arr[j] > arr[j+1]) {​
// Swap elements​
int temp = arr[j];​
arr[j] = arr[j+1];​
arr[j+1] = temp;​
}​
}​
}​

printf("Sorted array in ascending order:\n");​
for (int i = 0; i < n; i++) {​
printf("%d ", arr[i]);​
}​
printf("\n");​

return 0;​
}​

5.​ (a) Convert Binary to Decimal of (1101.01)2 To convert (1101.01)_2 to decimal: For the
integer part (1101)_2: 1 \times 2^3 + 1 \times 2^2 + 0 \times 2^1 + 1 \times 2^0 = 1 \times
8 + 1 \times 4 + 0 \times 2 + 1 \times 1 = 8 + 4 + 0 + 1 = 13For the fractional part (.01)_2:
0 \times 2^{-1} + 1 \times 2^{-2} = 0 \times 0.5 + 1 \times 0.25 = 0 + 0.25 = 0.25Combining
both parts: 13 + 0.25 = (13.25)_{10}Therefore, (1101.01)_2 = (13.25)_{10}.(b) What is 2's
Complement? Show by example. 2's Complement is a mathematical operation on
binary numbers, and it is the most common method of representing signed (positive and
negative) integers in computers. It's used for performing arithmetic operations, especially
subtraction, by converting it into an addition problem.To find the 2's complement of a
binary number:
1.​ Find the 1's complement: Invert all the bits (change 0s to 1s and 1s to 0s).
2.​ Add 1 to the 1's complement: Add 1 to the least significant bit (rightmost bit) of
the 1's complement result.
Example: Find the 2's complement of (0101)_2 (which is +5 in decimal for a 4-bit
system).
1.​ Binary number: 0101
2.​ 1's complement: Invert bits: 1010
3.​ Add 1 to 1's complement:​
1010​
+ 0001​
-------​
1011​

So, the 2's complement of 0101_2 is 1011_2. In a 4-bit signed representation,


1011_2 represents -5.Example 2: Find the 2's complement of (1010)_2 (which is -6
in decimal for an 8-bit system if it's already in 2's complement, let's assume this is
the positive number +10 in 8 bits for demonstration): Assume we want to find the
2's complement of +10 which is 00001010_2 (8-bit).
1.​ Binary number: 00001010
2.​ 1's complement: 11110101
3.​ Add 1 to 1's complement:​
11110101​
+ 00000001​
-----------​
11110110​
So, 11110110_2 represents -10 in 2's complement for an 8-bit system.(c) Using 2's
Complement do 10101_2 - 1101_2 To perform subtraction using 2's complement,
we convert the subtraction into addition. A - B = A + (-B) Here, A = (10101)_2 and B
= (1101)_2. We need to find the 2's complement of B, i.e., -B. First, ensure both
numbers have the same number of bits. A has 5 bits, B has 4 bits. Let's extend B to
5 bits: 01101_2.
1.​ Find the 1's complement of B = (01101)_2: Invert bits: 10010_2
2.​ Find the 2's complement of B (add 1 to 1's complement): 10010_2 + 1_2 =
10011_2 So, (-B) = (10011)_2.
3.​ Add A and the 2's complement of B:​
10101 (A)​
+ 10011 (-B in 2's complement)​
-------​
101000​

Since we are working with 5-bit numbers, an overflow (the leftmost 1) occurs. In 2's complement
arithmetic, for addition, if a carry is generated out of the most significant bit, it is
discarded.Discarding the carry bit, the result is (01000)_2.Let's verify in decimal: (10101)_2 = 1
\times 2^4 + 0 \times 2^3 + 1 \times 2^2 + 0 \times 2^1 + 1 \times 2^0 = 16 + 0 + 4 + 0 + 1 =
21_{10} (1101)_2 = 1 \times 2^3 + 1 \times 2^2 + 0 \times 2^1 + 1 \times 2^0 = 8 + 4 + 0 + 1 =
13_{10} 21 - 13 = 8_{10}(01000)_2 = 0 \times 2^4 + 1 \times 2^3 + 0 \times 2^2 + 0 \times 2^1 +
0 \times 2^0 = 0 + 8 + 0 + 0 + 0 = 8_{10} The result matches.Therefore, (10101)_2 - (1101)_2 =
(01000)_2.(d) Convert Decimal to Octal (5673)_{10} To convert a decimal number to octal,
repeatedly divide the decimal number by 8 and record the remainders. The octal number is
formed by reading the remainders from bottom to top.5673 \div 8 = 709 remainder 1 709 \div 8 =
88 remainder 5 88 \div 8 = 11 remainder 0 11 \div 8 = 1 remainder 3 1 \div 8 = 0 remainder
1Reading the remainders from bottom to top: 13051.Therefore, (5673)_{10} = (13051)_8.

You might also like