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.
When we mention the prototype of a function?
Defining
Declaring
Prototyping
Calling
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

A function prototype in C or C++ is a declaration of a function that omits the function body but does specify the function's name, argument types and return type.

While a function definition specifies what a function does, a function prototype can be thought of as specifying its interface.


2.
A long double can be used if range of a double is not enough to accommodate a real number.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

True, we can use long double; if double range is not enough.

double = 8 bytes.
long double = 10 bytes.


3.
How many times the while loop will get executed if a short int is 2 byte wide?
#include<stdio.h>
int main()
{
    int j=1;
    while(j <= 255)
    {
        printf("%c %d\n", j, j);
        j++;
    }
    return 0;
}
Infinite times
255 times
256 times
254 times
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The while(j <= 255) loop will get executed 255 times. The size short int(2 byte wide) does not affect the while() loop.


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.
Point out the error in the following program.
#include<stdio.h>
int main()
{
    struct emp
    {
        char name[20];
        float sal;
    };
    struct emp e[10];
    int i;
    for(i=0; i<=9; i++)
        scanf("%s %f", e[i].name, &e[i].sal);
    return 0;
}
Suspicious pointer conversion
Floating point formats not linked (Run time error)
Cannot use scanf() for structures
Strings cannot be nested inside structures
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Compile and Run the above program in Turbo C:

C:\>myprogram.exe
Sundar
2555.50
scanf : floating point formats not linked
Abnormal program termination

The program terminates abnormally at the time of entering the float value for e[i].sal.

Solution:

Just add the following function in your program. It will force the compiler to include required libraries for handling floating point linkages.

static void force_fpf() /* A dummy function */
{
    float x, *y; /* Just declares two variables */
    y = &x;      /* Forces linkage of FP formats */
    x = *y;      /* Suppress warning message about x */
}


6.
If return type for a function is not specified, it defaults to int
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

True, The default return type for a function is int.


7.
What will be the output of the program?
#include<stdio.h>
#define CUBE(x) (x*x*x)

int main()
{
    int a, b=3;
    a = CUBE(b++);
    printf("%d, %d\n", a, b);
    return 0;
}
9, 4
27, 4
27, 6
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The macro function CUBE(x) (x*x*x) calculates the cubic value of given number(Eg: 103.)

Step 1: int a, b=3; The variable a and b are declared as an integer type and varaible b id initialized to 3.

Step 2: a = CUBE(b++); becomes

=> a = b++ * b++ * b++;

=> a = 3 * 3 * 3; Here we are using post-increement operator, so the 3 is not incremented in this statement.

=> a = 27; Here, 27 is store in the variable a. By the way, the value of variable b is incremented by 3. (ie: b=6)

Step 3: printf("%d, %d\n", a, b); It prints the value of variable a and b.

Hence the output of the program is 27, 6.


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

int main()
{
    char *str;
    str = "%s";
    printf(str, "K\n");
    return 0;
}
Error
No output
K
%s
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    char str1[] = "Hello";
    char str2[] = "Hello";
    if(str1 == str2)
        printf("Equal\n");
    else
        printf("Unequal\n");
    return 0;
}
Equal
Unequal
Error
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: char str1[] = "Hello"; The variable str1 is declared as an array of characters and initialized with a string "Hello".

Step 2: char str2[] = "Hello"; The variable str2 is declared as an array of characters and initialized with a string "Hello".

We have use strcmp(s1,s2) function to compare strings.

Step 3: if(str1 == str2) here the address of str1 and str2 are compared. The address of both variable is not same. Hence the if condition is failed.

Step 4: At the else part it prints "Unequal".


10.
If the size of pointer is 32 bits What will be the output of the program ?
#include<stdio.h>

int main()
{
    char a[] = "Visual C++";
    char *b = "Visual C++";
    printf("%d, %d\n", sizeof(a), sizeof(b));
    printf("%d, %d", sizeof(*a), sizeof(*b));
    return 0;
}
10, 2
2, 2
10, 4
1, 2
11, 4
1, 1
12, 2
2, 2
Your Answer: Option
(Not Answered)
Correct Answer: Option

11.
The elements of union are always accessed using & operator
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    int a=250;
    printf("%1d\n", a);
    return 0;
}
1250
2
50
250
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

int a=250; The variable a is declared as an integer type and initialized to value 250.

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

Hence the output of the program is 250.


13.
We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind()
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

True, we should not be able to read a file after writing in that file without calling the below functions.

int fflush ( FILE * stream ); If the given stream was open for writing and the last i/o operation was an output operation, any unwritten data in the output buffer is written to the file.

int fseek ( FILE * stream, long int offset, int origin ); Its purpose is to change the file position indicator for the specified stream.

void rewind ( FILE * stream ); Sets the position indicator associated with stream to the beginning of the file.


14.
What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three
/* myprog.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    int i;
    for(i=1; i<argc; i++)
        printf("%c", argv[i][0]);
    return 0;
}
oot
ott
nwh
eoe
Your Answer: Option
(Not Answered)
Correct Answer: Option

15.
If the following program (myproc.c) is present in the directory "C:\TC" then what will be output of the program if run it from DOS shell?
/* myproc.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("%s", argv[0]);
    return 0;
}
SAMPLE.C
C:\TC\MYPROC.EXE
C:\TC
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
In order to execute it from DOS shell, we have to run the created EXE file by entering the exe file name as C:\TC>myproc <enter>.

16.
Point out the error in the program (in Turbo-C).
#include<stdio.h>
#define MAX 128

int main()
{
    const int max=128;
    char array[max];
    char string[MAX];
    array[0] = string[0] = 'A';
    printf("%c %c\n", array[0], string[0]);
    return 0;
}
Error: unknown max in declaration/Constant expression required
Error: invalid array string
None of above
No error. It prints A A
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: A macro named MAX is defined with value 128

Step 2: const int max=128; The constant variable max is declared as an integer data type and it is initialized with value 128.

Step 3: char array[max]; This statement reports an error "constant expression required". Because, we cannot use variable to define the size of array.

To avoid this error, we have to declare the size of an array as static. Eg. char array[10]; or use macro char array[MAX];

Note: The above program will print A A as output in Unix platform.


17.
Which header file should you include, if you are going to develop a function, which can accept variable number of arguments?
varagrg.h
stdlib.h
stdio.h
stdarg.h
Your Answer: Option
(Not Answered)
Correct Answer: Option

18.
What do the following declaration signify?
int *f();
f is a pointer variable of function type.
f is a function returning pointer to an int.
f is a function pointer.
f is a simple declaration of pointer variable.
Your Answer: Option
(Not Answered)
Correct Answer: Option

19.
What do the following declaration signify?
char *scr;
scr is a pointer to pointer variable.
scr is a function pointer.
scr is a pointer to char.
scr is a member of function pointer.
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    char str[] = "IndiaBIX";
    printf("%.#s %2s", str, str);
    return 0;
}
Error: in Array declaration
Error: printf statement
Error: unspecified character in printf
No error
Your Answer: Option
(Not Answered)
Correct Answer: Option

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