C Programming - Online Quiz



Following quiz provides Multiple Choice Questions (MCQs) related to C Programming Framework. You will have to read all the given answers and click over the correct answer. If you are not sure about the answer then you can check the answer using Show Answer button. You can use Next Quiz button to check new set of questions in the quiz.

Questions and Answers
cprogramming_questions_answers.htm

Q 1 - What is your comment on the below C statement?

   signed int *p=(int*)malloc(sizeof(unsigned int));

A - Improper type casting

B - Would throw Runtime error

C - Memory will be allocated but cannot hold an int value in the memory

D - No issue with statement

Answer : D

Explanation

Option (d), as the size of int and unsigned is same, no problem in allocating memory.

Q 2 - What is the output of the following program?

#include<stdio.h>

main()
{ 
   int a[] = {1,2}, *p = a;
   
   printf("%d", p[1]); 
}

A - 1

B - 2

C - Compile error

D - Runtime error

Answer : B

Explanation

2, as p holds the base address then we can access array using p just like with a

Q 3 - Identify the C compiler of UNIX.

A - gcc

B - cc

C - Borland

D - vc++

Answer : B

Explanation

cc full form is C Compiler and is the compiler for UNIX. gcc is GNU C compiler for linux. Borland and vc++ (Microsoft visual c++) for windows.

Q 4 - What is the output of the following program?

#include<stdio.h>

void f(int a[])
{  
   int i;
   
   for(i=0; i<3; i++)
      a[i]++;
}
main()
{	
   int i,a[] = {10, 20, 30};
   
   f(a);
   for(i=0; i<3; ++i)
   {
      printf("%d ",a[i]);
   }
}

A - 10 20 30

B - 11 21 31

C - Compile error

D - Runtime error

Answer : B

Explanation

Arrays are always passed by reference.

Q 5 - What is the output of the following statement?

#include<stdio.h>

main()
{
   printf("%d", -1<<1 );  
}

A - 2

B - -2

C - 1

D - -1

Answer : B

Explanation

A negative number stored in twos compliment of positive number. After shifting we get 1110, which is equivalent to -2.

Q 6 - How to round-off a value 5.77 to 6.0?

A - ceil(5.77)

B - round-off(5.77)

C - round-up(5.77)

D - floor(5.77)

Answer : A

Explanation

ceil( ) function in C returns nearest integer value which is greater than or equal tothe argument passed to thefunction.

#include <math.h>
#include <stdio.h>

 int main()
{
   float x=5.77;
   printf("ceil of  %f is  %f\n", x, ceil(x));
   return 0;
}

Q 7 - Which header file supports the functions -malloc()andcalloc()?

A - stdlib.h

B - memory.h

C - math.h

D - stdio.h

Answer : A

Explanation

void *malloc(size_t size) : Allocates the requested memory and returns a pointer to it.

void *calloc(size_t nitems, size_t size): Allocates the requested memory and returns a pointer to it.

Answer : A

Explanation

In C programming, thefflush() functionwrites any unwritten data instream'sbuffer. If, streamis a null pointer, fflush() function will flush all streams with unwritten data in the buffer.

int fflush(FILE *stream);

Answer : A

Explanation

Unary operator acts on single expression.

Q 10 - According to ANSI specification, how to declare main () function with command-line arguments?

A - int main(int argc, char *argv[])

B - int char main(int argc, *argv)

C -

int main()
{
   Int char (*argv argc);
)

D - None of the above

Answer : A

Explanation

Some time, it becomes necessary to deliver command line values to the C programming to execute the particular code when the code of the program is controlled from outside. Those command line values are called command line arguments. The command line arguments are handled by the main() function.

Declaration of main () with command-line argument is,

int main(int argc, char *argv[])

Where, argc refers to the number of arguments passed, and argv[]is a pointer array which points to each argument passed to the program.

Advertisements