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, 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;
}
There should be a condition in the for loop
The two semicolons should be dropped
The for loop should be replaced with while loop.
No error
Your Answer: Option
(Not Answered)
Correct Answer: Option
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


2.
Which of the following statements are correct about the below program?
#include<stdio.h>
int main()
{
    int i = 10, j = 15;
    if(i % 2 = j % 3)
        printf("IndiaBIX\n");
    return 0;
}
Error: Expression syntax
Error: Lvalue required
Error: Rvalue required
The Code runs successfully
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

if(i % 2 = j % 3) This statement generates "LValue required error". There is no variable on the left side of the expression to assign (j % 3).


3.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=-3, j=2, k=0, m;
    m = ++i || ++j && ++k;
    printf("%d, %d, %d, %d\n", i, j, k, m);
    return 0;
}
2, 2, 0, 1
1, 2, 1, 0
-2, 2, 0, 0
-2, 2, 0, 1
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.

Step 2: m = ++i || ++j && ++k; here (++j && ++k;) this code will not get executed because ++i has non-zero value.
becomes m = -2 || ++j && ++k;
becomes m = TRUE || ++j && ++k; Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.

Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of variable 'i' only increemented by '1'(one). The variable j,k are not increemented.

Hence the output is "-2, 2, 0, 1".


4.
Which of the following statements are correct about the below program?
#include<stdio.h>
int main()
{
    float a=1.5, b=1.55;
    if(a=b)
        printf("a and b are equal\n");
    else
        printf("a and b are not equal\n");
    return 0;
}
Output: "a and b are equal"
Output: "a and b are not equal"
Floats cannot be compared using if
Switch should be used to compare floats
Your Answer: Option
(Not Answered)
Correct Answer: Option

5.
Point out the error in the program

f(int a, int b)
{
    int a;
    a = 20;
    return a;
}
Missing parenthesis in return statement
The function should be defined as int f(int a, int b)
Redeclaration of a
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

f(int a, int b) The variable a is declared in the function argument statement.

int a; Here again we are declaring the variable a. Hence it shows the error "Redeclaration of a"


6.
What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile?
#include<stdio.h>
#define SWAP(a, b, c)(c t; t=a, a=b, b=t)
int main()
{
    int x=10, y=20;
    SWAP(x, y, int);
    printf("%d %d\n", x, y);
    return 0;
}
It compiles
Compiles with an warning
Not compile
Compiles and print nothing
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
The code won't compile since declaration of t cannot occur within parenthesis.

7.
Which of the statements is correct about the program?
#include<stdio.h>

int main()
{
    float a=3.14;
    char *j;
    j = (char*)&a;
    printf("%d\n", *j);
    return 0;
}
It prints ASCII value of the binary number present in the first byte of a float variable a.
It prints character equivalent of the binary number present in the first byte of a float variable a.
It will print 3
It will print a garbage value
Your Answer: Option
(Not Answered)
Correct Answer: Option

8.
Will the program compile?
#include<stdio.h>
int main()
{
    char str[5] = "IndiaBIX";
    return 0;
}
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

C doesn't do array bounds checking at compile time, hence this compiles.

But, the modern compilers like Turbo C++ detects this as 'Error: Too many initializers'.

GCC would give you a warning.


9.
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

10.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    printf("India", "BIX\n");
    return 0;
}
Error
India BIX
India
BIX
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

printf("India", "BIX\n"); It prints "India". Because ,(comma) operator has Left to Right associativity. After printing "India", the statement got terminated.


11.
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

12.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    FILE *ptr;
    char i;
    ptr = fopen("myfile.c", "r");
    while((i=fgetc(ptr))!=NULL)
        printf("%c", i);
    return 0;
}
Print the contents of file "myfile.c"
Print the contents of file "myfile.c" upto NULL character
Infinite loop
Error in program
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
The program will generate infinite loop. When an EOF is encountered fgetc() returns EOF. Instead of checking the condition for EOF we have checked it for NULL. so the program will generate infinite loop.

13.
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample Jan Feb Mar
/* sample.c */
#include<stdio.h>
#include<dos.h>

int main(int arc, char *arv[])
{
    int i;
    for(i=1; i<_argc; i++)
        printf("%s ", _argv[i]);
    return 0;
}
No output
sample Jan Feb Mar
Jan Feb Mar
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option

14.
Which bitwise operator is suitable for checking whether a particular bit is on or off?
&& operator
& operator
|| operator
! operator
Your Answer: Option
(Not Answered)
Correct Answer: Option

15.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int i=4, j=8;
    printf("%d, %d, %d\n", i|j&j|i, i|j&&j|i, i^j);
    return 0;
}
4, 8, 0
1, 2, 1
12, 1, 12
0, 0, 0
Your Answer: Option
(Not Answered)
Correct Answer: Option

16.
malloc() returns a NULL if it fails to allocate the requested memory.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

17.
Point out the error if any in the following program (Turbo C).
#include<stdio.h>
#include<stdarg.h>
void display(int num, ...);

int main()
{
    display(4, 'A', 'a', 'b', 'c');
    return 0;
}
void display(int num, ...)
{
    char c; int j;
    va_list ptr;
    va_start(ptr, num);
    for(j=1; j<=num; j++)
    {
        c = va_arg(ptr, char);
        printf("%c", c);
    }
}
Error: unknown variable ptr
Error: Lvalue required for parameter
No error and print A a b c
No error and print 4 A a b c
Your Answer: Option
(Not Answered)
Correct Answer: Option

18.
What do the following declaration signify?
void *cmp();
cmp is a pointer to an void type.
cmp is a void type pointer variable.
cmp is a function that return a void pointer.
cmp function returns nothing.
Your Answer: Option
(Not Answered)
Correct Answer: Option

19.
Function can return a floating point number.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

20.
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 'b' */
        ungetc(c, stdout);
        printf("%c", c);
        ungetc(c, stdin);
    }
    return 0;
}
bbbb
bbbbb
b
Error in ungetc statement.
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The ungetc() function pushes the character c back onto the named input stream, which must be open for reading.

This character will be returned on the next call to getc or fread for that stream.

One character can be pushed back in all situations.

A second call to ungetc without a call to getc will force the previous character to be forgotten.


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