0% found this document useful (0 votes)
40 views11 pages

PYQ With Solution-5

Uploaded by

reshantgarg0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views11 pages

PYQ With Solution-5

Uploaded by

reshantgarg0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

PYQ with Solution

PPS (ESC-103)
End Semester Paper December 2019

Part-A (1.5 mark each)


1. What are Data Types? Name different data types available in C.
2. Define Pointer.
3. What do you mean by sorting? Name any two sorting techniques.
4. Define string with syntax.
5. Differentiate between Algorithm and Flowcharts.
6. What is a function prototype?
7. Write the output of the following program:
main()
{ int x=5, *p;
p=&x;
printf(“%d”,*p);
getch();
}
8. Write the function of gets() and puts() commands in strings.
9. Define recursion.
10. Write the syntax of fopen and fclose commands in C file system.

Part-B

1. (a) Draw and explain memory hierarchy in a computer system. (5 mark)


(b) Explain all types of loops available in C with their syntax. Write a program to generate table
of a number “n” being entered by user. (10 mark)

2. (a) Draw a flowchart for finding out the greatest between three numbers. (5 mark)
(b) What is a function? What is the difference between call by value and call by reference method
of calling a function? Explain with proper example. (10 mark)

3. (a) WAP to calculate factorial of a number using recursion. (5 mark)


(b) WAP to calculate length of a string without using string inbuilt function. (10 mark)

4. (a) WAP to search an element in an array using linear search. (5 mark)


(b) Define array. WAP to find the sum of two matrices A and B in third matrix C. (10 mark)

5. (a) WAP to copy the contents of a file into another. (5 mark)


(b) What is an operating system? Write down various functions of an operating system. Explain
memory management in detail. (10 mark)

6. Write a short note on the following:- (5*3 mark)


(a) Compiler and Interpreter
(b) Strings
(c) Storage classes in C
Solutions
Solutions for Part-A

1. What are Data Types? Name different data types available in C.

Definition:
Data types in C are classifications that specify the type of data a variable can hold, defining
its size, range, and operations that can be performed on it. They help ensure proper memory
allocation and operations.

Detailed Answer:
In C, data types are broadly categorized into three types:

 Primary Data Types:


o int: Used for integers, e.g., int a = 10;
o float: Used for decimal numbers, e.g., float b = 10.5;
o char: Used for single characters, e.g., char c = 'A';
o double: Used for large floating-point numbers, e.g., double d = 12.3456;
 Derived Data Types:
o Array: Collection of elements of the same type, e.g., int arr[5];
o Pointer: Stores the address of a variable, e.g., int *p;
o Structure: Groups different data types under one name, e.g., struct Point
{int x; float y;};
 User-defined Data Types:
o typedef and enum, allowing creation of custom types.
 Void Type:
Represents no value or type, e.g., void functionName();

2. Define Pointer.

Definition:
A pointer is a variable in C that stores the memory address of another variable. It enables
dynamic memory allocation and efficient handling of arrays, strings, and functions.

Detailed Answer:
Pointers are declared using the * operator. For example:

int x = 10;
int *p = &x;

Here, p is a pointer storing the address of x. The & operator retrieves the address of a variable,
and the * operator (dereferencing) accesses the value at that address. Pointers are widely used
for:

 Accessing arrays dynamically.


 Passing variables to functions by reference.
 Memory management using malloc, calloc, and free.
3. What do you mean by sorting? Name any two sorting techniques.

Definition:
Sorting is the process of arranging data in a specific order (ascending or descending) to
enable efficient searching and analysis.

Detailed Answer:
Two common sorting techniques are:

1. Bubble Sort: Repeatedly compares adjacent elements and swaps them if they are in
the wrong order.
o Time Complexity: O(n2)O(n^2)O(n2)
o Space Complexity: O(1)O(1)O(1)
2. Insertion Sort: Builds the sorted list one element at a time by comparing and
inserting elements into their correct position.
o Time Complexity: O(n2)O(n^2)O(n2)
o Space Complexity: O(1)O(1)O(1)

4. Define string with syntax.

Definition:
A string in C is a one-dimensional array of characters terminated by a null character \0.
Strings are used to store and manipulate text.
Strings are declared using the char keyword. Syntax:

char str[size];

For example:

char str[10] = "Hello";

Here, "Hello" is stored as {'H', 'e', 'l', 'l', 'o', '\0'}. Common string functions
include:

 strlen: To find the length of the string.


 strcpy: To copy strings.
 strcmp: To compare strings.

5. Differentiate between Algorithm and Flowcharts.

Algorithm Flowchart
A step-by-step procedure for solving a
A graphical representation of an algorithm.
problem.
Algorithm Flowchart
Uses symbols like ovals, rectangles, and
Written in natural language or pseudocode.
arrows.
Focuses on the logic of the solution. Focuses on the process flow.
Example: Steps to calculate factorial. Example: Flowchart to find factorial.
No visual representation. Easy to visualize and understand.

6. What is a function prototype?

Definition:
A function prototype is a declaration of a function that specifies its name, return type, and
parameters but not its body.

Detailed Answer:
The prototype informs the compiler about the function details before its actual definition.
Syntax:

return_type function_name(parameter_list);

Example:

int add(int a, int b);

Here, add is a function that takes two integers and returns an integer. Prototypes are crucial
for type-checking during compilation.

7. Write the output of the following program:

Code:

main()
{
int x = 5, *p;
p = &x;
printf("%d", *p);
getch();
}

Answer:

 p = &x; assigns the address of x to pointer p.


 *p dereferences the pointer to fetch the value of x.
 Output: 5.

8. Write the function of gets() and puts() commands in strings.


Definition:

 gets(): Reads a string from standard input until a newline is encountered.


 puts(): Writes a string to the standard output, followed by a newline.

Detailed Answer:
Syntax:

char str[50];
gets(str); // Reads input into str
puts(str); // Prints str with a newline

Example:

char name[20];
gets(name); // Input: "Alice"
puts(name); // Output: "Alice"

Note: gets() is unsafe due to buffer overflow risks and is replaced by fgets() in modern C
standards.

9. Define recursion.

Definition:
Recursion is a programming technique where a function calls itself to solve smaller instances
of a problem.
A recursive function has a base case (termination condition) and a recursive step. Syntax:

return_type function_name(parameters) {
if (base_condition)
return base_case_result;
else
return recursive_call;
}

Example (Factorial):

int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}

Pros: Simplifies complex problems.


Cons: Risk of stack overflow.

10. Write the syntax of fopen and fclose commands in C file system.

Definition:
 fopen: Opens a file in a specified mode.
 fclose: Closes the opened file and releases resources.

Detailed Answer:
Syntax:

FILE *fopen(const char *filename, const char *mode);


int fclose(FILE *stream);

Modes for fopen:

 "r": Read
 "w": Write
 "a": Append

Example:

FILE *file = fopen("data.txt", "r");


if (file != NULL) {
fclose(file);
}

Solutions for Part-B

1. (a) Draw and explain memory hierarchy in a computer system. (5 marks)

Explanation:
The memory hierarchy in a computer system is organized into layers to balance speed, cost,
and capacity. The hierarchy ranges from the fastest and most expensive memory (CPU
registers) to the slowest but most affordable and voluminous storage (secondary storage).

Diagram:

Registers (Fastest, Smallest, Most Expensive)


Cache Memory (L1, L2, L3)
Main Memory (RAM)
Secondary Storage (HDD, SSD)
Tertiary Storage (CD/DVD, Tape Drives)

 Registers: Directly accessed by the CPU for immediate operations.


 Cache Memory: High-speed memory close to the CPU to store frequently accessed
data.
 Main Memory (RAM): Temporarily stores data and instructions for currently
running programs.
 Secondary Storage: Permanent storage for data (e.g., hard drives).
 Tertiary Storage: Long-term archival storage, slower and inexpensive.

1. (b) Explain all types of loops available in C with their syntax. Write a program to
generate a table of a number “n” being entered by the user. (10 marks)
Explanation of Loops in C:

1. for Loop: Used when the number of iterations is known in advance.


o Syntax:

for(initialization; condition; increment/decrement) {


// Statements
}

2. while Loop: Used when the condition is evaluated before executing the loop body.
o Syntax:

while(condition) {
// Statements
}

3. do-while Loop: Executes at least once since the condition is checked after execution.
o Syntax:

do {
// Statements
} while(condition);

Program to Generate Table of n:

#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Table of %d:\n", n);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}

2. (a) Draw a flowchart for finding out the greatest among three numbers. (5 marks)

Flowchart:

1. Input three numbers (a, b, c).


2. Compare a with b and c.
3. Compare b with c if a is not the largest.
4. Output the largest number.

[Start] --> [Input a, b, c] --> [a > b and a > c?]


Yes --> [Print "a is greatest"] --> [End]
No --> [b > c?]
Yes --> [Print "b is greatest"] --> [End]
No --> [Print "c is greatest"] --> [End]
2. (b) What is a function? Difference between call by value and call by reference with
examples. (10 marks)

Definition of Function:
A function is a reusable block of code that performs a specific task. Functions simplify code,
allow modularity, and improve readability.

Call by Value vs. Call by Reference:

Call by Value Call by Reference


Copies the value of the variable. Passes the address of the variable.
Changes do not affect the original variable. Changes reflect in the original variable.
Example: void func(int x) Example: void func(int *x)

Examples:

Call by Value:

void func(int a) {
a = 10;
}
int main() {
int x = 5;
func(x); // x is still 5
}

Call by Reference:

void func(int *a) {


*a = 10;
}
int main() {
int x = 5;
func(&x); // x becomes 10
}

3. (a) WAP to calculate factorial of a number using recursion. (5 marks)

#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
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;
}
3. (b) WAP to calculate the length of a string without using inbuilt functions. (10 marks)

#include <stdio.h>
int stringLength(char str[]) {
int count = 0;
while (str[count] != '\0') {
count++;
}
return count;
}
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
printf("Length of the string is %d\n", stringLength(str));
return 0;
}

4. (a) WAP to search an element in an array using linear search. (5 marks)

#include <stdio.h>
int main() {
int arr[100], n, search, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the element to search: ");
scanf("%d", &search);
for (i = 0; i < n; i++) {
if (arr[i] == search) {
printf("Element found at index %d\n", i);
return 0;
}
}
printf("Element not found.\n");
return 0;
}

4. (b) Define array. WAP to find the sum of two matrices in a third matrix. (10 marks)

Definition:
An array is a collection of elements of the same data type stored in contiguous memory
locations.

Program:

#include <stdio.h>
int main() {
int A[3][3], B[3][3], C[3][3], i, j;
printf("Enter elements of matrix A:\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
scanf("%d", &A[i][j]);
printf("Enter elements of matrix B:\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
scanf("%d", &B[i][j]);
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
C[i][j] = A[i][j] + B[i][j];
printf("Sum of matrices:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
}

5. (a) WAP to copy the contents of a file into another. (5 marks)

#include <stdio.h>
int main() {
FILE *source, *dest;
char ch;
source = fopen("source.txt", "r");
dest = fopen("dest.txt", "w");
if (source == NULL || dest == NULL) {
printf("Error opening file.\n");
return 1;
}
while ((ch = fgetc(source)) != EOF) {
fputc(ch, dest);
}
fclose(source);
fclose(dest);
printf("File copied successfully.\n");
return 0;
}

5. (b) What is an operating system? Write functions and explain memory management.
(10 marks)

Definition:
An operating system (OS) is software that acts as an interface between the user and the
hardware, managing resources and providing services.

Functions:

1. Process Management: Scheduling and execution of processes.


2. Memory Management: Allocation and deallocation of memory.
3. File System Management: Managing files and directories.
4. Device Management: Controlling peripheral devices.
5. Security and Protection: Preventing unauthorized access.
Memory Management:
The OS manages memory using techniques like paging, segmentation, and virtual memory. It
ensures efficient allocation, protection, and reclamation of memory.

6. Short Notes:

(a) Compiler and Interpreter:

 Compiler: Translates the entire source code to machine code before execution.
Example: C compiler.
 Interpreter: Translates and executes code line by line. Example: Python interpreter.

(b) Strings:
Strings are arrays of characters terminated by \0. They allow representation and manipulation
of text in C. Example: "Hello".

(c) Storage Classes in C:

 Automatic (auto): Default for local variables.


 Static (static): Retains value across function calls.
 Extern (extern): Accesses global variables across files.
 Register (register): Requests storage in CPU registers for faster access.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy