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 I

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

 #include<stdio.h> main() { int const a = 5; a++; printf(%d,a); } 

A - 5

B - 6

C - Runtime error

D - Compile error

Answer : D

Explanation

Compile error - constant variable cannot be modified.

 main() { int const a = 5; a++; printf(%d,a); } 

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

 #include<stdio.h> main() { const int a = 5; a++; printf("%d", a); } 

A - 5

B - 6

C - Runtime error

D - Compile error

Answer : D

Explanation

Compile error - constant variable cannot be modified.

 main() { const int a = 5; a++; printf("%d", a); } 

Q 3 - What is the output of the below code snippet?

 #include<stdio.h> main() { char s[]="hello", t[]="hello"; if(s==t){ printf("eqaul strings");	} } 

A - Equal strings

B - Unequal strings

C - No output

D - Compilation error

Answer : C

Explanation

No output, as we are comparing both base addresses and they are not same.

Q 4 - What is the output of the below code snippet?

 #include<stdio.h> main() { int a = 5, b = 3, c = 4; printf("a = %d, b = %d\n", a, b, c); } 

A - a=5, b=3

B - a=5, b=3, c=0

C - a=5, b=3, 0

D - compile error

Answer : A

Explanation

a=5,b=3 , as there are only two format specifiers for printing.

Q 5 - What is the output of the below code snippet?

 #include<stdio.h> main() { int a = 1; float b = 1.3; double c; c = a + b; printf("%.2lf", c); } 

A - 2.30

B - 2.3

C - Compile error

D - 2.0

Answer : A

Explanation

2.30, addition is valid and after decimal with is specified for 2 places.

Q 6 - What is the outpout of the following program?

 #include<stdio.h> main() { enum { india, is=7, GREAT }; printf("%d %d", india, GREAT); } 

A - 0 1.

B - 0 2

C - 0 8

D - Compile error

Answer : C

Explanation

0 8, enums gives the sequence starting with 0. If assigned with a value the sequence continues from the assigned value.

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

 #include<stdio.h> main() { char c = 'A'+255; printf("%c", c); } 

A - A

B - B

C - Overflow error at runtime

D - Compile error

Answer : A

Explanation

A, the range of ASCII values for the ASCII characters is 0-255. Hence the addition operation circulates back to A

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

 #include<stdio.h> main() { short unsigned int i = 0; printf("%u\n", i--); } 

A - 0

B - Compile error

C - 65535

D - 32767

Answer : A

Explanation

0, with post decrement operator value of the variable will be considered as the expressions value and later gets decremented.

Q 9 - What is the output of the below code snippet?

 #include<stdio.h> main() { unsigned x = 5, y=&x, *p = y+0; printf("%u",*p); } 

A - Address of x

B - Address of y

C - Address of p

D - 5

Answer : D

Explanation

5, as p holds the address of x which is y+0

Q 10 - 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 11 - What is the output of the following code snippet?

 #include<stdio.h> main() { int x = 5; if(x==5) { if(x==5) break; printf("Hello"); } printf("Hi"); } 

A - Compile error

B - Hi

C - HelloHi

D - Hello

Answer : A

Explanation

compile error, keyword break can appear only within loop/switch statement.

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

 #include<stdio.h> main() { int x = 5; if(x=5) { if(x=5) break; printf("Hello"); } printf("Hi"); } 

A - Compile error

B - Hi

C - HelloHi

D - Compiler warning

Answer : A

Explanation

compile error, keyword break can appear only within loop/switch statement.

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

 #include<stdio.h> main() { int x = 5; if(x=5) { if(x=5) printf("Hello"); } printf("Hi"); } 

A - HelloHi

B - Hi

C - Hello

D - Compiler error

Answer : A

Explanation

HelloHi, both the if statements expression evaluates to be true.

Q 14 - 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 : A

Explanation

infinite loop, with second expression of for being absent it is considered as true by default.

Q 15 - 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 16 - What is the output of the below code snippet?

 #include<stdio.h> main() { for(1;2;3) printf("Hello"); } 

A - Infinite loop

B - Prints Hello once.

C - No output

D - Compile error

Answer : A

Explanation

infinite loop, as the second expression is non-0, hence the condition is always true.

Q 17 - int x=~1; What is the value of 'x'?

A - 1

B - -1

C - 2

D - -2

Answer : D

Explanation

-2, the ones compliment of 1 is 1110 (binary) which is equivalent to twos compliment of 2, ie -2.

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

 #include<stdio.h> void f() { static int i; ++i; printf("%d", i); } main() { f(); f(); f(); } 

A - 1 1 1

B - 0 0 0

C - 3 2 1

D - 1 2 3

Answer : D

Explanation

1 2 3, A static local variables retains its value between the function calls and the default value is 0.

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

 #include<stdio.h> main() { register int x = 5; int *p; p=&x; x++; printf("%d",*p); } 

A - Compile error

B - 5

C - 6

D - Garbage value

Answer : A

Explanation

Compile error, we cannot take the address of a register variable.

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

 #include<stdio.h> main() { int x = 65, *p = &x; void *q=p; char *r=q; printf("%c",*r); } 

A - Garbage character.

B - A

C - 65

D - Compile error

Answer : B

Explanation

A, void pointer is a generic pointer and can hold any variables address. ASCII character for the value 65 is A

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

 #include<stdio.h> main() { printf("\"); } 

A - \

B - \"

C - "

D - Compile error

Answer : D

Explanation

Compile error, Format string of printf is not terminated.

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

 #include<stdio.h> { int x = 1; switch(x) { default: printf("Hello"); case 1: printf("hi"); break; } } 

A - Hello

B - Hi

C - HelloHi

D - Compile error

Answer : B

Explanation

Hi, control reaches default-case after comparing the rest of case constants.

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

 #include<stdio.h> main() { struct { int x;} var = {5}, *p = &var; printf("%d %d %d",var.x,p->x,(*p).x); } 

A - 5 5 5

B - 5 5 garbage value

C - 5 5 0

D - Compile error

Answer : A

Explanation

5 5 5, the two possible ways of accessing structure elements using pointer is by using -> (arrow operator) OR *.

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

 #include<stdio.h> void swap(int m, int n) { int x = m; m = n; n = x; } main() { int x=5, y=3; swap(x,y); printf("%d %d", x, y); } 

A - 3 5

B - 5 3

C - 5 5

D - Compile error

Answer : B

Explanation

5 3, call by value mechanism cant alter actual arguments.

 #include <stdio.h> void swap(int m, int n) { int x = m; m = n; n = x; } main() { int x=5, y=3; swap(x,y); printf("%d %d", x, y); } 

Q 27 - What will be printed for the below statement?

 #include<stdio.h> main() { printf("%d",strcmp("strcmp()","strcmp()")); } 

A - 0

B - 1

C - -1

D - Invalid use of strcmp() function

Answer : A

Explanation

0, strcmp return 0 if both the strings are equal

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

 #include<stdio.h> main() { int r, x = 2; float y = 5; r = y%x; printf("%d", r); } 

A - 1

B - 0

C - 2

D - Compile error

Answer : D

Explanation

Compile Error, It is invalid that either of the operands for the modulus operator (%) is a real number.

Q 30 - Which operator is used to continue the definition of macro in the next line?

A - #

B - ##

C - $

D - \

Answer : D

Explanation

\, the first two are stringize and token pasting operators respectively. There is no such operator called $.

Q 31 - What is the size of the following union definition?

 #include<stdio.h> union abc { char a,b,c,d,e,f,g,h; int i; }abc; main() { printf( "%d", sizeof( abc )); } 

A - 1

B - 2

C - 4

D - 8

Answer : C

Explanation

union size is biggest element size of it. All the elements of the union share common memory.

Q 32 - What is the size of int?

A - 2

B - 4

C - 8

D - Compiler dependent

Answer : D

Explanation

The size of int depends upon the complier i.e. whether it is a 16 bit or 32 bit.

Q 33 - The type name/reserved word short is ___

A - short long

B - short char

C - short float

D - short int

Answer : D

Explanation

short is used as an alternative of short int.

Q 34 - What is the value of y for the following code snippet?

 #include<stdio.h> main() { int x = 1; float y = x>>2; printf( "%f", y ); } 

A - 4

B - 0.5

C - 0

D - 1

Answer : C

Explanation

0, data bits are lost for the above shift operation hence the value is 0.

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

 #include<stdio.h> main() { float t = 2; switch(t) { case 2: printf("Hi"); default: printf("Hello"); } } 

A - Hi

B - HiHello

C - Hello

D - Error

Answer : D

Explanation

Error, switch expression cant be float value.

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

 #include<stdio.h> main() { int i = 1; while(++i <= 5) printf("%d ",i++); } 

A - 1 3 5

B - 2 4

C - 2 4 6

D - 2

Answer : B

Explanation

2 4, at while first incremented and later compared and in printf printed first and incremented later

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

 #include<stdio.h> main() { int i = 1; while( i++<=5 ) printf("%d ",i++); } 

A - 1 3 5

B - 2 4

C - 2 4 6

D - 2

Answer : C

Explanation

2 4 6, at while first compared and later incremented and in printf printed first and incremented later.

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

 #include<stdio.h> main() { int i = 1; while(i++<=5); printf("%d ",i++); } 

A - 4

B - 6

C - 2 6

D - 2 4

Answer : B

Explanation

6, there is an empty statement following while.

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

 #include<stdio.h> main() { int x = 1; do printf("%d ", x); while(x++<=1); } 

A - 1

B - 1 2

C - No output

D - Compile error

Answer : B

Explanation

1 2, do..while is an entry control loop. As the expression x++ is post form loop continues for 2nd time also.

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

 #include<stdio.h> main() { int a[3] = {2,1}; printf("%d", a[a[1]]); } 

A - 0

B - 1

C - 2

D - 3

Answer : B

Explanation

1, The inner indirection evaluates to 1, and the value at index 1 for outer indirection is 1.

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

 #include<stdio.h> main() { int a[3] = {2,,1}; printf("%d", a[a[0]]); } 

A - 0

B - 1

C - 2

D - Compile error

Answer : D

Explanation

Compile error, invalid syntax in initializing the array.

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

 #include<stdio.h> main() { int a[] = {2,1}; printf("%d", *a); } 

A - 0

B - 1

C - 2

D - Compile error.

Answer : C

Explanation

2, as a refers to base address.

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

 #include<stdio.h> main() { int i = 1; Charminar: printf("%d ",i++); if(i==3) break; if(i<=5) goto Charminar; } 

A - 1 2

B - 1 2 3

C - 1 2 4 5

D - Compile error

Answer : D

Explanation

Compile error, wrong place for break to appear.

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

 #include<stdio.h> main() { int i = 13, j = 60; i ^= j; j ^= i; i ^= j; printf("%d %d", i, j); } 

A - 73 73

B - 60 13

C - 13 60

D - 60 60

Answer : B

Explanation

60 13, its swapping.

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

 #include<stdio.h> main() { union abc { int x; char ch; }var; var.ch = 'A'; printf("%d", var.x); } 

A - A

B - Garbage value

C - 65

D - 97

Answer : C

Explanation

65, as the union variables share common memory for all its elements, x gets A whose ASCII value is 65 and is printed.

Q 47 - Identify the incorrect file opening mode from the following.

A - r

B - w

C - x

D - a

Answer : C

Explanation

x, there is no such mode called x.

Q 48 - Function fopen() with the mode "r+" tries to open the file for __

A - reading and writing

B - reading and adding new content

C - only for reading

D - it works only for directories

Answer : A

Explanation

Option (a), the file should exist and opens for both reading & writing.

Q 49 - 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 50 - First operating system designed using C programming language.

A - DOS

B - Windows

C - UNIX

D - Mac

Answer : C

Explanation

UNIX. C actually invented to write an operation system called UNIX. By 1973 the entire UNIX OS is designed using C.

Answer Sheet

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