Pps 2022
Pps 2022
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;
}
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
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.