0% found this document useful (0 votes)
8 views23 pages

Programming in C Two Mark Question With ANswer

The document contains a series of two-mark questions and answers covering fundamental concepts in C programming, including programming paradigms, data types, arrays, string operations, and algorithms like selection sort, linear search, and binary search. It also discusses modular programming, function prototypes, recursion, and built-in functions. Each section provides concise definitions, examples, and explanations to aid understanding of C programming principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views23 pages

Programming in C Two Mark Question With ANswer

The document contains a series of two-mark questions and answers covering fundamental concepts in C programming, including programming paradigms, data types, arrays, string operations, and algorithms like selection sort, linear search, and binary search. It also discusses modular programming, function prototypes, recursion, and built-in functions. Each section provides concise definitions, examples, and explanations to aid understanding of C programming principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Two Mark Question with Answer

Unit - I
Introduction to Programming Paradigms
1. Q: What is a programming paradigm? A: A programming paradigm is a style or way of
programming. Examples include procedural, object-oriented, and functional
programming.
2. Q: Name a procedural programming language. A: C is a procedural programming
language.
3. Q: What distinguishes procedural programming from object-oriented programming? A:
Procedural programming focuses on functions or procedures to operate on data, while
object-oriented programming organizes data and functions into objects.
Applications of C Language
4. Q: Name an application of the C language in system programming. A: C is used for
developing operating systems like UNIX.
5. Q: Why is C language popular in embedded systems? A: C is popular in embedded
systems because it allows direct manipulation of hardware and memory.
Structure of a C Program
6. Q: What is the first function that gets called in a C program? A: The main() function is
the first function that gets called.
7. Q: What is the purpose of the #include directive in C? A: The #include directive is used
to include the contents of a file or library in the program.
C Programming: Data Types
8. Q: Name the primary data types in C. A: The primary data types in C are int, char, float,
and double.
9. Q: What is the size of a char data type in C? A: The size of a char data type in C is 1 byte.
Constants
10. Q: How do you define a constant in C? A: You can define a constant using the const
keyword or #define preprocessor directive.
11. Q: Give an example of defining a constant using #define. A: #define PI 3.14
Enumeration Constants
12. Q: What is an enumeration in C? A: An enumeration is a user-defined data type
consisting of a set of named integer constants.
13. Q: Provide an example of an enumeration. A: enum week {Sunday, Monday, Tuesday,
Wednesday, Thursday, Friday, Saturday};
Keywords
14. Q: What is a keyword in C? A: A keyword is a reserved word that has a predefined
meaning in C and cannot be used as an identifier.
15. Q: Name three keywords in C. A: int, return, void
Operators: Precedence and Associativity
16. Q: What is operator precedence? A: Operator precedence determines the order in which
operators are evaluated in an expression.
17. Q: What is associativity of operators? A: Associativity defines the direction (left-to-right
or right-to-left) in which an expression is evaluated.
Expressions
18. Q: What is an expression in C? A: An expression is a combination of variables, constants,
and operators that yields a value.
19. Q: Give an example of a simple arithmetic expression. A: a + b * c
Input/Output Statements
20. Q: Which function is used to read input from the user in C? A: The scanf function is used
to read input from the user.
21. Q: Which function is used to print output to the console in C? A: The printf function is
used to print output to the console.
Assignment Statements
22. Q: What is an assignment statement in C? A: An assignment statement assigns a value to
a variable, e.g., a = 5;.
23. Q: Can you chain assignment statements in C? Provide an example. A: Yes, you can
chain assignment statements, e.g., a = b = c = 0;.
Decision Making Statements
24. Q: What is the syntax of an if statement in C?
A: if (condition) {
// statements
}
25. Q: How do you write an if-else statement in C?
A: if (condition) {
// statements
} else {
// statements
}
Switch Statement
26. Q: What is a switch statement used for in C?
A: A switch statement is used for selecting one of many code blocks to be executed.

27. Q: Provide a basic example of a switch statement.


A: switch (variable) {
case 1:
// statements
break;
case 2:
// statements
break;
default:
// statements
}

Looping Statements
28. Q: What is the syntax of a for loop in C?
A: for (initialization; condition; increment) {
// statements
}
29. Q: How do you write a while loop in C?
A: while (condition) {
// statements
}
Preprocessor Directives
30. Q: What is a preprocessor directive in C?
A: A preprocessor directive gives instructions to the compiler to preprocess the information
before actual compilation starts.
31. Q: Name two common preprocessor directives.
A: #include and #define
Compilation Process
32. Q: What are the four main stages of the C compilation process?
A: Preprocessing, compilation, assembly, and linking.
33. Q: What does the linker do in the compilation process?
A: The linker combines multiple object files into a single executable file and resolves
references to external symbols.
Unit – II
Introduction to Arrays: Declaration, Initialization
Q: What is an array in C programming?
A: An array is a collection of elements of the same data type, stored in contiguous memory
locations. It allows for efficient access and manipulation of data elements using an index.
Q: How do you declare an array in C?
A: An array is declared by specifying the data type, the array name, and the size in square
brackets. For example:
int numbers[10];
Q: How do you initialize an array at the time of declaration?
A: An array can be initialized at the time of declaration by providing values in curly braces. For
example:
int numbers[5] = {1, 2, 3, 4, 5};
One Dimensional Array
Q: How do you access elements of a one-dimensional array in C?
A: Elements of a one-dimensional array are accessed using the index, with the first element
having an index of 0. For example:
int numbers[5] = {1, 2, 3, 4, 5};
int first = numbers[0]; // Accesses the first element
Q: Write a simple program to find the sum of all elements in a one-dimensional array.
A: #include <stdio.h>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int sum = 0;
int size = sizeof(numbers) / sizeof(numbers[0]);
for (int i = 0; i < size; i++) {
sum += numbers[i];
}
printf("Sum of all elements: %d\n", sum);
return 0;
}
Two Dimensional Arrays
Q: How do you declare a two-dimensional array in C?
A: A two-dimensional array is declared by specifying the data type, the array name, and the
sizes in square brackets for both dimensions. For example:
int matrix[3][3];
Q: How do you initialize a two-dimensional array at the time of declaration?
A: A two-dimensional array can be initialized at the time of declaration by providing values in
nested curly braces. For example:
int matrix[2][2] = {{1, 2}, {3, 4}};
Q: Write a simple program to print all elements of a 2x2 matrix.
A: #include <stdio.h>
int main() {
int matrix[2][2] = {{1, 2}, {3, 4}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}return 0;
}
String Operations: Length, Compare, Concatenate, Copy
Q: How do you find the length of a string in C?
A: The length of a string can be found using the strlen function from the string.h library. For
example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
int length = strlen(str);
printf("Length of the string: %d\n", length);
return 0;
}
Q: How do you compare two strings in C?
A: Two strings can be compared using the strcmp function from the string.h library. For
example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else {
printf("Strings are not equal\n");
}
return 0;
}

Q: How do you concatenate two strings in C?


A: Two strings can be concatenated using the strcat function from the string.h library. For
example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = " World";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
Q: How do you copy one string to another in C?
A: One string can be copied to another using the strcpy function from the string.h library. For
example:
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello";
char dest[20];
strcpy(dest, src);
printf("Copied string: %s\n", dest);
return 0;
}
Selection Sort
Q: What is the basic idea behind selection sort?
A: The basic idea behind selection sort is to divide the list into two parts: the sorted part at the
beginning and the unsorted part at the end. The algorithm repeatedly selects the smallest element
from the unsorted part and swaps it with the first unsorted element, thus expanding the sorted
part by one element.

Q: Write a simple implementation of selection sort in C.


A: #include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, minIdx, temp;
for (i = 0; i < n-1; i++) {
minIdx = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Linear Search
Q: What is linear search?
A: Linear search is a simple searching algorithm that checks each element in the list one by
one until the desired element is found or the list ends.

Q: Write a simple implementation of linear search in C.


A:
#include <stdio.h>
int linearSearch(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = linearSearch(arr, n, x);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
Binary Search
Q: What is binary search?
A: Binary search is a searching algorithm that works on a sorted array by repeatedly dividing
the search interval in half. If the value of the search key is less than the item in the middle of the
interval, the interval is narrowed to the lower half. Otherwise, it is narrowed to the upper half.
Q: Write a simple implementation of binary search in C.
A: #include <stdio.h>

int binarySearch(int arr[], int l, int r, int x) {


while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x) {
return m;
}
if (arr[m] < x) {
l = m + 1;
} else {
r = m - 1;
}
}
return -1;
}
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n-1, x);
if (result != -1) {
printf("Element found at index %d\n", result);
} else
Unit – III
Modular Programming
Q: What is modular programming?
A: Modular programming is a software design technique that emphasizes separating the
functionality of a program into independent, interchangeable modules, each of which contains
everything necessary to execute only one aspect of the desired functionality.
Q: Why is modular programming beneficial?
A: Modular programming improves code readability, maintainability, and reusability. It also
makes debugging and testing easier because individual modules can be tested independently.
Function Prototype, Definition, and Call
Q: What is a function prototype in C?
A: A function prototype is a declaration of a function that specifies the function's name, return
type, and parameters without providing the function body. It informs the compiler about the
function's details before its actual definition.
Q: Provide an example of a function prototype.
A:
int add(int, int);
Q: What is a function definition in C?
A: A function definition provides the actual body of the function. It includes the function's name,
return type, parameters, and the block of code that performs the function's task.
Q: Provide an example of a function definition.
A:int add(int a, int b) {
return a + b;
}
Q: How do you call a function in C?
A: A function is called by using its name followed by arguments in parentheses. For example:
int sum = add(5, 3);
Built-in Functions
Q: What are built-in functions in C?
A: Built-in functions are pre-defined functions provided by C standard libraries to perform
common tasks, such as mathematical calculations, string manipulations, input/output operations,
etc.
Q: Name two commonly used string functions in C and their purpose.
A: strlen is used to find the length of a string, and strcpy is used to copy one string to another.
Q: Name two commonly used math functions in C and their purpose.
A: sqrt is used to calculate the square root of a number, and pow is used to raise a number to
the power of another number.
Recursion
Q: What is recursion in C programming?
A: Recursion is a programming technique where a function calls itself directly or indirectly to
solve a smaller instance of the same problem.
Q: Provide an example of a simple recursive function in C.
A: int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
Binary Search using Recursive Functions
Q: How does binary search work using recursion?
A: Binary search recursively divides the sorted array into halves and checks whether the
middle element is the target value. If not, it recursively searches in the appropriate half (left or
right).
Q: Provide a recursive implementation of binary search in C.
A: int binarySearch(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x) {
return mid;
}
if (arr[mid] > x) {
return binarySearch(arr, l, mid - 1, x);
}
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
Pointers
Q: What is a pointer in C?
A: A pointer is a variable that stores the memory address of another variable. Pointers are used
for dynamic memory allocation, arrays, strings, and function arguments.
Q: How do you declare a pointer in C?
A: A pointer is declared using the * operator. For example:
int *ptr;
Pointer Operators
Q: What does the * operator do when used with a pointer?
A: The * operator, when used with a pointer, is the dereference operator, which accesses the
value stored at the memory address pointed to by the pointer.
Q: What does the & operator do in C?
A: The & operator is the address-of operator, which returns the memory address of a variable.
Pointer Arithmetic
Q: What operations can be performed on pointers?
A: Pointers can be incremented (ptr++), decremented (ptr--), and can have integer values
added or subtracted (ptr + n, ptr - n). These operations move the pointer by the size of the data
type it points to.
Q: Provide an example of pointer arithmetic in C.
A: int arr[] = {10, 20, 30};
int *ptr = arr;
ptr++; // Points to arr[1]
Arrays and Pointers
Q: How are arrays and pointers related in C?
A: In C, the name of an array acts as a pointer to the first element of the array. This means that
arr is equivalent to &arr[0].
Q: Provide an example of accessing array elements using pointers.
A: int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d\n", *(ptr + 1)); // Accesses arr[1]
Array of Pointers
Q: What is an array of pointers in C?
A: An array of pointers is an array where each element is a pointer. This is useful for creating
arrays of strings or arrays of arrays.
Q: Provide an example of declaring and initializing an array of pointers.
A: const char *fruits[] = {"Apple", "Banana", "Cherry"};
Parameter Passing: Pass by Value, Pass by Reference
Q: What is the difference between pass by value and pass by reference?
A: In pass by value, a copy of the actual parameter is passed to the function, so changes made
to the parameter inside the function do not affect the original value. In pass by reference, a
reference (or pointer) to the actual parameter is passed, so changes made to the parameter inside
the function affect the original value.
Detailed Answers with Code Examples
Example of Pass by Value:
#include <stdio.h>
void increment(int a) {
a = a + 1;
}
int main() {
int x = 10;
increment(x);
printf("Value of x: %d\n", x); // Output: 10
return 0;
}
Example of Pass by Reference:
#include <stdio.h>
void increment(int *a) {
*a = *a + 1;
}
int main() {
int x = 10;
increment(&x);
printf("Value of x: %d\n", x); // Output: 11
return 0;
}
Unit – IV
Structure
Q: What is a structure in C?
A: A structure in C is a user-defined data type that allows grouping of variables of different
data types under a single name. It is used to represent a record.
Q: How do you declare a structure in C?
A: A structure is declared using the struct keyword. For example:
struct Person {
char name[50];
int age;
float salary;
};
Q: How do you define a structure variable in C?
A: After declaring a structure, you can define structure variables using the structure name. For
example:
struct Person person1;
Nested Structures
Q: What is a nested structure in C?
A: A nested structure in C is a structure within another structure. It allows hierarchical
organization of data.
Q: Provide an example of a nested structure.
A: struct Date {
int day;
int month;
int year;
};
struct Person {
char name[50];
struct Date birthdate;
};
Pointer and Structures
Q: How do you declare a pointer to a structure in C?
A: You declare a pointer to a structure using the * operator.
For example:
struct Person *ptr;
Q: How do you access members of a structure using a pointer?
A: You access members of a structure using the -> operator when you have a pointer to the
structure. For example:
struct Person person1;
struct Person *ptr = &person1;
ptr->age = 25;
Array of Structures
Q: What is an array of structures in C?
A: An array of structures is an array where each element is a structure. It is used to store
multiple records of the same type.
Q: Provide an example of an array of structures.
A:
struct Person {
char name[50];
int age;
};
struct Person people[5];
Self Referential Structures
Q: What is a self-referential structure?
A: A self-referential structure is a structure that includes a pointer to an instance of the same
structure. It is commonly used in data structures like linked lists and trees.
Q: Provide an example of a self-referential structure.
A:
struct Node {
int data;
struct Node *next;
};
Dynamic Memory Allocation
Q: What is dynamic memory allocation in C?
A: Dynamic memory allocation in C allows programs to allocate memory during runtime
using functions like malloc, calloc, realloc, and free from the stdlib.h library.
Q: Provide an example of dynamic memory allocation for a structure.
A:
struct Person {
char name[50];
int age;
};
struct Person *ptr = (struct Person*)malloc(sizeof(struct Person));
Singly Linked List
Q: What is a singly linked list?
A: A singly linked list is a linear data structure where each element, called a node, contains
data and a pointer to the next node in the sequence.
Q: Provide a simple implementation of a singly linked list node in C.
A:
struct Node {
int data;
struct Node *next;
};
Q: How do you insert a new node at the beginning of a singly linked list?
A:
void insertAtBeginning(struct Node **head, int newData) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = newData;
newNode->next = *head;
*head = newNode;
}
Typedef
Q: What is typedef in C?
A: typedef is a keyword in C used to create new names (aliases) for existing data types,
making the code more readable and easier to manage.
Q: Provide an example of using typedef with a structure.
A: typedef struct {
char name[50];
int age;
} Person;
Union
Q: What is a union in C?
A: A union is a user-defined data type similar to a structure but with a key difference: all
members of a union share the same memory location. This means only one member can hold a
value at any given time.
Q: How do you declare a union in C?
A: A union is declared using the union keyword. For example:
union Data {
int i;
float f;
char str[20];
};
Q: Provide an example of defining and accessing a union variable.
A:
union Data data;
data.i = 10;
printf("data.i = %d\n", data.i);
data.f = 220.5;
printf("data.f = %.1f\n", data.f);
Storage Classes and Visibility
Q: What are the different storage classes in C?
A: The different storage classes in C are auto, register, static, and extern. They define the
scope, visibility, and lifetime of variables.
Q: What is the scope of a variable with the auto storage class?
A: The auto storage class is the default for local variables. Variables declared with auto have
block scope and are not visible outside the block in which they are declared.
Q: What is the purpose of the static storage class?
A: The static storage class extends the lifetime of variables to the duration of the entire
program, but their scope remains limited to the block in which they are declared. For functions,
static limits the visibility to the file in which they are defined.
Q: Provide an example of a static variable in C.
A:
void function() {
static int count = 0;
count++;
printf("Count is %d\n", count);
}
Unit – V
Files
Q: What is a file in the context of C programming?
A: A file in C programming is a container in a storage device where data is stored. It allows
for permanent storage of data for retrieval and modification.
Q: What are the basic operations you can perform on files in C?
A: The basic operations on files in C are creating, opening, reading, writing, and closing files.
Q: How do you declare a file pointer in C?
A: A file pointer is declared using the FILE type. For example:
FILE *filePointer;
Types of File Processing: Sequential Access, Random Access
Q: What is sequential access file processing?
A: Sequential access file processing means that data in the file is accessed in a linear
sequence, one record after another.
Q: What is random access file processing?
A: Random access file processing allows data to be read or written at any position in the file
without reading through the preceding data.
Sequential Access File
Q: How do you open a file for reading in C?
A: You open a file for reading using the fopen function with the "r" mode. For example:
FILE *file = fopen("example.txt", "r");
Q: How do you open a file for writing in C?
A: You open a file for writing using the fopen function with the "w" mode. For example:
FILE *file = fopen("example.txt", "w");
Q: How do you read a single character from a file in C?
A: You read a single character from a file using the fgetc function. For example:
char c = fgetc(file);
Q: How do you write a single character to a file in C?
A: You write a single character to a file using the fputc function. For example:
fputc('A', file);
Q: How do you read a string from a file in C?
A: You read a string from a file using the fgets function. For example:
char str[100];
fgets(str, 100, file);
Q: How do you write a string to a file in C?
A: You write a string to a file using the fputs function. For example:
fputs("Hello, World!", file);
Q: How do you read formatted data from a file in C?
A: You read formatted data from a file using the fscanf function. For example:
int num;
fscanf(file, "%d", &num);
Q: How do you write formatted data to a file in C?
A: You write formatted data to a file using the fprintf function. For example:
fprintf(file, "Number: %d", num);
Q: How do you close a file in C?
A: You close a file using the fclose function. For example:
fclose(file);
Random Access File
Q: How do you move the file pointer to a specific position in a file in C?
A: You move the file pointer using the fseek function. For example:
fseek(file, 10, SEEK_SET);
Q: How do you get the current position of the file pointer in C?
A: You get the current position of the file pointer using the ftell function. For example:
long pos = ftell(file);
Q: How do you rewind the file pointer to the beginning of the file in C?
A: You rewind the file pointer using the rewind function. For example:
rewind(file);
Q: Provide an example of random access file processing to read from a specific position.
A:
fseek(file, 10, SEEK_SET);
char c = fgetc(file);
Q: Provide an example of random access file processing to write at a specific position.
A:
fseek(file, 20, SEEK_SET);
fputc('X', file);
Command Line Arguments
Q: What are command line arguments in C?
A: Command line arguments are parameters passed to the main function of a C program when
it is executed. They allow the user to specify input values when running the program.

Q: How do you define the main function to accept command line arguments in C?
A: The main function is defined to accept command line arguments as follows:
int main(int argc, char *argv[]) {
// code
}
Q: What do argc and argv represent in the main function?
A: argc (argument count) represents the number of command line arguments passed, and argv
(argument vector) is an array of strings representing the command line arguments.

Q: How do you access the first command line argument in C?


A: The first command line argument can be accessed using argv[1]. For example:
char *arg1 = argv[1];
Q: Provide an example program that prints all command line arguments.
A:
#include <stdio.h>
int main(int argc, char *argv[]) {
for (int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
Q: How can command line arguments be used to open a file specified by the user?
A:#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
FILE *file = fopen(argv[1], "r");
if (file == NULL) {
printf("Could not open file %s\n", argv[1]);
return 1;
}
// Process the file
fclose(file);
return 0;
}

You might also like