• C Programming Video Tutorials

C Programming Mock Test



This section presents you various set of Mock Tests related to C Programming Framework. You can download these sample mock tests at your local machine and solve offline at your convenience. Every mock test is supplied with a mock test key to let you verify the final score and grade yourself.

Questions and Answers

C Programming Mock Test II

Answer : B

Explanation

(b). Linker links the object code of your program and library code to produce executable.

Answer : B

Explanation

(b). Compilation is the process of translating high level language statements into equivalent machine code, which is object code.

Q 3 - Special symbol permitted with in the identifier name.

A - $

B - @

C - _

D - .

Answer : C

Explanation

The only permitted special symbol is under score (_) in the identifier.

Q 4 - A single line comment in C language source code can begin with _____

A - ;

B - :

C - /*

D - //

Answer : D

Explanation

Two immediate forward slashes are used to comment a single line. A single can be commented by beginning with /* and should be terminated with */ , in general used for multi-line comments.

Q 5 - A macro can execute faster than a function.

A - true

B - false

Answer : A

Explanation

As the code of macro gets expanded at the line of call, therefore macro gets executed faster with no overhead of context switch.

Q 6 - 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 7 - Choose the application option for the following program?

#include<stdio.h>

main()
{
   int *p, **q;
   
   printf("%u\n", sizeof(p));
   printf("%u\n", sizeof(q));
}

A - Both the printf() will print the same value

B - First printf() prints the value less than the second.

C - Second printf() prints the value less than the first.

D - Error in the code.

Answer : A

Explanation

Irrespective of any data type every type of pointer variable occupies same amount of memory.

Q 8 - What is the built in library function to adjust the allocated dynamic memory size.

A - malloc

B - calloc

C - realloc

D - resize

Answer : C

Explanation

There is no built-in function with the name resize(). Malloc() & calloc() allocates memory but won’t resize.

Q 9 - 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 10 - Following is the invalid inclusion of a file to the current program. Identify it.

A - #include <file>

B - #include “file”

C - #include < file

D - All of the above are invalid.

Answer : C

Explanation

option (a) & (b) are valid. There is no such syntax or provision as in option (c).

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

#include<stdio.h>

int* f() 
{
   int x = 5;
   
   return &x;
}	
main() 
{
   printf("%d", *f());
}

A - 5

B - Address of ‘x’

C - Compile error

D - Runtime error

Answer : D

Explanation

It is invalid to return local variable address as the local variable gets removed after the functions execution is completed.

Q 12 - 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 13 - The default executable generation on UNIX for a C program is ___

A - a.exe

B - a

C - a.out

D - out.a

Answer : C

Explanation

"a.out" is the default name of the executable generated on both the UNIX and Linux operating systems.

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

#include<stdio.h>

void f() 
{
   static int i = 3;
   
   printf("%d ", i);
   if(--i) f();
}
main() 
{
   f();
}

A - 3 2 1 0

B - 3 2 1

C - 3 3 3

D - Compile error

Answer : B

Explanation

As the static variable retains its value from the function calls, the recursion happens thrice.

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

#include<stdio.h>

main() 
{
}

A - No output

B - Garbage

C - Compile error

D - Runtime error

Answer : A

Explanation

It is valid to have main() function empty, therefore producing no displayable output.

Q 16 - Choose the correct option in respect to the following program.

#include<stdio.h>

void f(int const i) 
{
   i=5;
}
main() 
{
   int x = 10;
   
   f(x);
}
  • I - Error in the statement ‘void f(int const i)’

  • II - Error in the statement i=5.

A - Statements I & II are true

B - Statements I & II are false.

C - Statement I is true

D - Statement II is true.

Answer : D

Explanation

We cannot modify a constant as in statement i=5.

Q 17 - To store a word/sentence declare a variable of the type ‘string’.

A - true

B - false

Answer : B

Explanation

There is no such data type called ‘string’ in C language.

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

#include<stdio.h>

int x = 5;
int* f() 
{
   return &x;
}
main()
{	
   *f() = 10;
   
   printf("%d", x);
}

A - Compile error

B - Runtime error

C - 5

D - 10

Answer : D

Explanation

The returned address is global variables and 10 being stored in it. Therefore x is 10.

Q 19 - What is the output of the below code snippet.

#include<stdio.h>

main()
{
   printf("%d", -11%2);
}

A - 1

B - -1

C - 5.5

D - -5.5

Answer : B

Explanation

Modulus (%) operator is meant to give reminder for integer division.

Answer : A

Explanation

When the program is in execution phase the possible unavoidable error is called as an exception.

Q 21 - 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.

Answer : C

Explanation

The text associated with the macro name gets expanded at the line of call. The expanded text is by default a double constant whereas no type is associated with PI.

Q 23 - Choose the invalid predefined macro as per ANSI C.

A - __FILE__

B - __DATE__

C - __TIME__

D - __C++__

Answer : D

Explanation

There is no macro define with the name __C++__, but __cplusplus is defined by ANSI)

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

#include<stdio.h>

main()
{	
   int a[] = {10, 20, 30};
   
   printf("%d", *a+1);
}

A - 10

B - 20

C - 11

D - 21

Answer : C

Explanation

*a refers to 10 and adding a 1 to it gives 11.

Q 25 - 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 26 - What is the output of the following program?

#include<stdio.h>

main()
{	
   char *s = "Hello, "
   "World!";

   printf("%s", s);
}

A - Hello, World!

B - Hello,

World!

C - Hello

D - Compile error

Answer : A

Explanation

Two immediate string constant are considered as single string constant.

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

#include<stdio.h>

main()
{	
   fprintf(stdout,"Hello, World!");
}

A - Hello, World!

B - No output

C - Compile error

D - Runtime error

Answer : C

Explanation

stdout is the identifier declared in the header file stdio.h, need to include the same.

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

#include<stdio.h>

main()
{	
   fprintf(stdout,"Hello, World!");
}

A - Hello, World!

B - No output

C - Compile error

D - Runtime error

Answer : A

Explanation

stdout is the identifier declared in the header file stdio.h which is connected to standard output device (monitor).

Q 29 - Which of the following is used in mode string to open the file in binary mode?

A - a

B - b

C - B

D - bin

Answer : B

Explanation

To perform unformatted data I/O a file is opened in binary mode and is represented with the alphabet ‘b’ in the mode string.

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

#include<stdio.h>

main()
{
   char s[] = "Fine";
   *s = 'N';
   
   printf("%s", s);
}

A - Fine

B - Nine

C - Compile error

D - Runtime error

Answer : B

Explanation

*s=’N’, changes the character at base address to ‘N’.

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

#include<stdio.h>

main()
{
   char *s = "Fine";
   *s = 'N';
   
   printf("%s", s);
}

A - Fine

B - Nine

C - Compile error

D - Runtime error

Answer : D

Explanation

*s=’N’, trying to change the character at base address to ‘N’ of a constant string leads to runtime error.

Q 32 - 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’.

Q 33 - What is the built in library function to compare two strings?

A - string_cmp()

B - strcmp()

C - equals()

D - str_compare()

Answer : B

Explanation

strcmp() is the built in function from “string.h” to compare two strings. Returns 0 if both are same strings. Returns -1 if first < second string. Returns 1 first > second.

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

#include<stdio.h>

main()
{ 
   char s1[50], s2[50] = "Hello";
   
   s1 = s2;
   printf("%s", s1);
}

A - Hello

B - No output

C - Compile error

D - Runtime error

Answer : C

Explanation

‘s1’ refers to base address and is constant. Hence raising to ‘lvalue’ required compile time error.

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

#include<stdio.h>

int main();
void main()
{
   printf("Okay"); 
}

A - Okay

B - No output

C - Compile error. We cannot declare main() function.

D - Compile error. Mismatch in declaration & definition.

Answer : D

Explanation

It’s compile error as the declaration of main() mismatches with the definition.

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

#include<stdio.h>

void main()
{
   char *s = "C++";
   
   printf("%s ", s);
   s++;
   printf("%s", s);
}

A - C++ C++

B - C++ ++

C - ++ ++

D - Compile error

Answer : B

Explanation

After s++, s points the string “++”.

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

#include<stdio.h>

void main()
{
   char s[] = "C++";
   
   printf("%s ",s);
   s++;
   printf("%s",s);
}

A - C++ C++

B - C++ ++

C - ++ ++

D - Compile error

Answer : D

Explanation

‘s’ refers to a constant address and cannot be incremented.

Answer : B

Explanation

All the local variables are stored in a memory called as stack.

Q 39 - 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 two’s compliment of positive number. After shifting we get 1110, which is equivalent to -2.

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

#include<stdio.h>

main()
{ 
   int x = 3;
   
   x += 2;
   x =+ 2;
   printf("%d", x); 
}

A - 2

B - 5

C - 7

D - Compile error

Answer : A

Explanation

+ in unary form is dummy operator, therefore ‘x’ is overwritten with the value +2 finally.

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

#include<stdio.h>

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

A - Abc

B - bc

C - Compile error

D - Runtime error

Answer : A

Explanation

Loop continues until *s not equal to ‘\0’, hence printing “Abc” where character is fetched first and address is incremented later.

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

#include<stdio.h>

main()
{ 
   char s[20] = "Hello\0Hi";
   
   printf("%d %d", strlen(s), sizeof(s));
}

A - 5 9

B - 7 20

C - 5 20

D - 8 20

Answer : C

Explanation

Length of the string is count of character upto ‘\0’. sizeof – reports the size of the array.

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

#include<stdio.h>

main()
{ 
   char s[] = "Hello\0Hi";
   
   printf("%d %d", strlen(s), sizeof(s));
}

A - 5 9

B - 7 20

C - 5 20

D - 8 20

Answer : A

Explanation

Length of the string is count of character upto ‘\0’. sizeof – reports the size of the array.

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

#include<stdio.h>

main()
{ 
   printf("%d", !0<2);
}

A - 0

B - 1

C - False

D - True

Answer : B

Explanation

Priority of ! is greater than <. Relational operator returns 1 if relation between the expressions is true otherwise 0.

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

#include<stdio.h>

main()
{ 
   struct student
   { 
       int num = 10;
   }var;

   printf("%d", var.num);
}

A - 10

B - Garbage

C - Runtime error

D - Compile error

Answer : D

Explanation

Structure elements cannot be initialized

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

#include<stdio.h>

#define sqr(i) i*i
main()
{
   printf("%d %d", sqr(3), sqr(3+1)); 
}

A - 9 16

B - 9 7

C - Error: macro cannot be defined in lower case.

D - None of the above.

Answer : B

Explanation

The equivalent expansion is -> printf("%d %d",3*3,3+1*3+1);

Q 47 - 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.

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

#include<stdio.h>

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

A - Hello

B - Compile error: there is no macro called “undef”

C - Compile error: improper place of #undef

D - Compile error: NULL is undeclared.

Answer : D

Explanation

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

Q 49 - C is the successor of ___ programming language.

A - C++

B - B++

C - B

D - Mini C

Answer : C

Explanation

B is a programming language developed at Bell Labs in 1969. It is derived from BCPL (Basic Combined Programming Language). It is designed by Ken Thompson with Dennis Ritchie.

Q 50 - Which of the following functions disconnects the stream from the file pointer.

A - fremove()

B - fclose()

C - remove()

D - file pointer to be set to NULL

Answer : B

Explanation

fclose(), it flushes the buffers associated with the stream and disconnects the stream with the file.

Answer Sheet

Question Number Answer Key
1 B
2 B
3 C
4 D
5 A
6 B
7 A
8 C
9 B
10 C
11 D
12 D
13 C
14 B
15 A
16 D
17 B
18 D
19 B
20 A
21 B
22 C
23 D
24 C
25 B
26 A
27 C
28 A
29 B
30 B
31 D
32 A
33 B
34 C
35 D
36 B
37 D
38 B
39 B
40 A
41 A
42 C
43 A
44 B
45 D
46 B
47 A
48 D
49 C
50 B
cprogramming_questions_answers.htm
Advertisements