Computer Knowledge
Programming, networking, database and OS questions
65 Questions 5 Topics Take Test
Advertisement
Showing 51–60 of 65 questions
Q.51 Hard C Programming
Which of the following is the correct way to pass a string to a function in C?
A void func(string s);
B void func(char s[]);
C void func(char *s);
D Both B and C
Correct Answer:  D. Both B and C
EXPLANATION

Both char s[] (array notation) and char *s (pointer notation) are valid ways to pass strings to functions in C. C does not have a built-in string type; strings are represented as character arrays or pointers to char.

Take Test
Q.52 Hard C Programming
What happens when you try to access an array element beyond its size in C?
A Compilation error
B Runtime error with exception
C Undefined behavior
D Returns 0 automatically
Correct Answer:  C. Undefined behavior
EXPLANATION

C does not perform bounds checking on arrays. Accessing beyond array bounds results in undefined behavior - it may access garbage values, crash, or seem to work without error. This is a common source of bugs.

Take Test
Q.53 Hard C Programming
Which of the following statements about static variables is TRUE?
A Static variables are re-initialized every function call
B Static variables retain their value between function calls
C Static variables cannot be used in functions
D Static variables are automatically deallocated
Correct Answer:  B. Static variables retain their value between function calls
EXPLANATION

Static variables are initialized only once and retain their value throughout the program execution. Their value persists between function calls.

Take Test
Q.54 Hard C Programming
What will be the result of executing: int a = 5, b = 10; int *ptr = &a; ptr = &b; printf("%d", *ptr);?
A 5
B 10
C Address of b
D Compilation error
Correct Answer:  B. 10
EXPLANATION

ptr is reassigned to point to b. When we dereference ptr using *ptr, we get the value stored at b, which is 10.

Take Test
Q.55 Hard C Programming
What is the time complexity of searching for an element in an unsorted array using linear search?
A O(1)
B O(log n)
C O(n)
D O(n²)
Correct Answer:  C. O(n)
EXPLANATION

Linear search checks each element sequentially. In the worst case, it needs to check all n elements, resulting in O(n) time complexity.

Take Test
Q.56 Hard C Programming
Consider a pointer ptr pointing to an integer array. What does ptr[2] represent?
A The address of the third element
B The value of the third element
C The pointer itself
D The size of the array
Correct Answer:  B. The value of the third element
EXPLANATION

ptr[2] is equivalent to *(ptr+2), which dereferences the pointer and returns the value at the third element (index 2).

Take Test
Q.57 Hard C Programming
What will be the output of: int x = 5; printf("%d", ++x);?
A 5
B 6
C 7
D Undefined
Correct Answer:  B. 6
EXPLANATION

The pre-increment operator (++x) increments x from 5 to 6 before using its value in printf(). So the output is 6.

Take Test
Q.58 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.59 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.60 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
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