BCA C Programming Question Paper Solution 2018 , 2019 , 2020 - 2nd Semester
BCA C Programming Question Paper Solution 2018 - 2nd Semester
Bachelor in Computer Applications
Course Title: C Programming
Code No: CACS151
Semester: II
Full Marks: 60
Pass Marks: 24
Time: 3 hours
Candidates are required to answer the questions in their own words as far as possible. Attempt all the questions.
Group A: Circle (O) the correct answer. [10x1 = 10]
BCA C Programming Question Paper Solution 2019 - 2nd Semester
Bachelor in Computer Applications
Course Title: English II
Code No: CACS151
Semester: II
Full Marks: 60
Pass Marks: 24
Time: 3 hours
Candidates are required to answer the questions in their own words as far as possible. Attempt all the questions.
Group B: Attempt any SIX questions. [6x5 = 30]
2. What is computer programming? Differentiate between the top-down and bottom-up approach to programming.
Computer programming refers to the process of writing instructions for a computer to execute. It involves designing, writing, testing, debugging, and maintaining the source code of computer programs. Programming languages such as Java, C, C++, Python, PHP, Perl, and Ruby are used for this purpose.
Top-Down Approach: - Divides a problem into smaller units and solves it. - Redundancy exists due to the division of the problem. - Communication among steps is not mandatory. - Structured programming languages like C use it. - Relation among modules is not always required. - Primarily used in implementation, test case generation, debugging, and module documentation. Bottom-Up Approach: - Starts from solving small modules and adds them together. - Redundancy is eliminated by thoroughly analyzing individual modules. - Communication among modules is mandatory. - Object-oriented programming (OOP) languages like C++ and Java use it. - Modules must be related for better communication and workflow. - Primarily used in testing.
3. What is the purpose of the sizeof operator? Write a C program to swap the values of two integers without using a third variable.
Answer:
The sizeof operator in C is used to determine the size of a variable or data type in bytes. It returns the amount of memory allocated to the specified data type or variable.
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("Size of int: %lu\n", sizeof(int));
printf("Size of float: %lu\n", sizeof(float));
printf("Size of char: %lu\n", sizeof(char));
printf("Size of double: %lu\n", sizeof(double));
return 0;
}
Program to swap values of two integers without using a third variable:
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("Before swap: a=%d, b=%d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After swap: a=%d, b=%d\n", a, b);
return 0;
}
```
4. Write a program in C to find all possible roots of the quadratic equation ax^2 + bx + c = 0.
Answer:
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b, and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b b - 4 a c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 a);
root2 = (-b - sqrt(discriminant)) / (2 a);
printf("Root 1 = %.2lf and Root 2 = %.2lf\n", root1, root2);
} else if (discriminant == 0) {
root1 = root2 = -b / (2 a);
printf("Root 1 = Root 2 = %.2lf\n", root1);
} else {
realPart = -b / (2 a);
imagPart = sqrt(-discriminant) / (2 a);
printf("Root 1 = %.2lf + %.2lfi and Root 2 = %.2f - %.2fi\n", realPart, imagPart, realPart, imagPart);
}
return 0;
}
5. Why is an array required in programming? Write a C program to input the age of 30 employees into an array and find out the second largest age from the array.
Answer:
Arrays are required in programming to store and manipulate multiple values of the same data type under a single name. They provide a convenient way to store large amounts of data and access them using indexing.
Program to input the age of 30 employees into an array and find out the second largest age:
#include <stdio.h>
int main() {
int ages[30], i, first, second;
printf("Enter the ages of 30 employees:\n");
for (i = 0; i < 30; i++) {
scanf("%d", &ages[i]);
}
first = second = INT_MIN;
for (i = 0; i < 30; i++) {
if (ages[i] > first) {
second = first;
first = ages[i];
} else if (ages[i] > second && ages[i] < first) {
second = ages[i];
}
}
printf("The second largest age is: %d\n", second);
return 0;
}
6. What is a recursive function? Write a C program to generate the Fibonacci series up to the 13th term using a recursive function.
Answer:
A recursive function is a function that calls itself directly or indirectly in order to solve a problem. It breaks down the problem into smaller sub-problems and calls itself to solve each sub-problem.
Program to generate the Fibonacci series up to the 13th term using recursion:
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int i;
printf("Fibonacci series up to 13th term:\n");
for (i = 0; i < 13; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
```
7. What is the advantage of a pointer? Write a C program to store 10 integers into memory and find out the minimum and maximum using dynamic memory allocation (DMA).
Answer:
Pointers in C provide several advantages, including:
- Efficient memory usage
- Ability to work with dynamic memory allocation
- Accessing array elements more efficiently
- Facilitating dynamic data structures like linked lists, trees, and graphs
Program to store 10 integers into memory and find out the minimum and maximum using dynamic memory allocation:
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr, i, min
, max;
arr = (int)malloc(10 sizeof(int));
printf("Enter 10 integers:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
min = max = arr[0];
for (i = 1; i < 10; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
printf("Minimum number: %d\n", min);
printf("Maximum number: %d\n", max);
free(arr);
return 0;
}
```
8. What is the use of initgraph() in a C program? Write a program to draw a line, circle, and rectangle using graphic functions.
Answer:
The initgraph() function in C is used to initialize the graphics system by loading the appropriate graphics driver and setting the system to graphics mode. It also resets or initializes all graphics settings such as color, palette, and current position to their default values.
Program to draw a line, circle, and rectangle using graphic functions:
#include <graphics.h>
#include <stdio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
// Draw a line
line(100, 100, 300, 100);
// Draw a circle
circle(200, 200, 50);
// Draw a rectangle
rectangle(50, 50, 150, 150);
getch();
closegraph();
return 0;
}
Group C : Attempt any two questions. [10*2=20]
9. What is a structure? Write a C program to enter the id, name, and address of 25 employees into a structure variable called 'employee' and sort them in alphabetical order of their names.
Answer:
A structure in C is a user-defined data type that allows storing a group of related data items under a single name. Each data item within a structure is called a member.
Program to enter the id, name, and address of 25 employees into a structure variable and sort them alphabetically by name:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee {
int id;
char name[50];
char address[100];
};
int main() {
int i, j, n = 25;
struct Employee employees[n], temp;
printf("Enter details of 25 employees:\n");
for (i = 0; i < n; i++) {
printf("Employee %d:\n", i + 1);
printf("Enter id: ");
scanf("%d", &employees[i].id);
printf("Enter name: ");
scanf("%s", employees[i].name);
printf("Enter address: ");
scanf("%s", employees[i].address);
}
// Sorting employees alphabetically by name
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (strcmp(employees[j].name, employees[j + 1].name) > 0) {
temp = employees[j];
employees[j] = employees[j + 1];
employees[j + 1] = temp;
}
}
}
printf("\nSorted list of employees by name:\n");
for (i = 0; i < n; i++) {
printf("Id: %d, Name: %s, Address: %s\n", employees[i].id, employees[i].name, employees[i].address);
}
return 0;
}
10. Is the 'break' keyword mandatory in a switch statement? Write a program to perform basic arithmetic operations (+, -, , /) using a switch-case statement.
Answer:
Yes, the 'break' keyword is mandatory in a switch statement to terminate the execution of a particular case and prevent fall-through to the next case.
Program to perform basic arithmetic operations using a switch-case statement:
#include <stdio.h>
int main() {
int a, b;
char op;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Enter the operation (+, -, , /): ");
scanf(" %c", &op);
switch(op) {
case '+':
printf("Sum: %d\n", a + b);
break;
case '-':
printf("Difference: %d\n", a - b);
break;
case '':
printf("Product: %d\n", a b);
break;
case '/':
if (b != 0) {
printf("Quotient: %d\n", a / b);
} else {
printf("Division by zero error!\n");
}
break;
default:
printf("Invalid operation!\n");
break;
}
return 0;
}
11. What do you mean by the Software Development Life Cycle (SDLC)? Explain the waterfall model with its advantages and disadvantages.
Answer:
The Software Development Life Cycle (SDLC) is a framework that outlines the processes involved in the development of software from inception to retirement. It consists of a set of phases, each with its own specific goals and deliverables. The typical phases include Planning, Requirements, Design, Implementation, Testing, Deployment, and Maintenance.
Waterfall Model:
The waterfall model is a linear sequential approach to software development in which progress flows downwards through several phases, similar to a waterfall cascading down. Each phase must be completed before the next phase begins.
Advantages of the Waterfall Model:
- Simple and easy to understand and use.
- Well-defined deliverables and milestones make it easy to measure progress.
- Provides a structured approach to development, making it easier to manage.
Disadvantages of the Waterfall Model:
- Lack of flexibility: Changes in requirements are difficult and costly to implement once a phase is completed.
- High risk: It assumes that all requirements can be defined upfront, which may not be practical.
- No working software until late in the project: Stakeholders may not see any tangible results until the end of the project, leading to potential misunderstandings and dissatisfaction.
SDLC provides a structured approach to software development, ensuring that projects are completed efficiently, on time, and within budget. However, it's essential to select the appropriate model based on project requirements and constraints.
BCA C Programming Question Paper Solution 2020 - 2nd Semester
Bachelor in Computer Applications
Course Title: C Programming
Code No: CACS151
Semester: II
Full Marks: 60
Pass Marks: 24
Time: 3 hours
Candidates are required to answer the questions in their own words as far as possible. Attempt all the questions.
Group A: 1. Circle (O) the correct answer. [10x1 = 10]
i. Which compilation unit is responsible for adding header files content in the source code?
a) Linker
b) Compiler
c) Assembler
d) Preprocessor
ii. What is the meaning of the following line of code? void sum (int, int);
a) Sum is a function which takes int arguments
b) Sum is a function which takes two int arguments and returns void
c) It will produce a compilation error
d) It will produce a runtime error
iii. Which header file is essential for using strcmp() function?
a) stdlib.h
b) string.h
c) text.h
d) strcmp.h
iv. Which of the following is not a branching statement in C?
a) if
b) else
c) return
d) continue
v. Array can be considered as set of elements stored in consecutive memory locations but having
a) Same size
b) Different data type
c) Same scope
d) None of the above
vi. If int a=5, b=8, then what will be the value of int c = b++ - a--?
a) 3
b) 5
c) 2
d) Can't comment
vii. Suppose you are given a piece of code: circle(100,100,50); in C, then what is meant by 50?
a) Center
b) Diameter
c) Perimeter
d) Radius
viii. Suppose that the code:
struct student
{
char name[20];
int age;
char address[25];
} std;
Then, what is the size of std?
a) 47 bytes
b) 49 bytes
c) 25 bytes
d) 20 bytes
ix. Suppose you are given a piece of code:
int a = 5, b;
b = &a;
printf("%u",b);
Then, what will be the output?
a) 5
b) Address of b
c) Address of a
d) Garbage value
x. Which of the following is not a valid file opening mode?
a) read
b) write
c) execute
d) append
Group B : Attempt any SIX questions.[6×5=30]
2. What are keywords? Explain the compiling process of C program. [1+4]
Answer:
Keywords:
These are the reserved words having predefined meanings in C. We cannot use keywords as programmer-defined identifiers. These keywords can be used only for their intended purpose. All keywords have fixed meaning and these meanings cannot be changed. Keywords serve as building blocks for program statements. All keywords must be written in lowercase. In standard C language, defined by ANSI, there are 32 keywords.
E.g. int, float, char, for, while etc.
Compiling process of C program
The compilation is a process of converting the source code into object code. It is done with the help of the compiler. The compiler checks the source code for the syntactical or structural errors, and if the source code is error-free, then it generates the object code. The compilation process can be divided into four steps, i.e., Pre-processing, Compiling,
Assembling, and Linking.
Source code
1. Preprocessor: The source code is passed to the preprocessor, which expands the code by processing directives like #include, #define, etc.
2. Compiler: The preprocessed code is then passed to the compiler, which converts it into assembly code.
3. Assembler: The assembly code is converted into object code by the assembler.
4. Linker: Finally, the linker combines the object code with other necessary object files and libraries to generate the executable code.
3. Differentiate between iteration and recursion
Answer:
Here are the differentiations between iteration and recursion presented in bullet points:
Iteration:
1. Iteration involves executing a set of instructions repeatedly in a loop until a certain condition is met or a specified number of iterations is reached.
2. It utilizes loop control mechanisms such as for loops, while loops, or do-while loops to repeat the instructions.
3. Typically consumes less memory as it does not involve function calls that create additional overhead on the stack.
4. Generally easier to understand and implement for simple repetitive tasks.
5. Examples of iterative constructs include for loops, while loops, and do-while loops in programming languages like C, Java, and Python.
Recursion:
1. Recursion is a process where a function calls itself, directly or indirectly, to solve a problem.
2. It breaks down the original problem into smaller subproblems with each recursive call until a base case is reached.
3. May use more memory because each recursive call creates a new activation record on the stack, potentially leading to stack overflow.
4. Well-suited for solving problems that can be broken down into smaller identical subproblems, such as factorial calculation or Fibonacci series generation.
5. Examples of recursive functions include calculating factorial, generating Fibonacci series, traversing tree structures, and solving problems like the Tower of Hanoi.
4. Define an array. Write a program to generate the following output using a loop. [1+4]
Answer:
An array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched.
Program to generate the following output:
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i=1; i<= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}
5. Define an operator. Explain any four types of operators available in C? [1+4]
Answer:
Operator:
Operators can be defined as basic symbols that help us work on logical and mathematical operations. Operators in C are tools or symbols that are used to perform mathematical operations concerning arithmetic, logical, conditional and bitwise operations. Arithmetic, Relational, Logical, Assignment, and Unary are the main types of operators available in C.
Arithmetic Operators:
All the basic arithmetic operations can be carried out in C. Unary operations operate on a single operand. Examples: + ,-, /, %, ++, --.
Relational Operators:
These operators compare the relationship between operands and return either true or false. Examples: ==, !=, >, <, >=, <=.
Logical Operators:
These operators are used to perform logical operations. Examples: && (logical AND), || (logical OR), ! (logical NOT).
Assignment Operators:
These operators are used to assign values to variables. Examples: =, +=, -=, =, /=, %=.
Unary Operators:
These operators operate on a single operand. Examples: ++, --, +, -, !.
6. Why is a function required in C programming? Write a program to find the smallest number from the array using a function. [1+4]
Answer:
Functions are required in C programming for several reasons:
- To improve the readability of code.
- To improve code reusability, as the same function can be used in different programs.
- To simplify debugging, as errors are easier to trace in modularized code.
- To reduce code redundancy and thus, the overall size of the code.
Program to find the smallest number from the array using a function:
#include<stdio.h>
void smallest(int a[], int n); // function prototype
int main()
{
int a[100], i, n;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i=0; i<n; ++i)
{
scanf("%d", &a[i]);
}
smallest(a, n); // function calling
return 0;
}
void smallest(int a[], int n)
{
int min = a[0];
for (int i=1; i<n; ++i)
{
if (a[i] < min)
{
min = a[i];
}
}
printf("Smallest Element: %d\n", min);
}
7. Why is DMA used in C language? Write a program to enter the age of 20 students and count ages between 18 and 25 from the array using DMA function. [1+4]
Answer:
DMA (Dynamic Memory Allocation) is used in C language to allocate memory during runtime. It allows the programmer to dynamically allocate memory as per the requirement during program execution. DMA functions are helpful when the size of data is not known at compile time or when memory needs to be allocated or deallocated based on certain conditions.
Program to enter the age of 20 students and count ages between 18 and 25 using DMA function:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int age;
int i, count = 0;
// Allocate memory for 20 elements dynamically
age = (int)malloc(20 sizeof(int));
printf("Enter ages of 20 students:\n");
for(i = 0; i < 20; i++)
{
scanf("%d", &age[i]);
}
// Count ages between 18 and 25
for(i = 0; i < 20; i++)
{
if(age[i] >= 18 && age[i] <= 25)
{
count++;
}
}
printf("Number of Ages between 18 to 25: %d\n", count);
// Free dynamically allocated memory
free(age);
return 0;
}
8. Write a C program to generate the following output using graphics functions. [5]
Answer:
#include <graphics.h>
#include <stdio.h>
int main(void)
{
int gdriver = DETECT, gmode;
int x = 200, y = 200;
// Initialize graphics mode
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
// Display text
outtextxy(x, y, "BCA Program");
// Close graphics mode
closegraph();
return 0;
}
Group C : Attempt any two questions [2×10=20]
9. What is a union? Write a C program to enter the bid, title, price, pages of 100 books into a structure variable called book and sort them in descending order based on their title using pointers. [2+8]
Answer:
Union:
A union is a data type in C programming that allows different data types to be stored in the same memory locations. Union provides an efficient way of using memory, as only one of its members can be accessed at a time.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book
{
char
title[100];
int bid;
float price;
int pages;
};
int main()
{
int i, n = 2, j;
struct Book b[n];
char temp[100];
for(i = 0; i < n; i++)
{
printf("Enter details of Book %d:\n", i+1);
printf("Enter the title: ");
scanf("%s", b[i].title);
printf("Enter the book id: ");
scanf("%d", &b[i].bid);
printf("Enter the price of the book: ");
scanf("%f", &b[i].price);
printf("Enter the number of pages: ");
scanf("%d", &b[i].pages);
}
// Sort the books based on title
for(i = 0; i < n; i++)
{
for(j = 0; j < n - i - 1; j++)
{
if(strcmp(b[j].title, b[j + 1].title) < 0)
{
strcpy(temp, b[j].title);
strcpy(b[j].title, b[j + 1].title);
strcpy(b[j + 1].title, temp);
}
}
}
// Print the sorted books
printf("\nBooks sorted by title in descending order:\n");
for(i = 0; i < n; i++)
{
printf("Title: %s, Book ID: %d, Price: %.2f, Pages: %d\n", b[i].title, b[i].bid, b[i].price, b[i].pages);
}
return 0;
}
10. What is the use of a data file in C? Write a C program to accept 100 numbers from the user and store them in odd.txt file (if the number is odd) or even.txt file (if the number is even), then display odd numbers reading from odd.txt file. [1+9]
Answer:
A data file in C is used to store information permanently on an auxiliary storage device. It allows us to store data so that it can be accessed later and modified whenever necessary. Data files are essential for preserving data even after the program terminates, transferring data between different computers, and storing a large number of data efficiently.
#include<stdio.h>
int main()
{
FILE all, even, odd;
int number, i, records;
// Open a file for writing
all = fopen("ANYNUMBER.txt", "w");
printf("INPUT THE TOTAL NUMBER OF RECORDS THAT YOU WANT TO ENTER: ");
scanf("%d", &records);
// Write numbers to the file
printf("Enter %d numbers:\n", records);
for(i = 1; i <= records; i++)
{
scanf("%d", &number);
fprintf(all, "%d ", number);
}
fclose(all);
// Open files for reading
all = fopen("ANYNUMBER.txt", "r");
even = fopen("even.txt", "w");
odd = fopen("odd.txt", "w");
// Read numbers from the file and write to even.txt or odd.txt accordingly
while(fscanf(all, "%d", &number) != EOF)
{
if(number % 2 == 0)
{
fprintf(even, "%d ", number);
}
else
{
fprintf(odd, "%d ", number);
}
}
fclose(all);
fclose(even);
fclose(odd);
// Display odd numbers from odd.txt
printf("Odd numbers:\n");
odd = fopen("odd.txt", "r");
while(fscanf(odd, "%d", &number) != EOF)
{
printf("%d ", number);
}
fclose(odd);
return 0;
}
11. What do you mean by System Design Tools? Explain any four system design tools with suitable examples.
Answer:
System design tools are techniques or methodologies used in system development to define the architecture, components, modules, interfaces, and data of a system to meet specified requirements. These tools help in designing the blueprint of a system before actual construction begins. Some common system design tools are:
a. Context Diagram: A context diagram is a visual representation that shows the interaction between a system and its environment. It illustrates the scope and boundaries of the system and its interactions with external entities. Example: A context diagram for an online shopping system showing interactions with customers, payment gateways, and inventory systems.
b. Data Flow Diagram (DFD): A DFD is a graphical representation of the flow of data through a system. It shows how data is input, processed, and output within the system. DFDs are used to model the functional aspects of a system and identify the processes, data stores, and data flows. Example: A DFD for a banking system showing the flow of customer transactions, account balances, and ATM withdrawals.
c. Entity-Relationship Diagram (E-R Diagram): An E-R diagram is a visual representation of the entities, attributes, and relationships within a database. It shows how data is organized and related to each other in a database system. E-R diagrams are used to design relational databases and understand the structure of the data. Example: An E-R diagram for a university database showing entities like students, courses, and instructors, and their relationships.
d. Algorithm: An algorithm is a step-by-step procedure or set of rules for solving a problem or performing a task. It describes the logic or flow of control in a program without specifying the syntax of a particular programming language. Algorithms are used to design efficient and correct solutions to problems in various domains. Example: An algorithm for sorting an array of numbers using the bubble sort technique.
These system design tools help in analyzing, designing, and implementing complex systems by providing a structured approach to problem-solving and decision-making. They enable system designers to understand the requirements, constraints, and interactions of a system and develop solutions that meet the desired objectives.
0 Comments