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.
What will you do to treat the constant 3.14 as a long double?
use 3.14LD
use 3.14L
use 3.14DL
use 3.14LF
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Given 3.14 is a double constant.

To specify 3.14 as long double, we have to add L to the 3.14. (i.e 3.14L)


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

int main()
{
    int i=0;
    i++;
    if(i<=5)
    {
        printf("IndiaBIX");
        exit(1);
        main();
    }
    return 0;
}
Prints "IndiaBIX" 5 times
Function main() doesn't calls itself
Infinite loop
Prints "IndiaBIx"
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int i=0; The variable i is declared as in integer type and initialized to '0'(zero).

Step 2: i++; Here variable i is increemented by 1. Hence i becomes '1'(one).

Step 3: if(i<=5) becomes if(1 <=5). Hence the if condition is satisfied and it enter into if block statements.

Step 4: printf("IndiaBIX"); It prints "IndiaBIX".

Step 5: exit(1); This exit statement terminates the program execution.

Hence the output is "IndiaBIx".


4.
In a macro call the control is passed to the macro.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

False, Always the macro is substituted by the given text/expression.


5.
Macros have a local scope.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

False, The scope of macros is globals and functions. Also the scope of macros is only from the point of definition to the end of the file.


6.
Will the program compile successfully?
#include<stdio.h>

int main()
{
    #ifdef NOTE
        int a;
        a=10;
    #else
        int a;
        a=20;
    #endif
    printf("%d\n", a);
    return 0;
}
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Yes, this program will compile and run successfully and prints 20.

The macro #ifdef NOTE evaluates the given expression to 1. If satisfied it executes the #ifdef block statements. Here #ifdef condition fails because the Macro NOTE is nowhere declared.

Hence the #else block gets executed, the variable a is declared and assigned a value of 20.

printf("%d\n", a); It prints the value of variable a 20.


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

int main()
{
    char str[] = "Nagpur";
    str[0]='K';
    printf("%s, ", str);
    str = "Kanpur";
    printf("%s", str+1);
    return 0;
}
Kagpur, Kanpur
Nagpur, Kanpur
Kagpur, anpur
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The statement str = "Kanpur"; generates the LVALUE required error. We have to use strcpy function to copy a string.

To remove error we have to change this statement str = "Kanpur"; to strcpy(str, "Kanpur");

The program prints the string "anpur"


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

int main()
{
    unsigned char;
    FILE *fp;
    fp=fopen("trial", "r");
    if(!fp)
    {
        printf("Unable to open file");
        exit(1);
    }
    fclose(fp);
    return 0;
}
Error: in unsigned char statement
Error: unknown file pointer
No error
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

This program tries to open the file trial.txt in read mode. If file not exists or unable to read it prints "Unable to open file" and then terminate the program.

If file exists, it simply close the file and then terminates the program.


9.
Point out the error in the program?
#include<stdio.h>

int main()
{
    char ch;
    int i;
    scanf("%c", &i);
    scanf("%d", &ch);
    printf("%c %d", ch, i);
    return 0;
}
Error: suspicious char to in conversion in scanf()
Error: we may not get input for second scanf() statement
No error
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

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

int main(int argc, char *argv[])
{
    while(--argc>0)
        printf("%s", *++argv);
    return 0;
}
sample monday tuesday wednesday thursday
monday tuesday wednesday thursday
monday tuesday thursday
tuesday
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

13.
If the different command line arguments are supplied at different times would the output of the following program change?
#include<stdio.h>

int main(int argc, char **argv)
{
    printf("%d\n", argv[argc]);
    return 0;
}
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

14.
Specify the 2 library functions to dynamically allocate memory?
malloc() and memalloc()
alloc() and memalloc()
malloc() and calloc()
memalloc() and faralloc()
Your Answer: Option
(Not Answered)
Correct Answer: Option

15.
Point out the correct statement will let you access the elements of the array using 'p' in the following program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int i, j;
    int(*p)[3];
    p = (int(*)[3])malloc(3*sizeof(*p));
    return 0;
}
for(i=0; i<3; i++)
{
    for(j=0; j<3; j++)
        printf("%d", p[i+j]);
}
for(i=0; i<3; i++)
    printf("%d", p[i]);
for(i=0; i<3; i++)
{
    for(j=0; j<3; j++)
        printf("%d", p[i][j]);
}
for(j=0; j<3; j++)
    printf("%d", p[i][j]);
Your Answer: Option
(Not Answered)
Correct Answer: Option

16.
Which of the following statement is correct prototype of the malloc() function in c ?
int* malloc(int);
char* malloc(char);
unsigned int* malloc(unsigned int);
void* malloc(size_t);
Your Answer: Option
(Not Answered)
Correct Answer: Option

17.
Can I increase the size of dynamically allocated array?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Use realloc(variable_name, value);

18.
A function that receives variable number of arguments should use va_arg() to extract arguments from the variable argument list.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    char far *near *ptr1;
    char far *far *ptr2;
    char far *huge *ptr3;
    printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
    return 0;
}
4, 4, 8
4, 4, 4
2, 4, 4
2, 4, 8
Your Answer: Option
(Not Answered)
Correct Answer: Option

20.
scanf() or atoi() function can be used to convert a string like "436" in to integer.
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

scanf is a function that reads data with specified format from a given string stream source.
scanf("%d",&number);

atoi() convert string to integer.
var number;
number = atoi("string");


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