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 in the following program.
#include<stdio.h>
int main()
{
    void v = 0;

    printf("%d", v);

    return 0;
}
Error: Declaration syntax error 'v' (or) Size of v is unknown or zero.
Program terminates abnormally.
No error.
None of these.
Your Answer: Option
(Not Answered)
Correct Answer: Option

2.
Point out the error, if any in the while loop.
#include<stdio.h>
int main()
{
    int i=1;
    while()
    {
        printf("%d\n", i++);
        if(i>10)
           break;
    }
    return 0;
}
There should be a condition in the while loop
There should be at least a semicolon in the while
The while loop should be replaced with for loop.
No error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The while() loop must have conditional expression or it shows "Expression syntax" error.

Example: while(i > 10){ ... }


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

int addmult(int ii, int jj)
{
    int kk, ll;
    kk = ii + jj;
    ll = ii * jj;
    return (kk, ll);
}

int main()
{
    int i=3, j=4, k, l;
    k = addmult(i, j);
    l = addmult(i, j);
    printf("%d, %d\n", k, l);
    return 0;
}
12, 12
7, 7
7, 12
12, 7
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int i=3, j=4, k, l; The variables i, j, k, l are declared as an integer type and variable i, j are initialized to 3, 4 respectively.

The function addmult(i, j); accept 2 integer parameters.

Step 2: k = addmult(i, j); becomes k = addmult(3, 4)

In the function addmult(). The variable kk, ll are declared as an integer type int kk, ll;

kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.

ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.

return (kk, ll); It returns the value of variable ll only.

The value 12 is stored in variable 'k'.

Step 3: l = addmult(i, j); becomes l = addmult(3, 4)

kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.

ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.

return (kk, ll); It returns the value of variable ll only.

The value 12 is stored in variable 'l'.

Step 4: printf("%d, %d\n", k, l); It prints the value of k and l

Hence the output is "12, 12".


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

int main()
{
    int a=10;
    void f();
    a = f();
    printf("%d\n", a);
    return 0;
}
void f()
{
    printf("Hi");
}
Error: Not allowed assignment
Error: Doesn't print anything
No error
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The function void f() is not visible to the compiler while going through main() function. So we have to declare this prototype void f(); before to main() function. This kind of error will not occur in modern compilers.


5.
Which of the following statements correctly declare a function that receives a pointer to pointer to a pointer to a float and returns a pointer to a pointer to a pointer to a pointer to a float?
float **fun(float***);
float *fun(float**);
float fun(float***);
float ****fun(float***);
Your Answer: Option
(Not Answered)
Correct Answer: Option

6.
Does this mentioning array name gives the base address in all the contexts?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

No, Mentioning the array name in C or C++ gives the base address in all contexts except one.

Syntactically, the compiler treats the array name as a pointer to the first element. You can reference elements using array syntax, a[n], or using pointer syntax, *(a+n), and you can even mix the usages within an expression.

When you pass an array name as a function argument, you are passing the "value of the pointer", which means that you are implicitly passing the array by reference, even though all parameters in functions are "call by value".


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

int main()
{
    char str1[5], str2[5];
    int i;
    gets(str1);
    gets(str2);
    i = strcmp(str1, str2);
    printf("%d\n", i);
    return 0;
}
Unpredictable integer value
0
-1
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

gets() gets collects a string of characters terminated by a new line from the standard input stream stdin.

The gets(str1) read the input string from user and store in variable str1.

The gets(str2) read the input string from user and store in variable str2.

The code i = strcmp(str1, str2); The strcmp not only returns -1, 0 and +1, but also other negative or positive values. So the value of i is "unpredictable integer value".

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


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

int main()
{
    char str[10] = "India";
    str[6] = "BIX";
    printf("%s\n", str);
    return 0;
}
India BIX
BIX
India
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

str[6] = "BIX"; - Nonportable pointer conversion.


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

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

The line char str = "IndiaBIX"; generates "Non portable pointer conversion" error.

To eliminate the error, we have to change the above line to

char *str = "IndiaBIX"; (or) char str[] = "IndiaBIX";

Then it prints "IndiaBIX".


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

int main()
{
    struct emp
    {
        char name[25];
        int age;
        float bs;
    };
    struct emp e;
    e.name = "Suresh";
    e.age = 25;
    printf("%s %d\n", e.name, e.age);
    return 0;
}
Error: Lvalue required/incompatible types in assignment
Error: invalid constant expression
Error: Rvalue required
No error, Output: Suresh 25
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

We cannot assign a string to a struct variable like e.name = "Suresh"; in C.

We have to use strcpy(char *dest, const char *source) function to assign a string.

Ex: strcpy(e.name, "Suresh");


11.
In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain?
"I am a boy\r\n\0"
"I am a boy\r\0"
"I am a boy\n\0"
"I am a boy"
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Declaration: char *fgets(char *s, int n, FILE *stream);

fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first.

Therefore, the string str contain "I am a boy\n\0"


12.
What is the purpose of "rb" in fopen() function used below in the code?
FILE *fp;
fp = fopen("source.txt", "rb");
open "source.txt" in binary mode for reading
open "source.txt" in binary mode for reading and writing
Create a new file "source.txt" for reading and writing
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The file source.txt will be opened in the binary mode.


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

14.
What will be the output of the program if it is executed like below?
cmd> sample
/* sample.c */
#include<stdio.h>

int main(int argc, char **argv)
{
    printf("%s\n", argv[argc-1]);
    return 0;
}
0
sample
samp
No output
Your Answer: Option
(Not Answered)
Correct Answer: Option

15.
In the statement expression1 >> expression2. if expression1 is a signed integer with its leftmost bit set to 1 then on right shifting it the result of the statement will vary from computer to computer
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

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.
malloc() allocates memory from the heap and not from the stack.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

18.
malloc() returns a float pointer if memory is allocated for storing float's and a double pointer if memory is allocated for storing double's.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

19.
va_list is an array that holds information needed by va_arg and va_end
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    static char *p = (char *)malloc(10);
    return 0;
}
Error: Lvalue required
Error: Rvalue required
Error: invalid *p declaration
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: