Online C Programming Test - C Programming Test - Random



Loading...  Loading Test...

Instruction:

  • Total number of questions : 20.
  • Time alloted : 30 minutes.
  • Each question carry 1 mark, no negative marks.
  • DO NOT refresh the page.
  • All the best :-).


1.

Point out the error in the following program.

#include<stdio.h>
#include<stdarg.h>
fun(...);

int main()
{
    fun(3, 7, -11.2, 0.66);
    return 0;
}
fun(...)
{
    va_list ptr;
    int num;
    va_start(ptr, n);
    num = va_arg(ptr, int);
    printf("%d", num);
}

A.
Error: fun() needs return type
B.
Error: ptr Lvalue required
C.
Error: Invalid declaration of fun(...)
D.
No error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

There is no fixed argument in the definition fun()

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum


2.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    struct byte
    {
        int one:1;
    };
    struct byte var = {1};
    printf("%d\n", var.one);
    return 0;
}

A.
1
B.
-1
C.
0
D.
Error

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


3.

What will be the output of the program?

#include<stdio.h>
void fun(int);
typedef int (*pf) (int, int);
int proc(pf, int, int);

int main()
{
    int a=3;
    fun(a);
    return 0;
}
void fun(int n)
{
    if(n > 0)
    {
        fun(--n);
        printf("%d,", n);
        fun(--n);
    }
}

A.
0, 2, 1, 0,
B.
1, 1, 2, 0,
C.
0, 1, 0, 2,
D.
0, 1, 2, 0,

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


4.

What will be the output of the program?

#include<stdio.h>
int i;
int fun();

int main()
{
    while(i)
    {
        fun();
        main();
    }
    printf("Hello\n");
    return 0;
}
int fun()
{
    printf("Hi");
}

A.
Hello
B.
Hi Hello
C.
No output
D.
Infinite loop

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Step 1: int i; The variable i is declared as an integer type.

Step 1: int fun(); This prototype tells the compiler that the function fun() does not accept any arguments and it returns an integer value.

Step 1: while(i) The value of i is not initialized so this while condition is failed. So, it does not execute the while block.

Step 1: printf("Hello\n"); It prints "Hello".

Hence the output of the program is "Hello".

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


5.

What will be the output of the program in DOS (Compiler - Turbo C)?

#include<stdio.h>
double i;

int main()
{
    (int)(float)(char) i;
    printf("%d", sizeof((int)(float)(char)i));
    return 0;
}

A.
1
B.
2
C.
4
D.
8

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Due to the C language is being platform dependent:

In Turbo C (DOS - 16 bit platform), the output will be 2.

But in GCC (Unix/Linux - 32 bit platform), the output will be 4.

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum


6.

What will be the output of the program (sample.c) given below if it is executed from the command line (turbo c under DOS)?
cmd> sample Good Morning

/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("%d %s", argc, argv[1]);
    return 0;
}

A.
3 Good
B.
2 Good
C.
Good Morning
D.
3 Morning

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum


7.

What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample friday tuesday sunday

/* sample.c */
#include<stdio.h>

int main(int sizeofargv, char *argv[])
{
    while(sizeofargv)
        printf("%s", argv[--sizeofargv]);
    return 0;
}

A.
sample friday tuesday sunday
B.
sample friday tuesday
C.
sunday tuesday friday sample
D.
sunday tuesday friday

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum


8.

Point out the correct statements about the program?

#include<stdio.h>

int main()
{
    FILE *fptr;
    char str[80];
    fptr = fopen("f1.dat", "w");
    if(fptr == NULL)
        printf("Cannot open file");
    else
    {
        while(strlen(gets(str))>0)
        {
            fputs(str, fptr);
            fputs("\n", fptr);
        }
        fclose(fptr);
    }
    return 0;
}

A.
The code copies the content of one file to another
B.
The code writes strings that are read from the keyboard into a file.
C.
The code reads a file
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

This program get the input string from the user through gets function and store it in the file f1.txt using fputs function.

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum


9.

Which of the following statement is correct about the program?

#include<stdio.h>

int main()
{
    FILE *fp;
    char str[11], ch;
    int i=0;
    fp = fopen("INPUT.TXT", "r");
    while((ch=getc(fp))!=EOF)
    {
        if(ch == '\n' || ch == ' ')
        {
            str[i]='\0';
            strrev(str);
            printf("%s", str);
            i=0;
        }
        else
            str[i++]=ch;
    }
    fclose(fp);
    return 0;
}

A.
The code writes a text to a file
B.
The code reads a text files and display its content in reverse order
C.
The code writes a text to a file in reverse order
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

This program reads the file INPUT.TXT and store it in the string str after reversing the string using strrev function.

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum


10.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    static int a[2][2] = {1, 2, 3, 4};
    int i, j;
    static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
    for(i=0; i<2; i++)
    {
        for(j=0; j<2; j++)
        {
            printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i), 
                                    *(*(i+p)+j), *(*(p+j)+i));
        }
    }
    return 0;
}

A.
1, 1, 1, 1
2, 3, 2, 3
3, 2, 3, 2
4, 4, 4, 4
B.
1, 2, 1, 2
2, 3, 2, 3
3, 4, 3, 4
4, 2, 4, 2
C.
1, 1, 1, 1
2, 2, 2, 2
2, 2, 2, 2
3, 3, 3, 3
D.
1, 2, 3, 4
2, 3, 4, 1
3, 4, 1, 2
4, 1, 2, 3

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Arrays

Discuss about this problem : Discuss in Forum


11.

What will be the output of the program?

#include<stdio.h>

int main()
{
    int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
    int *p, *q;
    p = &arr[1][1][1];
    q = (int*) arr;
    printf("%d, %d\n", *p, *q);
    return 0;
}

A.
8, 10
B.
10, 2
C.
8, 1
D.
Garbage values

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


12.

Point out the error in the program?

#include<stdio.h>

int main()
{
    struct bits
    {
        float f:2;
    }bit;

    printf("%d\n", sizeof(bit));
    return 0;
}

A.
4
B.
2
C.
Error: cannot set bit field for float
D.
Error: Invalid member access in structure

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


13.

What will be the output of the program in 16 bit platform (Turbo C under DOS)?

#include<stdio.h>
int main()
{
    extern int i;
    i = 20;
    printf("%d\n", sizeof(i));
    return 0;
}

A.
2
B.
4
C.
vary from compiler
D.
Linker Error : Undefined symbol 'i'

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Linker Error : Undefined symbol 'i'
The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name 'i' is available in any other program with memory space allocated for it. Hence a linker error has occurred.

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum


14.

If a function contains two return statements successively, the compiler will generate warnings. Yes/No ?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Yes. If a function contains two return statements successively, the compiler will generate "Unreachable code" warnings.

Example:


#include<stdio.h>
int mul(int, int); /* Function prototype */

int main()
{
    int a = 4, b = 3, c;
    c = mul(a, b);
    printf("c = %d\n", c);
    return 0;
}
int mul(int a, int b)
{
   return (a * b);
   return (a - b); /* Warning: Unreachable code */
}

Output:
c = 12

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


15.

Which of the following statements are FALSE about the below code?

int main(int ac, char *av[])
{
}

A.
ac contains count of arguments supplied at command-line
B.
av[] contains addresses of arguments supplied at a command line
C.
In place of ac and av, argc and argv should be used.
D.
The variables ac and av are always local to main()

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum


16.

What will you do to treat the constant 3.14 as a long double?

A.
use 3.14LD
B.
use 3.14L
C.
use 3.14DL
D.
use 3.14LF

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Given 3.14 is a double constant.

To specify 3.14 as long double, we have to add L to the 3.14. (i.e 3.14L)

Learn more problems on : Floating Point Issues

Discuss about this problem : Discuss in Forum


17.

What will be the output of the program?

#include<stdio.h>
#include<stdarg.h>
void dumplist(int, ...);

int main()
{
    dumplist(2, 4, 8);
    dumplist(3, 6, 9, 7);
    return 0;
}
void dumplist(int n, ...)
{
    va_list p; int i;
    va_start(p, n);

    while(n-->0)
    {
        i = va_arg(p, int);
        printf("%d", i);
    }
    va_end(p);
    printf("\n");
}

A.
2 4
3 6
B.
2 4 8
3, 6, 9, 7
C.
4 8
6 9 7
D.
1 1 1
1 1 1 1

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum


18.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    float a=3.15529;
    printf("%2.1f\n", a);
    return 0;
}

A.
3.00
B.
3.15
C.
3.2
D.
3

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

float a=3.15529; The variable a is declared as an float data type and initialized to value 3.15529;

printf("%2.1f\n", a); The precision specifier tells .1f tells the printf function to place only one number after the .(dot).

Hence the output is 3.2

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum


19.

Which standard library function will you use to find the last occurance of a character in a string in C?

A.
strnchar()
B.
strchar()
C.
strrchar()
D.
strrchr()

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

strrchr() returns a pointer to the last occurrence of character in a string.

Example:


#include <stdio.h>
#include <string.h>

int main()
{
    char str[30] = "12345678910111213";
    printf("The last position of '2' is %d.\n",
            strrchr(str, '2') - str);
    return 0;
}

Output: The last position of '2' is 14.

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


20.

A function that receives variable number of arguments should use va_arg() to extract arguments from the variable argument list.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum


Submit your test now to view the Results and Statistics with answer explanation.