C Programming Online Quiz


Advertisements


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

Q 1 - What is the output of the following code snippet?

#include<stdio.h>

main()
{ 
   int *p = 15; 
   printf("%d",*p);
}

A - 15

B - Garbage value

C - Runtime error

D - Compiler error

Answer : C

Explanation

Runtime error, as the pointer variable is not holding proper address, writing/reading the data from the same raises runtime error.

Q 2 - What is the following program doing?

#include<stdio.h>

main()
{
   FILE *stream=fopen("a.txt",'r');
}

A - Trying to open “a.txt” in read mode

B - Trying to open “a.txt” in write mode.

C - “stream” is an invalid identifier

D - Compile error

Answer : D

Explanation

Compile error, second argument for fopen is invalid, should be a string.

Q 3 - Identify the invalid constant used in fseek() function as ‘whence’ reference.

A - SEEK_SET

B - SEEK_CUR

C - SEEK_BEG

D - SEEK_END

Answer : C

Explanation

SEEK_BEG, all the rest are valid constants defined in ‘stdio.h’

Q 4 - Does both the loops in the following programs prints the correct string length?

#include<stdio.h>

main()
{
   int i;
   char s[] = "hello";

   for(i=0; s[i]; ++i);
      printf("%d ", i);

   i=0; 
   while(s[i++]);
      printf("%d ", i);
}

A - Yes, both the loops prints the correct length

B - Only for loop prints the correct length

C - Only while loop prints the correct length

D - Compile error in the program.

Answer : B

Explanation

In while loop ‘i’ gets incremented after checking for ‘\0’, hence giving 1 more than the length.

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

#include<stdio.h>

main()
{
   char *s = "Hello";
   
   while(*s!=NULL)
   printf("%c", *s++);
}

A - Hello

B - Helloellolloloo

C - ello

D - Compile error

Answer : A

Explanation

NULL is equivalent to ‘\0’ in value. Statement *s++ prints the character first and increments the address later.

Answer : B

Explanation

The double constant 3.14 can be converted into long double by adding “L” after the constant value, i.e; 3.14L.

Q 7 - In Decimal system you can convert the binary number 1011011111000101 very easily.

A - Yes

B - Hexadecimal system

C - Octal system

D - Both, Octal & Decimal

Answer : B

Explanation

Hexadecimal is also known as hex or base 16. It is a system help in writing and presenting numerical values. Binary (base 2) is a popular numeral system (represent numbers by just two digit values “0 and 1”), used to present the language of computers. Hexadecimal system can easily convert those numbers.

Answer : A

Explanation

In C library, NULL Macro is the value of a null pointer constant. It may be defined as ((void*)0), 0 or 0L depending on the compiler merchant.

Q 9 - What will be the output of the following program?

#include<stdio.h>

int main()
{
   const int x = 5;
   
   const int *ptrx;
   ptrx = &x;
   *ptrx = 10;
   printf("%d\n", x);
   return 0;
}

A - 10

B - 20

C - 0

D - The program will return error

Answer : D

Explanation

The above program will return error

#include<stdio.h>

int main()
{
   const int x = 5;
   
   const int *ptrx;
   ptrx = &x;
   *ptrx = 10;
   printf("%d\n", x);
   return 0;
}

Q 10 - In the given below code, what will be the value of a variable x?

#include<stdio.h>

int main()
{
    int y = 100;
    const int x = y;
    
    printf("%d\n", x);
    return 0;
}

A - 100

B - 0

C - Print x

D - Return Error

Answer : A

Explanation

Although, integer y = 100; and constant integer x is equal to y. here in the given above program we have to print the x value, so that it will be 100.


cprogramming_questions_answers.htm

Advertisements