Programming Principles & Algorithm Important Questions for exam | BCA 1st semester CCSU

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:

  1. Predecessors: Derived from ALGOL, BCPL, and B languages.
  2. Purpose: Initially designed to develop the UNIX operating system.
  3. Portability: C gained popularity due to its ability to run on different systems.
  4. Standardization: In 1989, ANSI standardized C as ANSI C, followed by ISO standardization.
  5. 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:

  1. Must begin with a letter or underscore (_).
  2. Can contain letters, digits, and underscores but no special characters or spaces.
  3. Case-sensitive in C.
  4. 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:

  1. Local Scope: Variable declared within a function/block, accessible only within it.
    void func() {
        int a = 10; // Local scope
    }
    
  2. Global Scope: Variable declared outside all functions, accessible throughout the program.
    int a = 20; // Global scope
    
  3. 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:

  1. Focus on Procedures: Emphasis on functions and sequences of instructions.
  2. Modularity: Programs are broken into smaller modules or functions.
  3. Top-Down Approach: Program execution starts from the main() function.
  4. 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:

  1. Primary Data Types:
    • int, float, char, double
  2. Derived Data Types:
    • Arrays, pointers, functions
  3. 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:

  1. Library Functions: Predefined functions (e.g., printf, scanf).
  2. 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:

  1. auto: Default storage for local variables.
  2. register: Stores variables in CPU registers for faster access.
  3. static: Retains variable value between function calls.
  4. 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:

  1. Definiteness: Clear and precise steps.
  2. Finiteness: Must terminate after a finite number of steps.
  3. Input/Output: Takes input and produces output.

Example:
Algorithm to find the largest of three numbers:

  1. Start
  2. Input three numbers a, b, c
  3. Compare a, b, and c
  4. Output the largest number
  5. 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:

  1. Best Case: Minimum time required.
  2. Worst Case: Maximum time required.
  3. Average Case: Average time required over all inputs.

Example: Linear Search has time complexity O(n)O(n), where nn 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:

  1. Standard Header Files:
    • Examples: <stdio.h>, <math.h>.
    • Provide standard functions like printf, scanf.
  2. User-defined Header Files:
    • Created by programmers (e.g., myheader.h).

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:

  1. Divide: Break the problem into subproblems.
  2. Conquer: Solve each subproblem recursively.
  3. 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:

  1. Using a Temporary Variable:

    void swap(int a, int b) {
        int temp = a;
        a = b;
        b = temp;
    }
    
  2. 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


NOTE : 
  • This is an expected question answer which you have appeared in most of the exams. You will be able to study chapterwise most important topics as per your knowledge and will not be afraid of giving the paper. This is a great tool to improve your preparation.
  • The answers which are given in short cut or less words, here you can write and read them according to your understanding.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.