PPS - Winter 2022 Paper Solution
PPS - Winter 2022 Paper Solution
: ________
jagrutawaaz.com Enrolment No.___________
Q.2 (a) Demonstrate the working of bitwise shift and sizeof operator with an 03
example.
(b) Differentiate while and do..while loop. 04
(c) Write a C program to check whether the entered character is alphabet, 07
z
digit, space or any special character.
OR aa
(c) Write a C program to print following pattern: 07
1
Aw
22
333
4444
ut
Q.3 (a) Find out error(s), if any in the following code and correct it: 03
int main() {
gr
if(10%2==0){
break;
Ja
}
}
(b) Examine following code and give output of it. 04
void main(){
int a=0;
while (a<10) {
a++;
if (a%3==1) {
continue;
}
printf (" %d", a);
}}
(c) Write a C program to find factorial of a given number. 07
OR
Q.3 (a) Find out error(s), if any in the following code and correct it: 03
int main() {
printf(“%f %d”, 7.0%5.0, 5 || -2);
}
(b) Examine following code and give output of it. 04
void main(){
int i=1;
Youtube: Jagrut Awaaz
1
jagrutawaaz.com for (i=10; i>=1 ; --i) {
printf (“ %d”, i);
i--;
}
}
(c) Write a C program to make sum of digits of a given number. (if input is 07
145, output should be 10)
z
handling functions.
aa
(c) Explain any four string handling functions with an example. 07
Aw
ut
gr
Ja
Q.1 (a) List out and briefly explain basic building blocks of Computer
system.
Ans.
➢ Central Processing Unit (CPU): Often referred to as the brain of
the computer, the CPU performs arithmetic and logical operations,
controls the flow of data, and executes instructions from
programs.
➢ Memory: This is where data and instructions are stored
temporarily or permanently. It includes:
RAM (Random Access Memory): Volatile memory that provides
space for the CPU to read and write data quickly. It is cleared
z
when the computer is turned off.
aa
ROM (Read-Only Memory): Non-volatile memory that stores
Aw
critical system instructions and firmware.
➢ Storage: Used for long-term data storage. It includes:
ut
z
process flow branches based on a condition (e.g., yes/no or
true/false). aa
4. Parallelogram (Input/Output): Used to show input to or output
Aw
pages or sections.
7. Document (Document): Represents a document or report that
is part of the process.
8. Predefined Process (Subroutine): Indicates a predefined
process or a set of operations that is defined elsewhere. It
represents a complex process that is not detailed in the current
flowchart.
➢ Integer Types:
• int: Represents integer values. The size of int is typically 4 bytes
(32 bits), but this can vary depending on the system.
• short: Represents smaller integer values. It usually takes 2 bytes
(16 bits).
• long: Represents larger integer values. It is typically 4 bytes (32
bits) or 8 bytes (64 bits), depending on the system.
• long long: Represents even larger integer values. It is usually 8
bytes (64 bits).
Each of these types can be signed or unsigned:
z
• signed: Can represent both positive and negative values.
•
aa
unsigned: Can only represent non-negative values (zero and
Aw
positive numbers).
➢ Character Type:
ut
encoding.
Ja
➢ Floating-Point Types:
• float: Represents single-precision floating-point numbers. It
typically uses 4 bytes (32 bits) and has about 7 decimal digits of
precision.
• double: Represents double-precision floating-point numbers. It
usually takes 8 bytes (64 bits) and has about 15 decimal digits
of precision.
• long double: Represents extended-precision floating-point
numbers. The size and precision can vary but it is generally
larger than double (e.g., 12 or 16 bytes).
➢ Void Type:
• void: Represents the absence of data. It is used in function
declarations to indicate that the function does not return a
value or as a pointer type to represent a generic pointer (e.g.,
void *).
➢ Derived Data Types:
• array: A collection of elements of the same type stored in
contiguous memory locations.
• pointer: A variable that stores the memory address of another
variable.
z
• structure: A user-defined data type that groups related
aa
variables of different types under a single name.
• union: A user-defined data type that allows storing different
Aw
data types in the same memory location, but only one member
at a time.
ut
➢ Enumerated Type:
gr
2. Right Shift (>>): Shifts bits to the right, effectively dividing the
number by 2^n, where n is the number of positions shifted.
➢ Example:
#include <stdio.h>
int main() {
unsigned int num = 8; // Binary: 0000 1000
z
aa
printf("Left Shift: %d << 2 = %d\n", num,
leftShift);
Aw
return 0;
}
➢ sizeof Operator
The sizeof operator is used to determine the size of a data type
or variable in bytes.
➢ Example:
#include <stdio.h>
int main() {
int intVar = 0;
float floatVar = 0.0;
char charVar = 'a';
z
sizeof(charVar));
aa
printf("Size of int: %zu bytes\n",
sizeof(int)); // Directly with data type
Aw
printf("Size of float: %zu bytes\n",
sizeof(float)); // Directly with data type
ut
return 0;
gr
}
Ja
z
loop.
5. In a while loop, at the
aa
In this, at the end of the
end of the condition, condition, there is a
Aw
int main() {
char ch;
z
digit.\n", ch);
}
aa
Aw
// Check if the character is a space
else if (isspace(ch)) {
printf("The character '%c' is a
ut
space.\n", ch);
gr
}
Ja
return 0;
}
OR
(c) Write a C program to print following pattern:
1
22
333
4444
Ans.
#include <stdio.h>
int main() {
int i, j;
z
aa
// Loop to control the number of rows
for (i = 1; i <= 4; i++) {
Aw
// Loop to print each number in the row
for (j = 1; j <= i; j++) {
ut
}
// Move to the next line after printing
Ja
return 0;
}
Q.3 (a) Find out error(s), if any in the following code and correct it:
int main() {
if(10%2==0){
break;
}
}
Ans.
The code provided has an issue because break is used
incorrectly outside of a loop or switch statement. The break
statement is intended to be used within loops (for, while, do-while)
or switch statements to exit from them. In the given code, break is
used in the if statement, which is not allowed.
➢ Corrected code:
#include <stdio.h>
z
int main() {
if (10 % 2 == 0) {
aa
Aw
// Use of `break` is not appropriate
here, so we will use a different statement
printf("10 is divisible by 2\n");
ut
}
gr
return 0;
Ja
void main() {
int a = 0;
while (a < 10) {
a++;
if (a % 3 == 1) {
continue;
}
printf(" %d", a);
}
}
Ans.
➢ Code Analysis:
1. Initialization: a is initialized to 0.
2. Loop: The while loop continues as long as a is less than 10.
3. Increment: Inside the loop, a is incremented by 1 with a++.
4. Condition Check: The if statement checks if a % 3 == 1. If true,
z
aa
continue is executed, which skips the rest of the loop body and
goes to the next iteration.
Aw
5. Printing: If a % 3 != 1, the printf function is executed to print the
value of a.
ut
➢ Output:
gr
// Function prototype
int factorial(int n);
int main() {
int num;
z
function
aa
printf("Factorial of %d is %d\n", num,
factorial(num));
Aw
}
ut
return 0;
gr
}
Ja
OR
Q.3 (a) Find out error(s), if any in the following code and correct it:
int main() {
printf(“%f %d”, 7.0%5.0, 5 || -2);
}
Ans.
➢ Invalid Use of % Operator with Floating-Point Numbers:
• The % operator is only applicable to integer types. In the code,
7.0 % 5.0 attempts to use % with floating-point numbers, which
is not allowed.
z
aa
➢ Incorrect Use of || Operator in printf:
• The || operator is a logical OR operator, which returns 1 for
Aw
➢ Corrected Code:
gr
#include <stdio.h>
Ja
int main() {
// Corrected version of the code
// Print only valid expressions
printf("%d %d\n", 5 % 2, 5 || -2);
return 0;
}
void main() {
int i = 1;
for (i = 10; i >= 1; --i) {
printf(" %d", i);
i--;
}
}
Ans.
➢ Code Analysis:
z
1. Initialization:
o
aa
int i = 1; initializes i to 1, but this value is immediately
Aw
overridden by the for loop initialization.
2. For Loop:
ut
z
o Print 6. aa
o Decrement i by 1, so i becomes 5.
Aw
o i is 4.
gr
o Print 4.
Ja
o Decrement i by 1, so i becomes 3.
o Decrement i by 1 again, so i becomes 2.
5. Fifth Iteration:
o i is 2.
o Print 2.
o Decrement i by 1, so i becomes 1.
o Decrement i by 1 again, so i becomes 0.
6. End of Loop:
z
int main() {
aa
Aw
int number, sum = 0, digit;
scanf("%d", &number);
Ja
z
sum); aa
Aw
return 0;
}
ut
gr
In this method, the value of each In this method, the address of actual
variable in the calling function is variables in the calling function is
copied into corresponding copied into the dummy variables of
dummy variables of the called the called function.
function.
With this method, the changes With this method, using addresses
made to the dummy variables in we would have access to the actual
the called function have no variables and hence we would be
effect on the values of actual able to manipulate them.
variables in the calling function.
z
the values of actual variables
aa
In call-by-values, we cannot alter In call by reference, we can alter the
values of variables through function
Aw
through function calls. calls.
ut
gr
Ans.
Recursion is a programming concept where a function calls
itself to solve a problem. The recursive approach divides a problem
into smaller sub-problems of the same type, solving each sub-
problem in turn. This process continues until a base case is reached,
which stops the recursion.
Base Case:
• The condition under which the recursion stops. It prevents
infinite recursion and allows the function to terminate.
Recursive Case:
z
}
aa
// Recursive case: factorial of n is n times
factorial of (n-1)
Aw
else {
return n * factorial(n - 1);
ut
}
gr
}
Ja
int main() {
int num;
return 0;
}
z
Ans. aa
#include <stdio.h>
Aw
int main() {
ut
int n, i;
int sum = 0;
gr
Ja
z
aa
printf("Sum of the array elements is %d\n",
sum);
Aw
return 0;
ut
}
gr
OR
Ja
z
educational purposes. aa
➢ extern Storage Class:
Aw
1. Scope:
o The extern storage class extends the visibility of a variable
ut
2. Lifetime:
o An extern variable has a global lifetime, meaning it exists
for the duration of the program. However, it must be
defined in one source file.
3. Initialization:
o The extern declaration does not initialize the variable. It is
only a declaration that tells the compiler that the variable
is defined elsewhere.
4. Usage:
z
➢ Syntax of switch Statement: aa
switch (expression) {
Aw
case constant1:
// Code to execute if expression ==
ut
constant1
break;
gr
case constant2:
Ja
➢ Example:
#include <stdio.h>
int main() {
int day;
z
switch (day) {
case 1:
aa
Aw
printf("Monday\n");
break;
case 2:
ut
printf("Tuesday\n");
gr
break;
Ja
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid input. Please enter a
number between 1 and 7.\n");
break;
}
z
return 0; aa
}
Aw
Ans.
gr
#include <stdio.h>
Ja
arr[j+1] = temp;
}
}
}
}
z
} aa
printf("\n");
Aw
}
ut
int main() {
gr
int n, i;
Ja
z
aa
return 0;
Aw
Ans.
gr
➢ getc()
Ja
Significance:
• Function: int getc(FILE *stream);
• Purpose: Reads the next character from the specified file
stream.
• Return Value: Returns the next character as an unsigned char
cast to an int or EOF if end-of-file is reached or an error
occurs.
• Usage: Useful for reading single characters from a file or input
stream one at a time.
➢ getw()
Significance:
• Function: int getw(FILE *stream);
• Purpose: Reads an integer from the specified file stream.
• Return Value: Returns the integer read from the file or EOF if
end-of-file is reached or an error occurs.
• Usage: Useful for reading integer values from a file. Note that
getw() is less commonly used today due to its dependence
on machine architecture and lack of portability.
➢ fscanf()
Significance:
z
• aa
Function: int fscanf(FILE *stream, const char *format, ...);
• Purpose: Reads formatted input from the specified file
Aw
stream. It can be used to read various data types, including
integers, floats, and strings, according to the specified format
string.
ut
reached.
• Usage: Useful for reading and parsing formatted input from a
file or input stream, similar to scanf() but for file streams.
z
Accessing A user can access A user can access only one
Members aa
individual members at
a given time.
member at a given time.
Aw
{ {
Ja
void printHello() {
z
printf("Hello, World!\n");
}
aa
Aw
int main() {
printHello(); // Function call
ut
return 0;
gr
}
Ja
int getRandomNumber() {
return 42; // Return a fixed number
int main() {
int num = getRandomNumber();
printf("The random number is %d\n", num);
return 0;
}
z
They are often used to perform operations or modify global
aa
variables or passed-by-reference arguments.
Aw
Example:
#include <stdio.h>
ut
int main() {
printSum(5, 7); // Function call
return 0;
}
z
int main() { aa
int result = add(3, 4);
Aw
printf("Result: %d\n", result);
return 0;
ut
}
gr
OR
Q.5 (a) List down and briefly explain methods for dynamic memory
Ja
allocation.
Ans.
Dynamic memory allocation allows you to allocate memory at
runtime using specific library functions. This is useful when the
amount of memory needed cannot be determined at compile time.
1. malloc() (Memory Allocation)
• Function: void* malloc(size_t size);
• Purpose: Allocates a block of memory of size bytes and returns
a pointer to the beginning of the block.
z
2. calloc() (Contiguous Allocation)
aa
Aw
• Function: void* calloc(size_t num, size_t size);
• Purpose: Allocates memory for an array of num elements, each
ut
• Example:
int *arr = (int*) calloc(10, sizeof(int)); //
Allocate and zero-initialize memory for an array
of 10 integers
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Use the allocated memory
free(arr); // Free the allocated memory
3. realloc() (Reallocation)
• Function: void* realloc(void* ptr, size_t size);
• Purpose: Resizes a previously allocated memory block to a new
size. If the new size is larger, the new memory is uninitialized. If
the new size is smaller, the excess memory is freed.
• Usage: Useful when you need to change the size of a previously
allocated memory block.
• Example:
int *arr = (int*) malloc(10 * sizeof(int)); //
Allocate initial memory
if (arr == NULL) {
z
aa
printf("Memory allocation failed\n");
return 1;
Aw
}
arr = (int*) realloc(arr, 20 * sizeof(int)); //
ut
return 1;
}
// Use the reallocated memory
free(arr); // Free the allocated memory
z
aa
(b) Write a C program to copy content of one file to other with the
Aw
help of file handling functions.
Ans.
ut
#include <stdio.h>
gr
int main() {
Ja
scanf("%s", destinationFileName);
z
"w"); aa
if (destinationFile == NULL) {
Aw
printf("Error opening destination file.\n");
fclose(sourceFile); // Close source file
before exiting
ut
return 1;
gr
}
Ja
fclose(destinationFile);
return 0;
}
z
• Syntax: size_t strlen(const char *str);
aa
• Example:
Aw
#include <stdio.h>
#include <string.h>
ut
gr
int main() {
const char *str = "Hello, World!";
Ja
2. strcpy
• Description: Copies the source string to the destination string.
• Syntax: char *strcpy(char *dest, const char
*src);
• Example:
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello, World!";
char dest[50];
strcpy(dest, src);
printf("Copied string: %s\n", dest);
return 0;
}
z
3. strncpy
•
aa
Description: Copies up to n characters from the source string to
the destination string.
Aw
• Example:
gr
#include <stdio.h>
Ja
#include <string.h>
int main() {
char src[] = "Hello, World!";
char dest[50];
strncpy(dest, src, 5);
dest[5] = '\0'; // Null-terminate the
destination
printf("Copied string: %s\n", dest);
return 0;
}
4. strcat
• Description: Appends the source string to the end of the
destination string.
• Syntax: char *strcat(char *dest, const char *src);
• Example:
#include <stdio.h>
#include <string.h>
int main() {
char dest[50] = "Hello";
z
char src[] = ", World!";
strcat(dest, src);
aa
Aw
printf("Concatenated string: %s\n", dest);
return 0;
}
ut
gr
Ja