IP 2 MARK QUESTIONS
1 Define an algorithm. 2M
1. Definition of an Algorithm:
o A step-by-step procedure to solve a problem.
o In simple terms, it is a sequence of statements to perform a task.
2. In Computer Science:
o An algorithm is a sequence of clear and unambiguous instructions.
o It is used to solve a problem and can be turned into a computer program.
2 List the different flow chart symbols. 2M
3 Write any four operators in C. 2M
Here are four operators in C:
1. Arithmetic Operator: Used for mathematical operations (e.g., +, -, *, /, %).
2. Relational Operator: Used to compare values (e.g., ==, !=, >, <, >=, <=).
3. Logical Operator: Used for logical operations (e.g., &&, ||, !).
4. Assignment Operator: Used to assign values to variables (e.g., =, +=, -=, *=, /=).
4 What is meant by type conversion? 2M
Type Conversion in C:
Type conversion in C means changing the data type of a variable or value into another
data type.
Types of Type Conversion:
1. Implicit Conversion (Type Promotion):
o Automatically done by the compiler.
o Happens when a smaller data type (e.g., int) is converted to a larger data
type (e.g., float) during operations.
Example:
int a = 5;
float b = a; // `a` is automatically converted to float.
2. Explicit Conversion (Type Casting):
o Done manually by the programmer.
o Uses a cast operator to specify the new data type.
Example:
float a = 5.6;
int b = (int)a; // `a` is explicitly converted to int.
5 Mention input and output statements in C. 2M
1. Input Statement:
• scanf():
Used to take input from the user.
Syntax:
scanf("format_specifier", &variable);
Example:
int num;
scanf("%d", &num); // Takes an integer input.
2. Output Statement:
printf():
Used to display output to the screen.
Syntax:
printf("text or format_specifier", variable);
Example:
int num = 10;
printf("The number is %d", num); // Displays: The number is 10
6 What is meant by control statement? 2M
Control Statements in C:
Control statements in C are used to control the flow of execution in a program. They
decide which part of the code will run based on certain conditions or repeatedly for a
number of times.
Types of Control Statements:
1. Decision-Making Statements:
o Used to execute a block of code based on conditions.
Examples:
▪ if
▪ if-else
▪ if-else-ladder
▪ nested if
▪ switch
2. Looping Statements:
o Used to repeat a block of code multiple times.
Examples:
▪ for
▪ while
▪ do-while
3. Jump Statements:
o Used to transfer control to another part of the program.
Examples:
▪ break
▪ continue
▪ goto
7 Write the syntax for nested if else statement. 2M
Syntax for Nested if-else Statement in C:
if (condition1) {
// Code block if condition1 is true
if (condition2) {
// Code block if condition2 is also true
} else {
// Code block if condition2 is false
}
} else {
// Code block if condition1 is false
}
Example:
if (age > 18) {
if (hasLicense) {
printf("You can drive.");
} else {
printf("You need a license to drive.");
}
} else {
printf("You are too young to drive.");
}
8 Compare while and do-while statement. 2M
9 Write the syntax for FOR Loop. 2M
The syntax for a for loop in C is as follows:
for (initialization; condition; increment/decrement) {
//body of the loop
// Statement to be executed
}
Example:
for(int i=0; i<5; i++)
{
printf(“%d”, i);
}
10 Short notes on break and continue keyword. 2M
Break and Continue Keywords in C
1. break Keyword
• Purpose: Used to immediately exit a loop or switch statement.
• Effect: The control jumps out of the current loop or switch, skipping the remaining
code inside it.
• Use Case: When you need to stop the loop or switch based on a specific condition.
2. continue Keyword
• Purpose: Used to skip the current iteration of a loop and proceed to the next
iteration.
• Effect: The control jumps back to the beginning of the loop, skipping the
remaining code for the current iteration.
• Use Case: When you want to skip specific values in the loop.
11 Define a 2D array. 2M
A two-dimensional (2D) array in C is a collection of data elements organized in rows and
columns, similar to a matrix. 2D arrays are a fundamental data structure in C
programming and are used to store and manipulate data in a grid-like format.
General Syntax:
data_type array_name[rows][columns];
Example:
int matrix[3][4];
12 Define a Multi-dimensional array. 2M
A multi-dimensional array can be defined as an array that has more than one dimension.
Having more than one dimension means that it can grow in multiple directions. Some
popular multidimensional arrays are 2D arrays and 3D arrays.
Syntax
The general form of declaring N-dimensional arrays is shown below:
type arr_name[size1][size2]….[sizeN];
Example:
int matrix[3][3][3];
13 Explain how to initialize the 1D array. 2M
In C, initializing a 1D array means assigning values to its elements when the array is
created. This can be done in several ways, either by specifying all the elements explicitly
or partially. Here's how you can initialize a 1D array:
Static Initialization:
The following are the different ways of array declaration:
1. int numbers[5] = {1, 2, 3, 4, 5};
2. int numbers[5] = {1, 2};
3. int numbers[] = {10, 20, 30}; // Size = 3
4. char name[] = "Hello"; // Adds '\0' at the end
5.
for (int i = 0; i < 5; ++i)
{
numers[i] = i + 1; // Assuming you want IDs starting from 1
}
14 Define a String. 2M
Definition of a String in C:
1. A string is a sequence of characters ending with a special character '\0' (null
character).
2. Strings in C are stored in a character array.
3. A string must have enough space to store characters plus the null character.
Declaring and Initializing Strings:
1. Using a Character Array:
char name[6] = "Hello"; // Includes '\0' at the end
2. Without Size:
char name[] = "Hello"; // Compiler determines size automatically
3. Manual Character Assignment:
char name[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
15 List the different string handling functions. 2M
Here are the most commonly used string handling functions in C from the <string.h>
library:
String Handling Functions:
1. strlen: Finds the length of a string.
int length = strlen(str);
2. strcpy: Copies one string into another.
strcpy(dest, src);
3. strcat: Appends (concatenates) one string to another.
strcat(dest, src);
4. strcmp: Compares two strings.
int result = strcmp(str1, str2);
5. strlwr: Converts a string to lowercase.
print(“Lower case=%s”,strlwr(str));
6. strupr: Converts a string to uppercase.
print(“Lower case=%s”,strupr(str));
16 What
2 is pointer? 2M
2
1. A pointer is a derived data type that stores the address of a variable or memory
3
location.
1 2. It allows accessing and modifying the data at that memory location.
3. Pointers can store addresses of variables, functions, or even other pointers.
6
4. They enable low-level memory access, dynamic memory allocation, and other
1 advanced functionalities.
5.
Declaration:
A pointer is declared using the * symbol.
datatype *pointerName ;
Example:
int *a;
float *b;
17 Explain how to assign an address to pointer variable. 2M
Assigning an address to a pointer variable in C involves using the address-of operator
(‘&’) along with the variable whose address you want to assign. Here's a step-by-step
explanation:
1. Declare a Pointer Variable:
Start by declaring a pointer variable of the appropriate type.
int myNumber; // Declare a variable
int *ptr; // Declare a pointer variable
2. Assign the Address using the Address-of Operator (&):
Use the address-of operator (‘&’) followed by the variable name.
myNumber = 42; // Assign a value to the variable
ptr = &myNumber; // Assign the address of the variable to the pointer
18 Define a void pointer. 2M
➢ A void pointer is a pointer that has no associated data type with it.
➢ A void pointer is also called as “Generic Pointer”.
➢ A void pointer can hold an address of any type and can be “typecasted” to any
type.
➢ We use the keyword "void" to create void pointer
Syntax of void pointer
1. void *pointer name;
Example:
int a=10;
float b=20;
void *ptr;
// stores address of ‘a’ variable
ptr=&a;
// stores address of ‘b’ variable
Ptr=&b;
19 What is meant by structure and write the syntax for structure declaration 2M
What is mean by structure?
➢ A structure in C is a user-defined data type that allows you to group different
variables (of different types) together under one name.
➢ It is helpful when you want to manage related data in one place.
Syntax for Structure Declaration:
struct StructureName {
dataType member1;
dataType member2;
.
.
.
datatype member;
};
Example:
struct Student {
char name[50];
int age;
float marks;
};
20 Differentiate structure and union. 2M
21 What is meant by function and list the different types of function. 2M
What is meant by function?
➢ Function is a self-contain block.
➢ We can divide a large program into the basic building blocks known as function.
➢ The function contains the set of programming statements enclosed by {}.
Syntax:
return_type function_name(data_type parameter...)
{
//code to be executed
return;
}
Example:
int sum(int a, int b)
{
return(a+b);
}
Different types of functions in C
There are two types of functions in C programming:
1. Library Functions
2. User-defined functions
22 What is meant by call-by-value? 2M
Call-by-Value in C:
1. The call by value is a method of passing arguments to a function
2. A copy of the variable's value is passed to the function.
3. Changes made inside the function do not affect the original variable.
4. It is safe as the original data remains unchanged.
Example:
void modify(int x)
{
x = 20; // Changes only the copy
}
int main()
{
int a = 10;
modify(a); // 'a' remains 10
return 0;
}
23 Define a Call-by-reference 2M
Call-by-reference in C:
1. A reference of the variable is passed to the function.
2. The function gets access to the variable's memory address, not just its value.
3. Changes made inside the function directly modify the original variable.
4. Used when you want the function to update or change the original data.
Example:
void modify(int *x)
{
*x = 20; // Modifies the original variable
}
int main()
{
int a = 10;
modify(&a); // 'a' becomes 20
return 0;
}
24 Define a file. 2M
What is a file?
1. A collection of data which is stored on a secondary device like a hard disk is
known as a file.
2. It allows reading and writing data, enabling persistent storage beyond program
execution.
3. Files in C are managed using pointers (e.g., FILE *ptr) and standard functions
like fopen(), fwrite(), fread(), etc.
25 List the different file operations. 2M
1. fopen()
2. fclose()
3. fgets()
4. fputs()
5. fprintf()
6. fscanf()
7. fgetc()
8. fputc()
9. fseek()
10. ftell()
11. rewind()
12. feof()
13. ferror()