In C, when passing arrays to functions, what is actually passed?
Answer: B
When an array is passed to a function in C, it decays to a pointer pointing to the first element. This is why changes made in the function affect the original array.
Q.22Hard
What will be the output of: int a = 5; int *p = &a; printf("%d %d", a, *p);?
Answer: B
p is a pointer storing the address of a. *p (dereferencing) gives the value at that address, which is 5. So both a and *p print 5.
Q.23Hard
What does the static keyword do when used with a global variable?
Answer: A
When static is used with a global variable, it restricts its scope to the file where it is declared. Without static, global variables are accessible across files using extern.
Q.24Hard
Consider: int arr[10]; int *p = arr; What is p[5] equivalent to?
Answer: C
Array name decays to pointer. p[5] is equivalent to *(p+5) which is *(arr+5) or arr[5]. This demonstrates pointer arithmetic.
Q.25Hard
What is the difference between declaration and definition in C?
Answer: B
Declaration tells the compiler about a variable's name and type (e.g., 'extern int x;'). Definition actually allocates memory (e.g., 'int x = 5;'). A variable can be declared multiple times but defined only once.
Advertisement
Q.26Hard
What happens if you declare a variable but don't initialize it in C?
Answer: B
Uninitialized local variables contain garbage values (unpredictable values from previous memory contents). Global and static variables are automatically initialized to 0, but local variables are not.
Q.27Hard
Consider: int *ptr = NULL; *ptr = 5; What will happen?
Answer: B
Attempting to dereference a NULL pointer causes undefined behavior, typically resulting in a segmentation fault (runtime error). A NULL pointer doesn't point to valid memory.
Q.28Hard
What does the 'volatile' keyword indicate in C?
Answer: A
The volatile keyword tells the compiler that a variable's value may change unexpectedly (e.g., due to hardware, interrupts, or other threads), preventing compiler optimizations that assume the value doesn't change.
Q.29Hard
Consider: int *ptr; int arr[] = {1,2,3}; ptr = arr; What is ptr[1]?
Answer: B
When ptr points to the array arr, ptr[1] is equivalent to *(ptr+1), which accesses the second element of the array, which is 2.
Q.30Hard
What is the difference between getchar() and scanf("%c", &ch); in C?
Answer: C
getchar() specifically reads a single character from the standard input stream, while scanf("%c", &ch) is a formatted input function. getchar() is more straightforward for reading single characters.
Q.31Hard
Which of the following correctly describes the relationship between arrays and pointers in C?
Answer: B
In C, when an array name is used in most contexts, it decays (converts) to a pointer to its first element. For example, in int arr[5];, arr behaves like &arr[0]. However, arrays and pointers are not identical; arrays have fixed size while pointers are variables.
Q.32Hard
What is the difference between a function declaration and a function definition in C?
Answer: A
A function declaration (or prototype) tells the compiler about the function's name, return type, and parameters. A function definition includes the declaration along with the function body containing the actual implementation. Declaration is optional if the function is defined before use.
Q.33Hard
Which of the following is the correct way to declare a pointer to a function that returns an int and takes two int parameters?
Answer: B
The correct syntax for a function pointer is: int (*ptr)(int, int);. The parentheses around (*ptr) are necessary to indicate that ptr is a pointer to a function, not a function returning a pointer. Option A declares a function returning an int pointer.
Q.34Hard
Consider: int *p, q; What is the type of q?
Answer: B
Only p is declared as a pointer (int *p). The variable q is declared as a regular integer (int q). The * applies only to p in this declaration.
Q.35Hard
What is printed by: char s[] = "hello"; printf("%zu", sizeof(s));?
Answer: B
sizeof(s) includes the null terminator '\0'. "hello" has 5 characters + 1 null terminator = 6 bytes. If s was a pointer, sizeof would give pointer size.
Q.36Hard
What will be the result of: 5 % 2 * 23?
Answer: B
Modulus, multiplication, and division have equal precedence (left-to-right): (5 % 2) = 1, then (1 * 3) = 3, then (23) = 1 (integer division). Answer is 1, not 2. Correction: 1 is correct.
Q.37Hard
In a struct, how is memory allocated for union members?
Answer: B
In a union, all members share the same memory location. The size of the union equals the size of the largest member. Only one member can hold a value at a time.
Q.38Hard
What is the difference between #include <stdio.h> and #include "stdio.h"?
Answer: D
Using <> tells the compiler to search in standard system directories. Using "" tells it to search in the current directory first, then system directories. Generally, "" is used for user-defined headers and <> for standard library headers.
Q.39Hard
Consider the code: int *p; int arr[5]; p = arr; What does p[2] represent?
Answer: B
When p points to arr, p[2] is equivalent to *(p+2) and accesses the value at arr[2]. Pointers and array names decay to pointers, and pointer indexing retrieves the value.
Q.40Hard
Which of the following will correctly allocate memory for an array of 10 integers?
Answer: B
malloc() allocates memory in bytes. To allocate for 10 integers, we need 10 * sizeof(int) bytes. Option A allocates only 10 bytes, insufficient for 10 integers.