• 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 IV

Answer : B

Explanation

By passing the name of an array as a function argument; the name contains the base address of an array and the base address (array value) get updated in the main function.

Q 2 - In the given below code, the function fopen()uses "r" to open the file “source.txt” in binary mode for which purpose?

#include<stdio.h>

int main ()
{
   FILE *fp;
   
   fp = fopen("source.txt", "r");
   return 0;
}

A - For reading

B - For reading and writing

C - For creating a new file "source.txt" for reading

D - For creating a new file "source.txt" for writing

Answer : A

Explanation

To open a file in C programming, we can use library function fopen(). In the given above code, fopen() function is opening a file “source.txt” for reading. Here, “r” stands for reading. If, fopen() function does not find any file for reading, returns NULL

#include<stdio.h>

int main ()
{
   FILE *fp;
   
   fp = fopen("source.txt", "r");
   return 0;
}

Q 3 - In DOS, how many bytes exist for near, far and huge pointers?

A - Near: 2, far: 4, huge: 7

B - near: 4, far: 2, huge: 8

C - near: 2, far: 4, huge: 4

D - near: 4, far: 0, huge: 0

Answer : C

Explanation

In DOS, numbers of byte exist for near pointer = 2, far pointer = 4 and huge pointer = 4.

In Windows and Linux, numbers of byte exist for near pointer = 4, far pointer = 4 and huge pointer = 4.

Q 4 - fgets() function is safer than gets() because in fgets() function you can specify the size of the buffer into which the supplied string will be stored.

A - True

D - False

Answer : A

Explanation

Both functions retrive and store a string from console or file, but fgets() functions are more safer to use then gets() because gets() doesn't facilitate to detail the length of the buffer to store the string in and fgets() facilitates to specify a maximum string length.

   char *fgets(char *s, int size, FILE *stream);
   char *gets(char *s);

Q 5 - Which scanf() statement will you use to scan a float value (a) and double value (b)?

   Float a;
   
   Double b;

A - scanf("%f %f", &a, &b);

B - scanf("%lf % lf ", &a, &b);

C - scanf("%Lf %Lf", &a, &b);

D - scanf("%f %lf", &a, &b);

Answer : D

Explanation

In C Programming, the scanf() can be used to read a formatted string from the stdin stream, where as; scanf() uses %lf to read a double value and %f to read a floating-point number in fixed decimal format.

int scanf(const char *format, ...)

Q 6 - 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 : 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 8 - In the given below code, the P2 is

   Typedef int *ptr;
   
   ptr p1, p2;

A - Integer

B - Integer pointer

C - Both, Integer & Integer pointer

D - None of above

Answer : B

Explanation

Ptr is an alias to int*.

Q 9 - In the following code, what is 'P'?

   Typedef char *charp;
   
   const charp P;

A - P is a constant

B - P is a character type

C - P is a pointer

D - None of the above

Answer : A

Explanation

Const charp P;

Although, the code itself indicates the keyword "const", so that; P is a constant.

Q 10 - What is x in the following program?

#include<stdio.h>

int main ()
{
   typedef char (*(*arrfptr[3])())[10];
   arrfptr x
   return 0;
}

A - x is a character pointer

B - x is an array of pointer

C - x is an array of three function pointers

D - Wrong declaration

Answer : C

Explanation

Here, x is an array of three function pointers

#include<stdio.h>

int main ()
{
   typedef char (*(*arrfptr[3])())[10];
   arrfptr x
   return 0;
}

Q 11 - What will be the resultant of the given below program?

#include<stdio.h>
#include<stdarg.h>

Void fun(char *msg, ...);
int main ()
{
   fun("IndiaMAX", 1, 4, 7, 11, 0);
   return 0;
}
   void fun(char *msg, ...)  
{
va_list ptr;{
   int num;
   va_start(ptr, msg);
   num = va_arg(ptr, int);    
   num = va_arg(ptr, int);
   printf("%d", num);
}

A - IndiaMAX 1, 7, 11, 0

B - IndiaMAX 1, 7

C - Only 4

D - 1, 7, 11, 0

Answer : C

Explanation

va_list ptr is an argument pointer that traverse through the variable arguments passed in the function, where, va_start points 'ptr' to the first argument (IndiaMix). In each call to the va_arg, ptr shift to the next variable argument and after two calls ptr ends up at '7' and return the number '4'.

#include<stdio.h>
#include<stdarg.h>

Void fun(char *msg, ...);
int main ()
{
   fun("IndiaMAX", 1, 4, 7, 11, 0);
   return 0;
}
   void fun(char *msg, ...)  
{
va_list ptr;{
   int num;
   va_start(ptr, msg);
   num = va_arg(ptr, int);    
   num = va_arg(ptr, int);
   printf("%d", num);
}

Q 12 - The correct order of evaluation for the expression “z = x + y * z / 4 % 2 – 1”

A - * / % = + -

B - / * % - + =

C - - + = * % /

D - * / % + - =

Answer : D

Explanation

* / % holds highest priority than + - . All with left to right associativity.

Q 13 - In C, what is the correct hierarchy of arithmetic operations?

A - */ + -

B - * +- /

C - / *+ -

D - + - / *

Answer : C

Explanation

In C, there are 5 arithmetic operators (+, -, *, /, and %) that can be used in performing arithmetic operations.

Q 14 - To print a double value which format specifier can be used?

A - %L

B - %lf

C - %Lf

D - None of the above

Answer : B

Explanation

Double value can be printed using %lf format specifier.

Q 15 - Which files will get closed through the fclose() in the following program?

#include<stdio.h>

int main ()
{
   FILE *fs, *ft, *fp;
   
   fp = fopen("ABC", "r");
   fs = fopen("ACD", "r");
   ft = fopen("ADF", "r");
   fclose(fp, fs, ft);
return 0;
}

A - "ABC"

B - "ACD"

C - "ADF"

D - Return error

Answer : D

Explanation

The syntax of fclose() function is wrong; it should be int fclose(FILE *stream); closes the stream. Here, fclose(fp, fs, ft); is using separator(,) which returns error by saying that extra parameter in call to fclose() function.

#include<stdio.h>

int main ()
{
   FILE *fs, *ft, *fp;
   
   fp = fopen("ABC", "r");
   fs = fopen("ACD", "r");
   ft = fopen("ADF", "r");
   fclose(fp, fs, ft);
return 0;
}

Q 16 - Which of the following statement shows the correct implementation of nested conditional operation by finding greatest number out of three numbers?

A - max = a>b ? a>c?a:c:b>c?b:c

B - a=b ? c=30;

C - a>b : c=30 : c=40;

D - return (a>b)?(a:b) ?a:c:b

Answer : A

Explanation

Syntax is exp1?exp2:exp3. Any ‘exp’ can be a valid expression.

Q 17 - Choose the correct order from given below options for the calling function of the code “a = f1(23, 14) * f2(12/4) + f3();”?

A - f1, f2, f3

B - f3, f2, f1

C - f2, f1, f3

D - Order may vary from one compiler to another

Answer : D

Explanation

Evaluation order is implementation dependent.

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

#include<stdio.h>

int main()
{
   const int i = 0;
    
   printf("%d\n", i++);
   return 0;
}

A - 100

B - Infinity

C - 0

D - Return error

Answer : D

Explanation

It is because ++needs a value and a const variable can’t be modified.

#include<stdio.h>

int main()
{
   const int i = 0;
    
   printf("%d\n", i++);
   return 0;
}

Answer : A

Explanation

Unary operator acts on single expression.

Answer : B

Explanation

As per the operators preference.

Q 21 - Which printf() statement will you use to print out a (float value) and b (double value)?

   Float a = 3.14;
   
   Double b = 3.14;

A - printf("%f %lf", a, b);

B - printf("%f %f", a, b);

C - printf("%Lf %Lf", a, b);

D - printf("%f %Lf", a, b);

Answer : A

Explanation

%f can be use to print out float value and %lf can be use to print out  double value.

Q 22 - To print a float value which format specifier can be used?

A - %f

B - %lf

C - %Lf

D - None of the above

Answer : A

Explanation

%f can be use to print out float value and %lf can be use to print out  double value.

Q 23 - Choose the correct unary operators in C – a) !, b) ~, c) ^&, d) ++

A - a, b, d

B - a, b, c

C - b, c, d

D - c, d, a

Answer : A

Explanation

In C, these are the unary operators,

   Logical NOT = !
   Address-of = &
   Cast Operator = ( )
   Pointer dereference = *
   Unary Plus = +
   Increment = ++
   Unary negation = –

Q 24 - 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;
}

Answer : B

Explanation

square parenthesis signify as array at declaration and type is int*, so array of integer pointers.

Q 26 - What is the role of "r+" on the file "NOTES.TXT" in the given below code?

#include<stdio.h>

int main ()
{
   FILE *fp;
   
   fp = fopen("NOTES.TXT", "r+");
   return 0;
}

A - "r+" open the file "NOTES.TXT" file for reading

B - "r+" open the file "NOTES.TXT" file for writing

C - "r+" open the file "NOTES.TXT" file for appending

D - "r+" open the file "NOTES.TXT" file for reading & writing both

Answer : D

Explanation

fopen() function is used to open a file and r++ enable the file for reading and writing.

#include<stdio.h>

int main ()
{
   FILE *fp;
   
   fp = fopen("NOTES.TXT", "r+");
   return 0;
}

Q 27 - In the given below code, what will be return by the function get ()?

#include<stdio.h>

int get();

int main()

{
   const int x = get();
   
   printf("%d", x);
   return 0;
}
   int get()
   {
    return 40;
}

A - 40

B - 20

C - 0

D - Error

Answer : A

Explanation

Firstly, “int get()” which is a get() function prototype returns an integer value without any parameters.

Secondly, const int x = get(); The constant variable x is declared as an integer data type and initialized with the value of get(). Hence, the value of get() is 40, printf("%d", x); will print the value of x, that means; 40. So, the program output will be 40.

#include<stdio.h>

int get();

int main()

{
   const int x = get();
   
   printf("%d", x);
   return 0;
}
   int get()
   {
    return 40;
}

Answer : A

Explanation

Preprocessing enlarges and boosts the C programming language by replacing preprocessing directive “#include<stdio.h>” with the content of the file stdio.h.

Q 29 - What value strcmp() function returns when two strings are the same?

A - 0

B - 2

C - 1

D - Error

Answer : A

Explanation

The C library function strcmp() compares two strings with each other and return the value accordingly.

   int strcmp(const char *str1, const char *str2)

Comparison happens between first string (str1) with second string (str2).

By comparing two strings, the values return by the function strcmp() are,

  • If, str1 is less than str2 then Return value < 0
  • If, str2 is less than str1 then Return value > 0
  • If, str1 is equal to str2 then Return value = 0

Q 30 - What will be the output of the given below program in TurboC

#include<stdio.h>

int fun(int **ptr);

int main()
{
    int i = 10, j = 20;
    const int *ptr = &i;
    
    printf(" i = %5X", ptr);
    printf(" ptr = %d", *ptr);
    ptr = &j;
    printf(" j = %5X", ptr);
    printf(" ptr = %d", *ptr);
    return 0;
}

A - i= FFE6 ptr=30 j=FFE4 ptr=36

B - i= FFE0 ptr=04 j=FFE1 ptr=30

C - i= FFE4 ptr=10 j=FFE2 ptr=20

D - None of the above

Answer : C

Explanation

First the pointer holds I’s address and changes later to hold j’s address. Hence printing the 10 & 20 respectively.

#include<stdio.h>

int fun(int **ptr);

int main()
{
    int i = 10, j = 20;
    const int *ptr = &i;
    
    printf(" i = %5X", ptr);
    printf(" ptr = %d", *ptr);
    ptr = &j;
    printf(" j = %5X", ptr);
    printf(" ptr = %d", *ptr);
    return 0;
}

Q 31 - 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;
}

Q 32 - Which statement can print \n on the screen?

A - printf("\\n");

B - printf("n\");

C - printf("n");

D - printf('\n');

Answer : A

Explanation

Option A is the correct answer. In C programming language, "\n" is the escape sequence for printing a new line character. In printf("\\n"); statement, "\\" symbol will be printed as "\" and “n” will be known as a common symbol.

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

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

Q 35 - The library function strrchr() finds the first occurrence of a substring in another string.

A - Yes

B - Strstr()

C - strchr()

D - strnset()

Answer : B

Explanation

Strstr() finds the first occurrence of a substring in another string.

Q 36 - If, the given below code finds the length of the string then what will be the length?

#include<stdio.h>

int xstrlen(char *s)
{
   int length = 0;
   
   while(*s!='\0')
   {length++; s++;}
   return (length);
}
   int main()
{
   char d[] = "IndiaMAX";
    
   printf("Length = %d\n", xstrlen(d));
   return 0;
}

A - Code returns error

B - Code returns the length 8

C - Code returns the length 6

D - Code returns the length 2

Answer : B

Explanation

Here, *s is char pointer that holds character string. To print whole string we use printf("%s",s) by using base address. s contains base address (&s[0]) and printf will print characters until '\0' occurs. *s only gives first character of input string, but s++ will increase the base address by 1 byte. When *s=='\0' encountered, it will terminate loop.

#include<stdio.h>

int xstrlen(char *s)
{
   int length = 0;
   
   while(*s!='\0')
   {length++; s++;}
   return (length);
}
   int main()
{
   char d[] = "IndiaMAX";
    
   printf("Length = %d\n", xstrlen(d));
   return 0;
}

Q 37 - The maximum combined length of the command-line arguments as well as the spaces between adjacent arguments is – a) 120 characters, b) 56 characters, c) Vary from one OS to another

A - a

B - a, b

C - a, b, c

D - c

Answer : D

Explanation

The maximum combined length of the command-line arguments and the spaces between adjacent arguments vary from one OS to another.

Q 38 - Choose the function that is most appropriate for reading in a multi-word string?

A - strnset()

B - scanf()

C - strchr()

D - gets()

Answer : D

Explanation

gets (); = Collects a string of characters terminated by a new line from the standard input stream stdin

Answer : D

Explanation

square parenthesis signify as array at declaration and type is char*, so array of character pointers.

Q 40 - In the given below statement, what does the “pf” indicate?

   int (*pf)();

A - pf is a pointer of a function which return int

B - pf is a pointer

C - pf is a function pointer

D - None of the above

Answer : A

Explanation

pf is a pointer as well holds some functions reference.

Q 41 - extern int fun(); - The declaration indicates the presence of a global function defined outside the current module or in another file.

A - True

B - False

Answer : A

Explanation

Extern is used to resolve the scope of global identifier.

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

#include<stdio.h>

main ()

{

   int i, j;

   for(i=5, j=1; i>j; i--, ++j) 

}

A - 5 2, 4 2

B - Compile error

C - 4 2

D - 5 1, 4 2

Answer : D

Explanation

5 1, 4 2- The condition fails when i=j=3.

#include<stdio.h>

main ()

{

   int i, j;

   for(i=5, j=1; i>j; i--, ++j) 

}

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

#include<stdio.h>

main ()

{ 
   int a=1, b=2, *p=&a, *q=&b, *r=p;

   p = q; q = r;

   printf("%d %d %d %d\n",a,b,*p,*q);

}

A - 1 2 2 1

B - 2 1 2 1

C - 1 2 1 2

D - Compile error

Answer : A

Explanation

1 2 2 1, the pointers are swapped not the values.

#include<stdio.h>

main ()

{ 
   int a=1, b=2, *p=&a, *q=&b, *r=p;

   p = q; q = r;

   printf("%d %d %d %d\n",a,b,*p,*q);

}

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

#include<stdio.h>

void g(void) { 

}

main ()

{ 
   void (*f)(void);

   f = g;

   f();

}

A - Hello

B - Calling f(); is invalid it should be (*f)();

C - void (*f)(void) is invalid declaration.

D - Instead of f=g it should be f=&g.

Answer : A

Explanation

Hello, every line is a valid statement.

#include<stdio.h>

void g(void) { 

}

main ()

{ 
   void (*f)(void);

   f = g;

   f();

}

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

#include<stdio.h>

int f(int i) {

}

main ()

{ 

   printf("%d",f(f(f(f(f(1))))));

}

A - 6

B - 5

C - 1

D - Compilation error

Answer : C

Explanation

1, the return value is post increment expression as it always receives 1 and returns 1.

#include<stdio.h>

int f(int i) {

}

main ()

{ 

   printf("%d",f(f(f(f(f(1))))));

}

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

#include<stdio.h>

main ()

{ 
   static int i = 1;

   if(i--) {

   printf("%d ",i);

   main();

  }

}

A - 0

B - 0 infinite

C - Programs hangs with stack overflow

D - Compile error

Answer : A

Explanation

0, static variable retains its value between the function calls.

#include<stdio.h>

main ()

{ 
   static int i = 1;

   if(i--) {

   printf("%d ",i);

   main();

  }

}

Answer : B

Explanation

(b), printfs need arguments.

Answer : D

Explanation

(d), It compiles successfully and can’t be executed. Use gcc –c option to compile the same with command line compiler (UNIX/Linux) or just compile without build in an IDE.

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

#include<stdio.h>

main ()

{ 
   int *p = NULL;

   #undef NULL 

   if(p==NULL) printf("NULL"); 

   else printf("Nill");

}

A - NULL

B - Nill

C - Compile error

D - Runtime error

Answer : C

Explanation

Compile error, as the macro is undefined NULL is considered as undeclared.

#include<stdio.h>

main ()

{ 
   int *p = NULL;

   #undef NULL 

   if(p==NULL) printf("NULL"); 

   else printf("Nill");

}

Answer : B

Explanation

Prints date, __DATE__ is a compiler defined macro.

Answer Sheet

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