Unit 5 - Practice Quiz

CSE109 50 Questions
0 Correct 0 Wrong 50 Left
0/50

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

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

2 What is the return type of the malloc function?

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

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

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

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

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

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

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

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

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

7 What is the primary purpose of the realloc function?

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

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

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

9 What constitutes a Memory Leak?

A. Using a pointer after it has been freed.
B. Accessing memory out of bounds.
C. Allocating memory but failing to release it when it is no longer needed.
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. Null Pointer Dereference
B. Dangling Pointer
C. Stack Overflow
D. Memory Leak

11 What are the arguments required for the calloc function?

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

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

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

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

A. Buffer Overflow
B. Memory Leak
C. Syntax Error
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. Pointer size
D. 4

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

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

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

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

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

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

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

A. scanf
B. fgets
C. gets
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 == digit)
C. if (ch >= 0 && ch <= 9)
D. if (ch > '0')

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

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

23 What does the function strlen(str) return?

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

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

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

25 What does strcat(s1, s2) do?

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

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

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

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

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

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

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

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

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

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

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

31 Which function converts a character to uppercase?

A. upper()
B. to_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. strsub
B. strstr
C. strrchr
D. strchr

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

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

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

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

36 Which code correctly iterates through a string s?

A. for(i=0; s[i] != NULL; i++)
B. for(i=0; s[i] > 0; i++)
C. for(i=0; s[i] != '\0'; i++)
D. for(i=0; i < s; 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. The compiler prevents it.
D. Memory is automatically resized.

38 What does strncpy offer over strcpy?

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

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

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

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

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

41 What is the return type of sizeof operator?

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

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

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

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

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

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

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

45 Which loop correctly converts a string s to lowercase?

A. s = tolower(s);
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. for(int i=0; s[i]; i++) s[i] = tolower(s[i]);

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

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

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. C compilers are smarter.
D. malloc is not available in C++.

48 Which format specifier reads a single character?

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

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

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

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

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