Top 50 VIVA Questions and Answers:  Arrays and Strings in C Programming

Top 50 VIVA Questions and Answers:  Arrays and Strings in C Programming


Prepare for your C programming VIVA with confidence! Explore our comprehensive list of the top 50 VIVA questions and detailed answers focused on arrays and strings. From array declarations to string manipulations, this guide is designed to enhance your understanding of these crucial concepts. Whether you're gearing up for an exam or an interview, these questions cover the breadth and depth of arrays and strings in C. Elevate your knowledge and conquer your VIVA with ease!


VIVA Questions and Answers:

Arrays:

  1. What is an array in C?

    • An array is a collection of elements of the same data type stored in contiguous memory locations.

  2. How do you declare an array in C?

    • The syntax is datatype arrayName[arraySize];

  3. Explain the concept of array indexing.

    • Array indexing is the process of accessing individual elements in an array using their position (index).

  4. Can an array have elements of different data types?

    • No, all elements in an array must be of the same data type.

  5. What is the difference between an array and a pointer in C?

    • An array is a collection of elements, and a pointer is a variable that stores the memory address of another variable.

  6. How do you initialize an array in C?

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

  7. Explain the purpose of a two-dimensional array.

    • A two-dimensional array is a matrix, allowing storage of data in rows and columns.

  8. What is the significance of the sizeof operator with arrays?

    • sizeof returns the total number of bytes occupied by the array.

  9. Can you change the size of an array once it's declared?

    • No, the size of an array is fixed upon declaration and cannot be changed.

  10. Discuss the role of an array in a function parameter.

    • Arrays can be passed to functions, and changes made to the array within the function affect the original array.

  11. Explain the concept of a dynamic array.

    • A dynamic array is created using pointers and memory allocation functions (malloc, calloc, realloc).

  12. How do you find the length of an array in C?

    • The length of an array can be determined by dividing the total size by the size of each element (sizeof).
  13. Discuss the relationship between arrays and pointers.

    • Arrays and pointers are closely related; the name of an array can be used as a pointer to its first element.

  14. What is an array of pointers?

    • An array of pointers is an array where each element is a pointer.

  15. Explain the concept of a jagged array.

    • A jagged array is an array of arrays, where each row can have a different number of elements.

  16. How do you pass a one-dimensional array to a function?

    • You can pass an array to a function by specifying its name without square brackets.

  17. What is the purpose of the memset function with arrays?

    • memset is used to fill a block of memory with a particular value (commonly used for initializing arrays).

  18. How are arrays used in searching and sorting algorithms?

    • Arrays are commonly used for implementing algorithms like linear search, binary search, and various sorting algorithms.
  19. Explain the concept of a static array.

    • A static array has a fixed size determined at compile-time, and its memory is allocated on the stack.

  20. What is the role of the const keyword with arrays?

    • The const keyword can be used to create constant arrays, making their elements immutable.

Strings:

  1. What is a string in C?

    • A string is an array of characters terminated by a null character ('\0').

  2. How do you declare and initialize a string in C?

    • char str[] = "Hello";

  3. Explain the purpose of the null character in strings.

    • The null character ('\0') marks the end of a string in C.

  4. What is the difference between char and char* in C?

    • char is a data type for a single character, and char* is a pointer to a character or the beginning of a string.

  5. How are strings passed to functions in C?

    • Strings are passed to functions as pointers to their first characters.

  6. Discuss the use of the strlen function with strings.

    • strlen returns the length of a string (number of characters) excluding the null character.

  7. What is the purpose of the strcpy function in C?

    • strcpy is used to copy the contents of one string into another.

  8. Explain the concept of a character array as a string.

    • A character array is a string if it is null-terminated.

  9. How do you concatenate two strings in C?

    • strcat is used for concatenating two strings.

  10. Discuss the importance of the strcmp function in C.

    • strcmp is used to compare two strings lexicographically.

  11. What is the role of the strncpy function in C?

    • strncpy is used to copy a specific number of characters from one string to another.

  12. How are strings manipulated using pointers in C?

    • Pointers can be used to iterate through strings, access individual characters, and perform various operations.

  13. What is the purpose of the strchr function with strings?

    • strchr searches for the first occurrence of a character in a string.

  14. Explain the concept of a null-terminated string.

    • A null-terminated string is a sequence of characters followed by a null character ('\0') to mark the end.

  15. How are strings stored in memory in C?

    • Strings are stored as arrays of characters, and each character occupies one byte of memory.

  16. What is the significance of the strstr function in C?

    • strstr searches for the first occurrence of a substring in a string.

  17. How can you convert a string to an integer in C?

    • atoi or strtol functions can be used for string to integer conversion.

  18. Discuss the purpose of the strncat function in C.

    • strncat is used to concatenate a specific number of characters from one string to another.

  19. What is the role of the strtok function in C?

    • strtok is used to tokenize a string, breaking it into smaller parts based on a delimiter.
  20. Explain the concept of a wide string in C.

    • Wide strings (wchar_t) are used to represent characters from the extended character set.

  21. How do you find the length of a string without using the strlen function?

    • Iterate through the characters until the null character is encountered.

  22. Discuss the use of the sprintf function with strings.

    • sprintf is used to format and store a series of characters in a string.

  23. What is the purpose of the strcspn function in C?

    • strcspn returns the length of the initial segment of a string consisting of characters not in a specified set.

  24. Explain the concept of a null pointer in C strings.

    • A null pointer in C strings refers to a pointer that does not point to a valid memory location.

  25. How do you reverse a string in C?

    • Iterate through the string, swapping the first and last characters, then the second and second-to-last, and so on.

  26. What is the purpose of the strlwr function in C?

    • strlwr converts all uppercase letters in a string to lowercase.

  27. Discuss the significance of the strrev function in C.

    • strrev is used to reverse the characters in a string.

  28. How can you check if two strings are equal without using strcmp?

    • Iterate through corresponding characters and check for equality manually.

  29. Explain the role of the strpbrk function in C.

    • strpbrk searches for the first occurrence of any character from a specified set in a string.

  30. What is the significance of the strxfrm function in C strings?

    • strxfrm is used to transform a string to a form suitable for locale-specific string comparison.