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 program?

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

int main()
{
    unsigned char;
    FILE *fp;
    fp=fopen("trial", "r");
    if(!fp)
    {
        printf("Unable to open file");
        exit(1);
    }
    fclose(fp);
    return 0;
}

A.
Error: in unsigned char statement
B.
Error: unknown file pointer
C.
No error
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

This program tries to open the file trial.txt in read mode. If file not exists or unable to read it prints "Unable to open file" and then terminate the program.

If file exists, it simply close the file and then terminates the program.

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum

2.

Point out the error in the following program.

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

int main()
{
    char *ptr;
    *ptr = (char)malloc(30);
    strcpy(ptr, "RAM");
    printf("%s", ptr);
    free(ptr);
    return 0;
}

A.
Error: in strcpy() statement.
B.
Error: in *ptr = (char)malloc(30);
C.
Error: in free(ptr);
D.
No error

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Answer: ptr = (char*)malloc(30);

Learn more problems on : Memory Allocation

Discuss about this problem : Discuss in Forum

3.

Bitwise & can be used to check if a bit in number is set or not.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Bitwise Operators

Discuss about this problem : Discuss in Forum

4.

In C all functions except main() can be called recursively.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Any function including main() can be called recursively.

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum

5.

Will it result in to an error if a header file is included twice?

A.
Yes
B.
No
C.
It is compiler dependent.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Unless the header file has taken care to ensure that if already included it doesn't get included again.

Turbo C, GCC compilers would take care of these problems, generate no error.

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum

6.

Point out the error in the program.

#include<stdio.h>
#define MAX 128

int main()
{
    char mybuf[] = "India";
    char yourbuf[] = "BIX";
    char *const ptr = mybuf;
    *ptr = 'a';
    ptr = yourbuf;
    return 0;
}

A.
Error: unknown pointer conversion
B.
Error: cannot convert ptr const value
C.
No error
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Step 1: char mybuf[] = "India"; The variable mybuff is declared as an array of characters and initialized with string "India".

Step 2: char yourbuf[] = "BIX"; The variable yourbuf is declared as an array of characters and initialized with string "BIX".

Step 3: char *const ptr = mybuf; Here, ptr is a constant pointer, which points at a char.

The value at which ptr it points is not a constant; it will not be an error to modify the pointed character; There will be an error only to modify the pointer itself.

Step 4: *ptr = 'a'; The value of ptr is assigned to 'a'.

Step 5: ptr = yourbuf; Here, we are changing the pointer itself, this will result in the error "cannot modify a const object".

Learn more problems on : Const

Discuss about this problem : Discuss in Forum

7.

Will the following declaration work?

typedef struct s
{
    int a;
    float b;
}s;

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum

8.

Declare the following statement?
"A pointer to a function which receives an int pointer and returns float pointer".

A.
float *(ptr)*int;
B.
float *(*ptr)(int)
C.
float *(*ptr)(int*)
D.
float (*ptr)(int)

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum

9.

In the expression a=b=5 the order of Assignment is NOT decided by Associativity of operators

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The equal to = operator has Right-to-Left Associativity. So it assigns b=5 then a=b.

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum

10.

Point out the error in the following program.

#include<stdio.h>
int main()
{
    struct emp
    {
        char name[20];
        float sal;
    };
    struct emp e[10];
    int i;
    for(i=0; i<=9; i++)
        scanf("%s %f", e[i].name, &e[i].sal);
    return 0;
}

A.
Suspicious pointer conversion
B.
Floating point formats not linked (Run time error)
C.
Cannot use scanf() for structures
D.
Strings cannot be nested inside structures

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Compile and Run the above program in Turbo C:

C:\>myprogram.exe
Sundar
2555.50
scanf : floating point formats not linked
Abnormal program termination

The program terminates abnormally at the time of entering the float value for e[i].sal.

Solution:

Just add the following function in your program. It will force the compiler to include required libraries for handling floating point linkages.

static void force_fpf() /* A dummy function */
{
    float x, *y; /* Just declares two variables */
    y = &x;      /* Forces linkage of FP formats */
    x = *y;      /* Suppress warning message about x */
}

Learn more problems on : Floating Point Issues

Discuss about this problem : Discuss in Forum

11.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"};
    int i;
    char *t;
    t = names[3];
    names[3] = names[4];
    names[4] = t;
    for(i=0; i<=4; i++)
        printf("%s,", names[i]);
    return 0;
}

A.
Suresh, Siva, Sona, Baiju, Ritu
B.
Suresh, Siva, Sona, Ritu, Baiju
C.
Suresh, Siva, Baiju, Sona, Ritu
D.
Suresh, Siva, Ritu, Sona, Baiju

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Step 1: char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"}; The variable names is declared as an pointer to a array of strings.

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

Step 3: char *t; The variable t is declared as pointer to a string.

Step 4: t = names[3]; names[3] = names[4]; names[4] = t; These statements the swaps the 4 and 5 element of the array names.

Step 5: for(i=0; i<=4; i++) printf("%s,", names[i]); These statement prints the all the value of the array names.

Hence the output of the program is "Suresh, Siva, Sona, Ritu, Baiju".

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum

12.

Which of the following statements correct about k used in the below statement?
char ****k;

A.
k is a pointer to a pointer to a pointer to a char
B.
k is a pointer to a pointer to a pointer to a pointer to a char
C.
k is a pointer to a char pointer
D.
k is a pointer to a pointer to a char

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum

13.

Which of the statements is correct about the program?

#include<stdio.h>

int main()
{
    int arr[3][3] = {1, 2, 3, 4};
    printf("%d\n", *(*(*(arr))));
    return 0;
}

A.
Output: Garbage value
B.
Output: 1
C.
Output: 3
D.
Error: Invalid indirection

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum

14.

What will be the output of the program?

#include<stdio.h>

int main()
{
    int y=128;
    const int x=y;
    printf("%d\n", x);
    return 0;
}

A.
128
B.
Garbage value
C.
Error
D.
0

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Step 1: int y=128; The variable 'y' is declared as an integer type and initialized to value "128".

Step 2: const int x=y; The constant variable 'x' is declared as an integer and it is initialized with the variable 'y' value.

Step 3: printf("%d\n", x); It prints the value of variable 'x'.

Hence the output of the program is "128"

Learn more problems on : Const

Discuss about this problem : Discuss in Forum

15.

malloc() returns a NULL if it fails to allocate the requested memory.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Memory Allocation

Discuss about this problem : Discuss in Forum

16.

Point out the error, if any in the while loop.

#include<stdio.h>
int main()
{
    void fun();
    int i = 1;
    while(i <= 5)
    {
        printf("%d\n", i);
        if(i>2)
            goto here;
    }
return 0;
}
void fun()
{
    here:
    printf("It works");
}

A.
No Error: prints "It works"
B.
Error: fun() cannot be accessed
C.
Error: goto cannot takeover control to other function
D.
No error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

A label is used as the target of a goto statement, and that label must be within the same function as the goto statement.

Syntax: goto <identifier> ;
Control is unconditionally transferred to the location of a local label specified by <identifier>.
Example:


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

Output: 1,2,3,4

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum

17.

What will be the output of the program?

#include<stdio.h>
int main()
{
    int x = 3;
    float y = 3.0;
    if(x == y)
        printf("x and y are equal");
    else
        printf("x and y are not equal");
    return 0;
}

A.
x and y are equal
B.
x and y are not equal
C.
Unpredictable
D.
No output

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Step 1: int x = 3; here variable x is an integer type and initialized to '3'.
Step 2: float y = 3.0; here variable y is an float type and initialized to '3.0'
Step 3: if(x == y) here we are comparing if(3 == 3.0) hence this condition is satisfied.
Hence it prints "x and y are equal".

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum

18.

What is the output of the program given below ?

#include<stdio.h>
int main()
{
    enum status { pass, fail, atkt};
    enum status stud1, stud2, stud3;
    stud1 = pass;
    stud2 = atkt;
    stud3 = fail;
    printf("%d, %d, %d\n", stud1, stud2, stud3);
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

enum takes the format like {0,1,2..) so pass=0, fail=1, atkt=2

stud1 = pass (value is 0)

stud2 = atkt (value is 2)

stud3 = fail (value is 1)

Hence it prints 0, 2, 1

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum

19.

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

20.

What will be the output of the program in Turb C (under DOS)?

#include<stdio.h>

int main()
{
    int arr[5], i=0;
    while(i<5)
        arr[i]=++i;

    for(i=0; i<5; i++)
        printf("%d, ", arr[i]);

    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the TurboC Compiler (Windows) output.

Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better.

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.