Showing 21–30 of 200 questions
in C Programming
What will be the value of x after executing: int x = 10; x += 5; x *= 2; ?
EXPLANATION
Step 1: x = 10. Step 2: x += 5 means x = x + 5 = 15. Step 3: x *= 2 means x = x * 2 = 15 * 2 = 30.
Which of the following correctly defines a structure in C?
A
struct { int a; float b; } MyStruct;
B
structure MyStruct { int a; float b; };
C
struct MyStruct { int a; float b; };
D
class MyStruct { int a; float b; };
Correct Answer:
C. struct MyStruct { int a; float b; };
EXPLANATION
The correct syntax uses the 'struct' keyword followed by the structure name and then the member declarations enclosed in braces.
What is the purpose of the free() function in C?
A
To allocate memory dynamically
B
To deallocate or release dynamically allocated memory
C
To initialize variables
D
To compare two memory blocks
Correct Answer:
B. To deallocate or release dynamically allocated memory
EXPLANATION
The free() function deallocates (releases) memory that was previously allocated using malloc(), calloc(), or realloc().
What is the output of: int x = 5; int y = ++x; ?
A
x = 5, y = 5
B
x = 6, y = 6
C
x = 6, y = 5
D
x = 5, y = 6
Correct Answer:
B. x = 6, y = 6
EXPLANATION
The pre-increment operator ++x increments x first (from 5 to 6), then assigns the new value to y. So x = 6 and y = 6.
What is the correct way to declare a pointer to an integer?
A
int p*;
B
int *p;
C
*int p;
D
pointer int p;
Correct Answer:
B. int *p;
EXPLANATION
The correct syntax is 'int *p;' where int is the data type, * indicates pointer, and p is the pointer variable name.
What is the output of: int a = 10, b = 20; int *p = &a, *q = &b; printf("%d", *p + *q);?
EXPLANATION
*p dereferences to a (10) and *q dereferences to b (20). Adding them: 10 + 20 = 30. Pointer arithmetic is used correctly here.
In a struct, how is memory allocated for union members?
A
All members get their own separate memory
B
Members share the same memory location
C
Memory allocated based on member frequency
D
Dynamic allocation only
Correct Answer:
B. Members share the same memory location
EXPLANATION
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.
What will be the result of: 5 % 2 * 3 / 2?
EXPLANATION
Modulus, multiplication, and division have equal precedence (left-to-right): (5 % 2) = 1, then (1 * 3) = 3, then (3 / 2) = 1 (integer division). Answer is 1, not 2. Correction: 1 is correct.
What is printed by: char s[] = "hello"; printf("%zu", sizeof(s));?
EXPLANATION
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.
Consider: int *p, q; What is the type of q?
A
Pointer to int
B
Integer
C
Pointer to pointer
D
Character
Correct Answer:
B. Integer
EXPLANATION
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.