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<string.h>

int main()
{
    char sentence[80];
    int i;
    printf("Enter a line of text\n");
    gets(sentence);
    for(i=strlen(sentence)-1; i >=0; i--)
        putchar(sentence[i]);
    return 0;
}

A.
The sentence will get printed in same order as it entered
B.
The sentence will get printed in reverse order
C.
Half of the sentence will get printed
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


2.

Declare the following statement?
"A pointer to an array of three 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 D

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum


3.

What will be the output of the program ?

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

int main()
{
    int i, n;
    char *x="Alice";
    n = strlen(x);
    *x = x[n];
    for(i=0; i<=n; i++)
    {
        printf("%s ", x);
        x++;
    }
    printf("\n", x);
    return 0;
}

A.
Alice
B.
ecilA
C.
Alice lice ice ce e
D.
lice ice ce e

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

If you compile and execute this program in windows platform with Turbo C, it will give "lice ice ce e".

It may give different output in other platforms (depends upon compiler and machine). The online C compiler given in this site will give the Option C as output (it runs on Linux platform).

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


4.

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;
}

A.
2, 2, 0, 1
B.
1, 2, 1, 0
C.
-2, 2, 0, 0
D.
-2, 2, 0, 1

Your Answer: Option (Not Answered)

Correct Answer: Option D

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

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum


5.

Point out the error in the following program.

#include<stdio.h>
void display(int (*ff)());

int main()
{
    int show();
    int (*f)();
    f = show;
    display(f);
    return 0;
}
void display(int (*ff)())
{
    (*ff)();
}
int show()
{
    printf("IndiaBIX");
}

A.
Error: invalid parameter in function display()
B.
Error: invalid function call f=show;
C.
No error and prints "IndiaBIX"
D.
No error and prints nothing.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum


6.

The binary equivalent of 5.375 is

A.
101.101110111
B.
101.011
C.
101011
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Floating Point Issues

Discuss about this problem : Discuss in Forum


7.

Bitwise & can be used in conjunction with ~ operator to turn off 1 or more bits in a number.

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Bitwise Operators

Discuss about this problem : Discuss in Forum


8.

Is the following declaration acceptable?

typedef long no, *ptrtono;
no n;
ptrtono p;

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


9.

What will be the output of the program?

#include<stdio.h>
void fun(int*, int*);
int main()
{
    int i=5, j=2;
    fun(&i, &j);
    printf("%d, %d", i, j);
    return 0;
}
void fun(int *i, int *j)
{
    *i = *i**i;
    *j = *j**j;
}

A.
5, 2
B.
10, 4
C.
2, 5
D.
25, 4

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Step 1: int i=5, j=2; Here variable i and j are declared as an integer type and initialized to 5 and 2 respectively.

Step 2: fun(&i, &j); Here the function fun() is called with two parameters &i and &j (The & denotes call by reference. So the address of the variable i and j are passed. )

Step 3: void fun(int *i, int *j) This function is called by reference, so we have to use * before the parameters.

Step 4: *i = *i**i; Here *i denotes the value of the variable i. We are multiplying 5*5 and storing the result 25 in same variable i.

Step 5: *j = *j**j; Here *j denotes the value of the variable j. We are multiplying 2*2 and storing the result 4 in same variable j.

Step 6: Then the function void fun(int *i, int *j) return back the control back to main() function.

Step 7: printf("%d, %d", i, j); It prints the value of variable i and j.

Hence the output is 25, 4.

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


10.

What will be the output of the program?

#include<stdio.h>
int fun(int **ptr);

int main()
{
    int i=10;
    const int *ptr = &i;
    fun(&ptr);
    return 0;
}
int fun(int **ptr)
{
    int j = 223;
    int *temp = &j;
    printf("Before changing ptr = %5x\n", *ptr);
    const *ptr = temp;
    printf("After changing ptr = %5x\n", *ptr);
    return 0;
}

A.
Address of i
Address of j
B.
10
223
C.
Error: cannot convert parameter 1 from 'const int **' to 'int **'
D.
Garbage value

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Const

Discuss about this problem : Discuss in Forum


11.

What will be the output of the program?

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

A.
Hello
Hi
B.
Hello
Bye
C.
Hi
D.
Bye

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

switch(i) has the variable i it has the value '1'(one).

Then case 1: statements got executed. so, it prints "Hi". The break; statement make the program to be exited from switch-case statement.

switch-case do not execute any statements outside these blocks case and default

Hence the output is "Hi".

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


12.

Which of the following statements correct about the below program?

#include<stdio.h>

int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a u1 = {512};
    union a u2 = {0, 2};
    return 0;
}
1: u2 CANNOT be initialized as shown.
2: u1 can be initialized as shown.
3: To initialize char ch[] of u2 '.' operator should be used.
4: The code causes an error 'Declaration syntax error'

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


13.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    union var
    {
        int a, b;
    };
    union var v;
    v.a=10;
    v.b=20;
    printf("%d\n", v.a);
    return 0;
}

A.
10
B.
20
C.
30
D.
0

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


14.

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;
}

A.
No output
B.
sample Jan Feb Mar
C.
Jan Feb Mar
D.
Error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum


15.

What will be the output of the program (sample.c) given below if it is executed from the command line (Turbo C in DOS)?
cmd> sample 1 2 3

/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    int j;
    j = argv[1] + argv[2] + argv[3];
    printf("%d", j);
    return 0;
}

A.
6
B.
sample 6
C.
Error
D.
Garbage value

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Here argv[1], argv[2] and argv[3] are string type. We have to convert the string to integer type before perform arithmetic operation.

Example: j = atoi(argv[1]) + atoi(argv[2]) + atoi(argv[3]);

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum


16.

Point out the error in the following program.

#include<stdio.h>

int main()
{
    char str[] = "IndiaBIX";
    printf("%.#s %2s", str, str);
    return 0;
}

A.
Error: in Array declaration
B.
Error: printf statement
C.
Error: unspecified character in printf
D.
No error

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


17.

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

#include<stdio.h>
int main()
{
    void fun();
    int i = 1;
    while(i <= 5)
    {
        printf("%d\n", i);
        if(i>2)
            goto here;
    }
return 0;
}
void fun()
{
    here:
    printf("It works");
}

A.
No Error: prints "It works"
B.
Error: fun() cannot be accessed
C.
Error: goto cannot takeover control to other function
D.
No error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

A label is used as the target of a goto statement, and that label must be within the same function as the goto statement.

Syntax: goto <identifier> ;
Control is unconditionally transferred to the location of a local label specified by <identifier>.
Example:


#include <stdio.h>
int main()
{
    int i=1;
    while(i>0)
    {
        printf("%d", i++);
        if(i==5)
          goto mylabel;
    }
    mylabel:
    return 0;
}
 

Output: 1,2,3,4

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


18.

What is the output of the program

#include<stdio.h>
int main()
{
    extern int fun(float);
    int a;
    a = fun(3.14);
    printf("%d\n", a);
    return 0;
}
int fun(int aa)
{
	return (int)++aa;
}

A.
3
B.
3.14
C.
D.
4
E.
Compile Error

Your Answer: Option (Not Answered)

Correct Answer: Option E

Explanation:

2 Errors
1. Type mismatch in redeclaration of fun
2. Type mismatch in parameter aa

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum


19.

FILE is a structure suitably typedef'd in "stdio.h".

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

FILE - a structure containing the information about a file or text stream needed to perform input or output operations on it, including:
=> a file descriptor, the current stream position,
=> an end-of-file indicator,
=> an error indicator,
=> a pointer to the stream's buffer, if applicable

fpos_t - a non-array type capable of uniquely identifying the position of every byte in a file.
size_t - an unsigned integer type which is the type of the result of the sizeof operator.

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


20.

In a function two return statements should never occur.

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

No, In a function two return statements can occur but not successively.

Example:


#include <stdio.h>
int mul(int, int); /* Function prototype */

int main()
{
    int a = 0, b = 3, c;
    c = mul(a, b);
    printf("c = %d\n", c);
    return 0;
}

/* Two return statements in the mul() function */
int mul(int a, int b)
{
   if(a == 0 || b == 0)
   {
        return 0;
   }
   else
   {
        return (a * b);
   }
}

Output:
c = 0

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


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