Unit - III - Question Bank
Unit - III - Question Bank
A) strcat()
B) strcpy()
C) strchr()
D) strncpy()
Answer: B) strcpy()
A) strchr()
B) strcmp()
C) strstr()
D) strcat()
Answer: B) strcmp()
A) strrchr()
B) strtok()
C) strchr()
D) strstr()
Answer: C) strchr()
7. Which function returns the length of the initial segment of a string that consists only of
characters from a given set?
A) strspn()
B) strcspn()
C) strlen()
D) strtok()
Answer: A) strspn()
A) strcpy()
B) strcat()
C) strcmp()
D) strchr()
Answer: B) strcat()
10. Which function returns a pointer to the first occurrence of a substring in a string?
A) strtok()
B) strstr()
C) strrchr()
D) strspn()
Answer: B) strstr()
Operations in Strings
1. What does the strlen() function in C return?
A) strcpy()
B) strcat()
C) strcmp()
D) strchr()
Answer: B) strcat()
3. What is the return value of the strcmp() function when two strings are
identical?
A) 1
B) 0
C) -1
D) NULL
Answer: B) 0
A) strchr()
B) strrchr()
C) strstr()
D) strcspn()
Answer: A) strchr()
5. What does the strtok() function do in C?
Answer: B) It copies only the first n characters from one string to another.
A) strchr()
B) strrchr()
C) strstr()
D) strcspn()
Answer: C) strstr()
A) strspn()
B) strcspn()
C) strlen()
D) strtok()
Answer: B) strcspn()
10. What happens when you use strcpy() to copy one string into another string
in C?
Answer: B) It copies the source string including the null character to the destination string.
A) Hello
B) Jello
C) Jell
D) Compilation error
Answer: B) Jello
12. How do you safely copy only the first 5 characters of a string into another
string using C?
A) strncpy(destination, source, 5)
B) strcpy(destination, source, 5)
C) strcat(destination, source, 5)
D) strcpy(destination, source)
14. Which function returns the length of the initial segment of a string that
consists only of characters from a given set?
A) strchr()
B) strcspn()
C) strspn()
D) strlen()
Answer: C) strspn()
2. What is the correct way to call a function named add that takes two integer
arguments in C?
A) add(5, 3)
B) call add(5, 3)
C) int add(5, 3)
D) add(5 + 3)
Answer: A) add(5, 3)
3. What is the return type of a function that doesn't return any value in C?
A) int
B) void
C) char
D) NULL
Answer: B) void
A) You can only call functions that are declared before main().
B) You can call functions in any order as long as they are declared before calling.
C) You can only call functions that are defined before main().
D) Functions cannot be called from main().
Answer: B) You can call functions in any order as long as they are declared before calling.
Answer: A) It terminates the function and returns control to the calling function.
Answer: B) The function add takes two integer arguments and returns an integer.
Answer: C) It is a declaration of the function that specifies its return type and parameters.
9. Which keyword is used to call a function that does not return any value in C?
A) void
B) return
C) exit
D) call
Answer: A) void
10. Which of the following correctly defines a function that takes two integer
arguments and returns the sum?
int main() {
printMessage();
return 0;
}
A) Hello
B) World!
C) Hello, World!
D) Compilation error
Answer: C) Hello, World!
12. What happens when you do not provide a return value in a non-void
function?
int main() {
int result = add(10, 20);
printf("%d", result);
return 0;
}
A) 30
B) 1020
C) 10 20
D) Compilation error
Answer: A) 30
A) The function multiply returns an integer and takes two integer parameters.
B) The function multiply returns void and takes two integer parameters.
C) The function multiply returns an integer and does not take any parameters.
D) The function multiply returns an integer and takes no parameters.
Answer: A) The function multiply returns an integer and takes two integer parameters.
A) By value
B) By reference
C) Both A and B
D) Arguments cannot be passed to a function in C.
A) By value
B) By reference (using pointers)
C) By copying the entire array to the function
D) Arrays cannot be passed to functions in C
Answer: A) A function that takes a pointer to an integer array and its size.
A) function(array);
B) function(&array);
C) function(*array);
D) function(array[0]);
Answer: A) function(array);
5. What will happen if you pass an array without its size to a function?
Answer: B) The function can still access the elements, but it won't know the array's size.
6. What is the purpose of using function pointers in C?
7. Which of the following is the correct way to declare a function pointer that
points to a function taking two int parameters and returning an int?
A) func_ptr = &function_name;
B) func_ptr = function_name;
C) Both A and B
D) func_ptr = *function_name;
A) function_ptr();
B) (*function_ptr)();
C) function_ptr()();
D) function_ptr = function_ptr();
Answer: B) (*function_ptr)();
10. What is the output of the following code?
c
Copy code
#include <stdio.h>
int main() {
int (*func_ptr)(int, int);
func_ptr = add;
printf("%d\n", func_ptr(5, 3)); // Output: 8
func_ptr = multiply;
printf("%d\n", func_ptr(5, 3)); // Output: 15
return 0;
}
A) 5 3
B) 8 15
C) 8 3
D) 15 8
Answer: B) 8 15
11. What happens if you declare a function pointer but do not assign it to any
function?
Answer: C) The pointer will remain uninitialized, leading to undefined behavior when called.
12. Which of the following best describes the behavior of passing an array to a
function in C?
A) The array is copied to the function, and any changes made to it inside the function will not
affect the original array.
B) The array is passed by reference, so any changes made to it inside the function will affect the
original array.
C) The array cannot be passed to functions.
D) The array is passed by value, and changes inside the function will be local.
Answer: B) The array is passed by reference, so any changes made to it inside the function will
affect the original array.
Part –B
1: Explain the various string functions available in C. Write a program using strlen(), strcpy(),
strcat(), and strcmp() to demonstrate the usage of these functions.
2: Discuss the differences between strcpy() and strncpy(). Write a program to demonstrate their
differences.
3: What is the purpose of the strchr() and strstr() functions? Write a program to demonstrate how
both functions can be used to find a character and a substring within a string.
2. Operations on Strings
1: Explain the different operations that can be performed on strings in C. Illustrate string
concatenation, comparison, and length calculation through examples.
2: Write a program to reverse a string in C using a string manipulation function.
3: What is the role of the null character (\0) in string operations in C? Explain its importance and
demonstrate with examples such as string initialization, comparison, and copying.
3. Functions in C
1: Define a function in C. Explain how parameters are passed to functions and describe the
difference between passing parameters by value and by reference. Demonstrate with examples.
2: Write a program in C to calculate the factorial of a number using recursion. Also, explain the
working of recursion in functions.
3: What are function prototypes in C? Explain their importance with an example and show how
you can declare and define a function in C.
4. Passing Array Elements to a Function
1: Explain how arrays can be passed to functions in C. Demonstrate with an example where you
pass an array to a function and modify its elements inside the function.
2: Write a program that passes an array of integers to a function that calculates and returns the
average of the array elements.
3: How are arrays passed to functions in C? Discuss the differences between passing an array by
value and by reference. Write a program to illustrate both methods.
5. Function Pointers
1: What are function pointers in C? How can they be used to implement callback functions? Write a
program demonstrating the use of function pointers to call different functions dynamically.
2: Explain the syntax and use of function pointers in C. Write a program that uses function pointers to
perform mathematical operations (addition, subtraction, etc.) based on user input.
3: What are the advantages of using function pointers in C? Write a program using a function pointer to
implement a simple menu-driven system to perform different operations.
Part C
1: Describe the different built-in string functions in C. Write a program to perform string
comparison, concatenation, and copying using the functions strcmp(), strcat(), and
strcpy(). Explain how they work with examples.
2: Discuss the importance of the null character \0 in string handling in C. Explain how different
string functions such as strlen(), strchr(), strstr(), and strcmp() treat the null character
with examples.
3: Write a program that demonstrates how to use strtok() to split a string into tokens. Explain
how the function works and the role of delimiters in splitting strings.
2. Operations on Strings
1: Explain the basic operations that can be performed on strings in C. Illustrate with examples of
string reversal, string comparison, string concatenation, and finding the length of a string using
different approaches (e.g., loops, built-in functions like strlen()).
2: Write a program to implement your own version of strcmp() to compare two strings without
using the standard strcmp() function. Explain how your implementation works.
3: Implement a C program that reverses a string using two different approaches: one using a loop
and another using recursion. Explain how both approaches work with a focus on memory
management and efficiency.
3. Functions in C
2: Write a program in C that defines a function to calculate the greatest common divisor (GCD)
of two numbers using Euclid’s algorithm. Explain how recursion works in this function.
3: Define a function in C with both return value and parameters. Write a program that uses a
function to compute the area and perimeter of a rectangle, taking the length and width as
parameters, and returning both values.
1: Discuss how arrays are passed to functions in C. Compare passing arrays by value and by
reference. Write a program that demonstrates modifying the contents of an array inside a
function by passing it by reference.
2: Write a program in C to find the maximum and minimum values in an array of integers. The
program should pass the array to a function, which will calculate and return the results.
3: Explain how multi-dimensional arrays are passed to functions in C. Write a program that finds
the sum of all elements in a 2D array by passing it to a function and returning the result.
5. Function Pointers
1: What are function pointers in C? How do they work? Write a program that demonstrates the
use of function pointers to call different mathematical functions (e.g., addition, subtraction,
multiplication, division) based on user input.
2: Explain the concept of callback functions using function pointers. Write a program that uses a
function pointer to implement a menu-driven program where the user can choose from several
different operations (such as addition, subtraction, multiplication, division).
3: Write a program that uses a function pointer to implement a sorting function for an array. The
program should use the function pointer to allow the user to select either ascending or
descending order for the sort operation.
Prepared by,
M.Chitra AP/CSE
S.Lakshmi AP/CSE
Ramapuram.