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 the output of the below code snippet?

#include<stdio.h>

main() 
{
   for()printf("Hello");
}

A - Infinite loop

B - Prints “Hello” once.

C - No output

D - Compile error

Answer : D

Explanation

Compiler error, semi colons need to appear though the expressions are optional for the ‘for’ loop.

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

#include<stdio.h>

void f() 
{
    printf(“Hello\n”);
}
main() 
{
 ;
}

A - No output

B - Error, as the function is not called.

C - Error, as the function is defined without its declaration

D -Error, as the main() function is left empty

Answer : A

Explanation

No output, apart from the option (a) rest of the comments against the options are invalid.

Q 3 - Choose the invalid identifier from the below

A - Int

B - volatile

C - DOUBLE

D - __0__

Answer : B

Explanation

volatile is the reserved keyword and cannot be used an identifier name.

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

#include<stdio.h>

main() 
{
   char *p = NULL;
   
   printf("%c", *p);
}

A - NULL

B - 0

C - Compile error

D - Runtime error.

Answer : D

Explanation

It is invalid to access the NULL address hence giving run time error.

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

#include<stdio.h>

main()
{ 
   int x;
   float y;
   
   y = x = 7.5;
   printf("x=%d y=%f", x, y);
}

A - 7 7.000000

B - 7 7.500000

C - 5 7.500000

D - 5 5.000000

Answer : A

Explanation

‘x’ gets the integral value from 7.5 which is 7 and the same is initialized to ‘y’.

Answer : B

Explanation

  • External Linkage-> A global, non-static variables and functions.
  • Internal Linkage-> A static variables and functions with file scope.
  • None Linkage-> A Local variables.

Q 7 - For a structure, if a variable behave as a pointer then from the given below operators which operator can be used to access data of the structure via the variable pointer?

A - .

B - %

C - ->

D - #

Answer : C

Explanation

For a structure, Dot(.) operator can be used to access the data using normal structure variable and arrow (->)can be used to access the data using pointer variable.

Q 8 - Choose the correct statement that is a combination of these two statements,

   Statement 1: char *p;
   
   Statement 2: p = (char*) malloc(100);

A - char p = *malloc(100);

B - char *p = (char*)malloc(100);

C - char *p = (char) malloc(100);

D - None of the above

Answer : B

Explanation

ptr = (data type *)malloc(size);

The given above code is a prototype of malloc() function, where ptr indicates the pointer.

char *p = (char*)malloc(100);

In this code, “*p” is a pointer of type char and malloc() function allocates the memory for char.

Answer : B

Explanation

As per the operators preference.

Q 10 - What will be the output of the given below code?

#include<stdio.h>

int main()
{
   const int *ptr = &i;
   
   char str[] = "Welcome";
   s = str;
   while(*s)
   printf("%c", *s++);
   return 0;
}

A - Welcome

B - 0

C - Wel

D - Come

Answer : A

Explanation

Although, char str[] = "Welcome"; and s = str;, the program will print the value of s.

#include<stdio.h>

int main()
{
   const int *ptr = &i;
   
   char str[] = "Welcome";
   s = str;
   while(*s)
   printf("%c", *s++);
   return 0;
}

Advertisements