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

VTU 1st sem PSP (21PSP23/13) solved model question paper with

answers 2022
1. A) Describe the evolution of Computers by mentioning how computers in one
generation are better than their predecessors.
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. Kilo byte
1. 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

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

2 A) What are enumeration variables? How are they declared? What are the advantages of
using them in a program?
An enumeration type declaration gives the name of the (optional) enumeration tag. And, it defines
the set of named integer identifiers (called the enumeration set, enumerator constants,
enumerators, or members). A variable of the enumeration type stores one of the values of the
enumeration set defined by that type.
The benefits of using enumerations include:
• Reduces errors caused by transposing or mistyping numbers.
• Makes it easy to change values in the future.
• Makes code easier to read, which means it is less likely that errors will creep into it.
• Ensures forward compatibility.

B) Explain the various rules for forming identifier names. Give examples of valid and invalid
identifiers names for the same.
Rules for forming identifier names:-
1. Identifier should contain only alphabets (A-Z), (a-z), numerals(0-9) and underscore (_)
2. The identifier should start with alphabet or underscore, but not a numeric character
3. The first 31 characters in an identifier are significant, the rest of characters are neglected
4. No reserve words (keywords) of C language can be used as identifiers
5. 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
C) Write a C program to calculate the area of a circle.
#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) Module-2
How similar and different while & for loops are? Give example
For loop While loop
The structure of for loop is –
for(initial condition; number of The structure of the while loop is-
iterations){//body of the loop } While(condition){statements;//body}
Iterates for a preset number of times. Iterates till a condition is met.
In the absence of a condition, the loop
iterates for an infinite number of times In the absence of a condition, the while loop
till it reaches the break command. shows an error.
Initialization in for loop is done only Initialization is done every time the loop is
once 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.
B) Write a C program to print numbers from m to n. (where n > m)
#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();
}
C) Demonstrate the working of break and continue statement with a suitable example
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.

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


#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;
}
B) Compare if..else with the switch statement
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 for switch statement test only for equality.
logical expression.
if statement evaluates integer, character, the switch statement evaluates only character
pointer or floating-point type, or boolean type. or integer value.
Either if statement will be executed or else the the switch statement executes one case after
statement is executed another till a break statement appears or the
end of the switch statement is reached.
If the condition inside if statements is false, If the condition inside switch statements does
then by default the else statement is executed not match with any of the cases, for that
if created. instance the default statement is executed if
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.
C) List various commonly used format specifiers. Explain any two of them.

5 A) Module-3
How 2D array is represented in memory? Explain with a suitable example
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
1. 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.
B) For an array declared as int a[50], compute the address of a[35] if a’s base address is
1000 and word size=2
The address of arr[35] for the given array is .
Explanation:
Let there be an array of 'n' elements having base address 'x'.
Then the size of each element in memory address is referred as the width of the array. Let that be
denoted by 'b'.
Then the address 'A' of 'i' index of the array is given
by, Here, we have an array 'arr'
and .

Then the required address is given by,

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


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.
#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
B) Mention various operations that can be performed on strings using built-in functions.
Explain any two functions.
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.
C) Mention the purpose of a Null Character in C strings.
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.
Module-4
7 A) Demonstrate the use of C user-defined functions with a suitable example.
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:
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 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

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:
What is recursion? Write a C program to computer factorial using recursion.
“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.
Parameters Call by value Call by reference
While calling a function, in
While calling a function, programming language instead of
when you pass values by copying the values of variables, the
copying variables, it is address of the variables is used it is
Definition known as “Call By Values.” known as “Call By References.
In this method, a copy of the In this method, a variable itself is
Arguments variable is passed. passed.
Changes made in a copy of a
variable never modify the Change in the variable also affects the
value of the variable outside value of the variable outside the
Effect the function. function.
Does not allow you to make Allows you to make changes in the
Alteration of any changes in the actual values of variables by using function
value variables. calls.
Values of variables are
Passing of passed using a Pointer variables are required to store
variable straightforward method. the address of variables.
Value
modification Original value not modified. The original value is modified.
Actual and formal arguments Actual and formal arguments
Memory will be created in different will be created in the same
Location memory location memory location
Actual arguments remain Actual arguments are not
safe as they cannot be Safe. They can be
modified accidentally modified, so you need to
Safety accidentally. handle argument operations carefully.
Default in many It is supported by most
programming programming languages like JAVA,
languages like C++.PHP. but
Default Visual Basic NET, and C#. not as default.
B) Define a function. List various advantages of a function.
“Functions are independent program modules that are designed to carry out a particular
task.”
In functions we have two types:
1. Built-in (Library) functions
2. 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
C) How actual parameters are different from formal parameters? Explain.
Answer:
Sl.no Actual parameters Formal parameters
When a function is called, the values The values which are received by the
that are passed in the call are called function, and are assigned to formal
1. actual parameters. parameters.
They are also called as dummy
2. They are also called as argument list. parameters.
The variables used in the function call The variables defined in function
3. is called actual parameters. header are called formal parameters.
These are used in calling function when These are used in function header of
4. a function is called or invoke. the called function.
Actual parameters sends data to the Formal parameters receives data
5. formal parameters. from actual parameters.
Module-5
9 A) How does a structure differ from an array?
Sl.no Arrays Structures
Array is a collection of related
data elements of the same data Structure is the collection of logically related
1. type data elements of different data types.
2. Array is derived data type Structure is a programmer-defined one
Array behaves like built-in data In case of Structures, first, we have to design
3. type and declare a data structure.
In structures after designing the structure and
In Arrays we have to declare an declaring then we can declare structure
4. array of variables and use them. variables and use them.
syntax:
struct tag_name
{
datatype member1;
datatypemember2;
————–
syntax: ————–
5. data_type array_name[size]; };
6. Example: Example:
int marks[5]; struct book_bank
{
chartitle[20];
char author[15];
int pages;
float price;
};
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.
#include <stdio.h>

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

struct dateOfBirth{
int dd;
int mm;
int yy;
}DOB; /*created structure varoable DOB*/
};
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
C) Write the result of the following C code:
int num1=10, num2=20;
int *p=&num1, *q=&num2;
p++=q++;
printf(“%d %d”,num1,num2);

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


A)

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


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.
C) Narrate the purpose of various C language Pre-processor Directives

You might also like