PC Questions
PC Questions
PC Questions
2 Marks Question
Q1. What do you mean by an array? Give any 2 differences between 1D array and 2D
array.
Two differences:
1. 1D array is a single line of elements, while 2D array is a grid.
2. In 1D array, you use one index to access elements; in 2D array, you use two (row
and column).
Q2 How to declare 1D and 2D arrays, and also initialize the 1D array and 2D array.
In this code, `arr1D` is a 1D array with 5 elements, and `arr2D` is a 2D array with
2 rows and 3 columns.
Q4. Main function is a special kind of user defined function, how is it different
from other user defined functions?
A4. `main` is the program's starting point, fixed in name, and runs automatically.
Other user-defined functions are created by the programmer and executed when
explicitly called.
Q5. Can we have the number of initializers in a given 1D array more than its size?
A5. No, the number of initializers in a 1D array cannot exceed its size. It must
have the same or fewer initializers than the size specified during declaration.
Q6. Can we access any given 1D array beyond its size? Comment.
A6. Accessing elements beyond the size of a 1D array can lead to unpredictable
behavior, memory corruption, or program crashes. It's crucial to ensure you stay
within the array's defined bounds to maintain program stability and correctness.
A7. Yes, you can use assignment operators (like '=') with string variables to
assign values to them or to update their contents in many programming languages.
A9. The null character (`'\0'`) is vital in strings as it indicates the string's
endpoint, enabling proper handling and determination of string length.
A13. Memory allocation for an array occurs either at **compile time** for static
arrays, where the size is known beforehand, or at **run time** for dynamic arrays,
where the size can be determined during program execution.
Q14. Is it compulsory to declare callee function before function call in the body
of a called function?
A14. It's not required, but it's a recommended practice to declare functions before
calling them within another function to ensure the compiler knows the function's
signature and improve code organization.
Q15. Can a function return more than 2 values? Justify your answer.
A15. A function can directly return only one value, but it can indirectly return
multiple values by encapsulating them in data structures like arrays, structs, or
tuples. This allows for practical multiple-value return.
Q16. Can we have more than 1 return statement in a user defined function?
A16. Yes, a user-defined function can have more than one return statement. The
function will return the value from the first executed return statement it
encounters, and subsequent return statements will terminate the function's
execution.
Q17. What is the default return type of a user defined function where a return type
is not specified?
A17. The default return type of a user-defined function, when not specified, is
typically assumed to be `int` in languages like C
Q20. What is the difference between structures and unions in terms of object size?
A20. Structures allocate separate memory for each member, potentially resulting in
a larger object size, while unions share the same memory space and have a size
equal to the largest member.
Q22. Write a program that prints the size of a structure data type.
return 0;
}
5 Marks Question
int main() {
int a[5][5];
return 0;
}
Q2. Write a Program to copy 1 string to another string without string copy
function.
int main() {
char source[] = "Hello, World!";
char destination[50];
stringCopy(destination, source);
return 0;
}
Q3. Write a code to input string without ‘scanf’ and ‘gets’ function.
int main() {
char inputString[100];
char c;
int i = 0;
inputString[i] = '\0';
Q4. Write down the name, purpose and general syntax of any 10 in-built string
functions.
Q5. Differentiate between the working of ‘gets’ and ‘scanf’ using a suitable code.
OR
Differentiate between the working of reading a string using ‘gets’ and ‘scanf’.
int main() {
char name[50];
fflush(stdin);
return 0;
}
int main() {
char strings[10][50];
printf("Enter 10 strings:\n");
return 0;
}
Q7. Write down the advantages of array. Or when it is necessary to use arrays.
3. **Memory Efficiency**: Arrays use minimal memory overhead as they store elements
in a contiguous block, avoiding the need for additional data structures to manage
element addresses.
4. **Simplicity**: Arrays are easy to understand and work with, using basic
indexing for element access.
1. **Organizing Data**: You need to group and manage related data efficiently
within a single structure.
3. **Fixed Size**: The number of elements is known and remains constant during
program execution.
4. **Performance**: Speed and efficiency are critical for data retrieval and
manipulation.
5. **Consistency**: You want a straightforward and uniform way to handle elements
of the same type.
Q8. What do you mean by modular programming? Write down its advantages.
Q9. Demonstrate the concepts of modular programming with the help of a small code.
int main() {
float length, width;
return 0;
}
Q10. Write a code to multiply 2 numbers using a function ‘mul()’ which takes 2
arguments and returns 1 value.
A10. #include <stdio.h>
int main() {
float number1, number2, result;
return 0;
}
Actual arguments and formal arguments are essential concepts related to function
parameters in programming. Here's a concise comparison in 5 points:
1. Actual arguments are the values or expressions provided when calling a function.
2. They represent the real data that the function will operate on during its
execution.
3. The data types of actual arguments must match the expected data types of the
corresponding formal parameters.
4. Actual arguments are typically passed by value, meaning a copy of the argument
is sent to the function.
5. For example, in a function call like `foo(5, "Hello")`, `5` and `"Hello"` are
actual arguments.
1. Formal arguments are the placeholders defined in the function's parameter list.
2. They act as variables within the function, receiving values from the actual
arguments when the function is called.
3. Formal arguments specify the expected data type and name of the parameters that
the function will work with.
4. By default, formal arguments are passed by value, where they receive copies of
values from the actual arguments.
5. In a function declaration like `void foo(int x, char *str)`, `int x` and `char
*str` are formal arguments.
**Comparison:**
- Actual arguments represent the actual data provided when calling a function,
while formal arguments are variables declared within the function's definition.
- There should be a match in data types between actual arguments and the expected
data types of the formal parameters in the function.
- Both actual and formal arguments are typically passed by value, which involves
working with copies of the values.
**Contrast:**
- Actual arguments are values or expressions passed during the function call, while
formal arguments are variables defined in the function's parameter list.
- Actual arguments represent specific, real values, while formal arguments act as
placeholders for data to be used within the function's scope.
Q12. What are the rules that govern the passing of arrays to a function?
A12. When passing arrays to a function in programming, there are important rules to
consider, summarized in 5 points:
2. **No Array Size**: You typically don't need to specify the array size when
passing it to a function. The function can determine the size based on the data
type and pointer arithmetic.
4. **Array Modification**: Any modifications made to the array within the function
affect the original array outside the function since you are working with the same
memory location.
Q13. Distinguish between global and local variables with the help of an example.
#include <stdio.h>
int main() {
printf("Global variable: %d\n", globalVar);
return 0;
}
Local Variables:
void exampleFunction() {
int localVar = 5; // Local variable
printf("Local variable: %d\n", localVar);
}
int main() {
exampleFunction();
// printf("Local variable: %d\n"); // This would result in an error
return 0;
}
Q15. Describe the ways of passing parameters to functions. When do you prefer to
use each of them?
A15. Parameters can be passed to functions in various ways in programming, and the
choice depends on the specific needs of your code. Here are common ways and when to
use them:
1. **Pass by Value**:
- A copy of the argument's value is passed to the function.
- Use when you want to work with a local copy of the data and prevent changes to
the original value.
2. **Pass by Reference/Pointer**:
- A reference or pointer to the original data is passed to the function.
- Use when you need to modify the original data or avoid copying large data
structures for efficiency.
3. **Passing Arrays**:
- Arrays are often passed as pointers to their first element (pass by
reference).
- Use this when you want to work with the original array and potentially modify
its contents.
```c
int factorial(int n) {
if (n == 0) {
return 1; // Base case
} else {
return n * factorial(n - 1); // Recursive case
}
}
A17. Structure:
Array:
1. Data Types: Arrays store variables of the same data type. All elements
in an array are of the identical data type.
2. Element Access: Elements in an array are accessed using indices
(numeric positions). They do not have unique names; their identification is based
on their position in the array.
3. Size: The size of an array is fixed when declared and remains constant
throughout its lifetime. It is determined by the number of elements and the data
type.
4. Usage: Arrays are used when you need a collection of homogeneous data,
such as a list of integers, or when you want to work with data in a systematic,
ordered manner.
2. **Order Matters**: The order in which you provide initial values should match
the order of structure members. The first value initializes the first member, the
second initializes the second member, and so on.
3. **Designated Initializers (C99+)**: In C99 and later, you can use designated
initializers to specify which members are being initialized. For instance, you can
write `struct Point p = {.x = 7};` to initialize only the `x` member.
5. **Omitted Members**: If you don't initialize all the members in a structure, the
remaining members will be initialized with default values based on their data types
(e.g., 0 for numeric types or `null` for pointers).
A19. Structures and unions are both compound data types in programming, but they
differ significantly:
**Structures**:
**Unions**:
10 Marks Questions
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int transposed[3][3];
printf("Original Matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
printf("Transposed Matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d\t", transposed[i][j]);
}
printf("\n");
}
return 0;
}
int main() {
int rows, cols;
int matrix1[rows][cols];
int matrix2[rows][cols];
int result[rows][cols];
return 0;
}
int main() {
int r1, c1, r2, c2;
printf("Enter dimensions: ");
scanf("%d%d%d%d", &r1, &c1, &r2, &c2);
if (c1 != r2) {
printf("Matrix multiplication is not possible.\n");
return 1;
}
int a[r1][c1], b[r2][c2], result[r1][c2];
for (int i = 0; i < r1; i++)
for (int j = 0; j < c1; j++)
scanf("%d", &a[i][j]);
for (int i = 0; i < r2; i++)
for (int j = 0; j < c2; j++)
scanf("%d", &b[i][j]);
for (int i = 0; i < r1; i++)
for (int j = 0; j < c2; j++) {
result[i][j] = 0;
for (int k = 0; k < c1; k++)
result[i][j] += a[i][k] * b[k][j];
}
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
printf("%d\t", result[i][j]);
}
printf("\n");
}
return 0;
}
int main() {
int n;
int matrix[n][n];
return 0;
}
// Function to print hotels with room charges less than a given value
void printHotelsWithChargesLessThan(struct Hotel hotels[], int numHotels, double
maxCharge) {
printf("Hotels with room charges less than %.2f:\n", maxCharge);
for (int i = 0; i < numHotels; i++) {
if (hotels[i].avgRoomCharge < maxCharge) {
printf("Name: %s\n", hotels[i].name);
printf("Address: %s\n", hotels[i].address);
printf("Room Charge: %.2f\n", hotels[i].avgRoomCharge);
printf("Grade: %c\n", hotels[i].grade);
printf("Number of Rooms: %d\n", hotels[i].numRooms);
printf("\n");
}
}
}
int main() {
struct Hotel hotels[5] = {
{"Hotel A", "123 Main St", 75.0, 'A', 50},
{"Hotel B", "456 Elm St", 90.0, 'B', 75},
{"Hotel C", "789 Oak St", 65.0, 'A', 60},
{"Hotel D", "101 Pine St", 100.0, 'C', 80},
{"Hotel E", "202 Cedar St", 55.0, 'B', 70}
};
printHotelsWithChargesLessThan(hotels, 5, maxCharge);
printf("\n");
printHotelsByGradeAndCharge(hotels, 5, gradeToPrint);
return 0;
}
```c
#include <stdio.h>
struct Student {
char name[50];
int age;
};
```
Here, we define a `Student` structure with `name` and `age` members to represent
student data.
**Creating an Array of Nested Structures:**
```c
int main() {
struct Student students[3];
```
This declares an array `students` capable of storing data for three students.
```c
strcpy(students[0].name, "Alice");
students[0].age = 20;
strcpy(students[1].name, "Bob");
students[1].age = 22;
strcpy(students[2].name, "Charlie");
students[2].age = 19;
```
This sets the `name` and `age` for each student in the array.
Loops help efficiently process array data. For example, printing student
information:
```c
for (int i = 0; i < 3; i++) {
printf("Student %d:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("\n");
}
return 0;
}