[go: up one dir, main page]

0% found this document useful (0 votes)
80 views20 pages

PST-II-QB-28.8.23 (1-5 Units)

Pst

Uploaded by

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

PST-II-QB-28.8.23 (1-5 Units)

Pst

Uploaded by

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

DEPARTMENT OF CENTRE OF EXCELLENCE

22CS201– PROBLEM SOLVING TECHNIQUES II


QUESTION BANK

UNIT I BASICS OF C PROGRAMMING

PART -A
1.What do you mean by keywords in C?
 Keywords are reserved words ,they have standard and predefined meaning.
 Keywords cannot be used as normal identifiers.
 Example:auto,break,char,continue,else,if,switch,struct,union
2.Illustrate the use of Ternary operator with an example.
?: is known as conditional operator. It evaluates the first expression if the condition is true
otherwise the second expression is evaluated.
Syntax : condition?exp1:exp2;
3. Explain compilation Process in C?
The compilation is the process of converting the source code of the C language into
machine code. As C is a mid-level language, it needs a compiler to convert it into an
executable code so that the program can be run on our machine.
The C program goes through the following phases during compilation:
4.What is the output of the programs given below?
#include<stdio.h>
int main()
{
int num1=20,num2=10,num3=15,num4=5;
int res;
res=(num1+num2)*num3/num4;
printf("Value of expression is :%d\n",res);
}
Ans:90
5. What is the output of the following program?
#include<stdio.h>
int main()
{
while(1,0)
{
printf("Hello While");
}
printf("Hello C");
}
Ans: Hello C
6. i)How do you declare and define a variable in C?
Declaration of Variable:
Syntax: data_type variable_name;
Example: int num;
Declaration and initializing value to a Variable:
Syntax: data_type variable_name = value;
Example: int num=5;
ii)What is the purpose of the "printf" function in C? Provide an example.
The printf function in C is used to display formatted output on the console or terminal. It
allows you to print text, numbers, and variables to the standard output stream.
Syntax:
printf("format_string", argument_list);
7. What will be the output of the following program?
#include<stdio.h>
int main()
{
int p,q,r;
p=3,q=1,r=2;
if(p+ (2&2&2) && q + (3&3&3) && r + (2^2^2))
{
p=p-2;
q=p;
}
else
{
p=r;
q=q^2;
}
printf("%d",p+q+r);
}
Ans:4
8. What is the difference between a compiler and an interpreter in C programming?
Compiler: A compiler translates the entire source code into machine code or an intermediate
representation called object code. The resulting object code is then executed directly by the
computer's processor. The compilation step is done only once, and the generated executable
file can be run multiple times.
Interpreter: An interpreter executes the source code line by line without prior translation. It
reads each instruction, interprets it, and executes it immediately. The interpretation happens at
runtime, and there is no separate compilation step. The interpreter needs to be present to run
the program.
9. Write a C program to swap two numbers without using a third variable?
#include <stdio.h>
int main() {
int a, b;
printf("Enter the first number (a): ");
scanf("%d", &a);
printf("Enter the second number (b): ");
scanf("%d", &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b);

return 0;
}

10. What will be the output of the following C code?


#include <stdio.h>
int main()
{
int i = 0;
int x = i++, y = ++i;
printf("%d % d\n", x, y);
return 0;
}
Ans: 0 2

PART B & C
1.Develop a C program for the following:
a. To find the area and circumference of a circle with radius r
b. To find the sum of first 100 integers.
2.Write a C program for the following:
a. To find the sum of digits of a number.(123=>1+2+3=6)
b. To generate the first n numbers in a Fibonacci series
3.Write a C program to generate Armstrong number between 100 and 999.
4. Write a program in C which is a Menu-Driven Program to perform a simple
calculation(1.Addition,2.Subtraction,3.Multiplication,4.Division).
5. Write a C program to implement the traffic signal system which you observe during road
transportation.
Cases to implement:
1. When Red light is displayed citizens are asked to stop
2. When green light is displayed citizens of the country are allowed to more forward
3. When Orange light is displayed citizens are asked to hold on and observe for the next
signal
4. When the signal system isn’t working, citizens are requested to look for the traffic police’s
instructions and act accordingly.
6. Explain about the following with appropriate examples
i) Keywords ii)Identifiers iii)Constants iv) pre processor directives
UNIT II ARRAYS AND STRINGS

PART- A
1.What will be the output of the following program?
#include<stdio.h>
main()
{
char x;

x = ‘a’;

printf(“%d \n”,x);
}
Ans:97
2. Define a float array of size 5 and assign 5 values to it.

Float arr[5]={1.5,2.5,7.5,9.2,4.2};

3. How do you find the length of an array in C?


length = sizeof(arr) / sizeof(arr[0]);

4. Define Array with an example.


An array is a collection of similar data elements stored at contiguous memory locations.
Example:
#include <stdio.h>

int main() {
int numbers[5]={10,20,30,40,50};

printf("Array elements:\n");

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


printf("%d\n", numbers[i]);
}

return 0;
}

5. Predict output of following program


int main()
{
int i;
int arr[5] = {1};
for (i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0;
}
Ans:1 0 0 0 0

6. What is 2D Array?
Two-dimensional array can be defined as an array of arrays.
The 2D array is organized as matrices which can be represented as the collection of rows and
columns.
int arr[r][c];
c moves along number of rows and c moves along number of columns.
7. Write the difference between scanf() and gets().
 Gets() treats new line or end of file as end of input while scanf() treats whitespace as
end of input
 Gets() is not always recommended because it reads data from user more than the size
specified and creates segmentation faults later, while scanf() doesn’t allow reading
more than the size.[compiler dependent]
8. Write a C Program to get the sentence as an input from the user and print it as
output.
#include <stdio.h>
int main() {
char sentence[1000];
printf("Enter a sentence: ");
gets(sentence);

printf("You entered: %s\n", sentence);


return 0;
}
9. Write a C program to find the length of a string without using built in functions?
#include <stdio.h>
int main() {
char str[1000];
int length = 0;
printf("Enter a string: ");
scanf("%[^\n]", str);
while (str[length] != '\0') {
length++;
}
printf("Length of the string: %d\n", length);
return 0;
}
10. Define indexing and its advantage.
Indexing: Numbers that range from 0 to size of the array -1 addressing each and every data in
array
Index varies from 0 to size-1
Array elements are accessed through indices.
PART B & C

1. Write a C program to calculate average of all elements in an array.

2. Determine the requirement that must be met before multiplying two matrices, and
Create a C program that read two matrices from the user and display the resulting
matrix.

3. Explain with example the following string function-


(i) Strlen( ) (ii) strcpy( )

(iii) strcat( ) (iv) strcmp( )

(v) strrev() (vi) strupr( )

(vii) strlwr( )

4. Explain the working of selection sort and implement it using a C program

5. Write the C program


i) to convert upper case to lower case and lower case to upper case without String
functions.
ii) Write the functionality of strlen( ) and strcpy( ) using C program. Do not use
library function.
6. Write a C program to search an element from the array.
UNIT III FUNCTIONS AND POINTERS
PART- A

1.Define Modular Programming


 Ease of Use : rather than focusing on the entire thousands and millions of lines code
in one go we can access it in the form of modules. This allows ease in debugging the
code and prone to less error.
 Reusability :It allows the user to reuse the functionality with a different interface
without typing the whole program again.
 Ease of Maintenance : It helps in less collision at the time of working on modules,
helping a team to work with proper collaboration while working on a large
application.

2. Predict the output.


i)#include<stdio.h>
int main()
{
int i = 12, *j, k;
j = &i;
printf("%d\n", i * *j * i + *j);
return 0;
}

ii) Predict the output of the following C code and justify the same.
#include <stdio.h>
void main()
{
int num = 50;
int *ptr = &num;
int **m = &ptr;
printf("%d\t%d\n", *ptr, **m);
**m=**m+42;
printf("%d\t%d\n", *ptr, **m);
}
3. What is the output of program?
#include<stdio.h>
int main()
{
int x = 40, *y, *z;
y = &x;
z = y;
*y++;
*z++;
x++;
printf("x = %d, y = %d, z = %d \n", x, y, z);
return 0;
}
4. What are actual parameters and formal parameters in function?
The data passed when the function is being invoked/called is known as the Actual
Parameters/ Arguments.
Parameters written in Function Definition is Called Formal Parameters/Parameters

5.List out the four different aspects of Functions


There are four different aspects of function calls.
function without arguments and without return value
function without arguments and with return value
function with arguments and without return value
function with arguments and with return value

6.Why base condition is needed in a Recursion Program.Justify the answer with an example.
Base condition is needed to stop the recursion otherwise infinite loop will occur.
#include<stdio.h>
int fact(int n)
{
if(n == 1)
return 1;
else
return n* fact(n-1);
}
int main()
{
int res=fact(5);
printf(“%d”,res);
return 0;
}
In the above program
if(n == 1)
return 1;
This is the base condition. This terminates the program. If base condition not given,
the recursion runs infinite times.

7.Difference between Looping and Recursion

Recursion Iteration/looping

It is used with functions. It is used generally with loops.

In each function call in recursion, extra Here, in the case of each iteration,
Space is required in stack memory. we do not require any space.

It terminates when the base condition is It terminates when a condition becomes


met. false.

Code size is large in the case of


Code size becomes smaller with recursion.
iteration.
Recursion is slower than looping Looping is faster than recursion

8.What is called Pointer with an example.


A pointer is defined as a derived data type, that can store the address of other variables or a
memory location.
void main()
{
int var = 10;
int * ptr; // declaration
ptr = &var; // initialization
printf(“%d”, *ptr) // Dereferencing // 10

9.Write a C program to explain the Array of Pointers.


#include<stdio.h>
int main()
{
int arr1[]={6,2,3,4,5};
int arr2[]={3,2,4,6,8};
int arr3[]={1,3,5,7,9};
int *parr[3] = {arr1, arr2, arr3};
int i;
for(i = 0;i<3;i++)
printf("%d\t", *parr[i]);
return 0;
}
OUTPUT:
631

10. Illustrate NULL Pointer with an example.


A null pointer is a variable that has no valid address and is allocated to zero or NULL.
#include<stdio.h>
int main(){
double *a=NULL; // NULL pointer
return 0;}

PART B & C
1.Write a C Program to explain all the four aspects of Function with an example
2. Write a C program to read ‘n’ elements of an array and to search for an element in the array
using an efficient searching algorithm that will reduce the search space into ‘half’ every time.
Implement using recursion.
3. i) Explain in detail about double pointers with example.
ii) Write a C program to find the average of array elements using pointers.

4. Write a C program
i) to print the Fibonacci series using recursion
ii) to print the Factorial of a number using recursion
5. What is the difference between Call by Value and Call by Reference. Write a C program to
swap two numbers using call by value and call by reference.
6. Write a C function .
i) to check whether the given number is prime or not .
ii) to calculate the sum of the elements in a given array using functions.
UNIT IV STRUCTURES AND UNION
PART- A
1. Define Structure in C with an example.
The structure in C is a user-defined data type that can be used to group items of
possibly different types into a single type.
The struct keyword is used to define the structure in the C programming language.
The items in the structure are called its member and they can be of any valid data
type.
Example:
struct str1
{
int i;
char c;
float f;
char s[30];
};

2. What is nested structure?


A nested structure is a structure that is defined within another structure. This allows
you to create complex data structures where one structure type is a member of another
structure type. Nested structures are often used to represent data with hierarchical
relationships.

3. How to access elements in nested structure?


Accessing inner structure members can be done by using dot operator twice. i.e.
outer_structure_variable.inner_structure_variable.inner_datamember

Example:
#include <stdio.h>
struct child {
int x;
char c;
};
struct parent {
int a;
struct child b;
};
int main()
{ struct parent var1 = { 25, 195, 'A' };
printf("var1.a = %d\n", var1.a);
printf("var1.b.x = %d\n", var1.b.x);
printf("var1.b.c = %c", var1.b.c);
}

4. What is a union in C?
The Union is a user-defined data type in C that can contain elements of the different
data types just like structure.
But unlike structures, all the members in the C union are stored in the same memory
location.
Due to this, only one member can store data at the given instance.
i.e. memory is shared between all data members of union
Example:
#include <stdio.h>
union test1 {
int x;
double y;
} t1;
union test2 {
int x;
char y;
} t2;
int main()
{
printf("Sizeof t1: %d\n",sizeof(t1));
printf("Sizeof t2: %d\n",sizeof(t2));
return 0;
}
5. What's the key difference between a structure and a union in C?

6. How do you declare and initialize an array of structures in C?


Declaring an array of structure is same as declaring an array of fundamental types.
Since an array is a collection of elements of the same type. In an array of structures,
each element of an array is of the structure type.

Example:

struct car
{
char make[20];
char model[30];
int year;
};
Declaring an array of structure car.

struct car arr_car[10];

7. What is a self-referential structure?


The self-referential structure is a structure that points to the same type of structure. It
contains one or more pointers that ultimately point to the same structure
struct structure_name
{
data_type member1;
data_type member2;
struct structure_name* str;
}

8. What will be the size of the following structure?


#include <stdio.h>
struct temp {
int a[10];
char p;
}t;
int main()
{

printf("%d",sizeof(t));
}
9. What will be the output of the following C code?
#include <stdio.h>
void main()
{
struct student
{
int no;
char name[20];
};
struct student s;
s.no = 8;
printf("%d", s.no);
}

10. Difference between Structure and Arrays?

Parameter Structure in C Array in C


Definition A Structure is a data structure An Array is a data structure that can only
that can contain variables of contain variables of the same data type.
different data types.
Memory Structures do not require the Arrays store data in contiguous memory
Allocation data to be stored in consecutive locations, meaning that memory blocks
memory locations. are assigned consecutively.
Accessibility To access elements in a Structur In Arrays, elements can be accessed by
their index.
-e, the name of the specific
element is required.
Pointer Structures do not use internal Arrays use internal Pointers that point to
Pointers. the first element in the array.

PART B & C

1. Explain the concept of a structure in C. Provide an example of how to declare, initialize,


and access members of a structure.

2. Write a C program that prepares a marksheet for 5 students using a structure named
Student with fields for roll number, name, and marks in three subjects.

3. What is a self-referential structure in C? Provide an example of how self-referential


structures can be used to create lists.

4. Explain what structure padding is in C and why it's necessary. Provide an example to
demonstrate how structure padding affects the memory layout of structures.

5. Describe what a union is in C and how it differs from a structure. Provide an example to
illustrate the concept of unions and their memory-saving properties.

6. Describe how nested structures are accessed and manipulated in C. How does the dot
operator work with nested structures?

UNIT 5 FILE PROCESSING


PART A
1. Identify the need of having file concepts.
 Reusability: The data stored in the file can be accessed, updated,and
deleted anywhere and anytime providing high reusability.

 Portability: Without losing any data, files can be transferred toanother


in the computer system.

 Efficient: A large amount of input may be required for some programs.


File handling allows you to easily access a part of a fileusing few
instructions which saves a lot of time and reduces the chance of errors.

 Storage Capacity: Files allow you to store a large amount of datawithout


having to worry about storing everything simultaneously in a program.

2. What is meant by file handling in c?


 File handing in C is the process in which we create, open, read,write,
and close operations on a file.
 C language provides different functions such as fopen(), fwrite(), fread(),
fseek(), fprintf(), etc. to perform input, output, and many different C file
operations in our program
3. Differentiate between two types of file handled in C.
i) Text Files
 A text file contains data in the form of ASCII characters and is
generally used to store a stream of characters.
 Each line in a text file ends with a new line character (‘\n’).
 They are generally stored with .txt file extension.
 Text files can also be used to store the source code.

ii) Binary Files


 A binary file contains data in binary form (i.e. 0’s and 1’s) insteadof ASCII
characters.
 The binary files can be created only from within a program andtheir
contents can only be read by a program.
 More secure as they are not easily readable.
 They are generally stored with .bin file extension
4. List the types of file operations done on a file.
 Creating a new file – fopen() with attributes as “a” or “a+” or “w”or “w+”
 Opening an existing file – fopen()
 Reading from file – fscanf() or fgets()
 Writing to a file – fprintf() or fputs()
 Moving to a specific location in a file – fseek(), rewind()
 Closing a file – fclose()
5. How do you create a file and list the various modes of creating a file?
 FILE *fptr
fptr = fopen(filename, mode);
 Modes :
 r- opens a text file in reading mode
 w - opens or create a text file in writing mode.
 a - opens a text file in append mode
 r+ - opens a text file in both reading and writing mode
 w+ - opens a text file in both reading and writing mode
 a+ - opens a text file in both reading and writing mode

6. Write a C program to create a file in a ‘w’ mode.


#include <stdio.h>
#include <stdlib.h>
int main()

{
FILE* fptr;

fptr = fopen("file.txt", "w");if


(fptr == NULL) {

printf("The file is not opened.”);

else {

printf("The file is created Successfully.");


}

7. How can the file contents can be read?


 For reading from file, we use fscanf() and fgets()
Syntax :

 fgets(char *str, int n, FILE *stream);


 char *str is a pointer to an array where the read values will be stored
 int n is the maximum number of characters that can be read
 FILE *stream is where the inputs are to be taken, it can be replacedwith
stdin if we want to take input from the console.

8. How can the contents be written into a file?


 fprintf() : writes the data into a file
Syntax :

 int fprintf(FILE *fptr, const char *str, ...);


 char *str is the data which has to be written into a file
 FILE *fptr is where the content has to written
9. Why do we need to close a file ?
 A file needs to be closed after a read or write operation to releasethe
memory allocated by the program.
 In C, a file is closed using the fclose() function.
 This returns 0 on success and EOF in the case of a failure.
Syntax :

 fclose(FILE *fptr);

10. Identify the significance of fopen() and differentiate between ‘a’ and ‘w’mode
in file creation.

 A file needs to be opened before processing.


 fopen() creates a file if the mentioned file isn’t existing. If the
mentioned file exists, the file is opened for processing.
FILE* fptr;
fptr = fopen("file.txt", "w");

 fptr moves begins from the start of the file thus replacing the dataalready
present.
FILE* fptr;
fptr = fopen("file.txt", "a");

 fptr moves to end of data already present in the file and starts attaching
the new data. So the previous content of file is not lost.

PART B & C

1. Give brief notes on the following :


a) Need of file handling in C
b) Types of file
c) Various file operations in C with appropriate examples
2. Write a C program to read contents of a file named “file1.txt” and print its
contents to console in uppercase. (Assume the file has onlycharacters).
3. Write a C program to read contents of a file named “file1.txt” and write into
a file named “file2.txt”.
4. Write a C program to read contents of a file named “file1.txt” and append
the contents to another file named “file2.txt”.
5. Write a C program to read contents of 2 files named “file1.txt” and another
file named “file2.txt” and write both the contents into a another file named
“file3.txt”.
6. Explain the concepts of file handling in C. How are files opened and closed,
and how can data be read from and written to files? Provide an example to
demonstrate file handling operations.

You might also like