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.

25 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed?

  1. unalloc()
  2. dropmem()
  3. dealloc()
  4. release()
  5. free()
Question 2 Multiple Choice (Single Answer)

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?

  1. 5
  2. 6
  3. 10
  4. 11
  5. 12
Question 3 Multiple Choice (Single Answer)

void *ptr;
myStruct myArray[10];
ptr = myArray;
Which of the following is the correct way to increment the variable "ptr"?

  1. ptr = ptr + sizeof(myStruct);
  2. ++(int*)ptr;
  3. ptr = ptr + sizeof(myArray);
  4. increment(ptr);
  5. ptr = ptr + sizeof(ptr);
Question 4 Multiple Choice (Single Answer)

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?

  1. y = HELLO
  2. y = ELLO
  3. y = LLO
  4. y = LO
  5. y = O
Question 5 Multiple Choice (Single Answer)

What is the difference between declaration and definition of a variable?

  1. Both can occur multiple times, but a declaration must occur first.
  2. There is no difference between them.
  3. A definition occurs once, but a declaration may occur many times.
  4. A declaration occurs once, but a definition may occur many times.
  5. Both can occur multiple times, but a definition must occur first.
Question 6 Multiple Choice (Single Answer)

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?

  1. 3
  2. 5
  3. 7
  4. 9
  5. 11
Question 7 Multiple Choice (Single Answer)

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?

  1. 12,10,11,13
  2. 22,10,11,13
  3. 22,11,11,11
  4. 12,11,11,11
  5. 22,13,13,13
Question 8 Multiple Choice (Single Answer)

11 ^ 5
What is the output of the above operation?

  1. 1
  2. 6
  3. 8
  4. 14
  5. 15
Question 9 Multiple Choice (Single Answer)

Which function will read a specified number of elements from a file?

  1. fileread()
  2. getline()
  3. readfile()
  4. fread()
  5. gets()
Question 10 Multiple Choice (Single Answer)

"My salary was increased by 15%!"
Select the statement which will EXACTLY reproduce the line of text above.

  1. printf(""My salary was increased by 15/%!"n");
  2. printf("My salary was increased by 15%!n");
  3. printf("My salary was increased by 15'%'!n");
  4. printf(""My salary was increased by 15%%!"n");
  5. printf(""My salary was increased by 15'%'!"n");
Question 11 Multiple Choice (Single Answer)

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?

  1. It will work correctly since the for loop covers the entire list.
  2. It may fail since each node nPtr is freed before its next address can be accessed.
  3. In the for loop, the assignment nPtr=nPtr->next should be changed to nPtr=nPtr.next.
  4. This is invalid syntax for freeing memory.
  5. The loop will never end.
Question 12 Multiple Choice (Single Answer)

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. 1, 2, 3, 4, 5, 5
  2. 4, 3, 2, 1, 0, 0
  3. 5, 4, 3, 2, 1, 0
  4. 0, 0, 1, 2, 3, 4
  5. 0, 1, 2, 3, 4, 5
Question 13 Multiple Choice (Single Answer)

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?

  1. - 3
  2. 0
  3. 4
  4. 4 + sizeof( int )
  5. 4 * sizeof( int)
Question 14 Multiple Choice (Single Answer)

#define MAX_NUM 15
Referring to the sample above, what is MAX_NUM?

  1. MAX_NUM is an integer variable.
  2. MAX_NUM is a linker constant.
  3. MAX_NUM is a precompiler constant.
  4. MAX_NUM is a preprocessor macro.
  5. MAX_NUM is an integer constant.
Question 15 Multiple Choice (Single Answer)

What is a proper method of opening a file for writing as a binary file?

  1. FILE *f = fwrite( test.bin, b );
  2. FILE *f = fopen ( test.bin, w );
  3. FILE *f = fopen( test.bin, wb );
  4. FILE *f = fwriteb( test.bin );
  5. FILE *f = fopen( test.bin, bw );
Question 16 Multiple Choice (Single Answer)

Which one of the following will turn off buffering for stdout?

  1. setbuf(stdout, FALSE);
  2. setvbuf(stdout, NULL);
  3. setbuf(stdout, NULL);
  4. setvbuf(stdout, _IONBF);
  5. setbuf(stdout, _IONBF);
Question 17 Multiple Choice (Single Answer)

Which of following functions is the correct choice for moving blocks of binary data which are having arbitrary size and position in memory?

  1. memcpy()
  2. memset()
  3. strncpy()
  4. strcpy()
  5. memmove()
Question 18 Multiple Choice (Single Answer)

int x = 2 * 3 + 4 * 5;
What will be the value of x in the sample code?

  1. 22
  2. 26
  3. 46
  4. 50
  5. 70
Question 19 Multiple Choice (Single Answer)

time_t t;
Which of the following statements will properly initialize the variable t with the current time from the sample above?

  1. t = clock();
  2. time( &t )
  3. t = ctime();
  4. t = localtime();
  5. None of the above
Question 20 Multiple Choice (Single Answer)

int var1;
If a variable has been declared with file scope, as above, can it safely be accessed globally from another file?

  1. Yes; it can be referenced through the register specifier.
  2. No; it would need to have been initially declared as a static variable.
  3. No; it would need to have been initially declared using the global keyword.
  4. Yes; it can be referenced through the publish specifier.
  5. Yes; it can be referenced through the extern specifier.
Question 21 Multiple Choice (Single Answer)

Which kind of language is language C?

  1. Machine
  2. Procedural
  3. Assembly
  4. Object-oriented
  5. Strictly-typed
Question 22 Multiple Choice (Single Answer)

Which one of the following provides conceptual support for function calls?

  1. The system stack
  2. The data segment
  3. The processor's registers
  4. The text segment
  5. The heap
Question 23 Multiple Choice (Single Answer)

int x = 0;
for (x=1; x<4; x++);
printf(x=%dn, x);
What will be the output of the sample code given above?

  1. x = 0
  2. x = 1
  3. x = 3
  4. x = 4
  5. x = 5
Question 24 Multiple Choice (Single Answer)

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?

  1. If the arguments to memcpy() refer to overlapping regions, the destination buffer will be subject to memory corruption.
  2. Array_dup() declares its first parameter to be a pointer, when the actual argument will be an array.
  3. The memory obtained from alloca() is not valid in the context of the caller. Moreover, alloca() is non-standard.
  4. Size_t is not a standard C defined type, and may not be kn.
  5. The definition of array_dup() is unusual. Functions cannot be defined using this syntax.
Question 25 Multiple Choice (Single Answer)

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. 1
  2. 2
  3. 3
  4. 4
  5. 5