Programming Principles & Algorithm Important Questions for exam | BCA 1st semester CCSU
1. Explain the history of C language.
The C language was developed by Dennis Ritchie at Bell Labs in 1972. It was created as an improvement of the B language to provide better low-level programming capabilities.
Key Historical Points:
- Predecessors: Derived from ALGOL, BCPL, and B languages.
- Purpose: Initially designed to develop the UNIX operating system.
- Portability: C gained popularity due to its ability to run on different systems.
- Standardization: In 1989, ANSI standardized C as ANSI C, followed by ISO standardization.
- Legacy: Many modern languages like C++, Java, and Python have borrowed features from C.
2. What do you understand by identifiers?
Identifiers are the names used to identify variables, functions, arrays, or other user-defined elements in a program.
Rules:
- Must begin with a letter or underscore (_).
- Can contain letters, digits, and underscores but no special characters or spaces.
- Case-sensitive in C.
- Should not be a keyword (e.g., int, return).
Example:
int age; // Here, "age" is an identifier.
3. Explain the concept of scope of variables.
The scope of a variable determines where the variable can be accessed in a program.
Types of Scope:
- Local Scope: Variable declared within a function/block, accessible only within it.
void func() { int a = 10; // Local scope }
- Global Scope: Variable declared outside all functions, accessible throughout the program.
int a = 20; // Global scope
- Function Scope: Scope of function parameters, limited to the function itself.
4. Why is C language known as a procedural language?
C is called a procedural language because it follows a structured approach, dividing a program into procedures (functions).
Features:
- Focus on Procedures: Emphasis on functions and sequences of instructions.
- Modularity: Programs are broken into smaller modules or functions.
- Top-Down Approach: Program execution starts from the main() function.
- Example:
void add() { printf("Addition"); }
5. What is data types?
Data types specify the type of data a variable can hold in a program.
Types:
- Primary Data Types:
- int, float, char, double
- Derived Data Types:
- Arrays, pointers, functions
- User-defined Data Types:
- Structures, unions, enums
Example:
int age = 25; // Integer data type
float price = 9.99; // Floating-point data type
6. What is flowchart? Explain with suitable examples and symbols.
A flowchart is a visual representation of the sequence of steps in a process or algorithm using symbols.
Common Symbols:
- Oval: Start/End
- Rectangle: Process
- Diamond: Decision
- Arrow: Flow of control
Example:
To find if a number is even or odd:
Start
|
v
Input n
|
v
n % 2 == 0? -- Yes --> Print "Even"
|
No
|
v
Print "Odd"
|
v
End
7. What do you mean by functions? Explain with its types.
A function is a block of reusable code that performs a specific task in a program.
Types of Functions:
- Library Functions: Predefined functions (e.g., printf, scanf).
- User-defined Functions: Created by the user.
Example:
void greet() { // User-defined function
printf("Hello, World!");
}
8. What do you mean by storage classes? Explain.
Storage classes define the scope, lifetime, and visibility of variables in C.
Types:
- auto: Default storage for local variables.
- register: Stores variables in CPU registers for faster access.
- static: Retains variable value between function calls.
- extern: Makes a variable accessible across multiple files.
Example:
static int count = 0; // Retains value between calls
9. What is algorithm? Explain.
An algorithm is a step-by-step procedure to solve a problem.
Characteristics:
- Definiteness: Clear and precise steps.
- Finiteness: Must terminate after a finite number of steps.
- Input/Output: Takes input and produces output.
Example:
Algorithm to find the largest of three numbers:
- Start
- Input three numbers a, b, c
- Compare a, b, and c
- Output the largest number
- End
10. Define time complexity.
Time Complexity is the measure of the time an algorithm takes to complete as a function of the input size.
Types:
- Best Case: Minimum time required.
- Worst Case: Maximum time required.
- Average Case: Average time required over all inputs.
Example: Linear Search has time complexity , where is the size of the array.
11. What are header files? Explain with its types.\
Header files in C provide prewritten code and declarations.
Types:
- Standard Header Files:
- Examples:
<stdio.h>
,<math.h>
. - Provide standard functions like printf, scanf.
- Examples:
- User-defined Header Files:
- Created by programmers (e.g.,
myheader.h
).
- Created by programmers (e.g.,
Example:
#include <stdio.h>
#include "myheader.h"
12. What do you understand by problem-solving techniques? Explain divide and conquer.
Divide and Conquer is a problem-solving technique that splits a problem into smaller subproblems, solves them, and combines the results.
Steps:
- Divide: Break the problem into subproblems.
- Conquer: Solve each subproblem recursively.
- Combine: Merge the results.
Example: Merge Sort uses Divide and Conquer.
13. Explain:
(a) Recursion with example:
Recursion occurs when a function calls itself.
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
(b) Storage Class: Defined in question 8.
(c) Switch Statement: Used for decision-making.
switch(choice) {
case 1: printf("One"); break;
default: printf("Other");
}
14. Explain Swapping.
Swapping is the process of exchanging the values of two variables. Swapping is often used in algorithms like sorting or rearranging data.
Methods of Swapping:
-
Using a Temporary Variable:
void swap(int a, int b) { int temp = a; a = b; b = temp; }
-
Without Temporary Variable (Using Arithmetic Operations):
void swap(int a, int b) { a = a + b; b = a - b; a = a - b; }
Example:
To swap a = 5
and b = 10
:
Before Swap: a = 5, b = 10
After Swap: a = 10, b = 5
15. What do you mean by call-by-value and call-by-reference? Explain both in brief and the difference between them.
Call-by-Value:
- In this method, a copy of the actual arguments is passed to the function.
- Changes made inside the function do not affect the original values.
Example:
void func(int x) {
x = x + 10; // Modifies the copy
}
Call-by-Reference:
- In this method, the memory address of the actual argument is passed.
- Changes made inside the function affect the original values.
Example:
void func(int *x) {
*x = *x + 10; // Modifies the original value
}
Difference:
Call-by-Value | Call-by-Reference |
---|---|
Passes a copy of value. | Passes a reference (address). |
Original variable is not modified. | Original variable is modified. |
Simpler and safer. | Risk of unintended changes. |
16. Write a 'C' program to find the sum of the first n numbers.
Program:
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i; // Add numbers from 1 to n
}
printf("The sum of the first %d numbers is: %d\n", n, sum);
return 0;
}
Output:
For input n = 5
:
The sum of the first 5 numbers is: 15
17. Write a program in C language to find if a given number is prime.
Program:
#include <stdio.h>
int main() {
int n, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n <= 1) {
isPrime = 0; // Numbers <= 1 are not prime
} else {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = 0; // Number is not prime
break;
}
}
}
if (isPrime)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);
return 0;
}
Output:
For input n = 7
:
7 is a prime number.
18. Write a program in C language to find if a number is even or odd.
Program:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("%d is an even number.\n", num);
else
printf("%d is an odd number.\n", num);
return 0;
}
Output:
For input num = 4
:
4 is an even number.
19. Write a 'C' program to find the factorial of a given number.
Program:
#include <stdio.h>
int main() {
int n;
long long factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n < 0) {
printf("Factorial of a negative number doesn't exist.\n");
} else {
for (int i = 1; i <= n; i++) {
factorial *= i; // Multiply numbers from 1 to n
}
printf("The factorial of %d is: %lld\n", n, factorial);
}
return 0;
}
Output:
For input n = 5
:
The factorial of 5 is: 120
20. Write a program in C to find the maximum of three numbers.
Program:
#include <stdio.h>
int main() {
int num1, num2, num3, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
max = num1; // Assume num1 is the largest
if (num2 > max)
max = num2; // Update max if num2 is larger
if (num3 > max)
max = num3; // Update max if num3 is larger
printf("The maximum of %d, %d, and %d is: %d\n", num1, num2, num3, max);
return 0;
}
Output:
For input num1 = 10, num2 = 25, num3 = 15
:
The maximum of 10, 25, and 15 is: 25