c programming banner

C Programming Multiple Choice Questions (MCQs) and Answers

Master C Programming with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of C Programming. Begin your placement preparation journey now!

Q121

Q121 How do you access a member of a structure using a structure pointer in C?

A

With the dot operator (.)

B

With the arrow operator (->)

C

With the ampersand (&)

D

With the asterisk (*)

Q122

Q122 What is the key difference between a structure and a union in C?

A

A structure can store multiple data types, a union cannot

B

A union can store multiple data types, a structure cannot

C

In a structure, only one member can be accessed at a time

D

In a union, only one member can be accessed at a time

Q123

Q123 What is an enumerated data type in C?

A

A data type that allows numeric values only

B

A data type for storing lists of named constants

C

A special kind of array

D

A structure that can hold any data type

Q124

Q124 How can you instantiate a structure in C?

A

By using the new keyword

B

By declaring a variable of the structure type

C

By defining the structure with #define

D

By calling a function that returns a structure

Q125

Q125 How are bit fields used in a structure in C?

A

To define the number of bits used by an integer type member

B

To encrypt data

C

To align data in memory

D

To define array sizes

Q126

Q126 What happens when a structure is passed to a function in C?

A

The entire structure is copied

B

Only the structure pointer is passed

C

The first element is passed

D

None of the above

Q127

Q127 What does the following C code do?
struct Point {
int x, y;
};
struct Point p1 = {1, 2};
printf("%d %d", p1.x, p1.y);

A

Declares a structure and prints its members

B

Causes a syntax error

C

Initializes a structure but fails to print

D

None of the above

Q128

Q128 Complete the C code:
union Data {
int i;
float f;
char str[20];
};
union Data data; ___;
printf("%s", data.str);
to store and print "Hello".

A

data.str = "Hello";

B

strcpy(data.str, "Hello");

C

data = {.str = "Hello"};

D

data.str = {'H', 'e', 'l', 'l', 'o', '\0'};

Q129

Q129 Identify the error in this C code:
struct Point { int x, y; };
struct Point p1; p1.x = 10; p1.y = 20;

A

No error

B

Struct Point not properly declared

C

Struct members not properly accessed

D

Struct Point not properly instantiated

Q130

Q130 Spot the mistake:
struct Data { int val; }; int main() {
struct Data *ptr;
ptr->val = 100;
printf("%d", ptr->val);
return 0;
}

A

Uninitialized pointer used

B

Syntax error

C

No error

D

Wrong printf format

Q131

Q131 What is a stream in the context of C programming?

A

A sequence of characters

B

A type of data structure

C

A flow of binary data

D

A method of structuring code

Q132

Q132 What function in C is typically used to open a file for reading or writing?

A

fopen()

B

open()

C

file_open()

D

stream()

Q133

Q133 How does file handling in binary mode differ from text mode in C?

A

Binary mode can read non-text files, text mode cannot

B

Binary mode is faster

C

Text mode translates newlines, binary mode does not

D

Text mode is more secure

Q134

Q134 What is the purpose of the feof function in C?

A

To check if the end-of-file has been reached

B

To open a file

C

To read a file character by character

D

To write to a file

Q135

Q135 What happens if you try to write to a file in C without opening it first?

A

The write operation succeeds with a warning

B

The write operation fails silently

C

An error occurs

D

The file is automatically created

Q136

Q136 Complete the C code to read a binary file:
FILE *file = fopen("example.bin", "rb"); ___; fclose(file);

A

fread(buffer, sizeof(buffer), 1, file);

B

read(file, buffer, sizeof(buffer));

C

file >> buffer;

D

getline(file, buffer);

Q137

Q137 Identify the error in this C code for file writing:
FILE *file = fopen("example.txt", "w"); fprintf(file, "Hello World"); fclose(file);

A

Missing file mode in fopen

B

Incorrect usage of fprintf

C

No error

D

File not closed properly

Q138

Q138 Spot the mistake:
FILE *file = fopen("example.bin", "wb"); fwrite(&data, sizeof(data), 1, file); fclose(file);

A

Data should not be passed by reference in fwrite

B

fwrite arguments are in the wrong order

C

No error

D

File not opened in binary mode

Q139

Q139 What is the purpose of the #include directive in C?

A

To include a library during program execution

B

To declare a variable

C

To include a header file at compile time

D

To initialize variables

Q140

Q140 What is the function of the #define directive in C?

A

To define a new function

B

To import a library

C

To define a macro or constant

D

To declare a variable

Q141

Q141 How does the #ifndef directive function in C?

A

It checks if a variable is not defined

B

It imports a file if not previously imported

C

It checks if a macro is not defined

D

It defines a new macro if a condition is met

Q142

Q142 What is the result of using the #undef directive in C?

A

It deletes a file from the project

B

It removes a defined macro

C

It undefines a variable

D

It causes a compilation error

Q143

Q143 What happens when a header file is included twice in a C program without using include guards?

A

The program will not compile

B

The program compiles with warnings

C

The program compiles successfully, but it may cause errors

D

Nothing specific

Q144

Q144 Identify the error in this C code snippet:
#include <stdio.h> #define MAX 100 #undef MAX int main() { printf("%d", MAX); return 0; }

A

Syntax error

B

Use of undefined MAX

C

No error

D

Incorrect usage of printf

Q145

Q145 Spot the mistake:
#ifndef FILE_H #define FILE_H /* File contents */ #endif in a header file named "file.h"

A

The macro name should match the file name

B

No error

C

The header file name should be uppercase

D

The #ifndef and #define order is incorrect

Q146

Q146 How are command line arguments passed to a C program?

A

Through global variables

B

Through function parameters

C

Through a dedicated command-line function

D

Through environment variables

Q147

Q147 What is the purpose of argc in a C program?

A

To count the number of functions

B

To store environment variables

C

To count the number of command line arguments

D

To store error codes

Q148

Q148 What is variable argument list in C, and which header file is required for it?

A

A list of arguments of variable types, stdlib.h required

B

A list of arguments of fixed types, stdio.h required

C

A list of arguments of variable length, stdarg.h required

D

A list of arguments of variable length, string.h required

Q149

Q149 How can you access the individual command line arguments in a C program?

A

Using a for loop with argc and argv

B

Through global variables

C

By indexing into a predefined array

D

By calling a special function

Q150

Q150 What is the use of the va_start, va_arg, and va_end macros in C?

A

They are used for file operations

B

They are used for memory allocation

C

They are used for handling variable arguments

D

They are used for error handling

ad verticalad vertical
ad