Online C Programming Test - C Programming Test - Random

Instruction:

  • This is a FREE online test. Beware of scammers who ask for money to attend this test.
  • Total number of questions: 20.
  • Time allotted: 30 minutes.
  • Each question carries 1 mark; there are no negative marks.
  • DO NOT refresh the page.
  • All the best!

Marks : 2/20


Total number of questions
20
Number of answered questions
0
Number of unanswered questions
20
Test Review : View answers and explanation for this test.

1.
Point out the error in the following program.
#include<stdio.h>
int main()
{
    void v = 0;

    printf("%d", v);

    return 0;
}
Error: Declaration syntax error 'v' (or) Size of v is unknown or zero.
Program terminates abnormally.
No error.
None of these.
Your Answer: Option
(Not Answered)
Correct Answer: Option

2.
How many times "IndiaBIX" is get printed?
#include<stdio.h>
int main()
{
    int x;
    for(x=-1; x<=10; x++)
    {
        if(x < 5)
            continue;
        else
            break;
        printf("IndiaBIX");
    }
    return 0;
}
Infinite times
11 times
0 times
10 times
Your Answer: Option
(Not Answered)
Correct Answer: Option

3.
What will be the output of the program?
#include<stdio.h>
int main()
{
    char str[]="C-program";
    int a = 5;
    printf(a >10?"Ps\n":"%s\n", str);
    return 0;
}
C-program
Ps
Error
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: char str[]="C-program"; here variable str contains "C-program".
Step 2: int a = 5; here variable a contains "5".
Step 3: printf(a >10?"Ps\n":"%s\n", str); this statement can be written as


if(a > 10)
{
    printf("Ps\n");
}
else
{
    printf("%s\n", str);
}

Here we are checking a > 10 means 5 > 10. Hence this condition will be failed. So it prints variable str.

Hence the output is "C-program".


4.
Can we use a switch statement to switch on strings?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
The cases in a switch must either have integer constants or constant expressions.

5.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int a=100, b=200, c;
    c = (a == 100 || b > 200);
    printf("c=%d\n", c);
    return 0;
}
c=100
c=200
c=1
c=300
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int a=100, b=200, c;
Step 2: c = (a == 100 || b > 200);
becomes c = (100 == 100 || 200 > 200);
becomes c = (TRUE || FALSE);
becomes c = (TRUE);(ie. c = 1)
Step 3: printf("c=%d\n", c); It prints the value of variable i=1
Hence the output of the program is '1'(one).


6.
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;
}
4, 3, 3
4, 3, 2
3, 3, 2
2, 3, 3
Your Answer: Option
(Not Answered)
Correct Answer: Option
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".


7.
What will be the output of the program?
#include<stdio.h>
int check (int, int);

int main()
{
    int c;
    c = check(10, 20);
    printf("c=%d\n", c);
    return 0;
}
int check(int i, int j)
{
    int *p, *q;
    p=&i;
    q=&j;
    i>=45 ? return(*p): return(*q);
}
Print 10
Print 20
Print 1
Compile error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

There is an error in this line i>=45 ? return(*p): return(*q);. We cannot use return keyword in the terenary operators.


8.
What will be the output of the program?
#include<stdio.h>
#define SWAP(a, b) int t; t=a, a=b, b=t;
int main()
{
    int a=10, b=12;
    SWAP(a, b);
    printf("a = %d, b = %d\n", a, b);
    return 0;
}
a = 10, b = 12
a = 12, b = 10
Error: Declaration not allowed in macro
Error: Undefined symbol 't'
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The macro SWAP(a, b) int t; t=a, a=b, b=t; swaps the value of the given two variable.

Step 1: int a=10, b=12; The variable a and b are declared as an integer type and initialized to 10, 12 respectively.

Step 2: SWAP(a, b);. Here the macro is substituted and it swaps the value to variable a and b.

Hence the output of the program is 12, 10.


9.
Preprocessor directive #undef can be used only on a macro that has been #define earlier
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
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


10.
Are the three declarations char **apple, char *apple[], and char apple[][] same?
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

11.
Will the following program give any warning on compilation in TurboC (under DOS)?
#include<stdio.h>

int main()
{
    int *p1, i=25;
    void *p2;
    p1=&i;
    p2=&i;
    p1=p2;
    p2=p1;
    return 0;
}
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

12.
The '->' operator can be used to access structures elements using a pointer to a structure variable only
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

13.
Which of the following operations can be performed on the file "NOTES.TXT" using the below code?
FILE *fp;
fp = fopen("NOTES.TXT", "r+");
Reading
Writing
Appending
Read and Write
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

r+ Open an existing file for update (reading and writing).


14.
Point out the correct statements about the program?
#include<stdio.h>

int main()
{
    FILE *fptr;
    char str[80];
    fptr = fopen("f1.dat", "w");
    if(fptr == NULL)
        printf("Cannot open file");
    else
    {
        while(strlen(gets(str))>0)
        {
            fputs(str, fptr);
            fputs("\n", fptr);
        }
        fclose(fptr);
    }
    return 0;
}
The code copies the content of one file to another
The code writes strings that are read from the keyboard into a file.
The code reads a file
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

This program get the input string from the user through gets function and store it in the file f1.txt using fputs function.


15.
Offset used in fseek() function call can be a negative number.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

True, offset in fseek() function can be a negative number. It makes the file pointer to move backwards from the current position.

Declaration: retval = fseek( fp, offset, from );

Where:

FILE *fp; = points to the file on which I/O is to be repositioned.

long offset; = is an integer giving the number of bytes to move forward or backward in the file. This may be positive or negative.

int from; = is one of the manifests SEEK_SET, SEEK_CUR, or SEEK_END.

int retval; = is non-zero if the seek operation was invalid (e.g. on a file not opened with a "b" option); otherwise, the return value is zero.


16.
What will be the output of the program?
#define P printf("%d\n", -1^~0);
#define M(P) int main()\
             {\
                P\
                return 0;\
             }
M(P)
1
0
-1
2
Your Answer: Option
(Not Answered)
Correct Answer: Option

17.
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;
}
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

18.
When we dynamically allocate memory is there any way to free memory during run time?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Using free()

19.
Declare the following statement?
"A pointer to a function which receives an int pointer and returns float pointer".
float *(ptr)*int;
float *(*ptr)(int)
float *(*ptr)(int*)
float (*ptr)(int)
Your Answer: Option
(Not Answered)
Correct Answer: Option

20.
The itoa function can convert an integer in decimal, octal or hexadecimal form to a string.
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

itoa() takes the integer input value input and converts it to a number in base radix. The resulting number a sequence of base-radix digits.

Example:


/* itoa() example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
	int no;
	char buff [50];
	printf ("Enter number: ");
	scanf ("%d",&no);
    
	itoa (no,buff,10);
	printf ("Decimal: %s\n",buff);
    
	itoa (no,buff,2);
	printf ("Binary: %s\n",buff);
    
	itoa (no,buff,16);
	printf ("Hexadecimal: %s\n",buff);
    
	return 0;
}

Output:
Enter a number: 1250
Decimal: 1250
Binary: 10011100010
Hexadecimal: 4e2


*** END OF THE TEST ***
Time Left: 00:29:56
Post your test result / feedback here: