Programming (C/C++)
Test your C programming skills with comprehensive questions on memory management, pointers, arrays, file I/O, operators, functions, and preprocessor directives - perfect for MCA entrance and campus placement preparation.
Questions
With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed?
- unalloc()
- dropmem()
- dealloc()
- release()
- free()
int z,x=5,y=-10,a=4,b=2;
z = x++ - --y * b / a;
What will be the value of z in the sample code given above?
- 5
- 6
- 10
- 11
- 12
void *ptr;
myStruct myArray[10];
ptr = myArray;
Which of the following is the correct way to increment the variable "ptr"?
- ptr = ptr + sizeof(myStruct);
- ++(int*)ptr;
- ptr = ptr + sizeof(myArray);
- increment(ptr);
- ptr = ptr + sizeof(ptr);
char* myFunc (char *ptr)
{
ptr += 3;
return (ptr);
}
int main()
{
char *x, *y;
x = HELLO;
y = myFunc (x);
printf (y = %s n, y);
return 0;
}
What will be the output when the sample code given above is executed?
- y = HELLO
- y = ELLO
- y = LLO
- y = LO
- y = O
What is the difference between declaration and definition of a variable?
- Both can occur multiple times, but a declaration must occur first.
- There is no difference between them.
- A definition occurs once, but a declaration may occur many times.
- A declaration occurs once, but a definition may occur many times.
- Both can occur multiple times, but a definition must occur first.
int testarray[3][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
What value does testarray[2][1][0] in the sample code above contain?
- 3
- 5
- 7
- 9
- 11
int a=10,b;
b=a++ + ++a;
printf(%d,%d,%d,%d,b,a++,a,++a);
What will be the output when following code is executed?
- 12,10,11,13
- 22,10,11,13
- 22,11,11,11
- 12,11,11,11
- 22,13,13,13
11 ^ 5
What is the output of the above operation?
- 1
- 6
- 8
- 14
- 15
Which function will read a specified number of elements from a file?
- fileread()
- getline()
- readfile()
- fread()
- gets()
"My salary was increased by 15%!"
Select the statement which will EXACTLY reproduce the line of text above.
- printf(""My salary was increased by 15/%!"n");
- printf("My salary was increased by 15%!n");
- printf("My salary was increased by 15'%'!n");
- printf(""My salary was increased by 15%%!"n");
- printf(""My salary was increased by 15'%'!"n");
struct node *nPtr, sPtr; / pointers for a linked list.
*/
for (nPtr=sPtr; nPtr; nPtr=nPtr->next)
{
free(nPtr);
}
The sample code above releases memory from a linked list. Which of the choices below accurately describes how it will work?
- It will work correctly since the for loop covers the entire list.
- It may fail since each node nPtr is freed before its next address can be accessed.
- In the for loop, the assignment nPtr=nPtr->next should be changed to nPtr=nPtr.next.
- This is invalid syntax for freeing memory.
- The loop will never end.
void myFunc (int x)
{
if (x > 0)
myFunc(--x);
printf(%d, , x);
}
int main()
{
myFunc(5);
return 0;
}
What will the output of the above code after execution?
- 1, 2, 3, 4, 5, 5
- 4, 3, 2, 1, 0, 0
- 5, 4, 3, 2, 1, 0
- 0, 0, 1, 2, 3, 4
- 0, 1, 2, 3, 4, 5
int x[] = { 1, 4, 8, 5, 1, 4 };
int *ptr, y;
ptr = x + 4;
y = ptr - x;
What is the value of y in the sample code given above?
- - 3
- 0
- 4
- 4 + sizeof( int )
- 4 * sizeof( int)
#define MAX_NUM 15
Referring to the sample above, what is MAX_NUM?
- MAX_NUM is an integer variable.
- MAX_NUM is a linker constant.
- MAX_NUM is a precompiler constant.
- MAX_NUM is a preprocessor macro.
- MAX_NUM is an integer constant.
What is a proper method of opening a file for writing as a binary file?
- FILE *f = fwrite( test.bin, b );
- FILE *f = fopen ( test.bin, w );
- FILE *f = fopen( test.bin, wb );
- FILE *f = fwriteb( test.bin );
- FILE *f = fopen( test.bin, bw );
Which one of the following will turn off buffering for stdout?
- setbuf(stdout, FALSE);
- setvbuf(stdout, NULL);
- setbuf(stdout, NULL);
- setvbuf(stdout, _IONBF);
- setbuf(stdout, _IONBF);
Which of following functions is the correct choice for moving blocks of binary data which are having arbitrary size and position in memory?
- memcpy()
- memset()
- strncpy()
- strcpy()
- memmove()
int x = 2 * 3 + 4 * 5;
What will be the value of x in the sample code?
- 22
- 26
- 46
- 50
- 70
time_t t;
Which of the following statements will properly initialize the variable t with the current time from the sample above?
- t = clock();
- time( &t )
- t = ctime();
- t = localtime();
- None of the above
int var1;
If a variable has been declared with file scope, as above, can it safely be accessed globally from another file?
- Yes; it can be referenced through the register specifier.
- No; it would need to have been initially declared as a static variable.
- No; it would need to have been initially declared using the global keyword.
- Yes; it can be referenced through the publish specifier.
- Yes; it can be referenced through the extern specifier.
Which kind of language is language C?
- Machine
- Procedural
- Assembly
- Object-oriented
- Strictly-typed
Which one of the following provides conceptual support for function calls?
- The system stack
- The data segment
- The processor's registers
- The text segment
- The heap
int x = 0;
for (x=1; x<4; x++);
printf(x=%dn, x);
What will be the output of the sample code given above?
- x = 0
- x = 1
- x = 3
- x = 4
- x = 5
void * array_dup (a, number, size)
const void * a;
size_t number;
size_t size;
{
void * clone;
size_t bytes;
assert(a != NULL);
bytes = number * size;
clone = alloca(bytes);
if (!clone)
return clone;
memcpy(clone, a, bytes);
return clone;
}
The function array_dup(), defined above, contains an error. Which of the following options correctly analyses it?
- If the arguments to memcpy() refer to overlapping regions, the destination buffer will be subject to memory corruption.
- Array_dup() declares its first parameter to be a pointer, when the actual argument will be an array.
- The memory obtained from alloca() is not valid in the context of the caller. Moreover, alloca() is non-standard.
- Size_t is not a standard C defined type, and may not be kn.
- The definition of array_dup() is unusual. Functions cannot be defined using this syntax.
int i,j;
int ctr = 0;
int myArray[2][3];
for (i=0; i<3; i++)
for (j=0; j<2; j++)
{
myArray[j][i] = ctr;
++ctr;
}
What is the value of myArray[1][2]; in the sample code?
- 1
- 2
- 3
- 4
- 5