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.

What will be the output of the program?

#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%d\n", FUN(va1, 2));
    return 0;
}

A.
10
B.
20
C.
1020
D.
12

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The following program will make you understand about ## (macro concatenation) operator clearly.

#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int First  	= 10;
    int Second  = 20;

    char FirstSecond[] = "IndiaBIX";

    printf("%s\n", FUN(First, Second) );

    return 0;
}

Output:
-------
IndiaBIX

The preprocessor will replace FUN(First, Second) as FirstSecond.

Therefore, the printf("%s\n", FUN(First, Second) ); statement will become as printf("%s\n", FirstSecond );

Hence it prints IndiaBIX as output.

Like the same, the line printf("%d\n", FUN(va1, 2)); given in the above question will become as printf("%d\n", va12 );.

Therefore, it prints 20 as output.

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum

2.

How many bytes of memory will the following code reserve?

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p;
    p = (int *)malloc(256 * 256);
    if(p == NULL)
        printf("Allocation failed");
    return 0;
}

A.
65536
B.
Allocation failed
C.
Error
D.
No output

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Hence 256*256 = 65536 is passed to malloc() function which can allocate upto 65535. So the memory allocation will be failed in 16 bit platform (Turbo C in DOS).

If you compile the same program in 32 bit platform like Linux (GCC Compiler) it may allocate the required memory.

Learn more problems on : Memory Allocation

Discuss about this problem : Discuss in Forum

3.

According to ANSI specifications which is the correct way of declaring main when it receives command-line arguments?

A.
int main(int argc, char *argv[])
B.
int main(argc, argv)
int argc; char *argv;
C.
int main()
{
    int argc; char *argv;
}
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum

4.

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

#include<stdio.h>

int main()
{
    struct value
    {
        int bit1:1;
        int bit3:4;
        int bit4:4;
    }bit;
    printf("%d\n", sizeof(bit));
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Since C is a compiler dependent language, in Turbo C (DOS) the output will be 2, but in GCC (Linux) the output will be 4.

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum

5.

Point out the error if any in the following program (Turbo C).

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

int main()
{
    display(4, 'A', 'a', 'b', 'c');
    return 0;
}
void display(int num, ...)
{
    char c; int j;
    va_list ptr;
    va_start(ptr, num);
    for(j=1; j<=num; j++)
    {
        c = va_arg(ptr, char);
        printf("%c", c);
    }
}

A.
Error: unknown variable ptr
B.
Error: Lvalue required for parameter
C.
No error and print A a b c
D.
No error and print 4 A a b c

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum

6.

In the following program add a statement in the function fun() such that address of a gets stored in j?

#include<stdio.h>
int main()
{
    int *j;
    void fun(int**);
    fun(&j);
    return 0;
}
void fun(int **k)
{
    int a=10;
    /* Add a statement here */
}

A.
**k=a;
B.
k=&a;
C.
*k=&a
D.
&k=*a

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum

7.

What will be the output of the program?

#include<stdio.h>
int main()
{
    int i=-3, j=2, k=0, m;
    m = ++i || ++j && ++k;
    printf("%d, %d, %d, %d\n", i, j, k, m);
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.

Step 2: m = ++i || ++j && ++k; here (++j && ++k;) this code will not get executed because ++i has non-zero value.
becomes m = -2 || ++j && ++k;
becomes m = TRUE || ++j && ++k; Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.

Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of variable 'i' only increemented by '1'(one). The variable j,k are not increemented.

Hence the output is "-2, 2, 0, 1".

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum

8.

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

9.

It is not necessary to typecast the address returned by malloc().

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum

10.

Names of functions in two different files linked together must be unique

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

True, If two function are declared in a same name, it gives "Error: Multiple declaration of function_name())".

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum

11.

The macro va_start is used to initialise a pointer to the beginning of the list of fixed arguments.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum

12.

va_list is an array that holds information needed by va_arg and va_end

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

13.

Point out the error in the program?

#include<stdio.h>

int main()
{
    struct emp
    {
        char name[25];
        int age;
        float bs;
    };
    struct emp e;
    e.name = "Suresh";
    e.age = 25;
    printf("%s %d\n", e.name, e.age);
    return 0;
}

A.
Error: Lvalue required/incompatible types in assignment
B.
Error: invalid constant expression
C.
Error: Rvalue required
D.
No error, Output: Suresh 25

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

We cannot assign a string to a struct variable like e.name = "Suresh"; in C.

We have to use strcpy(char *dest, const char *source) function to assign a string.

Ex: strcpy(e.name, "Suresh");

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum

14.

What will be the output of the program?

#include<stdio.h>

int addmult(int ii, int jj)
{
    int kk, ll;
    kk = ii + jj;
    ll = ii * jj;
    return (kk, ll);
}

int main()
{
    int i=3, j=4, k, l;
    k = addmult(i, j);
    l = addmult(i, j);
    printf("%d %d\n", k, l);
    return 0;
}

A.
12 12
B.
No error, No output
C.
Error: Compile error
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum

15.

What will the function rewind() do?

A.
Reposition the file pointer to a character reverse.
B.
Reposition the file pointer stream to end of file.
C.
Reposition the file pointer to begining of that line.
D.
Reposition the file pointer to begining of file.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

rewind() takes the file pointer to the beginning of the file. so that the next I/O operation will take place at the beginning of the file.
Example: rewind(FilePointer);

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum

16.

While defining a variable argument list function we drop the ellipsis(...)?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum

17.

Will the program compile successfully?

#include<stdio.h>

int main()
{
    #ifdef NOTE
        int a;
        a=10;
    #else
        int a;
        a=20;
    #endif
    printf("%d\n", a);
    return 0;
}

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Yes, this program will compile and run successfully and prints 20.

The macro #ifdef NOTE evaluates the given expression to 1. If satisfied it executes the #ifdef block statements. Here #ifdef condition fails because the Macro NOTE is nowhere declared.

Hence the #else block gets executed, the variable a is declared and assigned a value of 20.

printf("%d\n", a); It prints the value of variable a 20.

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum

18.

Point out the error in the program?

#include<stdio.h>

int main()
{
    FILE *fp;
    fp=fopen("trial", "r");
    fseek(fp, "20", SEEK_SET);
    fclose(fp);
    return 0;
}

A.
Error: unrecognised Keyword SEEK_SET
B.
Error: fseek() long offset value
C.
No error
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Instead of "20" use 20L since fseek() need a long offset value.

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum

19.

Which of the following sentences are correct about a switch loop in a C program?
1: switch is useful when we wish to check the value of variable against a particular set of values.
2: switch is useful when we wish to check whether a value falls in different ranges.
3: Compiler implements a jump table for cases used in switch.
4: It is not necessary to use a break in every switch statement.

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum

20.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    void fun(int, int[]);
    int arr[] = {1, 2, 3, 4};
    int i;
    fun(4, arr);
    for(i=0; i<4; i++)
        printf("%d,", arr[i]);
    return 0;
}
void fun(int n, int arr[])
{
    int *p=0;
    int i=0;
    while(i++ < n)
        p = &arr[i];
    *p=0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Step 1: void fun(int, int[]); This prototype tells the compiler that the function fun() accepts one integer value and one array as an arguments and does not return anything.

Step 2: int arr[] = {1, 2, 3, 4}; The variable a is declared as an integer array and it is initialized to

a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4

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

Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function.

Step 5: for(i=0; i<4; i++) { printf("%d,", arr[i]); } The for loop runs untill the variable i is less than '4' and it prints the each value of array a.

Hence the output of the program is 1,2,3,4

Learn more problems on : Arrays

Discuss about this problem : Discuss in Forum

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