Unit 5 - Practice Quiz

CSE109

1 Which header file must be included to use dynamic memory allocation functions like malloc and calloc?

A. <stdio.h>
B. <stdlib.h>
C. <string.h>
D. <mem.h>

2 What is the return type of the malloc function?

A. int *
B. char *
C. void *
D. null *

3 Which of the following functions allocates memory and initializes all bits to zero?

A. malloc
B. realloc
C. calloc
D. free

4 In which memory segment is dynamically allocated memory (using malloc) stored?

A. Stack
B. Heap
C. Data Segment
D. Code Segment

5 What is the correct syntax to allocate an array of 10 integers dynamically?

A. int *ptr = (int*) malloc(10);
B. int *ptr = (int*) malloc(10 * sizeof(int));
C. int *ptr = malloc(int, 10);
D. int *ptr = (int) malloc(10 * sizeof(int));

6 What happens if malloc fails to allocate the requested memory?

A. It returns a generic pointer.
B. The program crashes immediately.
C. It returns NULL.
D. It returns a pointer to the stack.

7 What is the primary purpose of the realloc function?

A. To free memory.
B. To initialize memory to zero.
C. To resize a previously allocated memory block.
D. To allocate memory on the stack.

8 Which function is used to deallocate memory to prevent memory leaks?

A. delete
B. dealloc
C. remove
D. free

9 What constitutes a Memory Leak?

A. Accessing memory out of bounds.
B. Allocating memory but failing to release it when it is no longer needed.
C. Using a pointer after it has been freed.
D. Allocating more memory than the RAM capacity.

10 Consider the following code:
c
int p = (int)malloc(sizeof(int));
free(p);
*p = 10;

What is this scenario called?

A. Memory Leak
B. Dangling Pointer
C. Null Pointer Dereference
D. Stack Overflow

11 What are the arguments required for the calloc function?

A. Total number of bytes.
B. Number of elements and size of each element.
C. Pointer to memory and new size.
D. Size of element only.

12 What is the result of sizeof(char) in C?

A. 1 byte
B. 2 bytes
C. 4 bytes
D. Dependent on the compiler

13 Identify the potential issue in this code snippet:
c
void fun() {
char str = (char )malloc(20);
strcpy(str, "Hello");
return;
}

A. Syntax Error
B. Buffer Overflow
C. Memory Leak
D. Type Mismatch

14 If ptr is a NULL pointer, what does free(ptr) do?

A. Causes a runtime error.
B. Causes a compilation error.
C. Does nothing.
D. Allocates new memory.

15 When initializing a string using char s[] = "Hello";, what is the size of the array s?

A. 5
B. 6
C. 4
D. Pointer size

16 Which character is used to terminate a string in C?

A. '.'
B. '\n'
C. '\0'
D. '0'

17 What is the correct format specifier to read a string using scanf without reading spaces?

A. %c
B. %s
C. %str
D. %d

18 Why is gets() generally avoided or removed in modern C standards?

A. It is too slow.
B. It does not support spaces.
C. It cannot check buffer bounds (Buffer Overflow risk).
D. It returns an integer instead of a pointer.

19 Which function is the safest alternative to read a line of text including spaces?

A. scanf
B. gets
C. fgets
D. getchar

20 What will be the output of printf("%d", 'c' - 'a'); assuming ASCII encoding?

A. 1
B. 2
C. 3
D. 99

21 Which logic correctly checks if a character variable ch is a digit?

A. if (ch >= 0 && ch <= 9)
B. if (ch >= '0' && ch <= '9')
C. if (ch == digit)
D. if (ch > '0')

22 Which header file defines string manipulation functions like strcpy and strlen?

A. <stdlib.h>
B. <string.h>
C. <ctype.h>
D. <text.h>

23 What does the function strlen(str) return?

A. The size of the array str.
B. The number of characters in str including \0.
C. The number of characters in str excluding \0.
D. The memory address of str.

24 What is the correct syntax to copy string src into dest?

A. strcopy(dest, src);
B. strcpy(src, dest);
C. strcpy(dest, src);
D. dest = src;

25 What does strcat(s1, s2) do?

A. Copies s2 into s1.
B. Compares s1 and s2.
C. Concatenates s2 to the end of s1.
D. Finds s2 inside s1.

26 If strcmp(s1, s2) returns 0, what does it imply?

A. s1 is greater than s2.
B. s1 is less than s2.
C. s1 is equal to s2.
D. The function failed.

27 What value does strcmp("Apple", "Banana") likely return?

A.
B. A positive value
C. A negative value
D. NULL

28 Which function is used to convert a string to an integer?

A. itoa
B. atoi
C. str2int
D. to_int

29 Consider char *s = "Hello";. What happens if you try s[0] = 'h';?

A. The string becomes "hello".
B. Compiler error.
C. Undefined behavior (often Segmentation Fault).
D. The pointer moves to the next character.

30 What is the purpose of strchr(str, ch)?

A. To count occurrences of ch in str.
B. To return a pointer to the first occurrence of ch in str.
C. To remove ch from str.
D. To replace ch in str.

31 Which function converts a character to uppercase?

A. to_upper()
B. upper()
C. toupper()
D. strupr()

32 How do you calculate the memory size required to store a string of length ?

A. bytes
B. bytes
C. bytes
D. bytes

33 Which function finds the first occurrence of a substring within a string?

A. strchr
B. strrchr
C. strstr
D. strsub

34 What is the result of "10" + "20" in C?

A. "1020"
B. 30
C. Compilation Error (Invalid operand for binary +).
D. Address addition.

35 What is the logic to convert a digit character c ('0'-'9') to its integer value?

A. c
B. c - 0
C. c - '0'
D. c + '0'

36 Which code correctly iterates through a string s?

A. for(i=0; s[i] != '\0'; i++)
B. for(i=0; i < s; i++)
C. for(i=0; s[i] != NULL; i++)
D. for(i=0; s[i] > 0; i++)

37 What happens in strcpy(str, "VeryLongString...") if str is declared as char str[5];?

A. The string is truncated automatically.
B. Buffer Overflow occurs.
C. Memory is automatically resized.
D. The compiler prevents it.

38 What does strncpy offer over strcpy?

A. It is faster.
B. It allows specifying the maximum number of characters to copy.
C. It automatically allocates memory.
D. It converts case during copy.

39 Which function is used to tokenize a string (split it by delimiters)?

A. strsplit
B. strtok
C. strbreak
D. strcut

40 When defining an array of strings, which declaration is valid?

A. char names[5][20];
B. char *names[5];
C. Both A and B.
D. Neither A nor B.

41 What is the return type of sizeof operator?

A. int
B. double
C. size_t
D. void

42 How do you correctly free a 2D array allocated via pointers to pointers?

A. Free the double pointer only.
B. Free each row pointer first, then the double pointer.
C. Free the double pointer first, then each row.
D. Use free_all.

43 Given char str[] = "Code";, what is *(str + 1)?

A. 'C'
B. 'o'
C. 'd'
D. Address of 'o'

44 What happens if you define a string as char s[5] = "Hello";?

A. It works perfectly.
B. The string is not null-terminated.
C. Compilation error.
D. The 'o' is truncated.

45 Which loop correctly converts a string s to lowercase?

A. for(int i=0; s[i]; i++) s[i] = tolower(s[i]);
B. for(int i=0; s[i]; i++) s[i] = s[i] - 32;
C. for(int i=0; s[i]; i++) tolower(s[i]);
D. s = tolower(s);

46 What is the output of printf("%s", "Hello" + 2);?

A. Hello
B. lo
C. llo
D. Compilation Error

47 Why must you cast the return value of malloc in C++ but not strictly in C?

A. C++ does not support void*.
B. C allows implicit conversion from void*, C++ does not.
C. malloc is not available in C++.
D. C compilers are smarter.

48 Which format specifier reads a single character?

A. %s
B. %c
C. %d
D. %chr

49 If realloc returns NULL, what happens to the original memory block?

A. It is freed automatically.
B. It remains valid and unchanged.
C. It becomes undefined.
D. It is moved to the stack.

50 What is the ASCII value of the null terminator \0?

A.
B. 1
C. -1
D. 32