Programming in C - UNIT II

43 flashcards · Other · by Kamala Viswanathan

  1. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array? A The element will be set to 0. B The compiler would report an error. C The program may crash if some important data gets overwritten. D The array size would appropriately grow.

    C. The program may crash if some important data gets overwritten.

  2. What does the following declaration mean? int (*ptr)[10]; A ptr is array of pointers to 10 integers B ptr is a pointer to an array of 10 integers C ptr is an array of 10 integers D ptr is an pointer to array

    B. ptr is a pointer to an array of 10 integers

  3. In C, if you pass an array as an argument to a function, what actually gets passed? A Value of elements in array B First element of the array C Base address of the array D Address of the last element of array

    C. Base address of the array

  4. What is right way to Initialize array? A intnum[6] = { 2, 4, 12, 5, 45, 5 }; B intn{} = { 2, 4, 12, 5, 45, 5 }; C intn{6} = { 2, 4, 12 }; D intn(6) = { 2, 4, 12, 5, 45, 5 };

    A intnum[6] = { 2, 4, 12, 5, 45, 5 };

  5. What will be the output of the program ? #include<stdio.h> void main() { int a[5] = {5, 1, 15, 20, 25}; inti, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); } A 3, 2, 15 B 2, 3, 20 C 2, 1, 15 D 1, 2, 5

    A. 3, 2, 15

  6. What will be the output of following program code? #include <stdio.h> intmain(void) { char p; char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8}; p = (buf + 1)[5]; printf("%d", p); return 0; } A. 5 B.6 C. 9 D. Error

    C. 9

  7. An array elements are always stored in ________ memory locations. A Sequential B Random C Sequential and Random D None of the above

    A. Sequential

  8. What is the maximum number of dimensions an array in C may have? A 2 B 8 C 20 D Theoratically no limit. The only practical limits are memory size and compilers.

    D.Theoratically no limit. The only practical limits are memory size and compilers.

  9. Size of the array need not be specified, when A Initialization is a part of definition B It is a declaratrion C It is a formal parameter D All of these

    D. All of these

  10. What will be printed after execution of the following code? void main() { intarr[10] = {1,2,3,4,5}; printf("%d", arr[5]); } A Garbage Value B 5 C 6 D 0

    D. 0

  11. What will be the output of the following code? void main() { int a[10]; printf("%d %d", a[-1], a[12]); } A 0 0 B Garbage value 0 C 0 Garbage Value D Garbage vlaue Garbage Value

    D. Garbage vlaue Garbage Value

  12. Array passed as an argument to a function is interpreted as A Address of the array. B Values of the first elements of the array. C Address of the first element of the array. D Number of element of the array.

    C. Address of the first element of the array.

  13. What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes? #include <stdio.h> void main() { int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; printf("%u, %u", a+1, &a+1); } A 65474, 65488 B 65480, 65488 C 65480, 65496 D 65474, 65476

    C. 65480, 65496

  14. What will be the output of the following program? #include <stdio.h> int main() { int arr[1] = {10}; printf("%d", 0[arr]); return 0; } A 1 B 0 C 10 D 6

    C. 10

  15. In C Programming, If we need to store word "INDIA" then syntax is as below – A char name[6] = {'I','N','D','I','A'}; B char name[6] = {'I','N','D','I','A','\0'} C char name[6] = {"I","N","D","I","A"} D name = "INDIA"

    B. char name[6] = {'I','N','D','I','A','\0'}

  16. What will be printed after execution of the following code? void main() { int arr[10] = {1,2,3,4,5}; printf("%d", arr[5]); } A Garbage Value B 5 C 6 D 0

    D. 0

  17. What will be the output of the program ? #include<stdio.h> intmain() { int a[5] = {51, 1, 5, 20, 25}; int x, y, z; x = ++a[1]; y = a[1]++; z = a[x++]; printf("%d, %d, %d", x, y, z); return 0; } A 2, 3, 20 B 2, 1, 5 C 1, 2, 5 D 3, 2, 5

    D. 3, 2, 5

  18. A string in C is A.1-D Array of Character B.2-D Array of Character C.Any of i& ii D.None of the above

    A.1-D Array of Character

  19. Any function working with String knows the String has ended when it encounters A.null character B.Empty space C."\1" D.pointer

    A.null character

  20. The ______ function appends not more than n characters. a) strcat() b) strcon() c) strncat() d) memcat()

    c) strncat() Explanation: The strncat() function appends not more than n characters from the array(s2) to the end of the string(s1).char *strncat(char *s1, const char *s2,size_t n);

  21. Which of the following is the variable type defined in header string. h? a) sizet b) size c) size_t d) size-t

    Answer: c Explanation: This is the unsigned integral type and is the result of the sizeof keyword.

  22. The______ function returns the number of characters that are present before the terminating null character. a) strlength() b) strlen() c) strlent() d) strchr()

    Answer: b Explanation: The strlen() function is used to return the number of characters that are present before the terminating null character.size-t strlen(const char *s);The length of the string pointed to by s is computed by strlen().

  23. which of the following function returns a pointer to the located string or a null pointer if string is not found. a) strtok() b) strstr() c) strspn() d) strrchr()

    Answer: b Explanation: The strstr() function is used to return a pointer to the located string, or if string is not found a null pointer is returned.

  24. Which of the given function is used to return a pointer to the located character? a) strrchr() b) strxfrm() c) memchar() d) strchar()

    Answer: d Explanation: The strchr() function is used to return a pointer to the located character if character does not occur then a null pointer is returned.

  25. What type of return-type used in String operations? a) void only b) void and (char *) only c) void and int only d) void, int and (char *) only

    d) void, int and (char *) only

  26. String operation such as strcat(s, t), strcmp(s, t), strcpy(s, t) and strlen(s) heavily rely upon. a) Presence of NULL character b) Presence of new-line character c) Presence of any escape sequence d) None of the mentioned

    Answer: a

  27. Which pre-defined function returns a pointer to the last occurence of a character in a string? a) strchr(s, c); b) strrchr(s, c); c) strlchr(s, c); d) strfchr(s, c);

    Answer: b

  28. Which of the following function compares 2 strings with case-insensitively? a) strcmp(s, t) b) strcmpcase(s, t) c) strcasecmp(s, t) d) strchr(s, t)

    Answer: c

  29. What will be the output of the following C code? #include <stdio.h> int main() { charstr[10] = "hello"; char *p = strrchr(str, 'l'); printf("%c\n", *(++p)); } a) l b) o c) e d) Compilation error

    Answer: b

  30. The ______ function appends not more than n characters. a) strcat() b) strcon() c) strncat() d) memcat()

    Answer c. Explanation: The strncat() function appends not more than n characters from the array(s2) to the end of the string(s1).char *strncat(char *s1, const char *s2,size_t n);

  31. NULL is the macro defined in the header string. h. a) true b) false

    Answer: a Explanation: NULL macro is the value of a null pointer constant.

  32. Which of the given function is used to return a pointer to the located character? a) strrchr() b) strxfrm() c) memchar() d) strchar()

    Answer: d Explanation: The strchr() function is used to return a pointer to the located character if character does not occur then a null pointer is returned.

  33. What is the advantage of selection sort over other sorting techniques? A) It requires no additional storage space B) It is scalable C) It works best for inputs which are already sorted D) It is faster than any other sorting technique

    A) It requires no additional storage space

  34. What is the disadvantage of selection sort? A) It requires auxiliary memory B) It is not scalable C) It can be used for small keys D) It takes linear time to sort the elements

    B) It is not scalable

  35. In the following scenarios, when will you use selection sort? A) The input is already sorted B) A large file has to be sorted C) Large values need to be sorted with small keys D) Small values need to be sorted with large keys

    C) Large values need to be sorted with small keys

  36. Select the appropriate code that performs selection sort. a) b) c) d)

    Explanation: Selection sort loops through the list, starting with the first element as the ‘min’ element, to pick the least element, which is then swapped with the ‘min’ element. Selection sort is a sorting algorithm for in-place comparisons in computer science. It has an O(n2) time complexity, making it inefficient on broad lists and generally outperforming the insertion type. Selection sort is known for its simplicity, and in some cases, such as when auxiliary memory is small, it outperforms…

  37. What is the advantage of recursive approach than an iterative approach? a) Consumes less memory b) Less code and easy to implement c) Consumes more memory d) More code has to be written

    Answer: b A recursive approach is easier to understand and contains fewer lines of code.

  38. Given an inputarr = {2,5,7,99,899}; key = 899; What is the level of recursion? a) 5 b) 2 c) 3 d) 4

    Answer: c Clarification : level 1: mid = 7 level 2: mid = 99 level 3: mid = 899(this is the key).

  39. Given an array an = {45,77,89,90,94,99,100} and key = 99; what are the mid values(corresponding array elements) in the first and second levels of recursion? a) 90 and 99 b) 90 and 94 c) 89 and 99 d) 89 and 94

    Answer: a Clarification: At first level key = 90 At second level key= 99 Here 90 and 99 are mid values.

  40. What is the worst case complexity of binary search using recursion? a) 0(nlogn) b) 0(logn) c) 0(n) d) 0(n2)

    Answer: b Clarification: Using the divide and conquer master theorem.

  41. . What is the average case time complexity of binary search using recursion? a) 0(nlogn) b) 0(logn) c) 0(n) d) 0(n2)

    Answer: b Clarification: T(n) = T(n/2) + 1, Using the divide and conquer master theorem.

  42. Which of the following is not an application of binary search? a) To find the lower/upper bound in an ordered sequence b) Union of intervals c) Debugging d) To search in unordered list

    Answer: d Clarification: In Binary search. the elements in the list should be sorted. It is applicable only for ordered list. Hence Binary search in unordered list is not an application. Choose among the following code for an iterative binary search.

  43. Choose the correct version of binary search program. a)

    Answer b Clarification: Find the 'mid', check if it equals the key, if not, continue the iterations until low <= high.