0% found this document useful (0 votes)
1 views9 pages

C MCQ

The document contains a series of programming questions and answers related to C language concepts, including loops, functions, data types, enums, typedefs, and unions. Each question is followed by multiple-choice answers, with the correct answer indicated. The content serves as a quiz or study guide for understanding fundamental C programming principles.

Uploaded by

Vandana Vijayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views9 pages

C MCQ

The document contains a series of programming questions and answers related to C language concepts, including loops, functions, data types, enums, typedefs, and unions. Each question is followed by multiple-choice answers, with the correct answer indicated. The content serves as a quiz or study guide for understanding fundamental C programming principles.

Uploaded by

Vandana Vijayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Simple for Loop


for (int i = 0; i < 4; i++) {
printf("%d ", i);
}

a) 0 1 2 3
b) 1 2 3 4
c) 0 1 2 3 4
d) Compile-time error
Answer: a) 0 1 2 3

2. Infinite while Loop


int i = 0;
while (i < 5) {
printf("%d ", i);
// Missing increment
}

a) 0 1 2 3 4
b) Infinite loop printing 0
c) Compile-time error
d) Undefined behavior
Answer: b) Infinite loop printing 0

3. Nested Loops
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
printf("%d ", i * j);
}
}

a) 1 2 3 2 4 6
b) 1 2 2 3 3 4
c) 1 2 3 1 2 3
d) Compile-time error
Answer: a) 1 2 3 2 4 6

4. do-while Loop
int i = 5;
do {
printf("%d ", i);
i--;
} while (i > 0);
a) 5 4 3 2 1
b) 4 3 2 1
c) 5 4 3 2 1 0
d) Compile-time error
Answer: a) 5 4 3 2 1

5. int arr[5] = {1, 2};

printf("%d", arr[3]);

a) 1
b) 2
c) 0
d) Garbage value
Answer: c) 0

1. Simple Function Call


int multiply(int a, int b) {
return a * b;
}
int main() {
printf("%d", multiply(3, 4));
}

a) 7
b) 12
c) 0
d) Compile-time error
Answer: b) 12

2. Function Without Return Statement


void printHello() {
printf("Hello");
}
int main() {
printHello();
}

a) Hello
b) Compile-time error
c) Nothing
d) Undefined behavior
Answer: a) Hello
3. Passing Arrays to Functions
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int arr[] = {1, 2, 3};
printArray(arr, 3);
}

a) 1 2
b) 1 2 3
c) Compile-time error
d) Garbage value
Answer: b) 1 2 3

4. Function Prototype Mismatch


int add(int, int);
int main() {
printf("%d", add(5, 6));
}
int add(int a, float b) {
return a + b;
}

a) 11
b) Compile-time error
c) Undefined behavior
d) None of the above
Answer: b) Compile-time error

5. Recursion
int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main() {
printf("%d", factorial(4));
}

a) 12
b) 24
c) 16
d) Compile-time error
Answer: b) 24

1. Function Declaration

Which of the following is a correct declaration of a function in C?


a) int add(a, b);
b) int add(int a, int b);
c) void add(int a, b);
d) add(int a, int b);
Answer: b) int add(int a, int b);

2. Purpose of a Function Prototype

What is the main purpose of a function prototype in C?


a) To define the function's body
b) To specify the function's name and parameters for the compiler
c) To execute the function
d) To allocate memory for the function
Answer: b) To specify the function's name and parameters for the compiler

3. Passing Arguments

What is the default method of passing arguments to a function in C?


a) Call by reference
b) Call by value
c) Call by pointer
d) None of the above
Answer: b) Call by value

4. Return Type of a Function

If a function has no return type, which keyword is used to define it?


a) int
b) void
c) null
d) none
Answer: b) void
5. Recursive Functions

Which of the following is true for recursive functions in C?


a) They can call themselves directly or indirectly.
b) They must have a void return type.
c) They do not require a base condition.
d) They cannot use arguments.
Answer: a) They can call themselves directly or indirectly.

1. Data Types

Which of the following is not a valid data type in C?


a) int
b) float
c) double
d) string
Answer: d) string

2. Storage Classes

Which storage class is used for global variables in C?


a) auto
b) register
c) static
d) extern
Answer: d) extern

3. Preprocessor Directives

What does the #include directive do in a C program?


a) Defines a function
b) Includes header files in the program
c) Allocates memory dynamically
d) Provides debugging information
Answer: b) Includes header files in the program

4. Loop Control Statements

Which statement is used to exit a loop prematurely in C?


a) continue
b) break
c) return
d) exit
Answer: b) break

5. Pointers

What does a pointer in C store?


a) The value of a variable
b) The data type of a variable
c) The address of a variable
d) None of the above
Answer: c) The address of a variable

1. Declaration of enum

Which of the following is a valid declaration of an enum in C?


a) enum days {Monday, Tuesday, Wednesday};
b) enum days (Monday, Tuesday, Wednesday);
c) enum days = {Monday, Tuesday, Wednesday};
d) enumerate days {Monday, Tuesday, Wednesday};
Answer: a) enum days {Monday, Tuesday, Wednesday};

2. Default Values in enum

What is the default value of the first constant in an enum declaration?

enum colors {RED, BLUE, GREEN};

a) 0
b) 1
c) Undefined
d) Depends on the compiler
Answer: a) 0

3. Purpose of typedef

What is the primary use of typedef in C?


a) To define constants
b) To create aliases for existing data types
c) To dynamically allocate memory
d) To define functions
Answer: b) To create aliases for existing data types

4. Example of typedef

What does the following typedef statement achieve?

typedef unsigned int uint;

a) Creates a new data type


b) Creates an alias for unsigned int as uint
c) Changes the behavior of unsigned int
d) None of the above
Answer: b) Creates an alias for unsigned int as uint

5. Valid Enum Usage

What will be the value of y after the following code executes?

enum status {ON = 1, OFF = 0};


enum status x = OFF;
int y = x + 2;

a) 0
b) 2
c) 3
d) Undefined
Answer: c) 3

1. Definition of Union

What is a union in C?
a) A data type that stores multiple variables of different types simultaneously
b) A data type that stores multiple variables of different types but uses the same memory
location
c) A data type similar to an array
d) A data type that allows dynamic memory allocation
Answer: b) A data type that stores multiple variables of different types but uses the same
memory location

2. Size of a Union
The size of a union in C is:
a) The sum of the sizes of its members
b) The size of its largest member
c) Always 4 bytes
d) None of the above
Answer: b) The size of its largest member

3. Memory Sharing in Union

In a union, if one member is updated, what happens to the other members?


a) They remain unchanged
b) They are also updated
c) They may contain garbage values
d) Compilation error occurs
Answer: c) They may contain garbage values

4. Valid Declaration of Union

Which of the following is a valid union declaration?


a) union data {int i; float f;};
b) union data = {int i; float f;};
c) union data (int i, float f);
d) data union {int i; float f;};
Answer: a) union data {int i; float f;};

5. Accessing Union Members

Consider the following code:

union data {
int i;
float f;
};
union data d;
d.i = 10;
printf("%f", d.f);

What will this program print?


a) 10.0
b) 0.0
c) Undefined behavior
d) Compile-time error
Answer: c) Undefined behavior

You might also like