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. <mem.h>
B. <string.h>
C. <stdio.h>
D. <stdlib.h>

2 What is the return type of the malloc function?

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

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

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

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

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

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

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

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

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

7 What is the primary purpose of the realloc function?

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

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

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

9 What constitutes a Memory Leak?

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

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. Stack Overflow
C. Memory Leak
D. Dangling Pointer

11 What are the arguments required for the calloc function?

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

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

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

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. Type Mismatch
C. Buffer Overflow
D. Memory Leak

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

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

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. '.'
B. '\n'
C. '\0'
D. '0'

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

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

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

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

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

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

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

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

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

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

23 What does the function strlen(str) return?

A. The number of characters in str excluding \0.
B. The size of the array str.
C. The number of characters in str including \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. Finds s2 inside s1.
B. Concatenates s2 to the end of s1.
C. Compares s1 and s2.
D. Copies s2 into s1.

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

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

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

A. 0
B. A positive value
C. NULL
D. A negative 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. The pointer moves to the next character.
B. Undefined behavior (often Segmentation Fault).
C. Compiler error.
D. The string becomes "hello".

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 replace ch in str.
D. To remove ch from str.

31 Which function converts a character to uppercase?

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

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. Address addition.
B. "1020"
C. 30
D. Compilation Error (Invalid operand for binary +).

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

38 What does strncpy offer over strcpy?

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

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

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

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

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

41 What is the return type of sizeof operator?

A. size_t
B. double
C. int
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. 'd'
B. Address of 'o'
C. 'C'
D. 'o'

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

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

45 Which loop correctly converts a string s to lowercase?

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

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

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

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

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

48 Which format specifier reads a single character?

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

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

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

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

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