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.

Which of the following are unary operators in C?
1. !
2. sizeof
3. ~
4. &&

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

An operation with only one operand is called unary operation.
Unary operators:
! Logical NOT operator.
~ bitwise NOT operator.
sizeof Size-of operator.

&& Logical AND is a logical operator.

Therefore, 1, 2, 3 are unary operators.

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum


2.

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


3.

Will the program compile successfully?

#include<stdio.h>
#define X (4+Y)
#define Y (X+3)

int main()
{
    printf("%d\n", 4*X+2);
    return 0;
}

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Reports an error: Undefined symbol 'X'

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum


4.

Assume integer is 2 bytes wide. What will be the output of the following code?

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

int main()
{
    int (*p)[MAXCOL];
    p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
    printf("%d, %d\n", sizeof(p), sizeof(*p));
    return 0;
}

A.
2, 8
B.
4, 16
C.
8, 24
D.
16, 32

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Memory Allocation

Discuss about this problem : Discuss in Forum


5.

Will the program outputs "IndiaBIX.com"?

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

int main()
{
    char str1[] = "IndiaBIX.com";
    char str2[20];
    strncpy(str2, str1, 8);
    printf("%s", str2);
    return 0;
}

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

No. It will print something like 'IndiaBIX(some garbage values here)' .

Because after copying the first 8 characters of source string into target string strncpy() doesn't terminate the target string with a '\0'. So it may print some garbage values along with IndiaBIX.

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


6.

Point out the error in the program

#include<stdio.h>

int main()
{
    int i;
    #if A
        printf("Enter any number:");
        scanf("%d", &i);
    #elif B
        printf("The number is odd");
    return 0;
}

A.
Error: unexpected end of file because there is no matching #endif
B.
The number is odd
C.
Garbage values
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The conditional macro #if must have an #endif. In this program there is no #endif statement written.

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum


7.

Can we use a switch statement to switch on strings?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The cases in a switch must either have integer constants or constant expressions.

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


8.

Range of double is -1.7e-38 to 1.7e+38 (in 16 bit platform - Turbo C under DOS)

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

False, The range of double is -1.7e+308 to 1.7e+308.

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum


9.

What will be the output of the program?

#include<stdio.h>
int main()
{
    int x=4, y, z;
    y = --x;
    z = x--;
    printf("%d, %d, %d\n", x, y, z);
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Step 1: int x=4, y, z; here variable x, y, z are declared as an integer type and variable x is initialized to 4.
Step 2: y = --x; becomes y = 3; because (--x) is pre-decrement operator.
Step 3: z = x--; becomes z = 3;. In the next step variable x becomes 2, because (x--) is post-decrement operator.
Step 4: printf("%d, %d, %d\n", x, y, z); Hence it prints "2, 3, 3".

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum


10.

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


11.

In the following code what is 'P'?

typedef char *charp;
const charp P;

A.
P is a constant
B.
P is a character constant
C.
P is character type
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Typedef

Discuss about this problem : Discuss in Forum


12.

What will be the output of the program?

#include<stdio.h>

int main()
{
    int i;
    char c;
    for(i=1; i<=5; i++)
    {
        scanf("%c", &c); /* given input is 'a' */
        printf("%c", c);
        ungetc(c, stdin);
    }
    return 0;
}

A.
aaaa
B.
aaaaa
C.
Garbage value.
D.
Error in ungetc statement.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

for(i=1; i<=5; i++) Here the for loop runs 5 times.

Loop 1:
scanf("%c", &c); Here we give 'a' as input.
printf("%c", c); prints the character 'a' which is given in the previous "scanf()" statement.
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

Loop 2:
Here the scanf("%c", &c); get the input from "stdin" because of "ungetc" function.
printf("%c", c); Now variable c = 'a'. So it prints the character 'a'.
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

This above process will be repeated in Loop 3, Loop 4, Loop 5.

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


13.

What will be the output of the program ?

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

int main()
{
    char str1[20] = "Hello", str2[20] = " World";
    printf("%s\n", strcpy(str2, strcat(str1, str2)));
    return 0;
}

A.
Hello
B.
World
C.
Hello World
D.
WorldHello

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Step 1: char str1[20] = "Hello", str2[20] = " World"; The variable str1 and str2 is declared as an array of characters and initialized with value "Hello" and " World" respectively.

Step 2: printf("%s\n", strcpy(str2, strcat(str1, str2)));

=> strcat(str1, str2)) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains "Hello World".

=> strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2.

Hence it prints "Hello World".

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


14.

If the size of pointer is 4 bytes then What will be the output of the program ?

#include<stdio.h>

int main()
{
    char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"};
    printf("%d, %d", sizeof(str), strlen(str[0]));
    return 0;
}

A.
22, 4
B.
25, 5
C.
24, 5
D.
20, 2

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Step 1: char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"}; The variable str is declared as an pointer to the array of 6 strings.

Step 2: printf("%d, %d", sizeof(str), strlen(str[0]));

sizeof(str) denotes 6 * 4 bytes = 24 bytes. Hence it prints '24'

strlen(str[0])); becomes strlen(Frogs)). Hence it prints '5';

Hence the output of the program is 24, 5

Hint: If you run the above code in 16 bit platform (Turbo C under DOS) the output will be 12, 5. Because the pointer occupies only 2 bytes. If you run the above code in Linux (32 bit platform), the output will be 24, 5 (because the size of pointer is 4 bytes).

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


15.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    int i;
    char a[] = "\0";
    if(printf("%s", a))
        printf("The string is not empty\n");
    else
        printf("The string is empty\n");
    return 0;
}

A.
The string is not empty
B.
The string is empty
C.
No output
D.
0

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The function printf() returns the number of charecters printed on the console.

Step 1: char a[] = '\0'; The variable a is declared as an array of characters and it initialized with "\0". It denotes that the string is empty.

Step 2: if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero). Hence the if condition is failed.

In the else part it prints "The string is empty".

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


16.

If return type for a function is not specified, it defaults to int

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

True, The default return type for a function is int.

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


17.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    struct value
    {
        int bit1:1;
        int bit3:4;
        int bit4:4;
    }bit={1, 2, 13};

    printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Note the below statement inside the struct:

int bit1:1; --> 'int' indicates that it is a SIGNED integer.

For signed integers the leftmost bit will be taken for +/- sign.

If you store 1 in 1-bit field:

The left most bit is 1, so the system will treat the value as negative number.

The 2's complement method is used by the system to handle the negative values.

Therefore, the data stored is 1. The 2's complement of 1 is also 1 (negative).

Therefore -1 is printed.


If you store 2 in 4-bits field:

Binary 2: 0010 (left most bit is 0, so system will treat it as positive value)

0010 is 2

Therefore 2 is printed.


If you store 13 in 4-bits field:

Binary 13: 1101 (left most bit is 1, so system will treat it as negative value)

Find 2's complement of 1101:

1's complement of 1101 : 0010
2's complement of 1101 : 0011 (Add 1 to the result of 1's complement)

0011 is 3 (but negative value)

Therefore -3 is printed.

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


18.

Is it true that a function may have several declarations, but only one definition?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Yes, but the function declarations must be identical.

Example:

#include<stdio.h>

void Display();
void Display();
void Display();

void Display()
{
   printf("Weclome to IndiaBIX.com..!");
}

int main()
{
    Display();
    return 0;
}

//Output:
Weclome to IndiaBIX.com..!

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>
#define str(x) #x
#define Xstr(x) str(x)
#define oper multiply

int main()
{
    char *opername = Xstr(oper);
    printf("%s\n", opername);
    return 0;
}

A.
Error: in macro substitution
B.
Error: invalid reference 'x' in macro
C.
print 'multiply'
D.
No output

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The macro #define str(x) #x replaces the symbol 'str(x)' with 'x'.

The macro #define Xstr(x) str(x) replaces the symbol 'Xstr(x)' with 'str(x)'.

The macro #define oper multiply replaces the symbol 'oper' with 'multiply'.

Step 1: char *opername = Xstr(oper); The varible *opername is declared as an pointer to a character type.

=> Xstr(oper); becomes,

=> Xstr(multiply);

=> str(multiply)

=> char *opername = multiply

Step 2: printf("%s\n", opername); It prints the value of variable opername.

Hence the output of the program is "multiply"

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum


20.

If scanf() is used to store a value in a char variable then along with the value a carriage return(\r) also gets stored it.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

No, the carriage return tells the compiler to read the input from the buffer after ENTER key is pressed.

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


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