C Coding Questions | SET-1 for Practice and Preparation

C Coding Questions | SET-1 for Practice and Preparation

Understanding Pointer Operations and Structures in C Programming

Pointers are a fundamental concept in C programming. They offer a way to directly manipulate memory addresses and manage dynamic memory allocation. Whether you’re preparing for a coding interview or working on a C project, understanding pointers, structures, and memory functions is essential.

Q1. Can a pointer variable be passed to a function as an argument?

Answer:
Yes! A pointer variable can indeed be passed to a function as an argument. This allows functions to manipulate the original data directly, rather than working on a copy.

Options:

  1. Changed within function
  2. Assigned an integer value
  3. None of these
  4. Passed to a function as argument (Correct)

Q2. Which of the following uses a structure?

Answer:
Structures are incredibly useful in C programming to store collections of different data types. A linked list, array of structures, and binary tree all make use of structures for organizing and managing data efficiently.

Options:

  1. Linked Lists
  2. Array of structures
  3. All of these (Correct)
  4. Binary Tree

Q3. What is the null-terminated character in a string array?

Answer:
In C, strings are arrays of characters. The last index of a string is always the null character, denoted by \0, which marks the end of the string.

Options:

  1. t
  2. 1
  3. \0 (Correct)
  4. n

Q4. Which of the following is a collection of different data types?

Answer:
A structure in C allows grouping different data types under one name. For example, a structure can combine integers, floats, and arrays into a single entity.

Options:

  1. String
  2. Structure (Correct)
  3. Array
  4. Files

Q5. What function should be used to free the memory allocated by calloc()?

Answer:
To deallocate memory that was allocated using calloc() (or malloc()), you should use the free() function in C.

Options:

  1. free(); (Correct)
  2. malloc(variable_name, 0)
  3. dealloc();
  4. memalloc(variable_name, 0)

Q6. Which header file is used for basic mathematical operations in C?

Answer:
To perform mathematical operations such as sin, cos, and square root, you need to include the math.h library in your program.

Options:

  1. conio.h
  2. stdio.h
  3. math.h (Correct)
  4. dos.h

Q7. What does the declaration int **ptr; mean?

Answer:
The declaration int **ptr; indicates that ptr is a pointer to a pointer to an integer, which is a more complex form of pointer usage in C.

Options:

  1. Pointer to integer
  2. None of these
  3. Pointer to pointer (Correct)
  4. Invalid declaration

Q8. Which of the following special symbols is allowed in a variable name?

Answer:
In C, you can use an underscore (_) in a variable name, but hyphens (-), pipes (|), and asterisks (*) are not valid.

Options:

  1. (underscore) (Correct)
    • (hyphen)
  2. | (pipeline)
    • (asterisk)

Q9. What case are C keywords written in?

Answer:
In C programming, all keywords are written in lowercase letters.

Options:

  1. Uppercase letters
  2. None of these
  3. Lowercase letters (Correct)
  4. Camel Case letters

Q10. What will the following program print?

cCopyEdit#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void myfunc(char** param) {
    ++param;
}

int main() {
    char* string = (char*)malloc(64);
    strcpy(string, "hello_World");
    myfunc(&string);
    myfunc(&string);
    printf("%s\n", string);
    return 0;
}

Answer:
The program will print “llo_World”. This is because the myfunc function increments the pointer, shifting the string content.

Options:

  1. hello_World
  2. ello_World
  3. llo_World (Correct)
  4. llo_World

Conclusion

Mastering pointers, structures, and memory management functions in C is essential for writing efficient programs. These concepts form the backbone of many data structures and algorithms used in real-world software. By practicing and understanding these questions, you’ll be well on your way to mastering C programming.

C Coding Questions
c