KCA 102
KCA 102
Answer: A compiler and an interpreter are both used to translate high-level programming languages
into machine code, but they work differently.
Answer: An identifier is the name assigned to variables, functions, classes, or other program
elements. The following rules must be followed when declaring identifiers:
Answer: Actual arguments: Passed to functions during a function call. It is defined inside the function
call. Example: sum(5,10) Here 5 and 10 are actual arguments.
Formal arguments: Defined in function headers and receive values from actual arguments. It is
defined the function definition. Example: def sum(a,b) Here a and b are actual arguments.
Answer4: The loop has an incorrect condition x = 3 (assignment, not comparison). It will run
infinitely if the compiler allows it.
Function Call Management: Each recursive function call is pushed onto the stack.
Stores Return Addresses: Keeps track of where each function should return after execution.
Stores Local Variables: Each call has its own copy of local variables stored on the stack.
Question 6: There is a two-dimensional array of type integer. Write the statement to display
Answer To display the base address of a two-dimensional integer array in C, you can use the
following statement:
printf("%p", array);
printf("%p", &array);
Question 8: On successful execution of the below code predict the result, if address of
void main( ) {
float a, *b;
b = &a;
b = b+3;
If a is at address 210, and assuming float size = 4 bytes, the output is 210 + (3 * 4) = 222.
Answer: The rectangle() function is commonly used in graphics programming to draw a rectangle.
Syntax: void rectangle(int left, int top, int right, int bottom);
Question 10: Though we can write our program without File handling, what is the need of file
handling in C?
Answer: We can write programs in C without file handling, it is essential when we need to store,
retrieve, and manipulate data persistently. Without file handling, all data stored in variables is lost
when the program terminates because it is stored in RAM (volatile memory).
Question 11: What is the use of a flow chart? List out the symbols used in a flow chart. Draw a
flow chart to find whether the given year is a leap year or not.
A leap year is divisible by 4 but not by 100, except if it is also divisible by 400.
Logic:
**
***
****
*****
Answer: Switch: A control flow statement called a switch statement enables a program to compare
an expression to a set of potential constant values by managing many scenarios according to the
expression's value. When handling several potential scenarios, the switch statement makes the code
easier to comprehend and maintain.
switch (expression) {
case value1:
break;
case value2:
break;
default:
If else:: Conditional control structure called if-else statements are used to allow the execution of a
particular code blocks on the basis that the given condition results in true of false. By running code in
this way i.e. selectively according to whether a certain condition is true or false, we can easily make
judgements.
if (condition) {
//code
else {
//code
}
Question 13: Illustrate the concept of recursion and base condition of recursion. Construct a
Recursion: Recursion is a technique where a function calls itself to solve a problem. It divides a large
problem into smaller subproblems and repeats the process until it reaches a stopping condition.
The base condition is a terminating condition that prevents infinite recursion. Without it, the
function would continue calling itself indefinitely, causing a stack overflow.
Factorial Formula:
N! = N *(N-1)*(N-2)* 1
Example:5! = 5 *4 *3 *2 *1 = 120
Program:
#include <stdio.h>
int factorial(int n) {
if (n == 0) // Base condition
return 1;
int main() {
int num;
scanf("%d", &num);
if (num < 0)
printf("Factorial is not defined for negative numbers.");
else
return 0;
Question 14: How a structure is different from an array? Write a C program to store employee
details such as Empid, Name, Salary and Age for 50 employees and display the employee details
who are getting salary more than 15000.
Answer: Structure: A structure is a user defined data type that groups different data types. It can
hold multiple data types. Each member of the structure is stored separately in memory. Members
are accessed using dot operator. It is used for grouping related data of different types.
Array: An array is a collection of elements of the same data type stored sequentially. It stores only
one data type (e.g. all int or all float). All elements are stored in contiguous memory locations.
Elements are accessed using indexing( [] ). It is used for storing multiple values of the same type.
#include <stdio.h>
struct Employee {
int empid;
char name[50];
float salary;
int age;
};
int main() {
int i, n;
// Input: Number of employees
scanf("%d", &n);
scanf("%d", &emp[i].empid);
printf("Name: ");
scanf("%s", emp[i].name);
printf("Salary: ");
scanf("%f", &emp[i].salary);
printf("Age: ");
scanf("%d", &emp[i].age);
printf("EmpID\tName\t\tSalary\t\tAge\n");
printf("--------------------------------------------------\n");
}
return 0;
Question 15: Develop a C program to copy the contents of one file to another file. The file name
must be pass through command line arguments.
#include <stdio.h>
#include <stdlib.h>
char ch;
if (argc != 3) {
return 1;
if (source == NULL) {
return 1;
fclose(source);
return 1;
fputc(ch, target);
fclose(source);
fclose(target);
return 0;
Question 16: Explain the different data types supported by C language? Explain primitive data
types in terms of memory size, format specifier and range.
Answer: Different Data Types in C Language : C supports various data types that define the nature of
variables. They are categorized into:
Primitive Data Types (Basic types): Primitive data types are the most basic data types that
are used for representing simple values such as integers, float, characters, etc.
Derived Data Types (Arrays, Pointers, Functions) : The data types that are derived from the
primitive or built-in datatypes are referred to as Derived Data Types.
User-Defined Data Types (Structures, Unions, Enums, Typedef) : The user-defined data types
are defined by the user himself.
1. Primitive Data Types in C: Primitive data types are fundamental types that store basic values like
numbers, characters, and floating points.
Question 17: What do you mean by operator precedence and associativity? Explain all bitwise
AND, bit-wise OR and bit-wise XOR operators with suitable example.
Answer: Operator Precedence and Associativity in C: Operator precedence determines the order in
which operators are evaluated in an expression. Operators with higher precedence are evaluated
before those with lower precedence.
Example:
int result = 10 + 5 * 2;
printf("%d", result);
Example:
int x = 5, y = 3, z;
z = x - y + 2;
- and + have the same precedence and left-to-right associativity. So, x - y is evaluated first, then the
result is added to 2.
#include <stdio.h>
int main() {
return 0;
#include <stdio.h>
int main() {
int result = a | b;
#include <stdio.h>
int main() {
int result = a ^ b;
return 0;
Question 18: Write a program to check the input number is an Armstrong number or not. 1
An Armstrong number (also known as a narcissistic number) is a number in which the sum of its
digits raised to the power of the number of digits is equal to the original number.
xyz = x3 + y3 + z3
13 + 53 + 33 = 1 + 125 + 27 = 153
C Program Implementation
#include <stdio.h>
#include <math.h>
originalNum = num;
while (temp != 0) {
temp /= 10;
digits++;
temp = num;
while (temp != 0) {
temp /= 10;
int main() {
int num;
scanf("%d", &num);
if (isArmstrong(num))
else
return 0;
}
Question 19: Identify the use of modular programming? Write a program by using user define
function to check given number is prime or not. Porotype of function should be like this int
is_prime(int).
Function Prototype
int is_prime(int);
C Program Implementation
#include <stdio.h>
if (num % i == 0)
int main() {
int num;
scanf("%d", &num);
if (is_prime(num))
else
return 0;
Question 20: Illustrate the different ways to initialize a string during compile time as well as during
run time. Write a program to count the number of words and number of characters in an input
string.
char str1[] = {'H', 'e', 'l', 'l', 'o', '\\0'}; // Explicit null termination
2. Run-Time Initialization (User Input): Strings can also be initialized at run-time using scanf() or
gets().
char str[100];
scanf("%s", str); // Takes input but stops at space
char str[100];
char str[100];
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int i = 0;
*wordCount = 0;
*charCount = 0;
int inWord = 0;
(*charCount)++;
if (!inWord) {
(*wordCount)++;
inWord = 1;
} else {
inWord = 0;
i++;
int main() {
char str[200];
str[strcspn(str, "\\n")] = 0;
// Displaying results
return 0;
Question 21: Define a pointer. How do you declare and initialize a pointer? Write a program to add
the contents of an integer array using pointer.
Answer: A pointer in C is a variable that stores the memory address of another variable.
Pointers allow efficient memory access and manipulation.
1. Declaration of a Pointer
data_type *pointer_name;
2. Pointer Initialization
int a = 10;
#include <stdio.h>
int sum = 0;
return sum;
int main() {
Question 22: Define scope, visibility, and lifetime of a variable. Explain in detail about all storage
classes supported in C language with reference to scope and lifetime, visibility, and default value.
2. Visibility – Defines which parts of the program can use the variable.
Storage Classes in C: C provides four storage classes that determine a variable’s scope, visibility,
lifetime, and default value.
Example
#include <stdio.h>
void func() {
int main() {
func();
return 0;
}
2. Register (`register`) Storage Class:
Example
#include <stdio.h>
void func() {
int main() {
func();
return 0;
Output:
Register variable x = 5
Visibility: Only within the function (if local) or entire file (if global).
#include <stdio.h>
void counter() {
count++;
int main() {
counter();
counter();
counter();
return 0;
Output:
Count = 1
Count = 2
Count = 3
Scope: Global.
File 1: `file1.c`
#include <stdio.h>
void display() {
File 2: `file2.c`
#include <stdio.h>
int main() {
return 0;
Question 23: How a structure is different from union? Discuss the concept of nested structure.
Write a program for your illustration.
Union: It allocates memory equal to the largest number. It uses less memory but only one member
holds a value at a time. It is used when only one member is needed at a time.
#include <stdio.h>
struct Employee {
int id;
float salary;
};
union Student {
int roll;
float marks;
};
int main() {
return 0;
Output:
Student Roll: 10
#include <stdio.h>
// Defining a nested structure
struct Address {
char city[30];
char state[30];
int pin;
};
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
scanf("%d", &emp.id);
scanf("%s", emp.name);
scanf("%f", &emp.salary);
scanf("%s", emp.address.state);
scanf("%d", &emp.address.pin);
printf("\nEmployee Details:\n");
return 0;
Example Output:
Enter State: NY
Employee Details:
ID: 101
Name: John
Salary: 55000.00
Question 24: What are the drawbacks of static memory allocation? Write a program to allocate
space dynamically to store N numbers. Find the sum and average of these numbers.
Answer: Static memory allocation means allocating memory at compile-time using arrays. However,
it has several limitations:
1. Fixed Size – Memory is allocated before execution, so the size cannot change dynamically.
2. Memory Waste – If the allocated memory is more than required, it results in wasted space.
3. Memory Shortage – If the allocated memory is less than required, it cannot be extended**.
4. Stack Overflow Risk– Large static arrays may cause stack overflow for big programs.
Dynamic memory allocation overcomes these issues by using heap memory via:
#include <stdio.h>
int main() {
int *arr;
int n, i;
scanf("%d", &n);
if (arr == NULL) {
scanf("%d", &arr[i]);
// Calculate average
avg = sum / n;
// Display results
free(arr);
return 0;
}
Example Output:
Enter 5 numbers: 10 20 30 40 50
Sum = 150.00
Average = 30.00
Question 25: What is the use of initgraph() function? Write a program to display 50 concentric
circles.
Answer: Use of `initgraph()` Function in C Graphics: The initgraph() function is used in C graphics
programming (Turbo C/C++ or Borland Graphics Interface). It initializes the graphics system by
loading the graphics driver.
Syntax
Parameters:
2. graphmode – Sets the graphics mode (`getmaxx()` and `getmaxy()` return screen dimensions).
3. pathtodriver – The path where BGI graphics driver files are stored (`""` if in the same directory).
Example of initgraph()
#include <graphics.h>
#include <conio.h>
int main() {
return 0;
}
#include <graphics.h>
#include <conio.h>
int main() {
return 0;
Expected Output
Answer: Types of Constants in C: In C programming, constants are fixed values that do not change
during program execution. Constants can be categorized into the following types:
1. Integer Constants: It represent whole numbers (positive, negative, or zero). It cannot have a
decimal point.
4. String Constants: A sequence of characters enclosed in double quotes (" "). It always ends with a
null character (\0).
Answer: Formatted Input/Output (I/O) functions in C allow structured input and output operations
with specific formatting rules. These functions are primarily:
printf() - Formatted Output Function: It is used to display output with format specifiers.
scanf() – Formatted Input Function: It is Used to read input from the user with format specifiers.
Question 30: Explain how the pointer variable declared and initialized.
Answer: A pointer in C is a variable that stores the memory address of another variable.
2. Intializing a pointer: A pointer is initialized by storing the address of a variable using the
address-of (&) operator.
Answer: An Enumerated Data Type (enum) in C is used to assign meaningful names to integral
constants. It improves code readability and maintainability by allowing programmers to use
symbolic names instead of numbers.
e,g, enum Days {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};
Answer: The fseek() function in C is used to move the file pointer to a specific position in a file. It
allows random access to file contents, meaning you can read or write at any position instead of
sequentially processing the file.
Syntax:
Parameters:
file → Pointer to the file (FILE *).
Question 33: How a high level programming language is useful for a systematic development
ofprograms? Explain.
Answer: High-level programming languages (e.g., C, Python, Java) provide structured, efficient, and
readable ways to develop programs systematically. They abstract low-level hardware details,
making programming easier and more productive.
1. Code Readability and Maintainability: High-level languages use human-readable syntax (if, for,
while), making programs easier to write, read, and debug.
2. Structured and Modular Programming: It supports functions and modular programming, making
large programs easier to manage.
3. Platform Independence and Portability: High-level languages are not dependent on a specific
machine. A C program written on Windows can be compiled and run on Linux/Mac with minimal
modifications.
4. Easier Debugging and Error Handling: High-level languages provide error messages and debugging
tools, making development faster.
5. Faster Development Time: High-level languages reduce manual memory management (e.g.,
Python manages memory automatically). Built-in libraries and functions simplify complex tasks.
Question 34: Explain the difference between for, while and do-while loops.
Answer: A For loop is used when the number of iterations is known. A While loop runs as long as a
condition is true. A Do-While loop runs at least once and then continues if a condition is true.
For loop: The for loop is used when you know in advance how many times you want to execute the
block of code. It iterates over a sequence (e.g., a list, tuple, string, or range) and executes the block
of code for each item in the sequence.
While loop: The while loop is used when you don’t know in advance how many times you want to
execute the block of code. It continues to execute as long as the specified condition is true.
Do while loop: The do-while loop is similar to the while loop, but with one key difference: it
guarantees that the block of code will execute at least once before checking the condition.
Syntax:
do {
} while (condition);
Question 35: Write a program to concatenate two strings without using standard library function.
Answer: Program
#include <stdio.h>
int i = 0, j = 0;
i++;
str1[i] = str2[j];
i++;
j++;
int main() {
gets(str2);
concatenate(str1, str2);
return 0;
Question 36: Explain the local and external variables. Explain different storage classes used in C.
Memory is allocated when the function is called and deallocated when it returns.
e.g.
#include <stdio.h>
void func() {
int main() {
func();
return 0;
External Variables
Accessible from any function in the same or other files (using extern).
Memory is allocated when the program starts and remains allocated until the program
terminates.
e.g.,
#include <stdio.h>
void func() {
int main() {
func();
printf("Value of x in main: %d\n", x);
return 0;
(i) initgraph()
(ii) rectangle()
(iii) line()
Answer: The line() function is commonly used in graphics programming to draw a straight line
between two points
Syntax: void line(int x1, int y1, int x2, int y2);
Parameters:
e.g,
#include <graphics.h>
#include <conio.h>
int main() {
getch();
closegraph();
return 0;
Example:
2. Finiteness
Bad Example:
Good Example:
(i) break
(ii) continue
(iii) goto
Answer: (i) break: The break statement is used to terminate the loop or statement in which it is
present. After that, the control will pass to the statements that are present after the break
statement, if available.
int main() {
if (i == 3) {
return 0;
(ii) continue: continue statement used to skip over the execution part of the loop on a certain
condition. After that, it transfers the control to the beginning of the loop. It skips its following
statements and continues with the next iteration of the loop.
e.g,
#include <stdio.h>
int main() {
if (i == 3) {
return 0;
}
(iii) goto: Goto statement is used to transfer control to the labeled statement. The label is the valid
identifier and is placed just before the statement from where the control is transferred.
e.g,
#include <stdio.h>
int main() {
int x = 1;
if (x == 1) {
skip:
return 0;
Question 40: Write a program to accept elements of an array from user and sort and display them
in ascending order.
Answer: Program:
#include <stdio.h>
int temp;
temp = arr[j];
arr[j + 1] = temp;
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
bubbleSort(arr, n);
// Displaying the sorted array
return 0;
(i) malloc()
(ii) calloc()
(iii) realloc()
(iv) free()
Answer: Dynamic memory allocation allows programs to allocate memory at runtime using the
heap. The following functions from <stdlib.h> are used for dynamic memory management:
Question 42: Discuss about the following operators in C language with example.
Answer: (ii) Increment Operator: These operators increase or decrease the value of a variable by 1.
e.g,
#include <stdio.h>
int main() {
int x = 5, y;
x = 5; // Reset x
x = 5; // Reset x
return 0;
(iii) Logical Operator: Logical operators are used for Boolean (true/false) operations, mainly in
conditional statements.
Logical Operators in C
` ` OR
e.g,
#include <stdio.h>
int main() {
int a = 5, b = -3;
printf("(a > 0) && (b > 0) = %d\n", (a > 0) && (b > 0)); // AND
return 0;
}
Question 43: What is type conversion? Explain two types of conversion with suitable examples.
Answer: Type conversion is the process of converting a variable from one data type to another in C.
It happens in two ways:
e.g.
#include <stdio.h>
int main() {
int a = 5;
float b = 2.5;
return 0;
Also known as type casting, explicit conversion is forced by the programmer using the cast operator
((type)).
Syntax:
(type) expression;
e.g.
#include <stdio.h>
int main() {
int a = 10, b = 3;
float result;
return 0;
Question 44: How to declare and initialize a Two-dimensional array? Discuss with examples.
Answer: A two-dimensional (2D) array is an array of arrays. It represents data in rows and columns,
like a table.
1. Declaring a 2D Array
Syntax:
data_type array_name[rows][columns];
e.g.
e.g.
#include <stdio.h>
int main() {
int matrix[2][3];
// Taking input
scanf("%d", &matrix[i][j]);
printf("Matrix:\n");
return 0;
Answer: (ii) Array of structure: An array of structures stores multiple structure variables of the same
type in an array.
Syntax:
e.g.,
#include <stdio.h>
struct Student {
char name[20];
int roll_no;
float marks;
};
int main() {
printf("Student Details:\n");
return 0;
Output:
Student Details:
Answer: Preprocessor directives in C start with # and are processed before compilation. They are
used to include files, define macros, and control compilation flow.
#include <stdio.h>
int main() {
return 0;
e.g.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
#include <stdio.h>
#define DEBUG // Define DEBUG mode
int main() {
#ifdef DEBUG
#else
printf("Normal mode\n");
#endif
return 0;
Answer: Keywords: Keywords are reserved words in C that have a predefined meaning and cannot
be used as variable names. They are used for programming constructs such as loops, conditions, and
data types. Some of the keywords are int, float, char, etc.
Identifiers: Identifiers are user-defined names for variables, functions, arrays, structures, etc. They
must be unique and follow naming rules.
Step-by-step evaluation:
Final Value: 0
Step-by-step evaluation:
Final Value: 0
void main()
int a, b *p;
a = 10;
p=&a; b = *p +20
printf(“%d”, b);
int a, b *p;
o Correction:
int a, *p, b;
2. Missing ; after b = *p + 20
b = *p + 20
o Correction:
b = *p + 20;
printf(“%d”, b);
o Correction:
printf("%d", b);
Corrected code:
#include <stdio.h>
a = 10;
Output: 30
Question 50: An array is declared as int a [100]; If the base address of the array is 5000, what will
be the address of a[59]. ( Consider 16 bit machine architecture)
Given Information:
Array declaration:
c
CopyEdit
int a[100];
where:
Question 51: Difference between dot operator and -> operator to access structure objects.
Answer: The feof() function is used to check whether the file pointer to a stream is pointing to the
end of the file or not. It returns a non-zero value if the end is reached, otherwise, it returns 0.
Syntax: feof(fptr);
Parameters
Return Value
Otherwise, it returns 0.
Question 52: Write an algorithm to find the roots of quadratic equation.
ax2+bx+c=0ax^2 + bx + c = 0ax2+bx+c=0
where a, b, and c are coefficients, and the roots (x values) are found using the quadratic formula:
Algorithm
Step 1: Start
Case 1 (D > 0): Two real and distinct roots x1=−b+D2a,x2=−b−D2ax_1 = \frac{-b + \sqrt{D}}
{2a}, \quad x_2 = \frac{-b - \sqrt{D}}{2a}x1=2a−b+D,x2=2a−b−D
Step 6: Stop.
Question 53: How entry controlled loop is different from exit controlled loop? Write a program to
print the pattern
2 3
4 5 6
7 8 9 10
Answer: entry-controlled loop is for and while and exit controlled loop is do while
int main() {
printf("%d\t", num);
return 0;
Question 54: What do you mean by function prototype? How call by value is different from call by
reference? Illustrate call by reference with a suitable example.
Answer: A function prototype in C is a declaration of a function that tells the compiler about:
Syntax:
#include <stdio.h>
// Function prototype
return 0;
// Function definition
return a + b;
Modification of ❌ Changes inside the function do not ✅ Changes inside the function affect
Original Value affect the original variable. the original variable.
Memory Usage Uses more memory (copies values). Uses less memory (passes addresses).
Function Parameter
Uses normal variables as parameters. Uses pointers as parameters.
Type
#include <stdio.h>
// Function prototype
void swap(int *x, int *y);
int main() {
int a = 5, b = 10;
return 0;
*x = *y;
*y = temp;
Output:
Question 55: Write a program to implement the menus based basic calculator using switch case.
int main() {
int choice;
// Display Menu
printf("6. Exit\n");
scanf("%d", &choice);
if (choice == 6) {
break;
switch (choice) {
case 1:
case 2:
break;
case 3:
break;
case 4:
if (num2 == 0) {
} else {
break;
case 5:
if ((int)num2 == 0) {
} else {
break;
default:
}
}
return 0;