Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C QPA - 2024

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

Semester B.C.A.

Degree Examination, February/March 2024


COMPUTER APPLICATIONS
Programming in C

Max. Marks: 60
Time: 2½ Hours

SECTION – A

1. Write any two merits of the C programming language.

● Portability: C programs are platform-independent, which means they can run on


different machines with minimal or no changes. This is especially beneficial for
embedded systems and operating systems.
● Speed and Efficiency: C allows developers to write low-level, hardware-friendly code
while ensuring high performance, making it ideal for system programming.

2. Mention any five 'C' keywords.


C has 32 reserved keywords that perform specific tasks. Examples:

● int: Used to declare integer variables.


● float: Used for floating-point numbers.
● char: Used to declare character variables.
● if: Used to implement conditional logic.
● else: Defines the alternative branch for an if condition.

3. Differentiate between getch() and getchar().

Feature getch() getchar()

Output Does not display input. Displays the input.

Use Often used for single-key input in programs Suitable for standard input
Case like games. operations.

Header <conio.h> (non-standard) <stdio.h> (standard)


Example:

char c = getch(); // Does not show input on the console


char d = getchar(); // Shows the input character

4. What are constants? Give examples.


Constants are fixed values that do not change during program execution. They make the code
more readable and help avoid accidental modifications.
Example:

#define PI 3.14
const int AGE = 25;

Types of constants:

● Integer constants (e.g., 10, -5)


● Floating-point constants (e.g., 3.14, -2.5)
● Character constants (e.g., 'A')

5. Write the general syntax of a conditional operator.


The conditional operator (? :) is a shorthand for if-else statements.
Syntax:

condition ? expression1 : expression2;

Example:

int max = (a > b) ? a : b; // Assigns the larger value to max

6. Differentiate between break and continue statements.

Feature break continue


Purpose Terminates the loop or switch. Skips the remaining code and starts the next
iteration.

Usage Exiting early when a condition is Skipping specific iterations.


met.

Example:

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


if (i == 3) break; // Stops the loop at i=3
printf("%d ", i);
}

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


if (i == 3) continue; // Skips printing 3
printf("%d ", i);
}

7. What is the use of the typedef keyword?


The typedef keyword is used to create a new name (alias) for existing data types. This improves
code readability and maintainability.
Example:

typedef unsigned int uint;


uint age = 30; // Now 'uint' can be used in place of 'unsigned int'

8. Mention the types of arrays.

1. One-dimensional array: Stores data in a linear format.


Example: int arr[5];
2. Two-dimensional array: Stores data in rows and columns.
Example: int mat[3][3];
3. Multi-dimensional array: Extends beyond 2D, used in advanced data structures.
Example: int cube[2][2][2];
9. How to access addresses and values of variables using pointers?
Pointers store the memory address of variables.

● Access Address: Use the & operator.


● Access Value: Use the * operator (dereferencing).
Example:

int a = 10;
int *ptr = &a; // Pointer stores the address of 'a'
printf("Address: %p, Value: %d", ptr, *ptr);

SECTION – B

10. What are the rules to be followed while constructing a variable? Give examples.

● Must start with a letter or underscore. (e.g., _count)


● Should only contain letters, digits, and underscores. (e.g., num_123)
● Cannot use keywords. (e.g., int float; is invalid)
● Case-sensitive: Var and var are different.

Example:

int age = 25;


float _score = 97.5;

11. Explain the for loop with an example.


The for loop repeats a block of code for a fixed number of iterations.
Syntax:

for (initialization; condition; increment/decrement) {


// Code block
}

Example:

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


printf("%d ", i); // Outputs: 1 2 3 4 5
}
12. What are the advantages and disadvantages of arrays?

● Advantages:
○ Stores multiple elements of the same type in a contiguous memory block.
○ Easy to iterate using loops.
● Disadvantages:
○ Fixed size, cannot dynamically expand.
○ Can only store one data type.

13. Explain any four character-handling functions with examples.

Function Description Example

toupper() Converts a character to uppercase. toupper('a'); // 'A'

tolower() Converts a character to lowercase. tolower('A'); // 'a'

isalpha() Checks if the character is alphabetic. isalpha('A'); // true

isdigit() Checks if the character is numeric. isdigit('5'); // true

14. Differentiate between structure and union.

Feature Structure Union

Memory Allocates memory for all members. Shares memory between members.

Access All members can be accessed at once. Only one member at a time.

Example:

struct Student { int id; char name[10]; };


union Data { int id; float score; };

15. What are the advantages and disadvantages of pointers?

● Advantages:
○ Dynamic memory allocation.
○ Access arrays and functions efficiently.
● Disadvantages:
○ Misuse can lead to segmentation faults.
○ Harder to debug.

SECTION – C

16. a) Explain the structure of a C program with an example.

● Documentation Section: Used for comments to describe the program.


● Link Section: Includes necessary header files, like <stdio.h> for input/output operations.
● Global Declarations: Any variables that need to be accessed globally.
● Function Prototype: Tells the compiler about a function before its actual definition.
● Main Function: Contains variable declarations, input/output operations, logic, and
function calls.
● User-Defined Function Definitions: Contains the actual definitions of any custom
functions used in the program.


Example:

16. b) Give the memory size (in bytes) of data types in C.


Memory size may vary based on the system architecture (32-bit or 64-bit). Common sizes on a
32-bit system:

Data Type Memory Size (in bytes)

char 1

int 4

float 4

double 8

long int 4 or 8

short int 2
17. a) Explain nested if with an example.
A nested if is an if statement inside another if statement, used to check multiple conditions.
Syntax:

if (condition1) {
if (condition2) {
// Code block
}
}

Example:

int a = 5, b = 10, c = 15;


if (a < b) {
if (b < c) {
printf("Both conditions are true.");
}
}

17. b) Write a program to find the sum of the first N natural numbers.
Code:

#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i; // Add each natural number
}
printf("Sum of first %d natural numbers is: %d\n", n, sum);
return 0;
}

Output Example:
For N = 5: The program will output 15.
18. a) Explain two-dimensional arrays.
A two-dimensional (2D) array is an array of arrays, often visualized as a table with rows and
columns. It is useful for storing matrices or tabular data.
Syntax:

data_type array_name[rows][columns];

Example:

int mat[2][3] = {
{1, 2, 3}, // Row 1
{4, 5, 6} // Row 2
};
printf("%d", mat[0][1]); // Accesses element at row 0, column 1 (Output: 2)

18. b) Write a C program to read, display, and find the trace of a square matrix.
The trace of a matrix is the sum of its diagonal elements.
Code:

#include <stdio.h>
int main() {
int n, trace = 0;
printf("Enter the size of the square matrix: ");
scanf("%d", &n);

int mat[n][n];
printf("Enter elements of the matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}

// Calculate trace
for (int i = 0; i < n; i++) {
trace += mat[i][i]; // Add diagonal elements
}

printf("Trace of the matrix is: %d\n", trace);


return 0;
}

19. a) Explain the different operations on strings.


String operations in C are performed using <string.h> library functions:

1. Concatenation: Combines two strings (strcat).


Example: strcat(s1, s2); // Appends s2 to s1.
2. Comparison: Compares two strings (strcmp).
Example: strcmp(s1, s2); returns 0 if equal.
3. Length: Finds the length of a string (strlen).
Example: strlen(s1);
4. Copy: Copies one string into another (strcpy).
Example: strcpy(dest, src);

19. b) Write a program to find the quadratic equation roots.


Quadratic equation: ax2+bx+c=0ax^2 + bx + c = 0
Roots:

● Discriminant D=b2−4acD = b^2 - 4ac


● Roots:
○ x1=(−b+D)/2ax1 = (-b + \sqrt{D}) / 2a
○ x2=(−b−D)/2ax2 = (-b - \sqrt{D}) / 2a

Code:

#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, D, root1, root2;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);

D = b * b - 4 * a * c; // Discriminant
if (D >= 0) {
root1 = (-b + sqrt(D)) / (2 * a);
root2 = (-b - sqrt(D)) / (2 * a);
printf("Roots are: %.2f and %.2f\n", root1, root2);
} else {
printf("Roots are imaginary.\n");
}
return 0;
}

20. Discuss the categories of user-defined functions.


User-defined functions in C are categorized based on arguments and return values:

1. Function with no arguments and no return value:

○ Executes a task without receiving inputs or returning a result.


Example:

void greet() {
printf("Hello, World!");
}

2. Function with arguments but no return value:

○ Takes inputs but doesn’t return a value.


Example:

void printSum(int a, int b) {


printf("Sum: %d", a + b);
}

3. Function with no arguments but returns a value:

○ No inputs, but provides a result.


Example:

int getValue() {
return 10;
}

4. Function with arguments and returns a value:


○ Accepts inputs and returns a result.
Example:

int add(int x, int y) {


return x + y;
}

You might also like