Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Psp set –1 answers

1.A) Describe the evolution of Computers by mentioning how computers in one

generation are better than their predecessors.

Answer :-First-generation computers:

The key features of first-generation computers are:

First-generation computers (1940-1958)

*They used vacuum tubes for circuitry and magnetic drums for memory

*They were big in size and each computer used to occupy the entire room space

*They were very expensive and used to consume a lot of electricity

*The programmers used to write programs only in machine language to perform

*various operations.

*They used to solve only one problem at a given time

Some of the examples are UNIVAC-I (UNIVersal Automatic Computer),

ENIAC (Electronic Numerical Integrator And Calculator) etc.

SecondGeneration:

*The transistors were the most important component which replaced vacuum tubes.

*Magnetic cores were used for memory.

*It was more reliable than first-generation computer.

*The assembly or symbolic language was used.

*The input and output mechanisms remained the same.

*The stored program concept was introduced which stores both data and program.

Third Generation:
*The Integrated circuits(IC) were the most important component.

*The transistors, diodes, resistors, and capacitors were integrated on a single chip.

*The high-level language was used like BASIC, C, C++, and JAVA.

*Memory capacity increased and a magnetic hard disk was used for the second generation.

*The third generation computers also had OS and computers could rum programs invoked by

multi-users.

Fourth Generation:

*The Microprocessor was the most important component.

*With the help of LSI (Large Scale Integration) and VLSI (Very Large Scale Integration),

the entire CPU is on a single chip.

*OS has moved from MSDOS to GUI (Graphical User Interface) like windows.

*The networking technology has also been improved.

*The size was reduced and the speed was increased.

Fifth Generation:

*Artificial Intelligence and the use of natural languages are the main features of this

generation.

*These systems are expected to interact with users in natural language.

*Speech recognition and speech output should also be possible.

*Computers are must be able to perform parallel processing.

*The quad-core and octa-core were also introduced.

*Neural networks and expert systems have been developed.

1.B) Define the following with a suitable example.

i. Bit ii. Nibble iii.Byte iv. Word v. Kilobyte

Answer:-
Bit:-

The data are represented by the state of electronic switches. A switch with

ON state represents 1 and OFF state represents 0 (zero). These values which represent

the state of a switch are called bits.

Example:-In other words, the binary digits 1 and 0 are called bits. Thus,

a bit is the smallest unit of data.

2. Nibble:-

Definition: A group of 4 bits (a half byte) is called a nibble or 1⁄2 byte.

Example:

An integer 9 can be represented using a nibble as shown below:

1001= nibble

3. Byte:-

A group of 8 bits is called a byte.

A byte is the smallest unit of data that can be manipulated by a computer

at any given time. After accessing a byte, the bits

or nibbles can be manipulated.

Example:-
4. Word:-

A word is the unit of information in which information may be stored, transmitted, or

operated within a given computer.

Example:-

for a 16-bit machine, 1 word = 16-bits = 2 bytes

For a 32-bit machine, 1 word = 32-bits = 4 bytes

For a 64-bit machine, 1 word = 64-bits = 8 bytes

5. Kilobyte:-

1 Kilobyte = 1024 bytes

Megabyte = 1024 Kilobytes

1.C) How primary memory is different from secondary memory? Explain

Answer:-

Primary Memory:-

*The memory that is accessible directly by the CPU of a computer is called primary memory.

*This memory is part of the main computer system which is plugged into the motherboard
along with the CPU.

*Hence, this memory is also called internal memory or main memory.

*This primary memory allows the CPU to store and retrieve data quickly.

*The primary memory is divided into the following types:

Random Access Memory (RAM): SRAM and DRAM

Read Only Memory (ROM, PROM, EPROM, EEPROM)

Cache memory (L1, L2 and L3)

CPU registers

Secondary Memory:-

*The memory that is not part of the computer’s main memory and which is not directly

connected to the CPU but connected to the motherboard through ports and connectors is

called secondary memory.

*In secondary memory, we can store all the data and programs permanently and can be moved

from one place to another place and can be connected to another computer.

*The secondary memory is also called auxiliary memory.

*The data and instructions are loaded from secondary memory to main memory so that the CPU

can process the data.

Ex: floppy diskette, hard disk, CD-ROMs, magnetic tapes, Blu-ray disk, flash

memory (Pen drive), etc.

*The different types of storage devices are shown below:

Floppy disk (1.2MB and 1.44MB) which is obsolete

Hard disk (500GB to 4TB)

Magnetic Tape (20TB)

Optical discs: CD-ROM, DVD-ROM, Blue-Ray Disk

Flash memory
2.B) Explain the various rules for forming identifier names. Give examples of valid and

invalid identifiers names for the same.

Answer:-

Rules for forming identifier names:-

*Identifier should contain only alphabets (A-Z), (a-z), numerals(0-9) and underscore (_)

*The identifier should start with alphabet or underscore, but not a numeric character

*The first 31 characters in an identifier are significant, the rest of characters are

neglected

*No reserve words (keywords) of C language can be used as identifiers

*Identifiers are case sensitive

Examples of valid identifiers:-

food, counter_7, max_1, min_1

Examples of Invalid identifiers:-

$num //$ is special character

int //keyword

name 1 //space not allowed

2.C) Write a C program to calculate the area of a circle.

Answer:-

#include<stdio.h>

int main() {
float radius, area;

printf("\nEnter the radius of Circle : ");

scanf("%d", &radius);

area = 3.14 * radius * radius;

printf("\nArea of Circle : %f", area);

return (0);

3.A) How similar and different while & for loops are? Give example

Answer:-

For loop While loop


The structure of for loop is – The structure of the while loop is-
for(initial condition; number of While(condition){statements;//body}
iterations){//body of the loop }
Iterates for a preset number of times. Iterates till a condition is met.
In the absence of a condition, the loop In the absence of a condition, the while loop
iterates for an infinite number of times till it shows an error.
reaches the break command.
Initialization in for loop is done only once Initialization is done every time the loop is
when the program starts. iterated.
Used to obtain the result only when the Used to satisfy the condition when the
number of iterations is known. number of iterations is unknown.

One similarity between while and for loop is that they both are entry controlled loops i.e.

they both will check the condition for true or false. If the condition is true then only the

entry within the loop is permitted

3.B) Write a C program to print numbers from m to n. (where n > m)


Answer:-

#include<stdio.h>

#include<conio.h>

void main()

int i,n,m; /* i for loop counter, m is lower limit, n for lower limit */ clrscr();

printf("\n Enter lower limit and upper limit");

scanf("%d%d", &m, &n);

printf("\n Numbers from %d to %d are \n", m, n);

for(i=m; i<=n; i++)

printf("%5d",i);

getch();

Output:

Enter lower limit and upper limit

10 20

Numbers from 10 to 20 are

10 11 12 13 14 15 16 17 18 19 20

3.C) Demonstrate the working of break and continue statement with a suitable example

Answer:-

Break and continue: break and continue are unconditional control constructs.

i. Break:-
This statement is useful to terminate a loop and transfer control out of

the loop under special situations.

*break statement works with while, do….while, for, and switch statements.

*Following program syntax diagrammatically represents the working

*mechanism of a break statement.

*Note: If a break statement is used in the nested loops, the control will come out

from the inner loop; still, the outer loop is active.

*Working of break statement:

When condition2 is true the loop gets terminated and control is transferred to

statement5 (which is outside the loop). Here we can observe that even though

Example Program for Break:-

void main( )

{
int i; for(i=1;i<=5;i++)

if(i==3)

break; printf(“%d ”,i);

*Output: 1 2

ii. Continue

*The statement is helpful to skip a particular set of statements in the

loop body and to continue execution from the beginning of the loop.

*Following syntax clearly depicts how control shifts to the beginning of a

loop on finding continue statement.

Working of continue statement:

When condition2 is true continue statement is executed which results in transfer


of control to beginning of while loop skipping statement4 and statement5.

Example Program for Continue:-

void main()

int i; for(i=1;i<=5;i++)

if(i==3) continue; printf(“%d ”,i);

Output: 1 2 4 5

4.A) Develop a C program to plot a Pascal’s triangle

Answer:-

#include < stdio.h >


long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d", & n);
for (i = 0; i < n; i++) {
for (c = 0; c <= (n - i - 2); c++) printf(" ");
for (c = 0; c <= i; c++) printf("%ld ", factorial(i) / (factorial(c)
* factorial(i - c)));
printf("\n");
}
return 0;
}
long factorial(int n) {
int c;
long result = 1;
for (c = 1; c <= n; c++) result = result * c;
return result;
}
4.B) Compare if..else with the switch statement

Answer:-

IF-ELSE SWITCH
Which statement will be executed depending
Which statement will be executed is decided
upon the output of the expression inside if
by user.
statement.
the if-else statement uses multiple statements switch statement uses a single expression for
for multiple choices. multiple choices.
if-else statement test for equality as well as
switch statement test only for equality.
for logical expression.
if statement evaluates integer, character, the switch statement evaluates only character
pointer or floating-point type, or boolean type. or integer value.
the switch statement executes one case after
Either if statement will be executed or else
another till a break statement appears or the
the statement is executed.
end of the switch statement is reached.
If the condition inside switch statements does
If the condition inside if statements is false,
not match with any of the cases, for that
then by default the else statement is
instance the default statement is executed if
executed if created.
created.
It is difficult to edit the if-else statement if the It is easy to edit switch cases as they are
nested if-else statement is used. recognized easily.
4.C) List various commonly used format specifiers. Explain any two of them.

Answer:-

5.A) How 2D array is represented in memory? Explain with a suitable example

Answer:-

Two-Dimensional Arrays:
A list of items can be given one variable name using two subscripts and such a variable
is called a Two-dimensional array.
It consists of both rows and columns. Ex: Matrix.

Declaration of Two-Dimensional Array:

Here is the general syntax for array declaration along with examples.

Initialization of Two-Dimensional Array:

After array is declared, the next is storing values into an array is called initialization.

There are two types of array initialization:

1. Compile-time initialization
2. Run-time initialization
3. Compile-time initialization:

If we assign values to the array during declaration it is called compile-time


initialization.

Following are the different methods of compile-time initialization.


2. Run time initialization:

Run time initialization is storing values in an array when the program is running or
executing.

The following example illustrates run time storing of values using scanf and for loop:

Example:

printf(“Enter the marks”);


for(i=0; i<3; i++) for(j=0;j<4;j++)
{
scanf(“ %d”, &marks[i][j]);
}

More Examples: Other way of initialization:


int a[ ][3]= { 0, 1, 2, 3,4,5,6,7,8};
int b[ ][4] ={1,2,3,4,5,6,7,8,9,10,11,12};
Example for Invalid initialization
int A[3][ ]={1,2,3};
Note: Never have column size undeclared in two dimension array.

5.C) What are strings? Write a C program to swap two strings

Answer:-

Definition of String :-

“String is a variable-length data stored in a character array”.

• In C a string is a data structure based on an array of char.


• A string is a sequence of elements of the char data type.
• There is no separate data type called a string in the C language.
• As strings are variable-size data we have to represent them using character
Arrays.

An example string is:

C program to swap two strings: –

#include<stdio.h>
#include<string.h>
main(){
char s1[10],s2[10],s3[10];
printf("Enter String 1\n");
gets(s1);
printf("Enter String 2\n");
gets(s2);
printf("Before Swapping\n");
printf("String 1 : %s\n",s1);
printf("String 2 : %s\n",s2);
strcpy(s3,s1);
strcpy(s1,s2);
strcpy(s2,s3);
printf("After Swapping:\n");
printf("String 1 : %s\n",s1);
printf("String 2 : %s\n",s2);
}
output:-
Enter String 1
Bgmi
Enter String 2
Leaks
Before Swapping
String 1: Bgmi
String 2: Leaks
After Swapping:
String 1: Leaks
String 2: Bgmi

6.A) Write a C program to implement the Bubble sort technique (ascending order)
and trace
the program for the following input: 58 42 10 25 60.

Answer:-

#include<stdio.h>
void main()
{
int a[10], n, i, j, temp;
printf("Enter the number of elements\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
for(j=1; j<n;j++)
{
for(i=0;i<n;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("The sorted array is\t");
for(i=0; i<n;i++)
printf(“%d\t”,a[i]);
}

Output
Enter the number of elements 7
Enter the array elements 12 23 10 13 15 1 8
The sorted array is 1 8 10 12 13 15 23

6. B) Mention various operations that can be performed on strings using built-in


functions.
Explain any two functions.

Answer:-

Function Work of Function


strlen() computes string’s length
strcpy() copies a string to another
strcat() concatenates(joins) two strings
strcmp() compares two strings
strlwr() converts string to lowercase
strupr() converts string to uppercase
1.strcpy( ): It is possible to assign a value to a string variable using strcpy( ). It allows
us to copy a string from one location to another. The string to be copied can be literal
or string variable.

The general form of call to strcpy is strcpy(dest,source);


Strcpy() function has two parameters.

The first is the dest, a string variable whose value is going to be changed.

The second is the source of the string literal or variable which is going to be copied to
the destination.

Case 1:

Ex: char str1[ ] = “america” ;


char str2[]= “india” ;
strcpy ( str1, str2 ) ;
strcpy(america,india);
america = 7character
india = 5 character
so first 5 character of america is replaced .
Output is inidaca

Case 2: Ex: char str1[10]=”india”


char str2[]= “america” ;
strcpy ( str1, str2 ) ;
strcpy(india,america);
india = 5 character
america = 7character
since 7 is greater than 5, all characters of india are replaced by america.
Output is America

2. strcmp(): It is used to compare the contents of the two strings.


Syntax:
int strcmp(string 1, string 2);
Example: char mystr_a[10] = “Hello”;
char mystr_b[10] = “Goodbye”;
– mystr_a == mystr_b; /* NOT allowed! The correct way is if (strcmp(mystr_a,
mystr_b )) */
printf (“Strings are NOT the same.”);
else
printf( “Strings are the same.”); Here it will check the ASCII value of H and G i.e, 72
and 71
and return the diference 1.

6. C) Mention the purpose of a Null Character in C strings.

Answer:-

If you use a literal string in a program, it is stored in consecutive bytes in memory


and the compiler places the null character at the end.

7. A) Demonstrate the use of C user-defined functions with a suitable example.


Answer:-
User-defined functions are written by programmers to serve their own purpose and are
not readily available.
Functions based modular programming is advantageous in many ways:
4. Managing huge programs and software packages is easier by dividing them into
functions/modules—Maintenance is easier
5. Error detection is easier—Debugging is easier
6. Functions once written can be reused in any other applications – Reusability is enhanced
7. We can protect our data from illegal users—Data protection becomes easier
Figure 1: Top-down modular programming using functions

Following block diagram explains how functions are useful:Instead of writing entire program (to
add and subtract) within main( ) function we can write independent modules add( ) and
subtract( ) as shown:
7. B) What is recursion? Write a C program to computer factorial using recursion.

Answer:-

“Recursion is the repetitive process in which a function calls itself”

• All recursive functions have two elements each call either solves one part of the
problem or it reduces the size of the problem.
• The statement that solves the problem is known as the base case.
• The rest of the function is known as a general case.

#include<stdio.h>
int fact(int);
void main()
{
int n,res;
printf(“enter the number to find its
factorial\n”);
scanf(“%d”,&n);
res=fact(n);
printf(“factorial of %d=%d”,n,res);
getch();
}
int fact(int n)
{
if(n==0) return 1;
else
return (n*fact(n-1));
}

8. A) Differentiate between call by value and call by reference using


suitable examples.
Answer:-

Parameters Call by value Call by reference


Definition While calling a function, While calling a function, in
when you pass values by programming language
copying variables, it is known instead of copying the values
as “Call By Values.” of variables, the address of
the variables is used it is
known as “Call By
References.
Arguments In this method, a copy of the In this method, a variable
variable is passed. itself is passed.
Effect Changes made in a copy of a Change in the variable also
variable never modify the affects the value of the
value of the variable outside variable outside the function.
the function.
Alteration of value Does not allow you to make Allows you to make changes
any changes in the actual in the values of variables by
variables. using function calls.
Passing of variable Values of variables are Pointer variables are
passed using a required to store the address
straightforward method. of variables.
Value modification Original value not modified. The original value is
modified.
Memory Location Actual and formal arguments Actual and formal arguments
will be created in different will be created in the same
memory location memory location
Safety Actual arguments remain Actual arguments are not
safe as they cannot be Safe. They can be
modified accidentally modified, so you
accidentally. need to handle argument
operations carefully.
Default Default in many It is supported by most
programming programming languages like
languages like C++.PHP. JAVA, but
Visual Basic NET, and C#. not as default.

8. B) Define a function. List various advantages of a function.

Answers:-

“Functions are independent program modules that are designed to carry out a
particular
task.”

In functions we have two types:

8. Built-in (Library) functions


9. User-defined functions

Advantages of using Functions:


1. Managing huge programs and software packages is easier by dividing them into
functions/modules—Maintenance is easier

2. Error detection is easier—Debugging is easier

3. Functions are once written can be reused in any other applications – Reusability is
enhanced

4. We can protect our data from illegal users—Data protection becomes easier

8. C) How actual parameters are different from formal parameters? Explain.

Answer:

Sl.no Actual parameters Formal parameters


When a function is called, the The values which are
values that are passed in the received by the function, and
1.
call are called actual are assigned to formal
parameters. parameters.
They are also called as They are also called as
2.
argument list. dummy parameters.
The variables used in the The variables defined in
3. function call is called actual function header are called
parameters. formal parameters.
These are used in calling
These are used in function
4. function when a function is
header of the called function.
called or invoke.
Actual parameters sends
Formal parameters receives
5. data to the formal
data from actual parameters.
parameters.
9.A) How does a structure differ from an array?

Answer:-

9. A) How does a structure differ from an array?

Answer :

Sl.no Arrays Structures


Structure is the collection of
Array is a collection of related
logically related data
1. data elements of the same
elements of different data
data type
types.
Structure is a programmer-
2. Array is derived data type
defined one
In case of Structures, first, we
Array behaves like built-in
3. have to design and declare a
data type
data structure.
In structures after designing
In Arrays we have to declare
the structure and declaring
4. an array of variables and use
then we can declare structure
them.
variables and use them.
syntax:
struct tag_name
{
syntax:
datatype member1;
5. data_type
datatypemember2;
array_name[size];
————–
————–
};
Example:
struct book_bank
{
Example: chartitle[20];
6.
int marks[5]; char author[15];
int pages;
float price;
};

9. B) Define a structure by the name DoB consisting of three variable members dd,
mm, and yy
of type integer. Develop a C program that would read values to the individual
member and display the date in mm/dd/yy form.

Answer:-

#include <stdio.h>

struct student{
char name[30];
int rollNo;

struct dateOfBirth{
int dd;
int mm;
int yy;
}DOB; /*created structure varoable DOB*/};

/* Scroll down for full code


*/

int main()
{
struct student std;

printf("Enter name: ");


scanf("%C",&std.name);
printf("Enter roll number: ");
scanf("%d",&std.rollNo);
printf("Enter Date of Birth [DD MM YY] format: ");
scanf("%d%d%d",&std.DOB.dd,&std.DOB.mm,&std.DOB.yy);
printf("\nName : %s \nRollNo : %d \nDate of birth :
%02d/%02d/%02d\n",std.name,std.rollNo,std.DOB.dd,std.DOB.mm,std.DOB.yy);

return 0;
}

Out put:-

Enter name: BGMI LEAKS


Enter roll number: 000
Enter Date of Birth [DD MM YY] format: 00 00 00

Name : BGMI LEAKS


RollNo : 000
Date of birth : 00/00/00

10.A) How structure in C is different from the union? Give example

Answer:-

Example of Structure

struct student
{
int rollno;
char name[50];
string phone;

};

Example of Union

union Student {
char name[32];
int age;
string email;
};

10.B) What is a pointer? Discuss Pointer arithmetic with suitable C


code
Answer:-
“A pointer is a variable that holds the address of another variable.
The pointer variable
contains only the address of the referenced variable, not the value
stored at that address.“
Pointer arithmetic is very important to understand if you want to have
complete knowledge of
pointer. In this topic, we will study how the memory addresses change
when you increment a
pointer.
In a 16 bit machine, the size of all types of the pointer, be it int,
float, char*, or double* is always 2
bytes. But when we perform any arithmetic function like increment on a
pointer, changes occur as
per the size of their primitive data type.
Examples for Pointer Arithmetic:-
Now let’s take a few examples and understand this more clearly.
int*i;
i++
In the above case, the pointer will be 2 bytes. And when we increment
it, it will increment by 2 bytes because int is also of 2 bytes.
float*i;
i++;
In this case, the size of a pointer is still 2 bytes. But now, when we
increment it, it will increment by 4 bytes because the float is 4
bytes.
double* i;
i++;
Similarly, in this case, the size of a pointer is still 2 bytes. But
now, when we increment it, it will
increment by 8 bytes because its data type is double.
10.C) Narrate the purpose of various C language Pre-processor Directives

Answer:-

You might also like