- int (*fptr)(float);
- int *(fptr(float));
- int (*fptr(float));
Explanation:
The accurate choice is b. The format for specifying a function pointer is returntype(*pointername)(parameter_list). The definition of fptr as a function pointer takes a float as a parameter and yields an integer.
- What will be the result of the given code snippet?
#include<stdio.h>
#include<stdlib.h>
int main(){
char *str;
str = (char *)malloc(15);
strcpy(str, "Hello, World!");
str = (char *)realloc(str, 25);
strcat(str, " C");
printf("%s\n", str);
free(str);
return 0;
}
- Hello, World! C
- Hello, World! C C
- Segmentation fault
- Hello, World!
Explanation:
The correct answer is a. The malloc function is assigned to 15 bytes of the string, and "Hello, World!" is copied over using strcpy. The size of the memory block is converted to 25 bytes by the realloc function, allowing strcat to append a "C" to the string. After that "Hello, World! C" is printed by the printf function.
- If file is not already present, which of the following creates a fopen function modes file and opens the file for reading and writing?
- "r+"
- "w+"
Explanation:
The option d is the accurate choice. By employing the w+ mode, a file can be opened for both reading from and writing to the file. In case the file does not currently exist, this mode will generate a new file. What will be the result of the code provided below?
#include<stdio.h>
typedef union {
int i;
float f;
char str[20];
} Data;
int main(){
Data data;
data.i = 10;
printf("data.i : %d\n", data.i);
data.f = 220.5;
printf("data.f : %f\n", data.f);
strcpy(data.str, "C Programming");
printf("data.str : %s\n", data.str);
return 0;
}
- data. i: 10 data. f: 220.500000 data. str: C Programming
- data. i: 10 data. f: 220.500000 data. str: C Programming data. i: 10
- data. i: 10 data. f: 0.000000 data. str: C Programming
- data.i : 0 data. f: 0.000000 data. str: C Programming
Explanation:
The correct answer is c. All members of the union are united in memory because they share a common place of memory. The information in the shared memory address will be modified when a value is assigned to the member, which affects the value of other members
- How to dynamically allocate memory for an array of 10 integers in C?
- int *arr = malloc(10);
- int arr = malloc(10 sizeof(int));
- int *arr = malloc(sizeof(10));
- int arr = malloc(10 int);
Explanation:
The accurate choice is b. In C programming, the malloc function is employed to dynamically assign memory. It necessitates the total number of bytes to be allocated as an argument.
- What is the outcome of the subsequent code snippet?
#include<stdio.h>
int factorial(int n){
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
Int main(){
printf("%d\n", factorial(5));
return 0;
}
Explanation:
The accurate response is option a. The factorial of 5 equals 120.
- What is the output of the following code snippet?
#include<stdio.h>
#define SQUARE(x)(x*x)
int main(){
int a = 3;
int b = SQUARE(a + 1);
printf("%d\n", b);
return 0;
}
Explanation:
The correct answer is a. The macro 'SQUARE(x)' is defined as '(x x)'. When 'SQUARE(a + 1)' is expanded, it becomes '(a + 1 a + 1)' because a is 3 here which is evaluated as ((3+1) * (3+1)). Therefore, the answer is 16.
- Which keyword from the following option will indicate to the compiler that it would be better to use code for the function instead of a function call to reduce the overhead of the function call?
- static
- register
- inline
- volatile
Explanation:
The correct choice is c. By using the inline keyword, the compiler is instructed to substitute a function call with the function's actual code, which helps in minimizing the overhead associated with function calls.
- What is the outcome of the code snippet provided below?
#include<stdio.h>
int main() {
int x = 4; // binary: 0100
int y = 9; // binary: 1001
printf("%d\n", x & y);
return 0;
}
Explanation:
The correct choice is option a. The & symbol is employed for performing the bitwise AND operation. The binary representation of 4 is 0100, while 9 is represented as 1001 in binary format.