0% found this document useful (0 votes)
11 views

Unit - III - Question Bank

Uploaded by

hzzz42765
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)
11 views

Unit - III - Question Bank

Uploaded by

hzzz42765
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/ 18

1. What is the return value of the strlen() function in C?

A) The number of characters in the string


B) The number of bytes in the string
C) The length of the string including the null character
D) The length of the string excluding the null character

Answer: D) The length of the string excluding the null character

2. Which function is used to copy one string into another in C?

A) strcat()
B) strcpy()
C) strchr()
D) strncpy()

Answer: B) strcpy()

3. Which of the following functions compares two strings in C?

A) strchr()
B) strcmp()
C) strstr()
D) strcat()

Answer: B) strcmp()

4. What does the function strncat() do in C?

A) It compares two strings


B) It concatenates up to n characters from one string to another
C) It returns a pointer to the first occurrence of a character in a string
D) It splits a string into tokens

Answer: B) It concatenates up to n characters from one string to another

5. Which function is used to find the first occurrence of a character in a string?

A) strrchr()
B) strtok()
C) strchr()
D) strstr()

Answer: C) strchr()

6. What is the purpose of strtok() in C?


A) To split a string into tokens based on delimiters
B) To find a substring in a string
C) To compare two strings
D) To find the length of a string

Answer: A) To split a string into tokens based on delimiters

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()

8. The strdup() function in C:

A) Duplicates a string by creating a new copy in memory


B) Finds the last occurrence of a character in a string
C) Compares two strings lexicographically
D) Concatenates two strings

Answer: A) Duplicates a string by creating a new copy in memory

9. Which of the following functions is used to concatenate two strings in C?

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) The total size of the string, including the null character.


B) The size of the string excluding the null character.
C) The address of the null character.
D) The number of bytes allocated for the string.

Answer: B) The size of the string excluding the null character.

2. Which function is used to concatenate two strings in C?

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

4. Which function is used to find the first occurrence of a character in a string?

A) strchr()
B) strrchr()
C) strstr()
D) strcspn()

Answer: A) strchr()
5. What does the strtok() function do in C?

A) It compares two strings lexicographically.


B) It splits a string into tokens based on a delimiter.
C) It finds the length of a string.
D) It reverses a string.

Answer: B) It splits a string into tokens based on a delimiter.

6. How does strncpy() function differ from strcpy()?

A) It copies a string and adds a null character at the end.


B) It copies only the first n characters from one string to another.
C) It concatenates strings.
D) It compares two strings.

Answer: B) It copies only the first n characters from one string to another.

7. Which function is used to find the first occurrence of a substring in a string?

A) strchr()
B) strrchr()
C) strstr()
D) strcspn()

Answer: C) strstr()

8. What does the strrev() function do in C?

A) It finds the last occurrence of a character in a string.


B) It splits a string into tokens.
C) It reverses the string.
D) It copies a string to another.

Answer: C) It reverses the string.


(Note: strrev() is not part of the C Standard Library, but some compilers, such as Turbo C,
include it.)
9. Which function is used to find the length of the initial segment of a string
that doesn't contain any characters from a given set?

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?

A) It adds a null character at the end of the destination string.


B) It copies the source string including the null character to the destination string.
C) It compares the two strings and returns the result.
D) It appends the source string to the destination string.

Answer: B) It copies the source string including the null character to the destination string.

11. What is the output of the following C code?


c
Copy code
char str[] = "Hello";
str[0] = 'J';
printf("%s", str);

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)

Answer: A) strncpy(destination, source, 5)

13. What does strcat() do in C?

A) It finds a substring in a string.


B) It compares two strings.
C) It concatenates one string to the end of another string.
D) It copies a string to another.

Answer: C) It concatenates one string to the end of another string.

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()

15. What does the function strrchr() do in C?

A) It finds the first occurrence of a character in a string.


B) It finds the last occurrence of a character in a string.
C) It reverses a string.
D) It splits a string into tokens.

Answer: B) It finds the last occurrence of a character in a string.


Functions in C
1. Which of the following is the correct syntax for defining a function in C?

A) function int add(int a, int b)


B) int add(int a, int b) { return a + b; }
C) int add(a, b) { int a, int b; return a + b; }
D) void add(int a, int b) { return a + b; }

Answer: B) int add(int a, int b) { return a + b; }

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

4. Which of the following is correct for calling a function from main()?

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.

5. What is the purpose of the return statement in a function?

A) It terminates the function and returns control to the calling function.


B) It defines the function’s parameters.
C) It makes the function infinite.
D) It defines the data type of the function.

Answer: A) It terminates the function and returns control to the calling function.

6. What does the following function declaration mean in C?


c
Copy code
int add(int a, int b);

A) The function add takes no parameters and returns an integer.


B) The function add takes two integer arguments and returns an integer.
C) The function add takes two integer arguments and returns void.
D) The function add returns an integer but does not take any parameters.

Answer: B) The function add takes two integer arguments and returns an integer.

7. Which of the following statements is true about function overloading in C?

A) C supports function overloading.


B) C does not support function overloading.
C) Function overloading is supported by C, but only for functions returning void.
D) C supports function overloading but requires the same return type for all functions.

Answer: B) C does not support function overloading.

8. What is a function prototype in C?

A) It is the body of the function.


B) It is the name of the function without parameters.
C) It is a declaration of the function that specifies its return type and parameters.
D) It is a list of functions that are called in main().

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?

A) int sum(int a, int b) { return a - b; }


B) sum(int a, int b) { return a + b; }
C) int sum(int a, int b) { return a + b; }
D) int sum(int, int) { return a + b; }

Answer: C) int sum(int a, int b) { return a + b; }

11. What is the output of the following C code?


c
Copy code
#include <stdio.h>
void printMessage() {
printf("Hello, World!\n");
}

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?

A) The program will give a warning but continue execution.


B) The program will give a compilation error.
C) The program will return 0 by default.
D) The function will return a random value.

Answer: B) The program will give a compilation error.

13. What will the following code output?


c
Copy code
#include <stdio.h>
int add(int a, int b) {
return a + b;
}

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

14. Which of the following statements is true about recursive functions in C?

A) Recursive functions never terminate.


B) A recursive function must have a base case.
C) A recursive function does not return a value.
D) Recursive functions must have a function prototype.

Answer: B) A recursive function must have a base case.


15. What does the following function prototype indicate in C?
c
Copy code
int multiply(int, int);

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.

16. How do you pass an argument to a function in C?

A) By value
B) By reference
C) Both A and B
D) Arguments cannot be passed to a function in C.

Answer: C) Both A and B

Function Pointers and passing array to the


function
1. How do you pass an array 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: B) By reference (using pointers)

2. What does the following function declaration represent?


c
Copy code
void printArray(int arr[], int size);
A) A function that takes a pointer to an integer array and its size.
B) A function that takes a pointer to an integer and its size.
C) A function that takes an array of integers and returns an integer.
D) A function that takes an integer and returns an array.

Answer: A) A function that takes a pointer to an integer array and its size.

3. What is the correct way to pass an array to a function in C?

A) function(array);
B) function(&array);
C) function(*array);
D) function(array[0]);

Answer: A) function(array);

4. How can you access an element of an array passed to a function?

A) Using the array index, like arr[i]


B) By dereferencing the pointer, like *(arr + i)
C) Both A and B
D) You cannot access array elements passed to a function.

Answer: C) Both A and B

5. What will happen if you pass an array without its size to a function?

A) The function will throw a compilation error.


B) The function can still access the elements, but it won't know the array's size.
C) The function will automatically determine the size.
D) The program will crash.

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?

A) To pass arrays to functions.


B) To allow dynamic function selection during program execution.
C) To pass values by reference.
D) To return multiple values from a function.

Answer: B) To allow dynamic function selection during program execution.

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) int* func_ptr(int, int);


B) int (*func_ptr)(int, int);
C) int func_ptr(int, int);
D) int (*func_ptr)[int, int];

Answer: B) int (*func_ptr)(int, int);

8. How do you assign a function to a function pointer?

A) func_ptr = &function_name;
B) func_ptr = function_name;
C) Both A and B
D) func_ptr = *function_name;

Answer: C) Both A and B

9. How do you call a function using a function pointer?

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 add(int a, int b) {


return a + b;
}

int multiply(int a, int b) {


return a * b;
}

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?

A) The pointer will automatically point to the main() function.


B) The function pointer will be NULL, and calling it will result in a segmentation fault.
C) The pointer will remain uninitialized, leading to undefined behavior when called.
D) The function pointer will be assigned to the default library 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

1: Explain the concept of functions in C. What is a function prototype? Write a program to


demonstrate a function that calculates the power of a number using both recursion and iteration.
Compare both approaches in terms of performance and memory usage.

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.

4. Passing Array Elements to a Function

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

SRM Institute of Science and Technology

Ramapuram.

You might also like