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.

The library function used to find the last occurrence of a character in a string is

A.
strnstr()
B.
laststr()
C.
strrchr()
D.
strstr()

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Declaration: char *strrchr(const char *s, int c);

It scans a string s in the reverse direction, looking for a specific character c.

Example:

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

int main(void)
{
   char text[] = "I learn through IndiaBIX.com";
   char *ptr, c = 'i';

   ptr = strrchr(text, c);
   if (ptr)
      printf("The position of '%c' is: %d\n", c, ptr-text);
   else
      printf("The character was not found\n");
   return 0;
}

Output:

The position of 'i' is: 19

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


2.

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


3.

How many bytes are occupied by near, far and huge pointers (DOS)?

A.
near=2 far=4 huge=4
B.
near=4 far=8 huge=8
C.
near=2 far=4 huge=8
D.
near=4 far=4 huge=8

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every pointers is 4 bytes long.

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


4.

What will be the output of the program?

#include<stdio.h>
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
int main()
{
    char *str1="India";
    char *str2="BIX";
    JOIN(str1, str2);
    return 0;
}

A.
str1=IndiaBIX str2=BIX
B.
str1=India str2=BIX
C.
str1=India str2=IndiaBIX
D.
Error: in macro substitution

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum


5.

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


6.

Is there easy way to print enumeration values symbolically?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

You can write a function of your own to map an enumeration constant to a string.

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


7.

In the following code, the P2 is Integer Pointer or Integer?

typedef int *ptr;
ptr p1, p2;

A.
Integer
B.
Integer pointer
C.
Error in declaration
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Typedef

Discuss about this problem : Discuss in Forum


8.

What will be the output of the program?

#include<stdio.h>

int main()
{
    int i;
    i = printf("How r u\n");
    i = printf("%d\n", i);
    printf("%d\n", i);
    return 0;
}

A.
How r u
7
2
B.
How r u
8
2
C.
How r u
1
1
D.
Error: cannot assign printf to variable

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

In the program, printf() returns the number of charecters printed on the console

i = printf("How r u\n"); This line prints "How r u" with a new line character and returns the length of string printed then assign it to variable i.
So i = 8 (length of '\n' is 1).

i = printf("%d\n", i); In the previous step the value of i is 8. So it prints "8" with a new line character and returns the length of string printed then assign it to variable i. So i = 2 (length of '\n' is 1).

printf("%d\n", i); In the previous step the value of i is 2. So it prints "2".

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


9.

A function cannot be defined inside another function

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

A function cannot be defined inside the another function, but a function can be called inside a another function.

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


10.

Point out the error in the program?

#include<stdio.h>

int main()
{
    struct bits
    {
        float f:2;
    }bit;

    printf("%d\n", sizeof(bit));
    return 0;
}

A.
4
B.
2
C.
Error: cannot set bit field for float
D.
Error: Invalid member access in structure

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


11.

What will be the output of the program?

#include<stdio.h>
int check(int);
int main()
{
    int i=45, c;
    c = check(i);
    printf("%d\n", c);
    return 0;
}
int check(int ch)
{
    if(ch >= 45)
        return 100;
    else
        return 10;
}

A.
100
B.
10
C.
1
D.
0

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Step 1: int check(int); This prototype tells the compiler that the function check() accepts one integer parameter and returns an integer value.

Step 2: int l=45, c; The variable i and c are declared as an integer type and i is initialized to 45.

The function check(i) return 100 if the given value of variable i is >=(greater than or equal to) 45, else it will return 10.

Step 3: c = check(i); becomes c = check(45); The function check() return 100 and it get stored in the variable c.(c = 100)

Step 4: printf("%d\n", c); It prints the value of variable c.

Hence the output of the program is '100'.

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


12.

The modulus operator cannot be used with a long double.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

fmod(x,y) - Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs floating point or long double divisions.

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


13.

Input/output function prototypes and macros are defined in which header file?

A.
conio.h
B.
stdlib.h
C.
stdio.h
D.
dos.h

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

stdio.h, which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations.

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


14.

What are the types of linkages?

A.
Internal and External
B.
External, Internal and None
C.
External and None
D.
Internal

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

External Linkage-> means global, non-static variables and functions.
Internal Linkage-> means static variables and functions with file scope.
None Linkage-> means Local variables.

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum


15.

Which of the following statements are correct about the below program?

#include<stdio.h>
int main()
{
    int i = 0;
    i++;
    if(i <= 5)
    {
        printf("IndiaBIX\n");
        exit(0);
        main();
    }
    return 0;
}

A.
The program prints 'IndiaBIX' 5 times
B.
The program prints 'IndiaBIX' one time
C.
The call to main() after exit() doesn't materialize.
D.
The compiler reports an error since main() cannot call itself.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Step 1: int i = 0; here variable i is declared as an integer type and initialized to '0'(zero).
Step 2: i++; here variable i is increemented by 1(one). Hence, i = 1
Step 3: if(i <= 5) becomes if(1 <= 5) here we are checking '1' is less than or equal to '5'. Hence the if condition is satisfied.
Step 4: printf("IndiaBIX\n"); It prints "IndiaBIX"
Step 5: exit(); terminates the program execution.

Hence the output is "IndiaBIX".

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


16.

How many times the program will print "IndiaBIX" ?

#include<stdio.h>

int main()
{
    printf("IndiaBIX");
    main();
    return 0;
}

A.
Infinite times
B.
32767 times
C.
65535 times
D.
Till stack overflows

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

A call stack or function stack is used for several related purposes, but the main reason for having one is to keep track of the point to which each active subroutine should return control when it finishes executing.

A stack overflow occurs when too much memory is used on the call stack.

Here function main() is called repeatedly and its return address is stored in the stack. After stack memory is full. It shows stack overflow error.

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


17.

Point out the error in the following program.

#include<stdio.h>
int main()
{
    int (*p)() = fun;
    (*p)();
    return 0;
}
int fun()
{
    printf("IndiaBix.com\n");
    return 0;
}

A.
Error: in int(*p)() = fun;
B.
Error: fun() prototype not defined
C.
No error
D.
None of these

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The compiler will not know that the function int fun() exists. So we have to define the function prototype of int fun();
To overcome this error, see the below program


#include<stdio.h>
int fun(); /* function prototype */

int main()
{
    int (*p)() = fun;
    (*p)();
    return 0;
}
int fun()
{
    printf("IndiaBix.com\n");
    return 0;
}

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum


18.

What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?

A.
The element will be set to 0.
B.
The compiler would report an error.
C.
The program may crash if some important data gets overwritten.
D.
The array size would appropriately grow.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

If the index of the array size is exceeded, the program will crash. Hence "option c" is the correct answer. But the modern compilers will take care of this kind of errors.

Example: Run the below program, it will crash in Windows (TurboC Compiler)

#include<stdio.h>

int main()
{
    int arr[2];
    arr[3]=10;
    printf("%d",arr[3]);
    return 0;
}

Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the Turbo-C 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


19.

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

#include<stdio.h>

int main()
{
    printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0));
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Step 1:

printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0));

The sizeof function returns the size of the given expression.

sizeof(3.0f) is a floating point constant. The size of float is 4 bytes

sizeof('3') It converts '3' in to ASCII value.. The size of int is 2 bytes

sizeof(3.0) is a double constant. The size of double is 8 bytes

Hence the output of the program is 4,2,8

Note: The above program may produce different output in other platform due to the platform dependency of C compiler.

In Turbo C, 4 2 8. But in GCC, the output will be 4 4 8.

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


20.

What will be the output of the program ?

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

int main()
{
    static char s[] = "Hello!";
    printf("%d\n", *(s+strlen(s)));
    return 0;
}

A.
8
B.
0
C.
16
D.
Error

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


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