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>
#include<stdlib.h>

int main()
{
    int i=0;
    i++;
    if(i<=5)
    {
        printf("IndiaBIX");
        exit(1);
        main();
    }
    return 0;
}

A.
Prints "IndiaBIX" 5 times
B.
Function main() doesn't calls itself
C.
Infinite loop
D.
Prints "IndiaBIx"

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Step 1: int i=0; The variable i is declared as in integer type and initialized to '0'(zero).

Step 2: i++; Here variable i is increemented by 1. Hence i becomes '1'(one).

Step 3: if(i<=5) becomes if(1 <=5). Hence the if condition is satisfied and it enter into if block statements.

Step 4: printf("IndiaBIX"); It prints "IndiaBIX".

Step 5: exit(1); This exit statement terminates the program execution.

Hence the output is "IndiaBIx".

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum

2.

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

#include<stdio.h>
int main()
{
    int i=1;
    for(;;)
    {
        printf("%d\n", i++);
        if(i>10)
           break;
    }
    return 0;
}

A.
There should be a condition in the for loop
B.
The two semicolons should be dropped
C.
The for loop should be replaced with while loop.
D.
No error

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Step 1: for(;;) this statement will genereate infinite loop.
Step 2: printf("%d\n", i++); this statement will print the value of variable i and increement i by 1(one).
Step 3: if(i>10) here, if the variable i value is greater than 10, then the for loop breaks.

Hence the output of the program is
1
2
3
4
5
6
7
8
9
10

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum

3.

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

4.

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

5.

What will be the output of the program?

#include<stdio.h>

int main()
{
    const char *s = "";
    char str[] = "Hello";
    s = str;
    while(*s)
        printf("%c", *s++);

    return 0;
}

A.
Error
B.
H
C.
Hello
D.
Hel

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Step 1: const char *s = ""; The constant variable s is declared as an pointer to an array of characters type and initialized with an empty string.

Step 2: char str[] = "Hello"; The variable str is declared as an array of charactrers type and initialized with a string "Hello".

Step 3: s = str; The value of the variable str is assigned to the variable s. Therefore str contains the text "Hello".

Step 4: while(*s){ printf("%c", *s++); } Here the while loop got executed untill the value of the variable s is available and it prints the each character of the variable s.

Hence the output of the program is "Hello".

Learn more problems on : Const

Discuss about this problem : Discuss in Forum

6.

What will be the output of the program?

#include<stdio.h>
#define PRINT(int) printf("int=%d, ", int);

int main()
{
    int x=2, y=3, z=4;   
    PRINT(x);
    PRINT(y);
    PRINT(z);
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The macro PRINT(int) print("%d,", int); prints the given variable value in an integer format.

Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type and initialized to 2, 3, 4 respectively.

Step 2: PRINT(x); becomes printf("int=%d,",x). Hence it prints 'int=2'.

Step 3: PRINT(y); becomes printf("int=%d,",y). Hence it prints 'int=3'.

Step 4: PRINT(z); becomes printf("int=%d,",z). Hence it prints 'int=4'.

Hence the output of the program is int=2, int=3, int=4.

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum

7.

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

8.

Is the following declaration correct?
typedef *void (*pfun)(**int, *float);

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum

9.

What do the following declaration signify?

char **argv;

A.
argv is a pointer to pointer.
B.
argv is a pointer to a char pointer.
C.
argv is a function pointer.
D.
argv is a member of function pointer.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum

10.

What do the following declaration signify?

void (*cmp)();

A.
cmp is a pointer to an void function type.
B.
cmp is a void type pointer function.
C.
cmp is a function that return a void pointer.
D.
cmp is a pointer to a function which returns void .

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum

11.

What will be the output of the program (16-bit platform)?

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

int main()
{
    int *p;
    p = (int *)malloc(20);
    printf("%d\n", sizeof(p));
    free(p);
    return 0;
}

A.
4
B.
2
C.
8
D.
Garbage value

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Memory Allocation

Discuss about this problem : Discuss in Forum

12.

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

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

13.

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

14.

What will be the output of the program?

#include<stdio.h>

int main()
{
    printf("%d %d\n", 32<<1, 32<<0);
    printf("%d %d\n", 32<<-1, 32<<-0);
    printf("%d %d\n", 32>>1, 32>>0);
    printf("%d %d\n", 32>>-1, 32>>-0);
    return 0;
}

A.
Garbage values
B.
64 32
0 32
16 32
0 32
C.
All zeros
D.
8 0
0 0
32 0
0 16

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Bitwise Operators

Discuss about this problem : Discuss in Forum

15.

Is there any difference int the following declarations?
int fun(int arr[]);
int fun(int arr[2]);

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

No, both the statements are same. It is the prototype for the function fun() that accepts one integer array as an parameter and returns an integer value.

Learn more problems on : Arrays

Discuss about this problem : Discuss in Forum

16.

If a char is 1 byte wide, an integer is 2 bytes wide and a long integer is 4 bytes wide then will the following structure always occupy 7 bytes?

struct ex
{
    char ch;
    int i;
    long int a;
};

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

A compiler may leave holes in structures by padding the first char in the structure with another byte just to ensures that the integer that follows is stored at an location. Also, there might be 2extra bytes after the integer to ensure that the long integer is stored at an address, which is multiple of 4. Such alignment is done by machines to improve the efficiency of accessing values.

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum

17.

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

18.

What will be the output of the program?

#include<stdio.h>
int main()
{
    int i=3;
    switch(i)
    {
        case 1:
            printf("Hello\n");
        case 2:
            printf("Hi\n");
        case 3:
            continue;
        default:
            printf("Bye\n");
    }
    return 0;
}

A.
Error: Misplaced continue
B.
Bye
C.
No output
D.
Hello Hi

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The keyword continue cannot be used in switch case. It must be used in for or while or do while loop. If there is any looping statement in switch case then we can use continue.

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum

19.

In the following code snippet can we declare a new typedef named ptr even though struct employee has not been completely declared while using typedef?

typedef struct employee *ptr;
struct employee
{
    char name[20];
    int age;
    ptr next;
}

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

20.

Does there any function exist to convert the int or float to a string?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

1. itoa() converts an integer to a string.
2. ltoa() converts a long to a string.
3. ultoa() converts an unsigned long to a string.
4. sprintf() sends formatted output to a string, so it can be used to convert any type of values to string type.

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

int main(void)
{
   int   num1 = 12345;
   float num2 = 5.12;
   char str1[20];
   char str2[20];

   itoa(num1, str1, 10); /* 10 radix value */
   printf("integer = %d string = %s \n", num1, str1);

   sprintf(str2, "%f", num2);
   printf("float = %f string = %s", num2, str2);

   return 0;
}

// Output:
// integer = 12345 string = 12345
// float = 5.120000 string = 5.120000

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum

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

Have a question?

Ask your question now !