5th and 6th Flashcards

1
Q

9.3: What two indices are typically used in a palindrome check?

A

A starting index (beginning of the string) and an ending index (last character before the null).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

13.3: What function is commonly used to input a string into a structure?

A

scanf() (or fgets() for safe input) can be used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

3.4: What does the pointer variable store?

A

It stores the memory address of the variable it is pointing to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

4.3: Write a code snippet that demonstrates how to dereference a pointer.

A

int num = 25; int *ptr = # printf("%d", *ptr);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

5.2: What is the advantage of using pointers for array I/O?

A

They allow for flexible traversal and manipulation of array elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

5.7: What is the role of pointer arithmetic in the context of array I/O?

A

It allows direct manipulation of addresses to sequentially access array elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

19.2: What determines the size of a union?

A

The size is determined by the largest member.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

14.5: How are elements in an array accessed?

A

Using indices, e.g., numbers[0] for the first element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

10.3: What condition must be met for a character to be considered for vowel/consonant counting?

A

The character must be an alphabet letter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

5.3: Show how pointer arithmetic can be used to access the third element of an array.

A

Using *(arr + 2) accesses the third element (zero-indexed).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

14.3: Provide an example of declaring an array of integers.

A

int numbers[5] = {1, 2, 3, 4, 5};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

13.5: How would you print the roll number from a student structure variable?

A

printf("Roll Number: %d\n", s.rollNumber);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

14.1: What is the primary characteristic of an array in C?

A

It is a homogeneous collection of elements of the same data type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

17.5: When might you choose to use a union over a structure?

A

When you need to store different types in the same memory space but only one at any given time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

17.4: Can you store values in multiple union members simultaneously?

A

No, only one member can hold a value at a time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

6.6: How do you display a string using an output function?

A

Using printf("%s", str); or fputs(str, stdout);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

4.2: What does the indirection operator do when applied to a pointer?

A

It accesses the value stored at the memory address the pointer points to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

1.7: What is the overall algorithmic complexity of this approach?

A

O(n) since it checks every element exactly once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

21.5: Provide an example of using fputs() to write a string to stdout.

A

fputs("This is a test.\n", stdout);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

1.6: How is the smallest element printed in the program?

A

Using printf("The smallest element is: %d\n", smallest);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

17.6: What is the memory advantage of using a union?

A

It uses less memory since all members share the same memory location.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

21.1: What is the primary use of fgets() in C?

A

To safely read a line from a file or standard input, preventing buffer overflows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

19.7: Give an example of when using a union for data storage is beneficial.

A

When a variable could be an integer or a float but never both at the same time, using a union conserves memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

4.6: Can you use the indirection operator with pointers to any data type?

A

Yes, it works with pointers to any data type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

5.6: How do you print array elements using a pointer?

A

Use a loop and dereference the pointer: printf("%d ", *(ptr + i));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

4.5: Why is pointer dereferencing essential in C programming?

A

It allows manipulation of data stored in memory via its address.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

9.7: How is the function typically structured to return whether a string is a palindrome?

A

It returns 1 (true) if all pairs match and 0 (false) if any pair does not match.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

18.1: What is the fundamental difference in memory allocation between unions and structures?

A

Structures allocate memory for each member separately; unions allocate a single shared memory block equal to the size of the largest member.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

1.5: Write the code snippet that initializes the variable holding the smallest element.

A

int smallest = arr[0]; // Assume first element is the smallest

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

8.5: How do you ensure that the concatenated string is properly terminated?

A

By setting *dest = ‘\0’; after the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

3.1: How do you declare a pointer to an integer in C?

A

int *ptr; declares a pointer to an integer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

9.2: How can you determine the length of a string manually?

A

By iterating through the string until the null terminator is reached.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

12.4: What is a common use case for structures?

A

Storing related but different data types for a single entity (e.g., student records).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

16.3: How do you declare a nested structure for a date within a student structure?

A

Define a Date structure and then include it in the Student structure, e.g., struct Date { int day; int month; int year; }; struct Student { char name[50]; int roll; struct Date dob; };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

6.7: Why is it important for a string to have a null terminator?

A

It marks the end of the string so functions know where to stop reading.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

10.1: How do you treat the case-sensitivity of characters when counting vowels and consonants?

A

Convert each character to lowercase using tolower() for uniform comparison.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

15.7: What potential issues might arise when copying structures with pointers?

A

Shallow copies may lead to shared pointers, requiring careful management of allocated memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

19.1: How are union members stored in memory?

A

All union members share the same memory location.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

7.4: How does strcat() differ from strcpy()?

A

strcat() appends the source string to the end of the destination string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

3.5: Provide the code snippet that demonstrates pointer declaration and initialization.

A

int var = 10; int *ptr = &var;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

14.2: How does a structure differ from an array?

A

A structure can hold different data types as its members.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

21.4: Provide an example of using fgets() to read from a file.

A

char buffer[100]; fgets(buffer, sizeof(buffer), fp);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

1.0Write a C program to find the smallest element in an array.

A

Approach:

Initialize the smallest element with the first array element.

Loop through the array comparing each element with the current smallest.

Update the smallest when a smaller element is found.

Code Example:

c
Copy
Edit
#include <stdio.h></stdio.h>

int main() {
int arr[] = {34, 15, 88, 2, 19};
int n = sizeof(arr) / sizeof(arr[0]);
int smallest = arr[0]; // Assume the first element is the smallest

// Loop through the array starting from the second element
for (int i = 1; i < n; i++) {
    if (arr[i] < smallest) {
        smallest = arr[i]; // Update smallest if current element is smaller
    }
}
printf("The smallest element is: %d\n", smallest);
return 0; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

17.3: Provide the basic syntax for declaring a union.

A

union Data { int i; float f; char str[20]; };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

10.2: How do you differentiate between a vowel and a consonant?

A

Check if the character is one of ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ (vowel) or any other letter (consonant).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

1.2: How do you determine the number of elements in an array in C?

A

Use sizeof(arr) divided by sizeof(arr[0]), e.g., int n = sizeof(arr) / sizeof(arr[0]).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

12.3: When is a union preferred over a structure?

A

When only one member will be used at a time and memory efficiency is required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

10.5: How can you maintain separate counts for each vowel?

A

Use an array (e.g., int vowels[5]) with each index corresponding to a vowel.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

2.7: Provide an example scenario where misuse of pointers may result in issues.

A

Incorrect pointer arithmetic that accesses memory out of bounds, leading to crashes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

16.2: Why would you use a nested structure?

A

To group related data logically, such as a date inside a student record.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

6.5: Write a code snippet to read a string using fgets().

A

char str[100]; fgets(str, sizeof(str), stdin);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

4.1: What is the indirection operator in C commonly known as?

A

The dereference operator (*)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

18.3: In what scenario is a union preferred due to its memory usage?

A

When you have mutually exclusive data types and need to conserve memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

12.2: When should you use a structure over a union?

A

Use structures when you need to store and use all the data members simultaneously.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

14.4: Provide an example of declaring a structure to hold a student’s data.

A

struct Student { char name[50]; int roll; float marks; };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

10.6: Provide an example code snippet that increments the count for a vowel.

A

if (ch == ‘a’) vowels[0]++;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

11.6: How is the count incremented once a complete word match is found?

A

Increase the counter variable by one for every valid match.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

13.7: Provide a complete example line of input for a student’s details.

A

scanf("%s %d %s %d", s.name, &s.rollNumber, s.subject, &s.iaMarks);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

3.7: Why is it recommended to cast pointers to (void *) when printing?

A

It ensures portability and correct formatting for pointer addresses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

6.1: How is a string represented in C?

A

As an array of characters terminated by a null character (‘\0’).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

11.1: What is the first step in counting a word’s occurrences in a text?

A

Read the entire text and the target word from the user.

62
Q

8.7: What is a key benefit of writing your own string concatenation function?

A

It deepens your understanding of pointer arithmetic and manual string manipulation in C.

63
Q

12.5: What is a common use case for unions?

A

Handling a variable that can store data of different types, but only one type at any time.

64
Q

13.1: What are the typical fields you might include in a student structure?

A

Fields like name, roll number, subject, and IA marks.

65
Q

9.1: What is a palindrome?

A

A string that reads the same forwards and backwards.

66
Q

15.2: What does the assignment operator do with structures?

A

It performs a member-wise copy of the structure.

67
Q

20.5: What parameters does fseek() take?

A

A file pointer, a long integer offset, and a whence parameter (SEEK_SET, SEEK_CUR, or SEEK_END).

68
Q

2.4: What is one major disadvantage of pointers?

A

They add complexity and can be difficult to debug, especially with pointer arithmetic.

69
Q

17.7: Write a code example that assigns a value to a union member.

A

union Data data; data.i = 42; // Now data.i holds the value 42

70
Q

14.7: What is one scenario where an array is more suitable than a structure?

A

When storing a list of values of the same type, like integers or characters.

71
Q

1.4: Why is it important to start the loop from the second element?

A

Because the first element is already assumed to be the smallest.

72
Q

6.3: What function can be used for safe string input in C?

A

fgets() is preferred over gets() for safe input.

73
Q

8.2: After finding the end of the first string, what comes next in manual concatenation?

A

Copy characters from the second string into the first string starting at the null terminator.

74
Q

18.7: How do the use cases differ between unions and structures?

A

Use structures when you need to access multiple pieces of data concurrently; use unions when memory efficiency is critical and only one piece of data is needed at a time.

75
Q

3.3: How do you initialize a pointer to point to a variable named var?

A

int *ptr = &var;

76
Q

20.3: What does the rewind() function do?

A

It resets the file pointer back to the beginning of the file.

77
Q

10.4: Which function can be used to safely read a sentence from the user?

A

fgets() can be used to read the entire sentence including spaces.

78
Q

7.2: How does strlen() function help with strings?

A

It returns the length of the string excluding the null character.

79
Q

17.2: How does a union differ from a structure in terms of memory allocation?

A

In a union, the memory size equals that of its largest member; structures allocate separate memory for each member.

80
Q

11.3: How can you perform a case-insensitive comparison between the text and the target word?

A

Use a function like strncasecmp() for case-insensitive comparison.

81
Q

21.3: What is the purpose of fputs() in file handling?

A

To write a string to a file or standard output without automatically appending a newline.

82
Q

12.1: How do structures store their members compared to unions?

A

Structures allocate separate memory for each member, whereas unions share the same memory.

83
Q

20.4: How is feof() used during file reading?

A

It checks if the end-of-file (EOF) has been reached.

84
Q

8.1: What is the first step in manually concatenating two strings?

A

Traverse the first string to find its null terminator.

85
Q

18.2: How do you access members in a structure versus a union?

A

In a structure, all members can be accessed independently; in a union, only the most recently assigned member is valid.

86
Q

8.3: Why must the concatenated string be null-terminated?

A

To mark the end of the string for proper string handling functions.

87
Q

Pointer Declaration and Initialization

88
Q

16.6: How do you access the nested structure member in an array of structures?

A

Using the dot operator, e.g., students[i].dob.day.

89
Q

21.7: In which scenario would you prefer using fputs() over printf()?

A

When you want to output a string exactly as is without formatting, or when writing to a file stream.

90
Q

8.4: Write the core loop logic for copying characters from src to dest.

A

while (*src) { *dest = *src; dest++; src++; }

91
Q

21.6: Why are fgets() and fputs() considered safer alternatives for file I/O?

A

They provide control over the number of characters read and do not cause buffer overflows.

92
Q

20.2: How do you use ftell() in a file operation?

A

It returns the current position of the file pointer.

93
Q

19.5: Why must you be cautious when working with unions?

A

Because writing to one member overwrites the storage of all other members.

94
Q

15.6: Why is it important to compare structure members individually?

A

Because there is no built-in structure comparison operator in C.

95
Q

7.5: Provide an example of copying a string using strcpy().

A

char src[] = "Hello"; char dest[20]; strcpy(dest, src);

96
Q

2.1: What is one key advantage of pointers related to memory management?

A

They allow dynamic memory allocation and deallocation at runtime.

97
Q

12.7: Write a code example declaration of a union holding an integer and a float.

A

union DataUnion { int i; float f; };

98
Q

17.1: What is a union in C?

A

A union is a user-defined data type in which all members share the same memory location.

99
Q

11.4: What condition verifies that the matched substring is a complete word?

A

Check that the character before (if any) and after the substring are not alphabetical.

100
Q

15.5: How would you manually compare two structure variables?

A

Compare each member individually: if (p1.x == p2.x && p1.y == p2.y) { … }

101
Q

2.6: Provide an example scenario where pointers are highly beneficial.

A

Passing a large structure to a function using its address to avoid expensive copying.

102
Q

10.7: How do you finally output the total consonant count after processing the sentence?

A

Using printf("Total consonants: %d\n", consonants);

103
Q

16.1: What is a nested structure in C?

A

A structure that is defined within another structure.

104
Q

3.6: How can you print the address stored in a pointer?

A

Using printf("%p", (void *)ptr);

105
Q

2.5: How can improper use of pointers lead to safety issues?

A

They can cause memory leaks, dangling pointers, and segmentation faults.

106
Q

1.3: What is the purpose of a loop in finding the smallest element?

A

To traverse the array, compare each element with the current smallest, and update if a smaller element is found.

107
Q

7.7: Why must the destination array be large enough in functions like strcpy() and strcat()?

A

To prevent buffer overflows, which can lead to undefined behavior or security vulnerabilities.

108
Q

13.6: Why might you use an array of structures for student information?

A

To store data for multiple students and loop through them for processing.

109
Q

2.0 Pointer Data Type – Advantages and Disadvantages

A

Advantages:

Dynamic Memory Management: Allows dynamic allocation and deallocation of memory.

Efficiency: Enables efficient array handling and function parameter passing (by reference).

Data Structures: Essential for implementing dynamic data structures (linked lists, trees, etc.).

Direct Memory Access: Useful for system-level programming (hardware interfacing).

Disadvantages:

Complexity: Can be difficult to understand and debug, especially with pointer arithmetic.

Safety Issues: Improper use can lead to memory leaks, dangling pointers, and segmentation faults.

Security Risks: Buffer overflows and unauthorized memory access are common pitfalls.

Scenario Examples:

Beneficial: Using pointers to pass large structures to functions to avoid copying overhead.

Issues: Incorrect pointer manipulation may cause a program crash.

110
Q

11.2: Why is it important to consider word boundaries when counting occurrences?

A

To ensure that substrings within larger words are not incorrectly counted.

111
Q

6.4: How is a string initialized using a pointer?

A

char *str = "Hello"; initializes a string literal.

112
Q

20.7: Why is it important to check the return value of file handling functions like fseek()?

A

To ensure the file pointer was moved successfully and to handle any potential errors.

113
Q

18.5: Provide one advantage of using structures over unions.

A

Structures allow you to maintain all members simultaneously without overwriting data.

114
Q

19.6: How does the shared memory layout of unions affect multi-member operations?

A

Only one member’s data is valid at any time, so simultaneous multi-type operations are not possible.

115
Q

8.6: Provide the complete function for concatenating strings without using strcat().

A

void myStrcat(char dest, const char src) { while (dest) dest++; while (src) { *dest = *src; dest++; src++; } *dest = ‘\0’; }

116
Q

15.1: How do you copy one structure variable to another in C?

A

By using the assignment operator, e.g., p2 = p1; (member-wise copy).

117
Q

4.4: How does dereferencing differ from accessing the pointer variable itself?

A

Dereferencing (*ptr) gives the value at the address, while ptr gives the address itself.

118
Q

14.6: How do you access a member of a structure variable?

A

Using the dot operator, e.g., student1.name.

119
Q

16.7: What advantage do nested structures offer in organizing data?

A

They create a clear and modular representation of complex data entities.

120
Q

11.7: Provide a brief explanation of why strncasecmp() is useful in this context.

A

It allows case-insensitive comparison, ensuring words like ‘Word’ and ‘word’ are counted as the same.

121
Q

12.6: Write a code example declaration of a structure holding an integer and a float.

A

struct Data { int i; float f; };

122
Q

16.5: Provide an example declaration of an array of student structures.

A

struct Student students[10];

123
Q

19.3: What is a key benefit of this storage method in unions?

A

It is memory efficient when you only need to store one value at a time.

124
Q

15.3: Is there a built-in operator to compare two structures in C?

A

No; you must compare each member manually.

125
Q

2.3: Name an advantage of pointers in implementing data structures.

A

Pointers are essential for creating dynamic data structures like linked lists and trees.

126
Q

21.2: How does fgets() differ from gets()?

A

fgets() limits the number of characters read, ensuring the buffer does not overflow, while gets() does not.

127
Q

19.4: What happens if you assign a value to one member of a union and then access another member?

A

The value will be interpreted based on the type of the accessed member, which may lead to undefined or unexpected results.

128
Q

20.1: What is the purpose of fseek() in file handling?

A

It moves the file pointer to a specified location in the file.

129
Q

9.6: Write a loop condition used in a palindrome check function.

A

while (start < end) { /* compare str[start] and str[end] */ }

130
Q

6.2: How can you declare a string in C?

A

For example: char str[100]; declares a string with space for 99 characters plus a null terminator.

131
Q

5.1: How can a pointer be used to access an array’s elements?

A

A pointer can be set to the array’s base address and then incremented using pointer arithmetic.

132
Q

2.2: How do pointers improve efficiency when passing large data structures?

A

They allow passing by reference, avoiding the cost of copying large structures.

133
Q

18.6: Provide one disadvantage of using unions compared to structures.

A

Unions limit you to storing only one type of data at a time, which may not be suitable for all applications.

134
Q

13.2: How do you declare a structure for student information in C?

A

struct Student { char name[50]; int rollNumber; char subject[30]; int iaMarks; };

135
Q

1.1: What is the first step when finding the smallest element in an array?

A

Assume the first element of the array is the smallest.

136
Q

11.5: Describe how to loop through the text to check for the target word.

A

Iterate through each character of the text and compare the substring starting at that position.

137
Q

4.7: What error might occur if you dereference an uninitialized pointer?

A

It can lead to undefined behavior or segmentation faults.

138
Q

13.4: How do you access a structure member?

A

Using the dot operator (e.g., s.name).

139
Q

7.6: When would you use strcmp() in a program?

A

To check if two strings are identical or to determine their lexicographical order.

140
Q

20.6: Provide a code snippet that demonstrates the use of fseek() and ftell().

A

fseek(fp, 10, SEEK_SET); long pos = ftell(fp);

141
Q

3.2: What operator is used to obtain the address of a variable?

A

The address-of operator (&).

142
Q

9.5: What result indicates that the string is not a palindrome?

A

If any pair of corresponding characters do not match.

143
Q

16.4: What is an array of structures used for?

A

To store multiple records of the same type (e.g., multiple students).

144
Q

18.4: What happens when you assign a new value to a union member?

A

It overwrites the value of the previously stored member because all members share the same memory.

145
Q

15.4: Provide a code snippet that demonstrates copying a structure.

A

struct Point p1 = {10, 20}; struct Point p2; p2 = p1;

146
Q

7.3: What is the purpose of strcmp() in C?

A

It compares two strings lexicographically and returns 0 if they are equal.

147
Q

5.4: Why might pointer-based array access be preferred in some scenarios?

A

It can lead to more concise code and sometimes optimize performance.

148
Q

7.1: What does the strcpy() function do?

A

It copies the source string into the destination string.

149
Q

5.5: Write a code snippet for reading an array using a pointer.

A

int arr[5]; int *ptr = arr; for (int i = 0; i < 5; i++) { scanf("%d", ptr + i); }

150
Q

9.4: What should be compared during the palindrome check?

A

The characters at the start and end indices moving toward the center.