Online C Programming Test - C Programming Test - Random



Loading...  Loading Test...

Instruction:

  • This is a FREE online test. DO NOT pay money to anyone to attend this test.
  • 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>

int main()
{
    int a[] = {10, 20, 30, 40, 50};
    int j;
    for(j=0; j<5; j++)
    {
        printf("%d\n", a);
        a++;
    }
    return 0;
}

A.
Error: Declaration syntax
B.
Error: Expression syntax
C.
Error: LValue required
D.
Error: Rvalue required

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


2.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    FILE *fp;
    unsigned char ch;
     /* file 'abc.c' contains "This is IndiaBIX " */
    fp=fopen("abc.c", "r");
    if(fp == NULL)
    {
        printf("Unable to open file");
        exit(1);
    }
    while((ch=getc(fp)) != EOF)
        printf("%c", ch);

    fclose(fp);
    printf("\n", ch);
    return 0;
}

A.
This is IndiaBIX
B.
This is
C.
Infinite loop
D.
Error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The macro EOF means -1.

while((ch=getc(fp)) != EOF) Here getc function read the character and convert it to an integer value and store it in the variable ch, but it is declared as an unsigned char. So the while loop runs infinitely.

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum


3.

Is the following declaration acceptable?

typedef long no, *ptrtono;
no n;
ptrtono p;

A.
Yes
B.
NO

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Typedef

Discuss about this problem : Discuss in Forum


4.

Which of the structure is correct?
1 :
struct book
{
    char name[10];
    float price;
    int pages;
};
2 :
struct aa
{
    char name[10];
    float price;
    int pages;
}
3 :
struct aa
{
    char name[10];
    float price;
    int pages;
}

A.
1
B.
2
C.
3
D.
All of above

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

In 2 and 3 semicolon are missing in structure element.

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum


5.

Which of the following statements correct about the below program?

#include<stdio.h>

int main()
{
    struct emp
    {
        char name[25];
        int age;
        float sal;
    };
    struct emp e[2];
    int i=0;
    for(i=0; i<2; i++)
        scanf("%s %d %f", e[i].name, &e[i].age, &e[i].sal);

    for(i=0; i<2; i++)
        scanf("%s %d %f", e[i].name, e[i].age, e[i].sal);
    return 0;
}

A.
Error: scanf() function cannot be used for structures elements.
B.
The code runs successfully.
C.
Error: Floating point formats not linked Abnormal program termination.
D.
Error: structure variable must be initialized.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


6.

What will be the output of the program?

#include<stdio.h>
#include<math.h>
int main()
{
    printf("%f\n", sqrt(36.0));
    return 0;
}

A.
6.0
B.
6
C.
6.000000
D.
Error: Prototype sqrt() not found.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

printf("%f\n", sqrt(36.0)); It prints the square root of 36 in the float format(i.e 6.000000).

Declaration Syntax: double sqrt(double x) calculates and return the positive square root of the given number.

Learn more problems on : Floating Point Issues

Discuss about this problem : Discuss in Forum


7.

What will be the output of the program?

#include<stdio.h>
#include<math.h>
int main()
{
    float n=1.54;
    printf("%f, %f\n", ceil(n), floor(n));
    return 0;
}

A.
2.000000, 1.000000
B.
1.500000, 1.500000
C.
1.550000, 2.000000
D.
1.000000, 2.000000

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

ceil(x) round up the given value. It finds the smallest integer not < x.
floor(x) round down the given value. It finds the smallest integer not > x.

printf("%f, %f\n", ceil(n), floor(n)); In this line ceil(1.54) round up the 1.54 to 2 and floor(1.54) round down the 1.54 to 1.

In the printf("%f, %f\n", ceil(n), floor(n)); statement, the format specifier "%f %f" tells output to be float value. Hence it prints 2.000000 and 1.000000.

Learn more problems on : Floating Point Issues

Discuss about this problem : Discuss in Forum


8.

Left shifting a number by 1 is always equivalent to multiplying it by 2.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

0001 => 1
0010 => 2
0100 => 4
1000 => 8

Learn more problems on : Bitwise Operators

Discuss about this problem : Discuss in Forum


9.

Does the data type of all elements in the union will be same.

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.

What will be the output of the program?

#include<stdio.h>

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

A.
10
B.
11
C.
No output
D.
Error: ++needs a value

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

This program will show an error "Cannot modify a const object".

Step 1: const int i=0; The constant variable 'i' is declared as an integer and initialized with value of '0'(zero).

Step 2: printf("%d\n", i++); Here the variable 'i' is increemented by 1(one). This will create an error "Cannot modify a const object".

Because, we cannot modify a const variable.

Learn more problems on : Const

Discuss about this problem : Discuss in Forum


11.

Range of float id -2.25e+308 to 2.25e+308

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

False, The range of float is -3.4e+38 to 3.4e+38.

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum


12.

How will you free the memory allocated by the following program?

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int **p, i, j;
    p = (int **) malloc(MAXROW * sizeof(int*));
    return 0;
}

A.
memfree(int p);
B.
dealloc(p);
C.
malloc(p, 0);
D.
free(p);

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Memory Allocation

Discuss about this problem : Discuss in Forum


13.

What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three

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

int main(int argc, char *argv[])
{
    int i;
    for(i=1; i<argc; i++)
        printf("%c", argv[i][0]);
    return 0;
}

A.
oot
B.
ott
C.
nwh
D.
eoe

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum


14.

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


15.

What will be the output of the program?

#include<stdio.h>
#include<math.h>

int main()
{
    float i = 2.5;
    printf("%f, %d", floor(i), ceil(i));
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Both ceil() and floor() return the integer found as a double.

floor(2.5) returns the largest integral value(round down) that is not greater than 2.5. So output is 2.000000.

ceil(2.5) returns 3, while converting the double to int it returns '0'.
So, the output is '2.000000, 0'.

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


16.

What will be the output of the program ?

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

int main()
{
    char str1[5], str2[5];
    int i;
    gets(str1);
    gets(str2);
    i = strcmp(str1, str2);
    printf("%d\n", i);
    return 0;
}

A.
Unpredictable integer value
B.
0
C.
-1
D.
Error

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

gets() gets collects a string of characters terminated by a new line from the standard input stream stdin.

The gets(str1) read the input string from user and store in variable str1.

The gets(str2) read the input string from user and store in variable str2.

The code i = strcmp(str1, str2); The strcmp not only returns -1, 0 and +1, but also other negative or positive values. So the value of i is "unpredictable integer value".

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

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


17.

The maximum combined length of the command-line arguments including the spaces between adjacent arguments is

A.
128 characters
B.
256 characters
C.
67 characters
D.
It may vary from one operating system to another

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum


18.

Preprocessor directive #undef can be used only on a macro that has been #define earlier

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

True, #undef can be used only on a macro that has been #define earlier

Example: #define PI 3.14

We can undefine PI macro by #undef PI

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum


19.

Point out the error in the program?

struct emp
{
    int ecode;
    struct emp *e;
};

A.
Error: in structure declaration
B.
Linker Error
C.
No Error
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

This type of declaration is called as self-referential structure. Here *e is pointer to a struct emp.

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


20.

Declare the following statement?
"An array of three pointers to chars".

A.
char *ptr[3]();
B.
char *ptr[3];
C.
char (*ptr[3])();
D.
char **ptr[3];

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum


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