Home Subjects Computer Knowledge C Programming

Computer Knowledge
C Programming

Programming, networking, database and OS questions

200 Q 2 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 181–190 of 200
Topics in Computer Knowledge
All C Programming 200 Database/SQL 100
Q.181 Easy C Programming
Which header file is required to use the printf() function in C?
A #include
B #include
C #include
D #include
Correct Answer:  B. #include
EXPLANATION

The stdio.h header file contains declarations for standard input/output functions like printf() and scanf().

Take Test
Q.182 Hard C Programming
What is the difference between struct and union in C?
A struct members share memory, union members have separate memory
B union members share memory, struct members have separate memory
C No difference in memory allocation
D struct is faster than union
Correct Answer:  B. union members share memory, struct members have separate memory
EXPLANATION

In a struct, each member has its own memory allocation, so the total size is the sum of all members. In a union, all members share the same memory location, so the size equals the largest member. Only one member can hold a value at a time in a union.

Take Test
Q.183 Hard C Programming
What is the output of the following C code?
#define MAX 5
int main() {
int arr[MAX];
printf("%d", sizeof(arr)/sizeof(arr[0]));
return 0;
}
A Compilation error
B 5
C 20
D 4
Correct Answer:  B. 5
EXPLANATION
Step 1: sizeof(arr) = 5 × sizeof(int) = 20 bytes (assuming 4-byte int). Step 2: sizeof(arr[0]) = sizeof(int) = 4 bytes. Step 3: 20 ÷ 4 = 5. This calculates the number of elements in the array.
Take Test
Q.184 Hard C Programming
Consider the following C code. What will be printed?
int arr[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
int *ptr = (int *)arr;
printf("%d", *(ptr + 5));
A 5
B 6
C 8
D 9
Correct Answer:  B. 6
EXPLANATION

A 2D array is stored in row-major order in memory: 1,2,3,4,5,6,7,8,9. When ptr is cast to int*, ptr+5 points to the 6th element (0-indexed), which is 6.

Take Test
Q.185 Hard C Programming
What is the correct way to declare a constant pointer to a constant integer?
A const int * const ptr;
B const int const *ptr;
C int const * const ptr;
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

Both declarations are equivalent. 'const int * const ptr' and 'int const * const ptr' declare a constant pointer to a constant integer. The first const makes the integer constant, the second const makes the pointer constant.

Take Test
Q.186 Hard C Programming
What is the output of the following C code?
#include
int main() {
int a = 5;
printf("%d %d %d", a++, ++a, a);
return 0;
}
A 5 7 7
B 6 7 7
C 5 6 6
D Undefined behavior
Correct Answer:  D. Undefined behavior
EXPLANATION

This code contains undefined behavior because variable 'a' is modified multiple times (a++, ++a) without intervening sequence points in the same expression. The order of evaluation is unspecified, making the result compiler-dependent.

Take Test
Q.187 Medium C Programming
What will be the output of the following C code?
int x = 10;
int y = 20;
int z = x < y ? x++ : y++;
printf("%d %d %d", x, y, z);
A 10 20 10
B 11 20 10
C 10 21 20
D 11 21 10
Correct Answer:  B. 11 20 10
EXPLANATION
Step 1: Evaluate condition x < y → 10 < 20 → true. Step 2: Execute true branch: x++ returns 10, then x becomes 11. Step 3: z = 10. Step 4: printf prints x=11, y=20, z=10.
Take Test
Q.188 Medium C Programming
Which of the following correctly describes the scope of a static variable declared inside a function?
A Local to the function, retains value between function calls
B Global scope, initialized once
C Local to the file only
D Creates a new instance on each function call
Correct Answer:  A. Local to the function, retains value between function calls
EXPLANATION

A static variable declared inside a function has local scope (visible only within that function) but persists for the entire program lifetime. Its value is retained between function calls and is initialized only once.

Take Test
Q.189 Medium C Programming
What is the output of the following C code?
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d", *(ptr + 1));
A 10
B 20
C 30
D Address of second element
Correct Answer:  B. 20
EXPLANATION

ptr points to arr[0]. ptr + 1 points to arr[1]. *(ptr + 1) dereferences to get the value at arr[1] which is 20. Pointer arithmetic adds sizeof(int) to the address for each increment.

Take Test
Q.190 Medium C Programming
What happens when you use the strcpy() function without bounds checking?
A It prevents buffer overflow automatically
B It may cause buffer overflow if source string is longer than destination
C It returns an error code
D It truncates the source string automatically
Correct Answer:  B. It may cause buffer overflow if source string is longer than destination
EXPLANATION

strcpy() does not perform bounds checking. If the source string is longer than the destination buffer, it will write beyond the buffer boundary, causing a buffer overflow. This is a security vulnerability. Using strncpy() is safer.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips