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.
What will be the output of the program in 16 bit platform (Turbo C under DOS)?
#include<stdio.h>
int main()
{
    extern int i;
    i = 20;
    printf("%d\n", sizeof(i));
    return 0;
}
2
4
vary from compiler
Linker Error : Undefined symbol 'i'
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Linker Error : Undefined symbol 'i'
The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name 'i' is available in any other program with memory space allocated for it. Hence a linker error has occurred.

2.
Which of the following cannot be checked in a switch-case statement?
Character
Integer
Float
enum
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value.


switch( expression )
{
    case constant-expression1:    statements 1;
    case constant-expression2:    statements 2;    
    case constant-expression3:    statements3 ;
    ...
    ...
    default : statements 4;
}

The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.


3.
Which of the following is not logical operator?
&
&&
||
!
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Bitwise operators:
& is a Bitwise AND operator.

Logical operators:
&& is a Logical AND operator.
|| is a Logical OR operator.
! is a NOT operator.

So, '&' is not a Logical operator.


4.
Which of the following errors would be reported by the compiler on compiling the program given below?
#include<stdio.h>
int main()
{
    int a = 5;
    switch(a)
    {
	case 1:
	printf("First");

	case 2:
	printf("Second");

	case 3 + 2:
	printf("Third");

	case 5:
	printf("Final");
	break;

    }
    return 0;
}
There is no break statement in each case.
Expression as in case 3 + 2 is not allowed.
Duplicate case case 5:
No error will be reported.
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Because, case 3 + 2: and case 5: have the same constant value 5.


5.
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;
}
The program prints 'IndiaBIX' 5 times
The program prints 'IndiaBIX' one time
The call to main() after exit() doesn't materialize.
The compiler reports an error since main() cannot call itself.
Your Answer: Option
(Not Answered)
Correct Answer: Option
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".


6.
Which of the following sentences are correct about a for loop in a C program?
1: for loop works faster than a while loop.
2: All things that can be done using a for loop can also be done using a while loop.
3: for(;;); implements an infinite loop.
4: for loop can be used if we want statements in a loop get executed at least once.
1
1, 2
2, 3
2, 3, 4
Your Answer: Option
(Not Answered)
Correct Answer: Option

7.
Associativity of an operator is either Left to Right or Right to Left.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Yes, the associativity of an operator is either Left to Right or Right to Left.


8.
Point out the compile time error in the program given below.
#include<stdio.h>

int main()
{
    int *x;
    *x=100;
    return 0;
}
Error: invalid assignment for x
Error: suspicious pointer conversion
No error
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
While reading the code there is no error, but upon running the program having an unitialised variable can cause the program to crash (Null pointer assignment).

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

int main()
{
    printf("%d\n", strlen("123456"));
    return 0;
}
6
12
7
2
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The function strlen returns the number of characters in the given string.

Therefore, strlen("123456") returns 6.

Hence the output of the program is "6".


10.
A structure can be nested inside another structure.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

11.
According to ANSI specifications which is the correct way of declaring main when it receives command-line arguments?
int main(int argc, char *argv[])
int main(argc, argv)
int argc; char *argv;
int main()
{
    int argc; char *argv;
}
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option

12.
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample friday tuesday sunday
/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("%c", *++argv[2] );
    return 0;
}
s
f
u
r
Your Answer: Option
(Not Answered)
Correct Answer: Option

13.
Which of the following statements are FALSE about the below code?
int main(int ac, char *av[])
{
}
ac contains count of arguments supplied at command-line
av[] contains addresses of arguments supplied at a command line
In place of ac and av, argc and argv should be used.
The variables ac and av are always local to main()
Your Answer: Option
(Not Answered)
Correct Answer: Option

14.
Point out the error in the program.
#include<stdio.h>
const char *fun();

int main()
{
    *fun() = 'A';
    return 0;
}
const char *fun()
{
    return "Hello";
}
Error: RValue required
Error: Lvalue required
Error: fun() returns a pointer const character which cannot be modified
No error
Your Answer: Option
(Not Answered)
Correct Answer: Option

15.
Point out the error in the program.
#include<stdio.h>
#include<stdlib.h>

union employee
{
    char name[15];
    int age;
    float salary;
};
const union employee e1;

int main()
{
    strcpy(e1.name, "K");
    printf("%s", e1.name);    
    e1.age=85;
    printf("%d", e1.age);
    printf("%f", e1.salary);
    return 0;
}
Error: RValue required
Error: cannot modify const object
Error: LValue required in strcpy
No error
Your Answer: Option
(Not Answered)
Correct Answer: Option

16.
If malloc() successfully allocates memory it returns the number of bytes it has allocated.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Syntax: void *malloc(size_t size);

The malloc() function shall allocate unused space for an object whose size in bytes is specified by size and whose value is unspecified.

The order and contiguity of storage allocated by successive calls to malloc() is unspecified. The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object in the space allocated (until the space is explicitly freed or reallocated). Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned points to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer shall be returned. If the size of the space requested is 0, the behavior is implementation-defined: the value returned shall be either a null pointer or a unique pointer.

17.
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>
void varfun(int n, ...);

int main()
{
    varfun(3, 7, -11.2, 0.66);
    return 0;
}
void varfun(int n, ...)
{
    float *ptr;
    int num;
    va_start(ptr, n);
    num = va_arg(ptr, int);
    printf("%d", num);
}
Error: too many parameters
Error: invalid access to list member
Error: ptr must be type of va_list
No error
Your Answer: Option
(Not Answered)
Correct Answer: Option

18.
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>

int main()
{
    void display(char *s, int num1, int num2, ...);
    display("Hello", 4, 2, 12.5, 13.5, 14.5, 44.0);
    return 0;
}
void display(char *s, int num1, int num2, ...)
{
    double c;
    char s;
    va_list ptr;
    va_start(ptr, s);
    c = va_arg(ptr, double);
    printf("%f", c);
}
Error: invalid arguments in function display()
Error: too many parameters
Error: in va_start(ptr, s);
No error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
We should have use va_start(ptr, num2);

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

int main()
{
    struct s1
    {
        char *z;
    int i;
    struct s1 *p;
    };
    static struct s1 a[] = {{"Nagpur", 1, a+1} , {"Chennai", 2, a+2} , 
                            {"Bangalore", 3, a} };

    struct s1 *ptr = a;
    printf("%s,", ++(ptr->z));
    printf(" %s,", a[(++ptr)->i].z);
    printf(" %s", a[--(ptr->p->i)].z);
    return 0;
}
Nagpur, Chennai, Bangalore
agpur, hennai, angalore
agpur, Chennai, angalore
agpur, Bangalore, Bangalore
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

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